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

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-08-16 15:19:41 +02:00
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// This is the utilities module.
//
// Copyright (c) 2013-2018, Jan-Carel Brand <jc@opkode.com>
2017-08-16 15:19:41 +02:00
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define */
2017-08-16 15:19:41 +02:00
(function (root, factory) {
define([
"../lodash.noconflict",
"./core",
"../templates/field.html"
2017-08-16 15:19:41 +02:00
], factory);
}(this, function (_, u, tpl_field) {
2017-08-16 15:19:41 +02:00
"use strict";
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") {
2017-08-16 15:19:41 +02:00
value = _.filter(field.value.split('\n'), _.trim);
} else if (field.tagName == "SELECT") {
value = u.getSelectValues(field);
2017-08-16 15:19:41 +02:00
} else {
value = field.value;
}
2018-01-02 21:44:46 +01:00
return u.stringToNode(
2017-08-16 15:19:41 +02:00
tpl_field({
'name': field.getAttribute('name'),
'value': value
2017-08-16 15:19:41 +02:00
})
2018-01-02 21:44:46 +01:00
);
2017-08-16 15:19:41 +02:00
};
return u;
}));