From 4e231f5c27a3b0b194d7e8c6466c45297db8e4f7 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 7 May 2020 11:48:55 +0200 Subject: [PATCH] MOBILE-3417 siteplugins: Let site plugins specify a fallback block --- .../classes/handlers/block-handler.ts | 32 +++++++++++++++---- src/core/siteplugins/providers/helper.ts | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/core/siteplugins/classes/handlers/block-handler.ts b/src/core/siteplugins/classes/handlers/block-handler.ts index a7cf18060..8ce5e433f 100644 --- a/src/core/siteplugins/classes/handlers/block-handler.ts +++ b/src/core/siteplugins/classes/handlers/block-handler.ts @@ -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 { - let className, - component; + async getDisplayData(injector: Injector, block: any, contextLevel: string, instanceId: number): Promise { + 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; } diff --git a/src/core/siteplugins/providers/helper.ts b/src/core/siteplugins/providers/helper.ts index caa617a9e..7fa9790e9 100644 --- a/src/core/siteplugins/providers/helper.ts +++ b/src/core/siteplugins/providers/helper.ts @@ -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; }