MOBILE-3029 core: Let site plugins override NavController events

main
Dani Palou 2019-07-08 15:41:14 +02:00
parent 6557793227
commit 632a0dc57a
6 changed files with 165 additions and 1 deletions

View File

@ -60,6 +60,7 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck {
protected element;
protected differ: any; // To detect changes in the jsData input.
protected creatingComponent = false;
protected pendingCalls = {};
constructor(protected compileProvider: CoreCompileProvider, protected cdr: ChangeDetectorRef, element: ElementRef,
@Optional() protected navCtrl: NavController, differs: KeyValueDiffers, protected domUtils: CoreDomUtilsProvider,
@ -165,6 +166,22 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck {
if (compileInstance.javascript) {
compileInstance.compileProvider.executeJavascript(this, compileInstance.javascript);
}
// Call the pending functions.
for (const name in compileInstance.pendingCalls) {
const pendingCall = compileInstance.pendingCalls[name];
if (typeof this[name] == 'function') {
// Call the function.
Promise.resolve(this[name].apply(this, pendingCall.params)).then(pendingCall.defer.resolve)
.catch(pendingCall.defer.reject);
} else {
// Function not defined, resolve the promise.
pendingCall.defer.resolve();
}
}
compileInstance.pendingCalls = {};
}
/**
@ -200,4 +217,39 @@ export class CoreCompileHtmlComponent implements OnChanges, OnDestroy, DoCheck {
}
}
}
/**
* Call a certain function on the component instance.
*
* @param {string} name Name of the function to call.
* @param {any[]} params List of params to send to the function.
* @param {boolean} [callWhenCreated=true] If this param is true and the component hasn't been created yet, call the function
* once the component has been created.
* @return {any} Result of the call. Undefined if no component instance or the function doesn't exist.
*/
callComponentFunction(name: string, params?: any[], callWhenCreated: boolean = true): any {
if (this.componentInstance) {
if (typeof this.componentInstance[name] == 'function') {
return this.componentInstance[name].apply(this.componentInstance, params);
}
} else if (callWhenCreated) {
// Call it when the component is created.
if (this.pendingCalls[name]) {
// Call already pending, just update the params (allow only 1 call per function until it's initialized).
this.pendingCalls[name].params = params;
return this.pendingCalls[name].defer.promise;
}
const defer = this.utils.promiseDefer();
this.pendingCalls[name] = {
params: params,
defer: defer
};
return defer.promise;
}
}
}

View File

@ -13,7 +13,7 @@
<ion-item text-center *ngIf="(!handlers || !handlers.length) && !handlersLoaded">
<ion-spinner></ion-spinner>
</ion-item>
<a ion-item *ngFor="let handler of handlers" [ngClass]="['core-moremenu-handler', handler.class]" (click)="openHandler(handler)" title="{{ handler.title | translate }}" detail-push>
<a ion-item *ngFor="let handler of handlers" [ngClass]="['core-moremenu-handler', handler.class || '']" (click)="openHandler(handler)" title="{{ handler.title | translate }}" detail-push>
<core-icon [name]="handler.icon" item-start></core-icon>
<p>{{ handler.title | translate}}</p>
<ion-badge item-end *ngIf="handler.showBadge" [hidden]="handler.loading || !handler.badge">{{handler.badge}}</ion-badge>

View File

@ -169,4 +169,15 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C
this.isDestroyed = true;
this.statusObserver && this.statusObserver.off();
}
/**
* Call a certain function on the component instance.
*
* @param {string} name Name of the function to call.
* @param {any[]} params List of params to send to the function.
* @return {any} Result of the call. Undefined if no component instance or the function doesn't exist.
*/
callComponentFunction(name: string, params?: any[]): any {
return this.content.callComponentFunction(name, params);
}
}

View File

@ -176,4 +176,17 @@ export class CoreSitePluginsPluginContentComponent implements OnInit, DoCheck {
this.fetchContent();
}
/**
* Call a certain function on the component instance.
*
* @param {string} name Name of the function to call.
* @param {any[]} params List of params to send to the function.
* @return {any} Result of the call. Undefined if no component instance or the function doesn't exist.
*/
callComponentFunction(name: string, params?: any[]): any {
if (this.compileComponent) {
return (<any> this.compileComponent).callComponentFunction(name, params);
}
}
}

View File

@ -48,4 +48,48 @@ export class CoreSitePluginsModuleIndexPage {
refresher.complete();
});
}
/**
* The page is about to enter and become the active page.
*/
ionViewWillEnter(): void {
this.content.callComponentFunction('ionViewWillEnter');
}
/**
* The page has fully entered and is now the active page. This event will fire, whether it was the first load or a cached page.
*/
ionViewDidEnter(): void {
this.content.callComponentFunction('ionViewDidEnter');
}
/**
* The page is about to leave and no longer be the active page.
*/
ionViewWillLeave(): void {
this.content.callComponentFunction('ionViewWillLeave');
}
/**
* The page has finished leaving and is no longer the active page.
*/
ionViewDidLeave(): void {
this.content.callComponentFunction('ionViewDidLeave');
}
/**
* The page is about to be destroyed and have its elements removed.
*/
ionViewWillUnload(): void {
this.content.callComponentFunction('ionViewWillUnload');
}
/**
* Check if we can leave the page or not.
*
* @return {boolean|Promise<void>} Resolved if we can leave it, rejected if not.
*/
ionViewCanLeave(): boolean | Promise<void> {
return this.content.callComponentFunction('ionViewCanLeave');
}
}

View File

@ -56,4 +56,48 @@ export class CoreSitePluginsPluginPage {
refresher.complete();
});
}
/**
* The page is about to enter and become the active page.
*/
ionViewWillEnter(): void {
this.content.callComponentFunction('ionViewWillEnter');
}
/**
* The page has fully entered and is now the active page. This event will fire, whether it was the first load or a cached page.
*/
ionViewDidEnter(): void {
this.content.callComponentFunction('ionViewDidEnter');
}
/**
* The page is about to leave and no longer be the active page.
*/
ionViewWillLeave(): void {
this.content.callComponentFunction('ionViewWillLeave');
}
/**
* The page has finished leaving and is no longer the active page.
*/
ionViewDidLeave(): void {
this.content.callComponentFunction('ionViewDidLeave');
}
/**
* The page is about to be destroyed and have its elements removed.
*/
ionViewWillUnload(): void {
this.content.callComponentFunction('ionViewWillUnload');
}
/**
* Check if we can leave the page or not.
*
* @return {boolean|Promise<void>} Resolved if we can leave it, rejected if not.
*/
ionViewCanLeave(): boolean | Promise<void> {
return this.content.callComponentFunction('ionViewCanLeave');
}
}