MOBILE-2528 siteplugins: Add outputs to WS call directives

main
Dani Palou 2018-07-31 16:30:53 +02:00
parent ac8c2bcee6
commit a7919c1d9a
4 changed files with 23 additions and 8 deletions

View File

@ -15,6 +15,7 @@
import { Input, OnInit, ElementRef } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreSitePluginsProvider } from '../providers/siteplugins';
import { CoreSitePluginsPluginContentComponent } from '../components/plugin-content/plugin-content';
import { CoreSitePluginsCallWSBaseDirective } from './call-ws-directive';
@ -28,10 +29,11 @@ import { CoreSitePluginsCallWSBaseDirective } from './call-ws-directive';
*/
export class CoreSitePluginsCallWSOnClickBaseDirective extends CoreSitePluginsCallWSBaseDirective implements OnInit {
@Input() confirmMessage: string; // Message to confirm the action. If not supplied, no confirmation. If empty, default message.
@Input() showError: boolean | string; // Whether to show an error message if the WS call fails. Defaults to true.
constructor(element: ElementRef, protected translate: TranslateService, protected domUtils: CoreDomUtilsProvider,
protected sitePluginsProvider: CoreSitePluginsProvider,
protected parentContent: CoreSitePluginsPluginContentComponent) {
protected parentContent: CoreSitePluginsPluginContentComponent, protected utils: CoreUtilsProvider) {
super(element, translate, domUtils, sitePluginsProvider, parentContent);
}
@ -66,7 +68,11 @@ export class CoreSitePluginsCallWSOnClickBaseDirective extends CoreSitePluginsCa
protected callWS(): Promise<any> {
const modal = this.domUtils.showModalLoading();
return super.callWS().finally(() => {
return super.callWS().catch((error) => {
if (typeof this.showError == 'undefined' || this.utils.isTrueOrOne(this.showError)) {
this.domUtils.showErrorModalDefault(error, 'core.serverconnection', true);
}
}).finally(() => {
modal.dismiss();
});
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import { Input, OnInit, OnDestroy, ElementRef } from '@angular/core';
import { Input, OnInit, OnDestroy, ElementRef, Output, EventEmitter } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { CoreDomUtilsProvider } from '@providers/utils/dom';
import { CoreSitePluginsProvider } from '../providers/siteplugins';
@ -30,6 +30,9 @@ export class CoreSitePluginsCallWSBaseDirective implements OnInit, OnDestroy {
// @see CoreSitePluginsProvider.loadOtherDataInArgs.
@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.
@Output() onSuccess: EventEmitter<any> = new EventEmitter<any>(); // Sends the result when the WS call succeeds.
@Output() onError: EventEmitter<any> = new EventEmitter<any>(); // Sends the error when the WS call fails.
@Output() onDone: EventEmitter<void> = new EventEmitter<void>(); // Notifies when the WS call is done (either success or fail).
protected element: HTMLElement;
protected invalidateObserver: Subscription;
@ -60,9 +63,15 @@ export class CoreSitePluginsCallWSBaseDirective implements OnInit, OnDestroy {
const params = this.getParamsForWS();
return this.sitePluginsProvider.callWS(this.name, params, this.preSets).then((result) => {
this.onSuccess.emit(result);
return this.wsCallSuccess(result);
}).catch((error) => {
this.domUtils.showErrorModalDefault(error, 'core.serverconnection', true);
this.onError.emit(error);
return Promise.reject(error);
}).finally(() => {
this.onDone.emit();
});
}

View File

@ -62,8 +62,8 @@ export class CoreSitePluginsCallWSNewContentDirective extends CoreSitePluginsCal
constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider,
sitePluginsProvider: CoreSitePluginsProvider, @Optional() parentContent: CoreSitePluginsPluginContentComponent,
protected utils: CoreUtilsProvider, @Optional() protected navCtrl: NavController) {
super(element, translate, domUtils, sitePluginsProvider, parentContent);
utils: CoreUtilsProvider, @Optional() protected navCtrl: NavController) {
super(element, translate, domUtils, sitePluginsProvider, parentContent, utils);
}
/**

View File

@ -56,8 +56,8 @@ export class CoreSitePluginsCallWSDirective extends CoreSitePluginsCallWSOnClick
constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider,
sitePluginsProvider: CoreSitePluginsProvider, @Optional() parentContent: CoreSitePluginsPluginContentComponent,
protected utils: CoreUtilsProvider, protected navCtrl: NavController) {
super(element, translate, domUtils, sitePluginsProvider, parentContent);
utils: CoreUtilsProvider, protected navCtrl: NavController) {
super(element, translate, domUtils, sitePluginsProvider, parentContent, utils);
}
/**