-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathui.js
118 lines (101 loc) · 3.67 KB
/
ui.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(function () {
var CALLBACK = function () {};
var ui = document.getElementById("ui");
var title = ui.getElementsByClassName("title")[0];
var content = ui.getElementsByClassName("content")[0];
function setText(el, value) {
if ("textContent" in el) {
el.textContent = value;
} else {
el.innerText = value;
}
}
window.ui = {
setTitle: function (t) {
setText(title, t);
},
reset: function (title) {
this.setTitle(title);
while (content.hasChildNodes()) {
content.removeChild(content.lastChild);
}
},
alert: function (c, title, button, callback) {
callback = callback || CALLBACK;
setText(content, c);
this.setTitle(title);
if (button) {
var ok = document.createElement("input");
ok.type = "button";
ok.value = button;
ok.onclick = function () {
callback();
};
var row = document.createElement("div");
row.className = "row";
row.appendChild(ok);
content.appendChild(row);
} else {
callback();
}
},
ask: function (questions, callback, title, button) {
callback = callback || CALLBACK;
var that = this;
this.reset(title);
var form = document.createElement("form");
for (question in questions) {
var row = document.createElement("div");
row.className = "row";
var text = document.createElement("input");
text.type = "text";
text.name = question;
row.appendChild(text);
var label = document.createElement("label");
label.for = question;
setText(label, questions[question] || "" + "\u00a0");
row.appendChild(label);
form.appendChild(row);
}
var submit = document.createElement("input");
var row = document.createElement("div");
row.className = "row";
submit.type = "submit";
submit.value = button || "Submit";
row.appendChild(submit);
form.appendChild(row);;
form.onsubmit = function () {
var inputs = form.getElementsByTagName("input"), answers = {};
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
if (input.type != "text") continue;
answers[input.name] = input.value;
}
that.reset();
callback(answers);
return false;
};
content.appendChild(form);
},
confirm: function (title, c, callback) {
callback = callback || CALLBACK;
var that = this;
this.setTitle(title);
setText(content, c);
var yes = document.createElement("input"), no = document.createElement("input");
yes.value = "Yes";
no.value = "No";
yes.type = no.type = "button";
yes.onclick = no.onclick = function () {
that.reset();
callback(this == yes);
return false;
};
var row = document.createElement("div");
row.className = "row";
row.appendChild(no);
row.appendChild(yes);
content.appendChild(row);
}
};
})();