xmpp.chapril.org-conversejs/src/headless/utils/form.js

37 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-08-16 15:19:41 +02:00
// Converse.js (A browser based XMPP chat client)
// https://conversejs.org
2017-08-16 15:19:41 +02:00
//
// This is the utilities module.
//
2019-02-18 19:17:06 +01:00
// Copyright (c) 2013-2019, Jan-Carel Brand <jc@opkode.com>
2017-08-16 15:19:41 +02:00
// Licensed under the Mozilla Public License (MPLv2)
2018-10-23 03:41:38 +02:00
import _ from "../lodash.noconflict";
import tpl_field from "../templates/field.html";
import u from "./core";
u.webForm2xForm = function (field) {
/* Takes an HTML DOM and turns it into an XForm field.
*
* Parameters:
* (DOMElement) field - the field to convert
*/
let value;
if (field.getAttribute('type') === 'checkbox') {
value = field.checked && 1 || 0;
} else if (field.tagName == "TEXTAREA") {
value = _.filter(field.value.split('\n'), _.trim);
} else if (field.tagName == "SELECT") {
value = u.getSelectValues(field);
} else {
value = field.value;
}
return u.stringToNode(
tpl_field({
'name': field.getAttribute('name'),
'value': value
})
);
};
export default u;