From b9d1ea38fb148a992a08ef3ca9a5fc503b105a4e Mon Sep 17 00:00:00 2001 From: Shlomi Assaf Date: Mon, 16 Nov 2020 00:03:15 +0200 Subject: [PATCH] fix(ngrid/block-ui): wait for grid init before creating view --- .../src/lib/block-ui/block-ui-plugin.ts | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/libs/ngrid/block-ui/src/lib/block-ui/block-ui-plugin.ts b/libs/ngrid/block-ui/src/lib/block-ui/block-ui-plugin.ts index 75f361c6b..64711aba0 100644 --- a/libs/ngrid/block-ui/src/lib/block-ui/block-ui-plugin.ts +++ b/libs/ngrid/block-ui/src/lib/block-ui/block-ui-plugin.ts @@ -53,10 +53,12 @@ export class PblNgridBlockUiPluginDirective implements OnDestroy { utils.unrx.kill(this, this._blockUi); } this._blockUi = value; - value.pipe(utils.unrx(this, this._blockUi)).subscribe( state => { - this._blockInProgress = state; - this.setupBlocker(); - }); + value + .pipe(utils.unrx(this, this._blockUi)) + .subscribe( state => { + this._blockInProgress = state; + this.setupBlocker(); + }); } else if (this._blockUi !== coerced) { this._blockUi = coerced; if (coerced !== 'auto') { @@ -66,7 +68,7 @@ export class PblNgridBlockUiPluginDirective implements OnDestroy { } } - private _blockInProgress: boolean = false; + private _blockInProgress = false; private _blockUi: boolean | 'auto' | Observable; private _blockerEmbeddedVRef: EmbeddedViewRef; private _removePlugin: (grid: PblNgridComponent) => void; @@ -84,6 +86,13 @@ export class PblNgridBlockUiPluginDirective implements OnDestroy { } }); + pluginCtrl.onInit() + .subscribe( isInitNow => { + if (isInitNow && this._blockUi && typeof this._blockUi === 'boolean') { + this.setupBlocker(); + } + }); + pluginCtrl.events .subscribe( event => { if (event.kind === 'onDataSource') { @@ -111,25 +120,26 @@ export class PblNgridBlockUiPluginDirective implements OnDestroy { }); } - ngOnDestroy(): void { utils.unrx.kill(this); this._removePlugin(this.grid); } private setupBlocker(): void { - const state = this._blockInProgress; - if (state) { - if (!this._blockerEmbeddedVRef) { - const blockerTemplate = this.grid.registry.getSingle('blocker'); - if (blockerTemplate) { - this._blockerEmbeddedVRef = this.grid.createView('afterContent', blockerTemplate.tRef, { $implicit: this.grid }); - this._blockerEmbeddedVRef.detectChanges(); + if (this.grid.isInit) { + const state = this._blockInProgress; + if (state) { + if (!this._blockerEmbeddedVRef) { + const blockerTemplate = this.grid.registry.getSingle('blocker'); + if (blockerTemplate) { + this._blockerEmbeddedVRef = this.grid.createView('afterContent', blockerTemplate.tRef, { $implicit: this.grid }); + this._blockerEmbeddedVRef.detectChanges(); + } } - } - } else if (this._blockerEmbeddedVRef) { - this.grid.removeView(this._blockerEmbeddedVRef, 'afterContent'); - this._blockerEmbeddedVRef = undefined; + } else if (this._blockerEmbeddedVRef) { + this.grid.removeView(this._blockerEmbeddedVRef, 'afterContent'); + this._blockerEmbeddedVRef = undefined; + } } } }