Skip to content

Commit

Permalink
MOBILE-3730: Load styles from site plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Jul 3, 2024
1 parent 42f75c6 commit ae6eaae
Show file tree
Hide file tree
Showing 39 changed files with 264 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck {
async ngOnChanges(changes: Record<string, SimpleChange>): Promise<void> {
// Only compile if text/javascript has changed or the forceCompile flag has been set to true.
if (this.text === undefined ||
!(changes.text || changes.javascript || (changes.forceCompile && CoreUtils.isTrueOrOne(this.forceCompile)))) {
!(changes.text || changes.javascript || changes.cssCode || changes.stylesPath ||
(changes.forceCompile && CoreUtils.isTrueOrOne(this.forceCompile)))) {
return;
}

Expand Down Expand Up @@ -182,17 +183,16 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck {
return;
}

if (this.stylesPath && !this.cssCode) {
this.cssCode = await CoreUtils.ignoreErrors(CoreWS.getText(this.stylesPath));
}

// Prepend all CSS rules with :host to avoid conflicts.
if (this.cssCode) {
if (!this.cssCode.includes(':host')) {
this.cssCode = CoreDom.prefixCSS(this.cssCode, ':host ::ng-deep', ':host');
}

return;
}

if (this.stylesPath && !this.cssCode) {
this.cssCode = await CoreUtils.ignoreErrors(CoreWS.getText(this.stylesPath));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/features/siteplugins/classes/call-ws-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class CoreSitePluginsCallWSBaseDirective implements OnInit, OnDestroy {
}

/**
* Directive destroyed.
* @inheritdoc
*/
ngOnDestroy(): void {
this.invalidateObserver?.unsubscribe();
Expand Down
16 changes: 10 additions & 6 deletions src/core/features/siteplugins/classes/compile-init-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class CoreSitePluginsCompileInitComponent {
jsData: Record<string, unknown> = {}; // Data to pass to the component.
protected handlerSchema?: CoreSitePluginsInitHandlerData; // The handler data.

stylesPath?: string; // Styles to apply to the component.

/**
* Function called when the component is created.
*
Expand All @@ -40,18 +42,18 @@ export class CoreSitePluginsCompileInitComponent {
/**
* Get the handler data.
*
* @param name The name of the handler.
* @param handlerName The name of the handler.
*/
getHandlerData(name: string): void {
async getHandlerData(handlerName: string): Promise<void> {
// Retrieve the handler data.
const handler = CoreSitePlugins.getSitePluginHandler(name);

this.handlerSchema = handler?.handlerSchema;
const handler = CoreSitePlugins.getSitePluginHandler(handlerName);

if (!this.handlerSchema) {
if (!handler?.handlerSchema) {
return;
}

this.handlerSchema = handler.handlerSchema;

// Load first template.
if (this.handlerSchema.methodTemplates?.length) {
this.content = this.handlerSchema.methodTemplates[0].html;
Expand All @@ -70,6 +72,8 @@ export class CoreSitePluginsCompileInitComponent {
if (this.handlerSchema.methodJSResult) {
this.jsData.CONTENT_JS_RESULT = this.handlerSchema.methodJSResult;
}

this.stylesPath = await CoreSitePlugins.getHandlerDownloadedStyles(handlerName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,28 @@ export class CoreSitePluginsCourseOptionHandler extends CoreSitePluginsBaseHandl
/**
* @inheritdoc
*/
getDisplayData(): CoreCourseOptionsHandlerData {
async getDisplayData(): Promise<CoreCourseOptionsHandlerData> {
const stylesPath = await this.handlerSchema.styles?.downloadedStyles;

return {
title: this.title,
class: this.handlerSchema.displaydata?.class,
page: `siteplugins/${this.name}`,
pageParams: {},
pageParams: {
stylesPath,
},
};
}

/**
* @inheritdoc
*/
getMenuDisplayData(course: CoreCourseAnyCourseDataWithOptions): CoreCourseOptionsMenuHandlerData {
async getMenuDisplayData(course: CoreCourseAnyCourseDataWithOptions): Promise<CoreCourseOptionsMenuHandlerData> {
const args = {
courseid: course.id,
};
const hash = Md5.hashAsciiStr(JSON.stringify(args));
const stylesPath = await this.handlerSchema.styles?.downloadedStyles;

return {
title: this.title,
Expand All @@ -99,6 +104,7 @@ export class CoreSitePluginsCourseOptionHandler extends CoreSitePluginsBaseHandl
args,
initResult: this.initResult,
ptrEnabled: this.handlerSchema.ptrenabled,
stylesPath,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import { CoreMainMenuHandler, CoreMainMenuHandlerData } from '@features/mainmenu/services/mainmenu-delegate';
import {
CoreSitePlugins,
CoreSitePluginsContent,
CoreSitePluginsMainMenuHandlerData,
CoreSitePluginsPlugin,
Expand Down Expand Up @@ -43,6 +44,8 @@ export class CoreSitePluginsMainMenuHandler extends CoreSitePluginsBaseHandler i
* @inheritdoc
*/
getDisplayData(): CoreMainMenuHandlerData {
const handlerName = CoreSitePlugins.getHandlerNameFromUniqueName(this.name, this.plugin.addon);

return {
title: this.title,
icon: this.handlerSchema.displaydata?.icon || 'fas-question',
Expand All @@ -52,6 +55,7 @@ export class CoreSitePluginsMainMenuHandler extends CoreSitePluginsBaseHandler i
title: this.title,
initResult: this.initResult,
ptrEnabled: this.handlerSchema.ptrenabled,
handlerName,
},
onlyInMore: true,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import { AddonMessageOutputHandler, AddonMessageOutputHandlerData } from '@addons/messageoutput/services/messageoutput-delegate';
import {
CoreSitePlugins,
CoreSitePluginsContent,
CoreSitePluginsMessageOutputHandlerData,
CoreSitePluginsPlugin,
Expand All @@ -40,6 +41,8 @@ export class CoreSitePluginsMessageOutputHandler extends CoreSitePluginsBaseHand
* @inheritdoc
*/
getDisplayData(): AddonMessageOutputHandlerData {
const handlerName = CoreSitePlugins.getHandlerNameFromUniqueName(this.name, this.plugin.addon);

return {
priority: this.handlerSchema.priority || 0,
label: this.title,
Expand All @@ -49,6 +52,7 @@ export class CoreSitePluginsMessageOutputHandler extends CoreSitePluginsBaseHand
title: this.title,
initResult: this.initResult,
ptrEnabled: this.handlerSchema.ptrenabled,
handlerName,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import { CoreSettingsHandler, CoreSettingsHandlerData } from '@features/settings/services/settings-delegate';
import {
CoreSitePlugins,
CoreSitePluginsContent,
CoreSitePluginsPlugin,
CoreSitePluginsSettingsHandlerData,
Expand Down Expand Up @@ -45,6 +46,8 @@ export class CoreSitePluginsSettingsHandler extends CoreSitePluginsBaseHandler i
* @returns Data.
*/
getDisplayData(): CoreSettingsHandlerData {
const handlerName = CoreSitePlugins.getHandlerNameFromUniqueName(this.name, this.plugin.addon);

return {
title: this.title,
icon: this.handlerSchema.displaydata?.icon,
Expand All @@ -54,6 +57,7 @@ export class CoreSitePluginsSettingsHandler extends CoreSitePluginsBaseHandler i
title: this.title,
initResult: this.initResult,
ptrEnabled: this.handlerSchema.ptrenabled,
handlerName,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class CoreSitePluginsUserProfileHandler extends CoreSitePluginsBaseHandle
* @inheritdoc
*/
getDisplayData(): CoreUserProfileHandlerData {

return {
title: this.title,
icon: this.handlerSchema.displaydata?.icon,
Expand All @@ -94,6 +95,8 @@ export class CoreSitePluginsUserProfileHandler extends CoreSitePluginsBaseHandle
event.preventDefault();
event.stopPropagation();

const handlerName = CoreSitePlugins.getHandlerNameFromUniqueName(this.name, this.plugin.addon);

const args = {
courseid: contextId,
userid: user.id,
Expand All @@ -108,6 +111,7 @@ export class CoreSitePluginsUserProfileHandler extends CoreSitePluginsBaseHandle
args,
initResult: this.initResult,
ptrEnabled: this.handlerSchema.ptrenabled,
handlerName,
},
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class CoreSitePluginsAssignFeedbackComponent extends CoreSitePluginsCompi
/**
* @inheritdoc
*/
ngOnInit(): void {
async ngOnInit(): Promise<void> {
// Pass the input and output data to the component.
this.jsData.assign = this.assign;
this.jsData.submission = this.submission;
Expand All @@ -50,7 +50,7 @@ export class CoreSitePluginsAssignFeedbackComponent extends CoreSitePluginsCompi
this.jsData.canEdit = this.canEdit;

if (this.plugin) {
this.getHandlerData(AddonModAssignFeedbackDelegate.getHandlerName(this.plugin.type));
await this.getHandlerData(AddonModAssignFeedbackDelegate.getHandlerName(this.plugin.type));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<core-compile-html [text]="content" [jsData]="jsData" (created)="componentCreated($event)" />
<core-compile-html [text]="content" [jsData]="jsData" (created)="componentCreated($event)" [stylesPath]="stylesPath" />
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class CoreSitePluginsAssignSubmissionComponent extends CoreSitePluginsCom
/**
* @inheritdoc
*/
ngOnInit(): void {
async ngOnInit(): Promise<void> {
// Pass the input and output data to the component.
this.jsData.assign = this.assign;
this.jsData.submission = this.submission;
Expand All @@ -48,7 +48,7 @@ export class CoreSitePluginsAssignSubmissionComponent extends CoreSitePluginsCom
this.jsData.allowOffline = this.allowOffline;

if (this.plugin) {
this.getHandlerData(AddonModAssignSubmissionDelegate.getHandlerName(this.plugin.type));
await this.getHandlerData(AddonModAssignSubmissionDelegate.getHandlerName(this.plugin.type));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<core-compile-html [text]="content" [jsData]="jsData" (created)="componentCreated($event)" />
<core-compile-html [text]="content" [jsData]="jsData" (created)="componentCreated($event)" [stylesPath]="stylesPath" />
11 changes: 6 additions & 5 deletions src/core/features/siteplugins/components/block/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ export class CoreSitePluginsBlockComponent extends CoreBlockBaseComponent implem
args?: Record<string, unknown>;
jsData?: Record<string, unknown>; // Data to pass to the component.
initResult?: CoreSitePluginsContent | null;
stylesPath?: string; // Styles to apply to the component.

constructor() {
super('CoreSitePluginsBlockComponent');
}

/**
* Detect changes on input properties.
* @inheritdoc
*/
ngOnChanges(): void {
async ngOnChanges(): Promise<void> {
if (this.component) {
return;
}
Expand All @@ -67,12 +68,12 @@ export class CoreSitePluginsBlockComponent extends CoreBlockBaseComponent implem
block: this.block,
};
this.initResult = handler.initResult;

this.stylesPath = await CoreSitePlugins.getHandlerDownloadedStyles(handlerName);
}

/**
* Invalidate block data.
*
* @returns Promise resolved when done.
* @inheritdoc
*/
async invalidateContent(): Promise<void> {
if (!this.component || !this.method) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<core-site-plugins-plugin-content *ngIf="component && method" [component]="component" [method]="method" [args]="args"
[initResult]="initResult" [data]="jsData" />
[initResult]="initResult" [data]="jsData" [stylesPath]="stylesPath" />
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<core-site-plugins-plugin-content *ngIf="component && method" [component]="component" [method]="method" [args]="args"
[initResult]="initResult" [data]="data" />
[initResult]="initResult" [data]="data" [stylesPath]="stylesPath" />
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ export class CoreSitePluginsCourseFormatComponent implements OnChanges {
args?: Record<string, unknown>;
initResult?: CoreSitePluginsContent | null;
data?: Record<string, unknown>;
stylesPath?: string; // Styles to apply to the component.

/**
* @inheritdoc
*/
ngOnChanges(): void {
async ngOnChanges(): Promise<void> {
if (!this.course || !this.course.format) {
return;
}
Expand All @@ -70,6 +71,8 @@ export class CoreSitePluginsCourseFormatComponent implements OnChanges {
courseid: this.course.id,
};
this.initResult = handler.initResult;

this.stylesPath = await CoreSitePlugins.getHandlerDownloadedStyles(handlerName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<core-site-plugins-plugin-content *ngIf="component && method" [component]="component" [method]="method" [args]="args"
[initResult]="initResult" [data]="jsData" [pageTitle]="pageTitle" [preSets]="preSets" (onContentLoaded)="contentLoaded($event)"
(onLoadingContent)="contentLoading()" />
(onLoadingContent)="contentLoading()" [stylesPath]="stylesPath" />

<core-course-module-navigation collapsible-footer [appearOnBottom]="collapsibleFooterAppearOnBottom" *ngIf="module" [courseId]="courseId"
[currentModuleId]="module.id" />
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C
isDestroyed = false;

jsData?: Record<string, unknown>; // Data to pass to the component.
stylesPath?: string; // Styles to apply to the component.

/**
* @inheritdoc
*/
ngOnInit(): void {
async ngOnInit(): Promise<void> {
if (!this.module) {
return;
}
Expand Down Expand Up @@ -110,6 +111,8 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C
this.ptrEnabled = !CoreUtils.isFalseOrZero(handlerSchema.ptrenabled);

this.collapsibleFooterAppearOnBottom = !CoreUtils.isFalseOrZero(handlerSchema.isresource);

this.stylesPath = await CoreSitePlugins.getHandlerDownloadedStyles(handlerName);
}

// Get the data for the context menu.
Expand All @@ -132,6 +135,8 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C

/**
* Function called when the data of the site plugin content is loaded.
*
* @param data Data received.
*/
contentLoaded(data: CoreSitePluginsPluginContentLoadedData): void {
this.addDefaultModuleInfo = !data.content.includes('<core-course-module-info');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class CoreSitePluginsOnlyTitleBlockComponent extends CoreBlockBaseCompone
args,
initResult: handler.initResult,
ptrEnabled: (<CoreSitePluginsUserHandlerData> handler.handlerSchema).ptrenabled,
handlerName,
},
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<core-loading [hideUntil]="dataLoaded" [fullscreen]="false">
<core-compile-html [text]="content" [javascript]="javascript" [jsData]="jsData" [forceCompile]="forceCompile" #compile />
<core-compile-html [text]="content" [javascript]="javascript" [jsData]="jsData" [forceCompile]="forceCompile" [stylesPath]="stylesPath"
#compile />
</core-loading>
Loading

0 comments on commit ae6eaae

Please sign in to comment.