forked from amol-mandhane/htmlPy
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinder.js
More file actions
99 lines (82 loc) · 3.31 KB
/
binder.js
File metadata and controls
99 lines (82 loc) · 3.31 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var link_bind = function(e) {
e.preventDefault();
var anchor = e.target || e.srcElement;
if (anchor.getAttribute("data-bind") != "true")
return true;
var call = anchor.href;
var params = anchor.getAttribute("data-params");
if (params !== null)
eval(call + "('" + params + "')");
else
eval(call + "()");
return false;
};
var form_bind = function (e) {
e.preventDefault();
var form = e.target || e.srcElement;
if (form.getAttribute("data-bind") != "true")
return true;
var action = form.action;
var formdata = {};
for (var i = 0, ii = form.length; i < ii; ++i) {
var input = form[i];
if (input.name && input.type !== "file")
formdata[input.name] = input.value;
}
var params = form.getAttribute("data-params");
exec = action + "('" + JSON.stringify(formdata);
exec += params !== null ? ("', '" + params) : "";
exec += "')";
console.log(exec);
eval(exec);
return false;
};
var file_dialog = function (e) {
e.preventDefault();
var id_of_pseudo_filebox = e.target.getAttribute("data-display");
var ext_filter_json = e.target.getAttribute("data-filter");
if (ext_filter_json === null || ext_filter_json === "null")
ext_filter_json = '[{"title": "Any file", "extensions": "*.*"}]';
var dialog = GUIHelper.file_dialog(ext_filter_json);
document.getElementById(id_of_pseudo_filebox).value = dialog;
return false;
};
var bind_all = function () {
var anchors = document.getElementsByTagName("a");
var forms = document.getElementsByTagName("form");
for (var i = anchors.length - 1; i >= 0; i--) {
if(!anchors[i].classList.contains("htmlpy-activated")){
anchors[i].onclick = link_bind;
anchors[i].classList.add("htmlpy-activated");
}
}
for (var fi = forms.length - 1; fi >= 0; fi--) {
if(!forms[fi].classList.contains("htmlpy-activated")){
forms[fi].onsubmit = form_bind;
form = forms[fi];
for (i = 0, ii = form.length; i < ii; ++i) {
var input = form[i];
if (input.type === "file") {
var fileboxname = input.getAttribute("name");
var disabledInput = document.createElement("input");
disabledInput.setAttribute("disabled", "disabled");
disabledInput.setAttribute("name", fileboxname);
disabledInput.setAttribute("id", fileboxname + "_path");
var button = document.createElement("button");
button.innerHTML = "Choose file";
button.setAttribute("data-display", fileboxname + "_path");
button.setAttribute("data-filter", input.getAttribute("data-filter"));
button.onclick = file_dialog;
input.parentNode.insertBefore(disabledInput, input.nextSibling);
input.parentNode.insertBefore(button, disabledInput.nextSibling);
input.style.display = "none";
form[i].remove();
}
}
forms[fi].classList.add("htmlpy-activated");
}
}
};
bind_all();
document.body.addEventListener("DOMNodeInserted", bind_all);
document.body.classList.add("htmlPy-active");