MOBILE-2333 siteaddons: Allow sending form data

main
Dani Palou 2018-02-20 15:35:53 +01:00
parent 2c858bc9d2
commit 91b020e2fb
3 changed files with 70 additions and 14 deletions

View File

@ -31,6 +31,8 @@ export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy {
@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.
// If supplied and form is found, the form data will be retrieved and sent to the WS.
protected element: HTMLElement;
protected invalidateObserver: Subscription;
@ -73,13 +75,8 @@ export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy {
* @return {Promise<any>} Promise resolved when done.
*/
protected callWS(): Promise<any> {
const modal = this.domUtils.showModalLoading();
let params = this.params;
if (this.parentContent) {
params = this.siteAddonsProvider.loadOtherDataInArgs(params, this.parentContent.otherData, this.useOtherDataForWS);
}
const modal = this.domUtils.showModalLoading(),
params = this.getParamsForWS();
return this.siteAddonsProvider.callWS(this.name, params, this.preSets).then((result) => {
return this.wsCallSuccess(result);
@ -90,6 +87,25 @@ export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy {
});
}
/**
* Get the params for the WS call.
*
* @return {any} Params.
*/
protected getParamsForWS(): any {
let params = this.params || {};
if (this.parentContent) {
params = this.siteAddonsProvider.loadOtherDataInArgs(params, this.parentContent.otherData, this.useOtherDataForWS);
}
if (this.form && document.forms[this.form]) {
params = Object.assign(params, this.domUtils.getDataFromForm(document.forms[this.form]));
}
return params;
}
/**
* Function called when the WS call is successful.
*
@ -105,11 +121,7 @@ export class CoreSiteAddonsCallWSBaseDirective implements OnInit, OnDestroy {
* @return {Promise<any>} Promise resolved when done.
*/
invalidate(): Promise<any> {
let params = this.params;
if (this.parentContent) {
params = this.siteAddonsProvider.loadOtherDataInArgs(params, this.parentContent.otherData, this.useOtherDataForWS);
}
const params = this.getParamsForWS();
return this.siteAddonsProvider.invalidateCallWS(this.name, params, this.preSets);
}

View File

@ -14,6 +14,7 @@
import { Directive, Input, OnInit, ElementRef, Optional } from '@angular/core';
import { NavController } from 'ionic-angular';
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
import { CoreUtilsProvider } from '../../../providers/utils/utils';
import { CoreSiteAddonsProvider } from '../providers/siteaddons';
import { CoreSiteAddonsAddonContentComponent } from '../components/addon-content/addon-content';
@ -48,11 +49,13 @@ export class CoreSiteAddonsNewContentDirective implements OnInit {
@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.
@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 new content.
protected element: HTMLElement;
constructor(element: ElementRef, protected utils: CoreUtilsProvider, protected navCtrl: NavController,
@Optional() protected parentContent: CoreSiteAddonsAddonContentComponent,
@Optional() protected parentContent: CoreSiteAddonsAddonContentComponent, protected domUtils: CoreDomUtilsProvider,
protected siteAddonsProvider: CoreSiteAddonsProvider) {
this.element = element.nativeElement || element;
}
@ -65,12 +68,16 @@ export class CoreSiteAddonsNewContentDirective implements OnInit {
ev.preventDefault();
ev.stopPropagation();
let args = this.args;
let args = this.args || {};
if (this.parentContent) {
args = this.siteAddonsProvider.loadOtherDataInArgs(this.args, this.parentContent.otherData, this.useOtherData);
}
if (this.form && document.forms[this.form]) {
args = Object.assign(args, this.domUtils.getDataFromForm(document.forms[this.form]));
}
if (this.utils.isTrueOrOne(this.samePage)) {
// Update the parent content (if it exists).
if (this.parentContent) {

View File

@ -264,6 +264,43 @@ export class CoreDomUtilsProvider {
}
}
/**
* Get the data from a form. It will only collect elements that have a name.
*
* @param {HTMLFormElement} form The form to get the data from.
* @return {any} Object with the data. The keys are the names of the inputs.
*/
getDataFromForm(form: HTMLFormElement): any {
if (!form || !form.elements) {
return {};
}
const data = {};
for (let i = 0; i < form.elements.length; i++) {
const element: any = form.elements[i],
name = element.name || '';
// Ignore submit inputs.
if (!name || element.type == 'submit' || element.tagName == 'BUTTON') {
return;
}
// Get the value.
if (element.type == 'checkbox') {
data[name] = !!element.checked;
} else if (element.type == 'radio') {
if (element.checked) {
data[name] = element.value;
}
} else {
data[name] = element.value;
}
}
return data;
}
/**
* Returns height of an element.
*