// (C) Copyright 2015 Martin Dougiamas // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import { Directive, Input, ElementRef, Optional } from '@angular/core'; import { NavController } from 'ionic-angular'; import { TranslateService } from '@ngx-translate/core'; import { CoreDomUtilsProvider } from '../../../providers/utils/dom'; import { CoreUtilsProvider } from '../../../providers/utils/utils'; import { CoreSiteAddonsProvider } from '../providers/siteaddons'; import { CoreSiteAddonsCallWSOnClickBaseDirective } from '../classes/call-ws-click-directive'; import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content'; /** * Directive to call a WS when the element is clicked and load a new content passing the WS result as args. This new content * can be displayed in a new page or in the same page (only if current page is already displaying a site addon content). * * If you don't need to load some new content when done, @see CoreSiteAddonsCallWSDirective. * * @see CoreSiteAddonsCallWSOnClickBaseDirective. * * Example usages: * * A button to get some data from the server without using cache, showing default confirm and displaying a new page: * * * * A button to get some data from the server using cache, without confirm, displaying new content in same page and using * userid from otherdata: * * */ @Directive({ selector: '[core-site-addons-call-ws-new-content]' }) export class CoreSiteAddonsCallWSNewContentDirective extends CoreSiteAddonsCallWSOnClickBaseDirective { @Input() component: string; // The component of the new content. @Input() method: string; // The method 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() samePage: boolean | string; // Whether to display the content in same page or open a new one. Defaults to new page. @Input() useOtherData: any[]; // Whether to include other data in the args. @see CoreSiteAddonsProvider.loadOtherDataInArgs. constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider, siteAddonsProvider: CoreSiteAddonsProvider, @Optional() parentContent: CoreSiteAddonsAddonContentComponent, protected utils: CoreUtilsProvider, protected navCtrl: NavController) { super(element, translate, domUtils, siteAddonsProvider, parentContent); } /** * Function called when the WS call is successful. * * @param {any} result Result of the WS call. */ protected wsCallSuccess(result: any): void { let args = this.args || {}; if (this.parentContent) { args = this.siteAddonsProvider.loadOtherDataInArgs(this.args, this.parentContent.otherData, this.useOtherData); } // Add the properties from the WS call result to the args. args = Object.assign(args, result); if (this.utils.isTrueOrOne(this.samePage)) { // Update the parent content (if it exists). if (this.parentContent) { this.parentContent.updateContent(this.component, this.method, args); } } else { this.navCtrl.push('CoreSiteAddonsAddonPage', { title: this.title, component: this.component, method: this.method, args: args }); } } }