-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsconfig.js
235 lines (235 loc) · 8.58 KB
/
tsconfig.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';
function TSConfig()
{
// backdrop
this._container = document.createElement('div');
this._container.setAttribute('style', 'position: fixed;z-index: 10;left: 0;top: 0;width: 100%;height: 100%;display: none;background: #FFF;color: #000;box-sizing: border-box;border: 8px solid rgba(0,0,0,.7);padding: 0 8px;overflow: auto;line-height: 1.2;');
document.body.appendChild(this._container);
// valid events
this._valid_events = ['save', 'cancel'];
this._event_handlers = {};
}
TSConfig.prototype.show = function(config_data, save_button_text, cancel_button_text) {
this._container.textContent = '';
var generators = {
'line': this._generate_line,
'text': this._generate_text,
'number': this._generate_number,
'check': this._generate_check,
'list': this._generate_list
};
// generating inputs
for (var i = 0, len = config_data.length; i < len; i) {
var item = config_data[i];
this._start_section(item.section);
var current_data = item.data;
for (var j = 0, len2 = current_data.length; j < len2; j) {
var current_data_item = current_data[j];
if (current_data_item.type in generators) {
generators[current_data_item.type].call(this, item.section, current_data_item);
}
}
}
// save/cancel buttons
this._make_save_cancel_buttons(save_button_text || 'Save', cancel_button_text || 'Cancel');
// show
this._container.style.display = 'block';
}
TSConfig.prototype.hide = function() {
this._container.style.display = 'none';
if (this._event_handlers.cancel) {
this._event_handlers.cancel();
}
}
TSConfig.prototype.on = function(event, func) {
if ((this._valid_events.indexOf(event) !== -1) && (typeof func === 'function')) {
this._event_handlers[event] = func;
}
}
TSConfig.prototype._generate_line = function(section, data) {
var wrap = this._make_wrapper();
var input = this._make_generic_input('text');
var label = document.createElement('label');
this._pass_attr(data, input, 'value');
this._pass_attr(data, input, 'maxlength');
this._pass_attr(data, input, 'title');
input.setAttribute('data-name', data.name);
input.setAttribute('data-section', section);
input.setAttribute('data-initial-value', input.value);
label.textContent = data.label;
label.appendChild(input);
wrap.appendChild(label);
this._container.appendChild(wrap);
}
TSConfig.prototype._generate_text = function(section, data) {
var wrap = this._make_wrapper();
var input = document.createElement('textarea');
input.setAttribute('style', 'box-sizing: border-box;border: 1px solid #AAA;width: 100%;padding: .2em .5ex;background: #FFF;color: #000;height: 4em;');
var label = document.createElement('label');
this._pass_attr(data, input, 'maxlength');
this._pass_attr(data, input, 'title');
if (data.value) {
input.value = data.value;
}
input.setAttribute('data-name', data.name);
input.setAttribute('data-section', section);
input.setAttribute('data-initial-value', input.value);
label.textContent = data.label;
label.appendChild(input);
wrap.appendChild(label);
this._container.appendChild(wrap);
}
TSConfig.prototype._generate_number = function(section, data) {
var wrap = this._make_wrapper();
var input = this._make_generic_input('number');
var label = document.createElement('label');
this._pass_attr(data, input, 'value');
this._pass_attr(data, input, 'maxlength');
this._pass_attr(data, input, 'title');
this._pass_attr(data, input, 'min');
this._pass_attr(data, input, 'max');
this._pass_attr(data, input, 'step');
input.setAttribute('data-name', data.name);
input.setAttribute('data-section', section);
input.setAttribute('data-initial-value', input.value);
label.textContent = data.label;
label.appendChild(input);
wrap.appendChild(label);
this._container.appendChild(wrap);
}
TSConfig.prototype._generate_check = function(section, data) {
var wrap = this._make_wrapper();
var input = document.createElement('input');
input.setAttribute('type', 'checkbox');
this._pass_attr(data, input, 'title');
input.setAttribute('data-name', data.name);
input.setAttribute('data-section', section);
if (data.value) {
input.checked = true;
}
input.setAttribute('data-initial-value', input.checked);
var label = document.createElement('label');
label.appendChild(input);
label.appendChild(document.createTextNode(' ' data.label));
wrap.appendChild(label);
this._container.appendChild(wrap);
}
TSConfig.prototype._generate_list = function(section, data) {
if (!data.value) return;
var wrap = this._make_wrapper();
var input = document.createElement('select');
this._pass_attr(data, input, 'title');
input.setAttribute('style', 'box-sizing: border-box;border: 1px solid #AAA;width: 100%;padding: .2em .5ex;background: #FFF;color: #000;');
input.setAttribute('data-name', data.name);
input.setAttribute('data-section', section);
var real_data = data.value.length ? data.value : [data.value];
for (var i = 0, len = real_data.length; i < len; i) {
var option = document.createElement('option');
option.textContent = real_data[i];
input.appendChild(option);
}
if ('selected' in data) {
input.selectedIndex = data.selected;
}
input.setAttribute('data-initial-value', input.selectedIndex);
var label = document.createElement('label');
label.textContent = data.label;
label.appendChild(input);
wrap.appendChild(label);
this._container.appendChild(wrap);
}
TSConfig.prototype._start_section = function(title) {
if (title) {
var heading = document.createElement('div');
heading.textContent = title;
heading.setAttribute('style', 'margin-bottom: .5em;padding: 1em 0 .5em 0; border-bottom: 1px solid #DDD;font-weight: bold;clear: left;');
this._container.appendChild(heading);
}
}
TSConfig.prototype._make_wrapper = function() {
var result = document.createElement('div');
result.setAttribute('style', 'margin-top: .5em;float: left;box-sizing: border-box;width: 50%;padding: 0 4px;');
return result;
}
TSConfig.prototype._make_generic_input = function(type) {
var result = document.createElement('input');
result.setAttribute('type', type);
result.setAttribute('style', 'box-sizing: border-box;border: 1px solid #AAA;width: 100%;padding: .2em .5ex;background: #FFF;color: #000;');
return result;
}
TSConfig.prototype._pass_attr = function(data, element, attr_name) {
if (attr_name in data) {
element.setAttribute(attr_name, data[attr_name]);
}
}
TSConfig.prototype._make_save_cancel_buttons = function(save_button_text, cancel_button_text) {
var w = document.createElement('div');
w.setAttribute('style', 'padding: .5em 0;clear: left;');
var button = document.createElement('button');
button.setAttribute('type', 'button');
button.setAttribute('style', 'padding: .2em 1ex;');
button.textContent = save_button_text;
button.addEventListener(
'click',
(function(){
var result = [];
var data_elements = this._container.querySelectorAll('input, textarea, select');
for (var i = 0, len = data_elements.length; i < len; i) {
var current_element = data_elements[i];
var current_value;
var is_changed = false;
switch (current_element.nodeName) {
case 'INPUT':
switch (current_element.getAttribute('type')) {
case 'text':
case 'number':
current_value = current_element.value;
is_changed = (current_value !== current_element.getAttribute('data-initial-value'));
break;
case 'checkbox':
current_value = current_element.checked;
var current_value_str = current_value ? 'true' : 'false';
is_changed = (current_value_str !== current_element.getAttribute('data-initial-value'));
break;
}
break;
case 'TEXTAREA':
current_value = current_element.value;
is_changed = (current_value !== current_element.getAttribute('data-initial-value'));
break;
case 'SELECT':
current_value = current_element.selectedIndex;
var current_value_str = current_value.toString();
is_changed = (current_value_str !== current_element.getAttribute('data-initial-value'));
break;
}
if (is_changed) {
result.push({
section: current_element.getAttribute('data-section'),
name: current_element.getAttribute('data-name'),
value: current_value
});
}
}
this._container.style.display = 'none';
if (this._event_handlers.save) {
this._event_handlers.save(result);
}
}).bind(this),
false
);
w.appendChild(button);
button = document.createElement('button');
button.setAttribute('type', 'button');
button.setAttribute('style', 'padding: .2em 1ex;margin-left: 1ex;');
button.textContent = cancel_button_text;
button.addEventListener(
'click',
(function(){
this.hide();
}).bind(this),
false
);
w.appendChild(button);
this._container.appendChild(w);
}