Merge pull request #2368 from dpalou/MOBILE-3417

MOBILE-3417 siteplugins: Let site plugins specify a fallback block
main
Juan Leyva 2020-05-07 18:59:21 +02:00 committed by GitHub
commit 648c00e5be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 7 deletions

View File

@ -13,8 +13,9 @@
// limitations under the License.
import { Injector } from '@angular/core';
import { CoreLogger } from '@providers/logger';
import { CoreSitePluginsBaseHandler } from './base-handler';
import { CoreBlockHandler, CoreBlockHandlerData } from '@core/block/providers/delegate';
import { CoreBlockDelegate, CoreBlockHandler, CoreBlockHandlerData } from '@core/block/providers/delegate';
import { CoreBlockPreRenderedComponent } from '@core/block/components/pre-rendered-block/pre-rendered-block';
import { CoreSitePluginsBlockComponent } from '@core/siteplugins/components/block/block';
import { CoreSitePluginsOnlyTitleBlockComponent } from '@core/siteplugins/components/only-title-block/only-title-block';
@ -24,9 +25,13 @@ import { CoreSitePluginsOnlyTitleBlockComponent } from '@core/siteplugins/compon
*/
export class CoreSitePluginsBlockHandler extends CoreSitePluginsBaseHandler implements CoreBlockHandler {
protected logger;
constructor(name: string, public title: string, public blockName: string, protected handlerSchema: any,
protected initResult: any) {
protected initResult: any, protected blockDelegate: CoreBlockDelegate) {
super(name);
this.logger = CoreLogger.instance.getInstance('CoreSitePluginsBlockHandler');
}
/**
@ -39,10 +44,9 @@ export class CoreSitePluginsBlockHandler extends CoreSitePluginsBaseHandler impl
* @param instanceId Instance id (not used)
* @return Data or promise resolved with the data
*/
getDisplayData(injector: Injector, block: any, contextLevel: string, instanceId: number):
CoreBlockHandlerData | Promise<CoreBlockHandlerData> {
let className,
component;
async getDisplayData(injector: Injector, block: any, contextLevel: string, instanceId: number): Promise<CoreBlockHandlerData> {
let className;
let component;
if (this.handlerSchema.displaydata && this.handlerSchema.displaydata.class) {
className = this.handlerSchema.displaydata.class;
@ -54,6 +58,22 @@ export class CoreSitePluginsBlockHandler extends CoreSitePluginsBaseHandler impl
component = CoreSitePluginsOnlyTitleBlockComponent;
} else if (this.handlerSchema.displaydata && this.handlerSchema.displaydata.type == 'prerendered') {
component = CoreBlockPreRenderedComponent;
} else if (this.handlerSchema.fallback && !this.handlerSchema.method) {
// Try to use the fallback block.
const originalName = block.name;
block.name = this.handlerSchema.fallback;
try {
const displayData = await this.blockDelegate.getBlockDisplayData(injector, block, contextLevel, instanceId);
this.logger.debug(`Using fallback "${this.handlerSchema.fallback}" for block "${originalName}"`);
component = displayData.component;
} catch (error) {
this.logger.error(`Error using fallback "${this.handlerSchema.fallback}" for block "${originalName}", ` +
'maybe it doesn\'t exist or isn\'t enabled.', error);
throw error;
}
} else {
component = CoreSitePluginsBlockComponent;
}

View File

@ -682,7 +682,7 @@ export class CoreSitePluginsHelperProvider {
prefixedTitle = this.getPrefixedString(plugin.addon, titleString);
this.blockDelegate.registerHandler(
new CoreSitePluginsBlockHandler(uniqueName, prefixedTitle, blockName, handlerSchema, initResult));
new CoreSitePluginsBlockHandler(uniqueName, prefixedTitle, blockName, handlerSchema, initResult, this.blockDelegate));
return uniqueName;
}