MOBILE-2333 siteplugins: Make content functions available in view

main
Dani Palou 2018-05-02 16:32:55 +02:00
parent eff08198e1
commit 2db7656666
8 changed files with 49 additions and 24 deletions

View File

@ -63,6 +63,6 @@ export class CoreSitePluginsCourseFormatComponent implements OnInit {
* @return {Promise<any>} Promise resolved when done. * @return {Promise<any>} Promise resolved when done.
*/ */
doRefresh(refresher?: any, done?: () => void): Promise<any> { doRefresh(refresher?: any, done?: () => void): Promise<any> {
return Promise.resolve(this.content.refreshData()); return Promise.resolve(this.content.refreshContent(false));
} }
} }

View File

@ -59,7 +59,7 @@ export class CoreSitePluginsCourseOptionComponent implements OnInit {
* @param {any} refresher Refresher. * @param {any} refresher Refresher.
*/ */
refreshData(refresher: any): void { refreshData(refresher: any): void {
this.content.refreshData().finally(() => { this.content.refreshContent(false).finally(() => {
refresher.complete(); refresher.complete();
}); });
} }

View File

@ -89,7 +89,7 @@ export class CoreSitePluginsModuleIndexComponent implements OnInit, OnDestroy, C
if (this.content) { if (this.content) {
this.refreshIcon = 'spinner'; this.refreshIcon = 'spinner';
return Promise.resolve(this.content.refreshData()).finally(() => { return Promise.resolve(this.content.refreshContent(false)).finally(() => {
refresher && refresher.complete(); refresher && refresher.complete();
done && done(); done && done();
}); });

View File

@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { Component, OnInit, Input, Output, EventEmitter, Optional } from '@angular/core';
import { NavController } from 'ionic-angular';
import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreSitePluginsProvider } from '../../providers/siteplugins'; import { CoreSitePluginsProvider } from '../../providers/siteplugins';
import { Subject } from 'rxjs'; import { Subject } from 'rxjs';
@ -39,7 +40,8 @@ export class CoreSitePluginsPluginContentComponent implements OnInit {
invalidateObservable: Subject<void>; // An observable to notify observers when to invalidate data. invalidateObservable: Subject<void>; // An observable to notify observers when to invalidate data.
jsData: any; // Data to pass to the component. jsData: any; // Data to pass to the component.
constructor(protected domUtils: CoreDomUtilsProvider, protected sitePluginsProvider: CoreSitePluginsProvider) { constructor(protected domUtils: CoreDomUtilsProvider, protected sitePluginsProvider: CoreSitePluginsProvider,
@Optional() protected navCtrl: NavController) {
this.onContentLoaded = new EventEmitter(); this.onContentLoaded = new EventEmitter();
this.onLoadingContent = new EventEmitter(); this.onLoadingContent = new EventEmitter();
this.invalidateObservable = new Subject<void>(); this.invalidateObservable = new Subject<void>();
@ -67,6 +69,11 @@ export class CoreSitePluginsPluginContentComponent implements OnInit {
this.otherData = result.otherdata; this.otherData = result.otherdata;
this.jsData = this.sitePluginsProvider.createDataForJS(this.initResult, result); this.jsData = this.sitePluginsProvider.createDataForJS(this.initResult, result);
// Pass some methods as jsData so they can be called from the template too.
this.jsData.openContent = this.openContent.bind(this);
this.jsData.refreshContent = this.refreshContent.bind(this);
this.jsData.updateContent = this.updateContent.bind(this);
this.onContentLoaded.emit(refresh); this.onContentLoaded.emit(refresh);
}).catch((error) => { }).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'core.errorloadingcontent', true); this.domUtils.showErrorModalDefault(error, 'core.errorloadingcontent', true);
@ -75,12 +82,30 @@ export class CoreSitePluginsPluginContentComponent implements OnInit {
}); });
} }
/**
* Open a new page with a new content.
*
* @param {string} title The title to display with the new content.
* @param {any} args New params.
* @param {string} [component] New component. If not provided, current component
* @param {string} [method] New method. If not provided, current method
*/
openContent(title: string, args: any, component?: string, method?: string): void {
this.navCtrl.push('CoreSitePluginsPluginPage', {
title: title,
component: component || this.component,
method: method || this.method,
args: args,
initResult: this.initResult
});
}
/** /**
* Refresh the data. * Refresh the data.
* *
* @param {boolean} [showSpinner] Whether to show spinner while refreshing. * @param {boolean} [showSpinner=true] Whether to show spinner while refreshing.
*/ */
refreshData(showSpinner?: boolean): Promise<any> { refreshContent(showSpinner: boolean = true): Promise<any> {
if (showSpinner) { if (showSpinner) {
this.dataLoaded = false; this.dataLoaded = false;
} }
@ -95,13 +120,13 @@ export class CoreSitePluginsPluginContentComponent implements OnInit {
/** /**
* Update the content, usually with a different method or params. * Update the content, usually with a different method or params.
* *
* @param {string} component New component.
* @param {string} method New method.
* @param {any} args New params. * @param {any} args New params.
* @param {string} [component] New component. If not provided, current component
* @param {string} [method] New method. If not provided, current method
*/ */
updateContent(component: string, method: string, args: any): void { updateContent(args: any, component?: string, method?: string): void {
this.component = component; this.component = component || this.component;
this.method = method; this.method = method || this.method;
this.args = args; this.args = args;
this.dataLoaded = false; this.dataLoaded = false;

View File

@ -53,8 +53,8 @@ import { CoreSitePluginsPluginContentComponent } from '../components/plugin-cont
selector: '[core-site-plugins-call-ws-new-content]' selector: '[core-site-plugins-call-ws-new-content]'
}) })
export class CoreSitePluginsCallWSNewContentDirective extends CoreSitePluginsCallWSOnClickBaseDirective { export class CoreSitePluginsCallWSNewContentDirective extends CoreSitePluginsCallWSOnClickBaseDirective {
@Input() component: string; // The component of the new content. @Input() component: string; // The component of the new content. If not provided, use the same component as current page.
@Input() method: string; // The method to get the new content. @Input() method: string; // The method to get the new content. If not provided, use the same method as current page.
@Input() args: any; // The params to get the new content. @Input() args: any; // The params to get the new content.
@Input() title: string; // The title to display with the new content. Only if samePage=false. @Input() title: string; // The title to display with the new content. Only if samePage=false.
@Input() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page. @Input() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page.
@ -84,13 +84,13 @@ export class CoreSitePluginsCallWSNewContentDirective extends CoreSitePluginsCal
if (this.utils.isTrueOrOne(this.samePage)) { if (this.utils.isTrueOrOne(this.samePage)) {
// Update the parent content (if it exists). // Update the parent content (if it exists).
if (this.parentContent) { if (this.parentContent) {
this.parentContent.updateContent(this.component, this.method, args); this.parentContent.updateContent(args, this.component, this.method);
} }
} else { } else {
this.navCtrl.push('CoreSitePluginsPluginPage', { this.navCtrl.push('CoreSitePluginsPluginPage', {
title: this.title, title: this.title,
component: this.component, component: this.component || (this.parentContent && this.parentContent.component),
method: this.method, method: this.method || (this.parentContent && this.parentContent.method),
args: args, args: args,
initResult: this.parentContent && this.parentContent.initResult initResult: this.parentContent && this.parentContent.initResult
}); });

View File

@ -74,7 +74,7 @@ export class CoreSitePluginsCallWSDirective extends CoreSitePluginsCallWSOnClick
if (this.utils.isTrueOrOne(this.goBackOnSuccess)) { if (this.utils.isTrueOrOne(this.goBackOnSuccess)) {
this.navCtrl.pop(); this.navCtrl.pop();
} else if (this.utils.isTrueOrOne(this.refreshOnSuccess) && this.parentContent) { } else if (this.utils.isTrueOrOne(this.refreshOnSuccess) && this.parentContent) {
this.parentContent.refreshData(true); this.parentContent.refreshContent(true);
} }
} }
} }

View File

@ -43,8 +43,8 @@ import { CoreSitePluginsPluginContentComponent } from '../components/plugin-cont
selector: '[core-site-plugins-new-content]' selector: '[core-site-plugins-new-content]'
}) })
export class CoreSitePluginsNewContentDirective implements OnInit { export class CoreSitePluginsNewContentDirective implements OnInit {
@Input() component: string; // The component of the new content. @Input() component: string; // The component of the new content. If not provided, use the same component as current page.
@Input() method: string; // The method to get the new content. @Input() method: string; // The method to get the new content. If not provided, use the same method as current page.
@Input() args: any; // The params to get the new content. @Input() args: any; // The params to get the new content.
@Input() title: string; // The title to display with the new content. Only if samePage=false. @Input() title: string; // The title to display with the new content. Only if samePage=false.
@Input() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page. @Input() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page.
@ -81,13 +81,13 @@ export class CoreSitePluginsNewContentDirective implements OnInit {
if (this.utils.isTrueOrOne(this.samePage)) { if (this.utils.isTrueOrOne(this.samePage)) {
// Update the parent content (if it exists). // Update the parent content (if it exists).
if (this.parentContent) { if (this.parentContent) {
this.parentContent.updateContent(this.component, this.method, args); this.parentContent.updateContent(args, this.component, this.method);
} }
} else { } else {
this.navCtrl.push('CoreSitePluginsPluginPage', { this.navCtrl.push('CoreSitePluginsPluginPage', {
title: this.title, title: this.title,
component: this.component, component: this.component || (this.parentContent && this.parentContent.component),
method: this.method, method: this.method || (this.parentContent && this.parentContent.method),
args: args, args: args,
initResult: this.parentContent && this.parentContent.initResult initResult: this.parentContent && this.parentContent.initResult
}); });

View File

@ -48,7 +48,7 @@ export class CoreSitePluginsPluginPage {
* @param {any} refresher Refresher. * @param {any} refresher Refresher.
*/ */
refreshData(refresher: any): void { refreshData(refresher: any): void {
this.content.refreshData().finally(() => { this.content.refreshContent(false).finally(() => {
refresher.complete(); refresher.complete();
}); });
} }