-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcharacterPicker.js
309 lines (239 loc) · 10 KB
/
characterPicker.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
function updateInputLength() {
var value = document.getElementById('input').value;
document.getElementById("length").textContent = calculateUTF16Length(value);
}
function calculateUTF16Length(input) {
var length = input.length;
for (var i=0; i<input.length; i++) {
if ((input.charCodeAt(i) & parseInt('1111110000000000', 2)) == parseInt('1101110000000000',2)) length--;
}
return length;
}
function addCharacterToCursorPosition(char) {
var textarea = document.getElementById("input");
var textareaStart = textarea.selectionStart;
textarea.value = textarea.value.substr(0, textarea.selectionStart) + char + textarea.value.substr(textarea.selectionEnd);
updateInputLength();
textarea.focus();
textarea.setSelectionRange(textareaStart+char.length, textareaStart+char.length);
//textarea.createTextRange(textareaStart, textareaStart);
}
function fixedFromCharCode (codePt) {
if (codePt > 0xFFFF) {
codePt -= 0x10000;
return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 + (codePt & 0x3FF));
}
else {
return String.fromCharCode(codePt);
}
}
function addButton(item) {
var element;
element = document.createElement("button");
var char = null;
if ('undefined' != typeof item['character']) char = item['character'];
else if ('undefined' != typeof item['charcode']) char = fixedFromCharCode(item['charcode']);
else return;
element.addEventListener('click', function(){addCharacterToCursorPosition(char)});
if ('undefined' != typeof item['caption']) element.textContent = item['caption'];
else element.textContent = char;
var title = item['title'];
if ('undefined' != typeof title) element.setAttribute('title', title);
var description = item['description'];
if ('undefined' != typeof description && description != "") element.setAttribute('title', title +" ("+ description +")");
//document.getElementById("buttons").appendChild(element);
return element;
}
/*function addButtons(buttonsArray) {
if ('undefined' != typeof buttonsArray['name']) {
var h2 = document.createElement("h2");
h2.textContent = buttonsArray['name'];
document.getElementById("buttons").appendChild(h2);
}
else addButtonsSeparator();
for (var i in buttonsArray['content']) addButton(buttonsArray['content'][i]);
}*/
function addButtons(buttonsArray) {
var categories = ["cat1", "cat2", "cat3", "cat4"];
for (var i in buttonsArray['content']) {
var element = addButton(buttonsArray['content'][i]);
var category = "buttons-category";
for (var cat in categories) {
if (buttonsArray['content'][i][categories[cat]] != "" && 'undefined' != typeof buttonsArray['content'][i][categories[cat]])
category += "-"+ buttonsArray['content'][i][categories[cat]];
//console.log(cat + " // "+ buttonsArray['content'][i][categories[cat]]);
}
if (!document.getElementById(category)) {
container = document.createElement('div');
var title = "";
for (var cat in categories) {
if (buttonsArray['content'][i][categories[cat]] != "" && 'undefined' != typeof buttonsArray['content'][i][categories[cat]]) {
if (cat > 0) title +=" » ";
title += buttonsArray['content'][i][categories[cat]];
}
}
container.id = category;
var h2 = document.createElement('h2');
h2.textContent = title;
container.appendChild(h2);
document.getElementById('buttons').appendChild(container);
}
document.getElementById(category).appendChild(element);
}
}
function addButtonsSeparator() {
document.getElementById("buttons").appendChild(document.createElement("hr"));
}
function loadFileList(filename) {
var list = null;
document.getElementById('blockSelection').disabled = true;
var http_request = new XMLHttpRequest();
http_request.open("GET", filename, true);
http_request.onreadystatechange = function() {
var done = 4, ok = 200;
if (http_request.readyState == done && http_request.status == ok) {
list = JSON.parse(http_request.responseText);
for (var i in list) {
addOption(list[i]);
}
}
else if (http_request.readyState == done && http_request.status != ok)
window.alert ("Unable to load file "+ filename +", returned error code "+ http_request.status);
}
http_request.send(null);
document.getElementById('blockSelection').disabled = false ;
}
function addOption(item) {
element = document.createElement('option');
element.value = item['filename'];
element.textContent = item['title']
document.getElementById('blockSelection').appendChild(element)
}
function loadCharactersMap(filename) {
var stateObj = { 'charMap': filename };
history.pushState(stateObj, filename, "#?load="+ filename);
store('charMap', filename);
var list = null;
var http_request = new XMLHttpRequest();
http_request.open("GET", "res/"+ filename, true);
http_request.onreadystatechange = function () {
var done = 4, ok = 200;
if (http_request.readyState == done && http_request.status == ok) {
list = JSON.parse(http_request.responseText);
debug_list = list; /// DEBUG
for (var i in list) {
addButtons(list[i]);
}
}
else if (http_request.readyState == done && http_request.status != ok)
window.alert ("Unable to load file "+ filename +", returned error code "+ http_request.status);
}
http_request.send(null);
}
var debug_list; /// DEBUG
function getQueryString() {
var queryString={};
var list=[]
if (window.location.hash.length > 1) {
var list = window.location.hash.slice(1).split('?')[1].split('&');
}
else if (window.location.search.length > 1) {
var list = window.location.search.slice(1).split('&');
}
for (var item in list) {
var keyval = list[item].split('=');
queryString[keyval[0]] = keyval[1];
}
return queryString;
}
function loadSelectedBlock(node) {
var index = node.selectedIndex;
if (node.options[index].value != "") {
removeButtons();
loadCharactersMap(node.options[index].value);
}
//else window.alert("No block selected.");
}
function removeButtons() {
document.getElementById('buttons').textContent = '';
}
function loadCharactersFile(filename) { // The other way around
removeButtons();
loadCharactersMap(filename);
}
function start() {
updateInputLength();
loadFileList('res/list.json');
var queryString = getQueryString();
loadState();
if (queryString['load'] != null) {
var filename = queryString['load'];
removeButtons();
loadCharactersMap(filename.slice(filename.lastIndexOf('/')+1));
}
else if (retrive('charMap') != null) {
var filename = retrive('charMap');
removeButtons();
loadCharactersMap(filename.slice(filename.lastIndexOf('/')+1));
}
else loadSelectedBlock(document.getElementById('blockSelection'));
document.getElementById('clear').addEventListener('click', clearTextarea);
window.applicationCache.addEventListener('updateready', onUpdateReady);
if(window.applicationCache.status === window.applicationCache.UPDATEREADY) {
onUpdateReady();
}
}
function onUpdateReady() {
console.log("Updating manifest…");
window.applicationCache.update();
}
function changeListStyle(style) {
document.getElementById('buttons').className = style;
}
function clearTextarea() {
console.log("Clear text area");
document.getElementById('input').value = '';
store('input', null);
}
function loadCSSFile(CSSFile, id) {
var e = document.createElement('link');
e.rel='stylesheet';
e.type = 'text/css';
e.href=CSSFile;
if (id != undefined) {
e.id=id;
removeElement(id)
}
var head = document.getElementsByTagName("head")[0];
head.appendChild(e);
}
function removeElement(id) {
var element = document.getElementById(id);
if (element != null) element.parentNode.removeChild(element);
}
function store(key, value) {
if (value == null) {
localStorage.removeItem(key);
sessionStorage.removeItem(key);
}
else if (localStorage)
localStorage.setItem(key, value);
else if (sessionStorage)
sessionStorage.setItem(key, value);
}
function retrive(key) {
if (localStorage)
return localStorage.getItem(key);
else if (sessionStorage)
return sessionStorage.getItem(key);
else return null;
}
function saveState() {
store('input', document.getElementById('input').value);
}
function loadState() {
if (retrive('input') != null)
document.getElementById('input').value = retrive('input');
var el = document.getElementById("input");
el.addEventListener("change", function(){store("input", el.value)}, false);
}