MOBILE-2333 siteaddons: Implement directive to call WS on load
parent
91b020e2fb
commit
823ea35b69
|
@ -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<any>} Promise resolved when done.
|
||||
*/
|
||||
protected callWS(): Promise<any> {
|
||||
const modal = this.domUtils.showModalLoading();
|
||||
|
||||
return super.callWS().finally(() => {
|
||||
modal.dismiss();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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<any>} Promise resolved when done.
|
||||
*/
|
||||
protected callWS(): Promise<any> {
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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:
|
||||
*
|
||||
* <span core-site-addons-call-ws-on-load name="mod_certificate_view_certificate" [params]="{certificateid: <% certificate.id %>}"
|
||||
* [preSets]="{getFromCache: 0, saveToCache: 0}"></span>
|
||||
*/
|
||||
@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.
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
]
|
||||
})
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
</ion-navbar>
|
||||
</ion-header>
|
||||
<ion-content>
|
||||
<ion-refresher [enabled]="content && content.dataLoaded" (ionRefresh)="refreshData($event)">
|
||||
<ion-refresher [enabled]="content && content.addonContent && content.addonContent.dataLoaded" (ionRefresh)="refreshData($event)">
|
||||
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
|
||||
</ion-refresher>
|
||||
<core-site-addons-module-index [module]="module" [courseId]="courseId"></core-site-addons-module-index>
|
||||
|
|
Loading…
Reference in New Issue