-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathglyph-tools-ui.js
352 lines (319 loc) · 12.8 KB
/
glyph-tools-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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright (c) 2015 gm9
* This software is released under the MIT License.
* http://opensource.org/licenses/mit-license.php
*/
(function(){
var igt = gm9.IngressGlyphTools;
// Export:
igt.ui = {
// Functions
putButton: putButton,
openTextDialog: openTextDialog,
// Classes
DataTable: DataTable,
DataTableRow: DataTableRow,
DataTableColumnSelector: DataTableColumnSelector,
DataTableRowFilter: DataTableRowFilter,
RecordRangeSelector: RecordRangeSelector
};
// -------------------------------------------------
// UI Library
// -------------------------------------------------
function putButton(parent, text, onclick){
var button = document.createElement("input");
button.type = "button";
button.value = text;
button.addEventListener("click", function(e){onclick();}, false);
parent.appendChild(button);
}
function openTextDialog(text, okFunc){
var dlg = document.createElement("div");
var textArea = document.createElement("textarea");
textArea.value = text || "";
dlg.appendChild(textArea);
var bottomBar = document.createElement("div");
dlg.appendChild(bottomBar);
function close(){
if(dlg.parentNode){
dlg.parentNode.removeChild(dlg);
}
}
if(okFunc){
putButton(bottomBar, "OK", function(){
okFunc(textArea.value);
close();
});
}
putButton(bottomBar, "Close", close);
dlg.style.position = "fixed";
dlg.style.left = "0";
dlg.style.top = "0";
dlg.style.right = "0";
dlg.style.bottom = "0";
dlg.style.background = "rgba(128,128,128,0.5)";
dlg.style.padding = "1em";
document.body.appendChild(dlg);
}
// -------------------------------------------------
// Data Table UI Library
// -------------------------------------------------
function DataTable(columns){
var element = document.createElement("table");
element.setAttribute("border", "1");
var sortOrderColumnIndex = 0;
var sortAscendant = true;
var dataRows = [];
// Header
var headerTr = document.createElement("tr");
var headerThs = [];
for(var ci = 0; ci < columns.length; ++ci){
var th = document.createElement("th");
th.style.display = columns[ci].visible ? "" : "none";
th.appendChild(document.createTextNode(columns[ci].text));
headerThs.push(th);
headerTr.appendChild(th);
(function(columnIndex){
th.addEventListener("click", function(e){
if(columnIndex == sortOrderColumnIndex){
sortAscendant = !sortAscendant;
}
else{
sortOrderColumnIndex = columnIndex;
sortAscendant = true;
}
updateTableHeaderSortMark();
sortDataRows();
}, false);
})(ci);
}
element.appendChild(headerTr);
// Sort Mark
var sortMark = document.createElement("span");
function updateTableHeaderSortMark(){
if(sortMark.parentNode){
sortMark.parentNode.removeChild(sortMark);
}
while(sortMark.firstChild){
sortMark.removeChild(sortMark.firstChild);
}
sortMark.appendChild(document.createTextNode(sortAscendant ? "↓" : "↑"));
headerThs[sortOrderColumnIndex].appendChild(sortMark);
}
updateTableHeaderSortMark();
// Column
function setColumnVisible(columnIndex, visible){
columns[columnIndex].visible = visible; ///@todo ここで変更すべきか、ColumnSelectorで変更すべきか。columnsとColumnSelectorとTableが1対1対多になることがあるのが問題。
headerThs[columnIndex].style.display = visible ? "" : "none";
dataRows.forEach(function(r){
r.setColumnVisible(columnIndex, visible);
});
}
// Row
function addRow(row){
element.appendChild(row.getTr());
for(var ci = 0; ci < columns.length; ++ci){
row.addColumn(columns[ci].formatter, columns[ci].visible);
}
row.update();
dataRows.push(row);
};
function updateAllRows(){
dataRows.forEach(function(r){r.update();});
}
function sortDataRows(){
dataRows.forEach(function(r){r.getTr().parentNode.removeChild(r.getTr());});
dataRows.sort(makeDataTableRowComparator(columns, sortOrderColumnIndex, sortAscendant));
dataRows.forEach(function(r){element.appendChild(r.getTr());});
}
// Public Properties
this.element = element;
this.sort = sortDataRows;
this.updateAllRows = updateAllRows;
this.addRow = addRow;
this.setColumnVisible = setColumnVisible;
this.getDataRows = function(){return dataRows;};
}
DataTable.formatter = {
number: function(value){return value.toString();},
percentage1: function(rate){return (Math.floor(rate*1000)/10).toFixed(1) + "%";},
averageCount1: function(count){return (Math.floor(count*10)/10).toFixed(1);},
seconds1: function(sec){return sec.toFixed(3) + "s";}
};
function DataTableRow(source){
var tr = document.createElement("tr");
this.getTr = function(){ return tr;};
var cols = [];
this.addColumn = function(formatter, visible){
var columnIndex = cols.length;
var td = document.createElement("td");
td.style.display = visible ? "" : "none";
tr.appendChild(td);
cols.push({td:td, formatter:formatter, source:null, value:undefined});
};
this.getColumnValue = getColumnValue;
function getColumnValue(columnIndex){
return (columnIndex >= 0 && columnIndex < cols.length) ?
cols[columnIndex].value : undefined;
}
function setColumnValue(columnIndex, value){
if(columnIndex >= 0 && columnIndex < cols.length){
var col = cols[columnIndex];
if(value != col.value){
var td = col.td;
while(td.firstChild){
td.removeChild(td.firstChild);
}
col.value = value;
var formatted = col.formatter ? col.formatter(value) : value.toString();
if(typeof(formatted) == "string"){
td.appendChild(document.createTextNode(formatted));
}
else{
td.appendChild(formatted);
}
}
}
}
this.setColumnVisible = setColumnVisible;
function setColumnVisible(columnIndex, visible){
if(columnIndex >= 0 && columnIndex < cols.length){
cols[columnIndex].td.style.display = visible ? "" : "none";
}
}
this.update = function(){
var values = source();
for(var i = 0; i < values.length; ++i){
setColumnValue(i, values[i]);
}
};
}
function makeDataTableRowComparator(columns, columnIndex, ascendant){
return function(lhs, rhs){
var comp = columns[columnIndex].comparator || defaultComparator;
var lv = lhs.getColumnValue(columnIndex);
var rv = rhs.getColumnValue(columnIndex);
var delta = comp(lv, rv);
if(delta == 0){
var comp0 = columns[0].comparator || defaultComparator;
var lv0 = lhs.getColumnValue(0);
var rv0 = rhs.getColumnValue(0);
delta = comp0(lv0, rv0);
}
return ascendant ? delta : -delta;
};
}
function defaultComparator(lhs, rhs){
return lhs < rhs ? -1 : lhs > rhs ? 1 : 0;
}
function DataTableColumnSelector(storageKey, columns, onChangeColumnVisible)
{
var targetTable = null;
this.setTargetTable = setTargetTable;
function setTargetTable(t){
targetTable = t;
}
function loadSettings(){
if(window.localStorage){
var checkedStr = localStorage.getItem(storageKey);
if(checkedStr){
for(var ci = 0; ci < checkedStr.length && ci < columns.length; ++ci){
columns[ci].visible = (checkedStr.charAt(ci) == "1");
}
}
}
}
loadSettings();
function saveSettings(){
if(window.localStorage){
localStorage.setItem(storageKey, columns.map(function(c){return c.visible ? "1" : "0";}).join(""));
}
}
var element = document.createElement("p");
element.style.lineHeight = "1.5";
this.element = element;
for(var ci = 0; ci < columns.length; ++ci){
(function(){
var columnIndex = ci;
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = columns[ci].visible;
checkbox.addEventListener("click", function(e){
var visible = checkbox.checked;
if(onChangeColumnVisible){
onChangeColumnVisible(columnIndex, visible);
}
if(targetTable){
targetTable.setColumnVisible(columnIndex, visible);
}
columns[columnIndex].visible = visible; ///@todo ここで変更すべきか、ColumnSelectorで変更すべきか。columnsとColumnSelectorとTableが1対1対多になることがあるのが問題。
saveSettings();
}, false);
var label = document.createElement("label");
label.style.display = "inline-block";
label.style.paddingRight = "0.5em";
label.appendChild(checkbox);
label.appendChild(document.createTextNode(columns[ci].text));
element.appendChild(label);
})();
}
}
function DataTableRowFilter(table, columnIndex){
var sel = document.createElement("select");
var values = [undefined].concat(table.getDataRows().map(function(row){return row.getColumnValue(columnIndex);}).filter(function(v,i,a){return a.indexOf(v) === i;}).sort());
values.forEach(function(value){
var opt = document.createElement("option");
opt.value = value;
opt.appendChild(document.createTextNode(value));
sel.appendChild(opt);
});
sel.addEventListener("change", onChange, false);
function onChange()
{
var selectedValue = values[sel.selectedIndex];
table.getDataRows().forEach(function(row){row.getTr().style.display = (selectedValue === undefined || row.getColumnValue(1) == selectedValue) ? "" : "none";});
}
this.element = sel;
}
// -------------------------------------------------
// UI Library for glyph-game-record.js
// -------------------------------------------------
function RecordRangeSelector(storageKey, onChange){
var sel = document.createElement("select");
var ranges = [
{value:0, text:"All"},
{value:1, text:"Today"},
{value:7, text:"Week"},
{value:30, text:"Month"}];
ranges.forEach(function(optData){
var opt = document.createElement("option");
opt.value = optData.value;
opt.appendChild(document.createTextNode(optData.text));
sel.appendChild(opt);
});
function loadSettings(){
if(window.localStorage){
var p = parseInt(localStorage.getItem(storageKey));
if(p >= 0 && p < sel.options.length){
sel.selectedIndex = p;
}
}
}
function saveSettings(){
if(window.localStorage){
window.localStorage.setItem(storageKey, sel.selectedIndex);
}
}
sel.addEventListener("change", function(e){
saveSettings();
if(onChange){
onChange();
}
}, false);
// Public Properties
this.element = sel;
this.getSelectedRangeDays = function(){
return ranges[sel.selectedIndex].value;
};
}
})();