From 823ea35b69bfbfe273c5fab848291958bd35065b Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 20 Feb 2018 16:28:35 +0100 Subject: [PATCH] MOBILE-2333 siteaddons: Implement directive to call WS on load --- .../classes/call-ws-click-directive.ts | 72 +++++++++++++++++++ .../siteaddons/classes/call-ws-directive.ts | 26 +------ .../directives/call-ws-new-content.ts | 10 ++- .../siteaddons/directives/call-ws-on-load.ts | 58 +++++++++++++++ src/core/siteaddons/directives/call-ws.ts | 10 ++- .../directives/directives.module.ts | 3 + .../pages/module-index/module-index.html | 2 +- 7 files changed, 144 insertions(+), 37 deletions(-) create mode 100644 src/core/siteaddons/classes/call-ws-click-directive.ts create mode 100644 src/core/siteaddons/directives/call-ws-on-load.ts diff --git a/src/core/siteaddons/classes/call-ws-click-directive.ts b/src/core/siteaddons/classes/call-ws-click-directive.ts new file mode 100644 index 000000000..7c055187d --- /dev/null +++ b/src/core/siteaddons/classes/call-ws-click-directive.ts @@ -0,0 +1,72 @@ +// (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 { Input, OnInit, ElementRef } from '@angular/core'; +import { TranslateService } from '@ngx-translate/core'; +import { CoreDomUtilsProvider } from '../../../providers/utils/dom'; +import { CoreSiteAddonsProvider } from '../providers/siteaddons'; +import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content'; +import { CoreSiteAddonsCallWSBaseDirective } from './call-ws-directive'; + +/** + * Base class for directives to call a WS when the element is clicked. + * + * The directives that inherit from this class will call a WS method when the element is clicked. + * + * @see CoreSiteAddonsCallWSBaseDirective + */ +export class CoreSiteAddonsCallWSOnClickBaseDirective extends CoreSiteAddonsCallWSBaseDirective implements OnInit { + @Input() confirmMessage: string; // Message to confirm the action. If not supplied, no confirmation. If empty, default message. + + constructor(element: ElementRef, protected translate: TranslateService, protected domUtils: CoreDomUtilsProvider, + protected siteAddonsProvider: CoreSiteAddonsProvider, protected parentContent: CoreSiteAddonsAddonContentComponent) { + super(element, translate, domUtils, siteAddonsProvider, parentContent); + } + + /** + * Component being initialized. + */ + ngOnInit(): void { + super.ngOnInit(); + + this.element.addEventListener('click', (ev: Event): void => { + ev.preventDefault(); + ev.stopPropagation(); + + if (typeof this.confirmMessage != 'undefined') { + // Ask for confirm. + this.domUtils.showConfirm(this.confirmMessage || this.translate.instant('core.areyousure')).then(() => { + this.callWS(); + }).catch(() => { + // User cancelled, ignore. + }); + } else { + this.callWS(); + } + }); + } + + /** + * Call a WS. + * + * @return {Promise} Promise resolved when done. + */ + protected callWS(): Promise { + const modal = this.domUtils.showModalLoading(); + + return super.callWS().finally(() => { + modal.dismiss(); + }); + } +} diff --git a/src/core/siteaddons/classes/call-ws-directive.ts b/src/core/siteaddons/classes/call-ws-directive.ts index be26166fb..453ac0285 100644 --- a/src/core/siteaddons/classes/call-ws-directive.ts +++ b/src/core/siteaddons/classes/call-ws-directive.ts @@ -20,15 +20,12 @@ import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content import { Subscription } from 'rxjs'; /** - * Base class for directives to call a WS when the element is clicked. - * - * The directives that inherit from this class will call a WS method when the element is clicked. + * Base class for directives that need to call a WS. */ export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy { @Input() name: string; // The name of the WS to call. @Input() params: any; // The params for the WS call. @Input() preSets: any; // The preSets for the WS call. - @Input() confirmMessage: string; // Message to confirm the action. If not supplied, no confirmation. If empty, default message. @Input() useOtherDataForWS: any[]; // Whether to include other data in the params for the WS. // @see CoreSiteAddonsProvider.loadOtherDataInArgs. @Input() form: string; // ID or name to identify a form. The form will be obtained from document.forms. @@ -46,22 +43,6 @@ export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy { * Component being initialized. */ ngOnInit(): void { - this.element.addEventListener('click', (ev: Event): void => { - ev.preventDefault(); - ev.stopPropagation(); - - if (typeof this.confirmMessage != 'undefined') { - // Ask for confirm. - this.domUtils.showConfirm(this.confirmMessage || this.translate.instant('core.areyousure')).then(() => { - this.callWS(); - }).catch(() => { - // User cancelled, ignore. - }); - } else { - this.callWS(); - } - }); - if (this.parentContent && this.parentContent.invalidateObservable) { this.invalidateObserver = this.parentContent.invalidateObservable.subscribe(() => { this.invalidate(); @@ -75,15 +56,12 @@ export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy { * @return {Promise} Promise resolved when done. */ protected callWS(): Promise { - const modal = this.domUtils.showModalLoading(), - params = this.getParamsForWS(); + const params = this.getParamsForWS(); return this.siteAddonsProvider.callWS(this.name, params, this.preSets).then((result) => { return this.wsCallSuccess(result); }).catch((error) => { this.domUtils.showErrorModalDefault(error, 'core.serverconnection', true); - }).finally(() => { - modal.dismiss(); }); } diff --git a/src/core/siteaddons/directives/call-ws-new-content.ts b/src/core/siteaddons/directives/call-ws-new-content.ts index 265927830..38d2cd145 100644 --- a/src/core/siteaddons/directives/call-ws-new-content.ts +++ b/src/core/siteaddons/directives/call-ws-new-content.ts @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core'; +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 { CoreSiteAddonsCallWSBaseDirective } from '../classes/call-ws-directive'; +import { CoreSiteAddonsCallWSOnClickBaseDirective } from '../classes/call-ws-click-directive'; import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content'; /** @@ -27,7 +27,7 @@ import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content * * If you don't need to load some new content when done, @see CoreSiteAddonsCallWSDirective. * - * @see CoreSiteAddonsCallWSBaseDirective. + * @see CoreSiteAddonsCallWSOnClickBaseDirective. * * Example usages: * @@ -52,7 +52,7 @@ import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content @Directive({ selector: '[core-site-addons-call-ws-new-content]' }) -export class CoreSiteAddonsCallWSNewContentDirective extends CoreSiteAddonsCallWSBaseDirective { +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. @@ -60,8 +60,6 @@ export class CoreSiteAddonsCallWSNewContentDirective extends CoreSiteAddonsCallW @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. - protected element: HTMLElement; - constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider, siteAddonsProvider: CoreSiteAddonsProvider, @Optional() parentContent: CoreSiteAddonsAddonContentComponent, protected utils: CoreUtilsProvider, protected navCtrl: NavController) { diff --git a/src/core/siteaddons/directives/call-ws-on-load.ts b/src/core/siteaddons/directives/call-ws-on-load.ts new file mode 100644 index 000000000..9a2a1a8c2 --- /dev/null +++ b/src/core/siteaddons/directives/call-ws-on-load.ts @@ -0,0 +1,58 @@ +// (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, OnInit, 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 { CoreSiteAddonsCallWSBaseDirective } from '../classes/call-ws-directive'; +import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content'; + +/** + * Directive to call a WS as soon as its loaded. + * This directive is meant for actions to do in the background, like calling logging WebServices. + * + * If you want to call a WS when the user clicks on a certain element, @see CoreSiteAddonsCallWSDirective. + * + * @see CoreSiteAddonsCallWSBaseDirective. + * + * Example usage: + * + * + */ +@Directive({ + selector: '[core-site-addons-call-ws-on-load]' +}) +export class CoreSiteAddonsCallWSOnLoadDirective extends CoreSiteAddonsCallWSBaseDirective implements OnInit { + + constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider, + siteAddonsProvider: CoreSiteAddonsProvider, @Optional() parentContent: CoreSiteAddonsAddonContentComponent) { + super(element, translate, domUtils, siteAddonsProvider, parentContent); + } + + /** + * Component being initialized. + */ + ngOnInit(): void { + super.ngOnInit(); + + // Call the WS immediately. + this.callWS().catch(() => { + // Ignore errors. + }); + } +} diff --git a/src/core/siteaddons/directives/call-ws.ts b/src/core/siteaddons/directives/call-ws.ts index f825b9d1e..360a7f39d 100644 --- a/src/core/siteaddons/directives/call-ws.ts +++ b/src/core/siteaddons/directives/call-ws.ts @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core'; +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 { CoreSiteAddonsCallWSBaseDirective } from '../classes/call-ws-directive'; +import { CoreSiteAddonsCallWSOnClickBaseDirective } from '../classes/call-ws-click-directive'; import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content'; /** @@ -27,7 +27,7 @@ import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content * * If you want to load a new content when the WS call is done, @see CoreSiteAddonsCallWSNewContentDirective. * - * @see CoreSiteAddonsCallWSBaseDirective. + * @see CoreSiteAddonsCallWSOnClickBaseDirective. * * Example usages: * @@ -49,13 +49,11 @@ import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content @Directive({ selector: '[core-site-addons-call-ws]' }) -export class CoreSiteAddonsCallWSDirective extends CoreSiteAddonsCallWSBaseDirective { +export class CoreSiteAddonsCallWSDirective extends CoreSiteAddonsCallWSOnClickBaseDirective { @Input() successMessage: string; // Message to show on success. If not supplied, no message. If empty, default message. @Input() goBackOnSuccess: boolean | string; // Whether to go back if the WS call is successful. @Input() refreshOnSuccess: boolean | string; // Whether to refresh the current view if the WS call is successful. - protected element: HTMLElement; - constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider, siteAddonsProvider: CoreSiteAddonsProvider, @Optional() parentContent: CoreSiteAddonsAddonContentComponent, protected utils: CoreUtilsProvider, protected navCtrl: NavController) { diff --git a/src/core/siteaddons/directives/directives.module.ts b/src/core/siteaddons/directives/directives.module.ts index 6bcf9302d..973182d91 100644 --- a/src/core/siteaddons/directives/directives.module.ts +++ b/src/core/siteaddons/directives/directives.module.ts @@ -15,18 +15,21 @@ import { NgModule } from '@angular/core'; import { CoreSiteAddonsCallWSDirective } from './call-ws'; import { CoreSiteAddonsCallWSNewContentDirective } from './call-ws-new-content'; +import { CoreSiteAddonsCallWSOnLoadDirective } from './call-ws-on-load'; import { CoreSiteAddonsNewContentDirective } from './new-content'; @NgModule({ declarations: [ CoreSiteAddonsCallWSDirective, CoreSiteAddonsCallWSNewContentDirective, + CoreSiteAddonsCallWSOnLoadDirective, CoreSiteAddonsNewContentDirective ], imports: [], exports: [ CoreSiteAddonsCallWSDirective, CoreSiteAddonsCallWSNewContentDirective, + CoreSiteAddonsCallWSOnLoadDirective, CoreSiteAddonsNewContentDirective ] }) diff --git a/src/core/siteaddons/pages/module-index/module-index.html b/src/core/siteaddons/pages/module-index/module-index.html index 463923a47..a0e0ea045 100644 --- a/src/core/siteaddons/pages/module-index/module-index.html +++ b/src/core/siteaddons/pages/module-index/module-index.html @@ -8,7 +8,7 @@ - +