2018-02-20 13:24:01 +01:00
|
|
|
// (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, OnDestroy, ElementRef } from '@angular/core';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
|
2018-03-09 14:36:30 +01:00
|
|
|
import { CoreSitePluginsProvider } from '../providers/siteplugins';
|
|
|
|
import { CoreSitePluginsPluginContentComponent } from '../components/plugin-content/plugin-content';
|
2018-02-20 13:24:01 +01:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
|
|
|
|
/**
|
2018-02-20 16:28:35 +01:00
|
|
|
* Base class for directives that need to call a WS.
|
2018-02-20 13:24:01 +01:00
|
|
|
*/
|
2018-03-09 14:36:30 +01:00
|
|
|
export class CoreSitePluginsCallWSBaseDirective implements OnInit, OnDestroy {
|
2018-02-20 13:24:01 +01:00
|
|
|
@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() useOtherDataForWS: any[]; // Whether to include other data in the params for the WS.
|
2018-03-09 14:36:30 +01:00
|
|
|
// @see CoreSitePluginsProvider.loadOtherDataInArgs.
|
2018-02-20 15:35:53 +01:00
|
|
|
@Input() form: string; // ID or name to identify a form. The form will be obtained from document.forms.
|
|
|
|
// If supplied and form is found, the form data will be retrieved and sent to the WS.
|
2018-02-20 13:24:01 +01:00
|
|
|
|
|
|
|
protected element: HTMLElement;
|
|
|
|
protected invalidateObserver: Subscription;
|
|
|
|
|
|
|
|
constructor(element: ElementRef, protected translate: TranslateService, protected domUtils: CoreDomUtilsProvider,
|
2018-03-09 14:36:30 +01:00
|
|
|
protected sitePluginsProvider: CoreSitePluginsProvider,
|
|
|
|
protected parentContent: CoreSitePluginsPluginContentComponent) {
|
2018-02-20 13:24:01 +01:00
|
|
|
this.element = element.nativeElement || element;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
|
|
|
ngOnInit(): void {
|
|
|
|
if (this.parentContent && this.parentContent.invalidateObservable) {
|
|
|
|
this.invalidateObserver = this.parentContent.invalidateObservable.subscribe(() => {
|
|
|
|
this.invalidate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call a WS.
|
|
|
|
*
|
|
|
|
* @return {Promise<any>} Promise resolved when done.
|
|
|
|
*/
|
|
|
|
protected callWS(): Promise<any> {
|
2018-02-20 16:28:35 +01:00
|
|
|
const params = this.getParamsForWS();
|
2018-02-20 13:24:01 +01:00
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
return this.sitePluginsProvider.callWS(this.name, params, this.preSets).then((result) => {
|
2018-02-20 13:24:01 +01:00
|
|
|
return this.wsCallSuccess(result);
|
|
|
|
}).catch((error) => {
|
|
|
|
this.domUtils.showErrorModalDefault(error, 'core.serverconnection', true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-20 15:35:53 +01:00
|
|
|
/**
|
|
|
|
* Get the params for the WS call.
|
|
|
|
*
|
|
|
|
* @return {any} Params.
|
|
|
|
*/
|
|
|
|
protected getParamsForWS(): any {
|
|
|
|
let params = this.params || {};
|
|
|
|
|
|
|
|
if (this.parentContent) {
|
2018-03-09 14:36:30 +01:00
|
|
|
params = this.sitePluginsProvider.loadOtherDataInArgs(params, this.parentContent.otherData, this.useOtherDataForWS);
|
2018-02-20 15:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.form && document.forms[this.form]) {
|
|
|
|
params = Object.assign(params, this.domUtils.getDataFromForm(document.forms[this.form]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
2018-02-20 13:24:01 +01:00
|
|
|
/**
|
|
|
|
* Function called when the WS call is successful.
|
|
|
|
*
|
|
|
|
* @param {any} result Result of the WS call.
|
|
|
|
*/
|
|
|
|
protected wsCallSuccess(result: any): void {
|
|
|
|
// Function to be overridden.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidate the WS call.
|
|
|
|
*
|
|
|
|
* @return {Promise<any>} Promise resolved when done.
|
|
|
|
*/
|
|
|
|
invalidate(): Promise<any> {
|
2018-02-20 15:35:53 +01:00
|
|
|
const params = this.getParamsForWS();
|
2018-02-20 13:24:01 +01:00
|
|
|
|
2018-03-09 14:36:30 +01:00
|
|
|
return this.sitePluginsProvider.invalidateCallWS(this.name, params, this.preSets);
|
2018-02-20 13:24:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Directive destroyed.
|
|
|
|
*/
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.invalidateObserver && this.invalidateObserver.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|