forked from unbug/codelf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormHandler.js
More file actions
43 lines (38 loc) · 1.19 KB
/
FormHandler.js
File metadata and controls
43 lines (38 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Navigator from './Navigator';
const FormHandler = new function () {
function getForm(method) {
let _form = document.createElement('form');
_form.setAttribute("style", "display:none;width:0;height:0;position: absolute;top:0;left:0;border:0;");
_form.setAttribute("method", method || 'POST');
document.body.appendChild(_form);
return _form;
}
this.asyncSubmit = function (action, data) {
this.submit(action, data, true);
}
this.submit = function (action, data, async) {
let target,
frame,
form = getForm(),
inputs = [],
itpl = '<input type="text" name="{N}" value="{V}" />';
if (async) {
target = '__formhandler_' + new Date().getTime();
frame = Navigator.getFrame(null, target);
form.setAttribute('target', target);
setTimeout(function () {
Navigator.removeFrame(frame);
}, 120000);
}
form.setAttribute('action', action);
data = data || {};
for (let key in data) {
inputs.push(itpl.replace('{N}', key).replace('{V}', data[key]));
}
form.innerHTML = inputs.join('');
action && setTimeout(function () {
form.submit();
}, 100);
}
};
export default FormHandler;