-
Notifications
You must be signed in to change notification settings - Fork 2
/
handleCode.js
496 lines (432 loc) · 17.1 KB
/
handleCode.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
var codePath;
////////////////////////////////////////////////////////////////////////////////
// CONSTANTS //
////////////////////////////////////////////////////////////////////////////////
const filesArray = ['html', 'css', 'js', 'fields', 'data']
const ini = `[HTML]
path = "html.txt"
[CSS]
path = "css.txt"
[JS]
path = "js.txt"
[FIELDS]
path = "fields.txt"
[DATA]
path = "data.txt"
`
////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS //
////////////////////////////////////////////////////////////////////////////////
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
function handleFile(file, type) {
let fr = new FileReader();
$(`.${type}-select span`).text(file.name);
fr.readAsText(file, "UTF-8");
fr.onload = function (data) {
switch (type) {
case 'html':
window.postMessage([data.target.result, 'html']);
break;
case 'css':
window.postMessage([data.target.result, 'css']);
break;
case 'js':
window.postMessage([data.target.result, 'js']);
break;
case 'fields':
window.postMessage([data.target.result, 'fields']);
break;
case 'data':
window.postMessage([data.target.result, 'data']);
break;
default:
break;
}
}
}
function handleZip(file) {
var zip = new JSZip();
zip.loadAsync(file)
.then(function (zip) {
$('.zip-select span').text(file.name)
if (!zip.files[`widget.ini`]) {
handleUnsupported(zip)
return
}
zip.files[`widget.ini`].async('string')
.then((data) => {
pathArray = handleIniData(data);
if (pathArray.length != 5) {
handleUnsupported(zip)
return
}
if (!zip.files[`${pathArray[0]}`] ||
!zip.files[`${pathArray[1]}`] ||
!zip.files[`${pathArray[2]}`] ||
!zip.files[`${pathArray[3]}`] ||
!zip.files[`${pathArray[4]}`]) {
alert('This widget was not set up properly. Opening manual upload dialog...')
handleUnsupported(zip)
return
}
readFiles(zip, pathArray);
})
});
}
function handleUnsupported(zip) {
// alert("This widget is not supported.")
resetSession()
waitForElm('#sigma-create-unsupported').then(() => {
$('#sigma-create-unsupported').click(() => {
$('#zip').val('')
$('.zip-select span').text('No file selected...')
handleCode(codePath)
});
});
filesArray.forEach((type) => {
waitForElm(`#${type}`).then(() => {
$(`#${type}`).on("change", function (evt) {
console.log('Change detected')
var files = evt.target.files;
for (var i = 0; i < files.length; i++) {
handleFile(files[i], type);
}
});
})
})
var backdrop = $(`
<md-backdrop class="md-dialog-backdrop md-opaque"
style="position: fixed;" aria-hidden="true">
</md-backdrop>`)
$('body').prepend(backdrop)
$('body').append(dialog)
// Handle click outside of dialog
$(window).click(() => {
$('.sigma-extension-dialog').remove()
$(backdrop).remove()
})
$('.sigma-dialog').click((event) => {
event.stopPropagation();
})
}
function handleIniData(data) {
var lines = data.split('\n');
var pathArray = [];
for (var i = 0; i < lines.length; i++) {
if (lines[i].includes('[HTML]')) {
var _temp = lines[i + 1].split('"');
pathArray.push(_temp[1]);
}
if (lines[i].includes('[CSS]')) {
var _temp = lines[i + 1].split('"');
pathArray.push(_temp[1]);
}
if (lines[i].includes('[JS]')) {
var _temp = lines[i + 1].split('"');
pathArray.push(_temp[1]);
}
if (lines[i].includes('[FIELDS]')) {
var _temp = lines[i + 1].split('"');
pathArray.push(_temp[1]);
}
if (lines[i].includes('[DATA]')) {
var _temp = lines[i + 1].split('"');
pathArray.push(_temp[1]);
}
}
return pathArray;
}
function readFiles(zip, pathArray) {
var htmlCode, cssCode, jsCode, fieldsCode, dataCode;
zip.files[`${pathArray[0]}`].async('string')
.then((data) => {
htmlCode = data;
zip.files[`${pathArray[1]}`].async('string')
.then((data) => {
cssCode = data;
zip.files[`${pathArray[2]}`].async('string')
.then((data) => {
jsCode = data;
zip.files[`${pathArray[3]}`].async('string')
.then((data) => {
fieldsCode = data;
zip.files[`${pathArray[4]}`].async('string')
.then((data) => {
dataCode = data;
code = [htmlCode, cssCode, jsCode, fieldsCode, dataCode]
window.postMessage([code, 'zip'])
return
});
});
});
});
});
}
function handleCode(path) {
$('widget-creator > button').click();
tab = $('.widget-creator__items').children()[4];
tab_content = $(tab).children()[4];
widget_button = $(tab_content).children()[5];
$(widget_button).click();
$('.custom-event-list-editor-button-container > button').click();
waitForElm('.html-editor .CodeMirror').then(() => {
const htmlEditor = $('.html-editor .CodeMirror')[0].CodeMirror
if (path[0]) {
htmlEditor.setValue(path[0]);
} else {
htmlEditor.setValue('');
}
const fields = $('._md-nav-bar-list').children();
$($(fields[1]).children()[0]).click();
});
waitForElm('.css-editor .CodeMirror').then(() => {
const cssEditor = $('.css-editor .CodeMirror')[0].CodeMirror;
if (path[1]) {
cssEditor.setValue(path[1]);
} else {
cssEditor.setValue('');
}
const fields = $('._md-nav-bar-list').children();
$($(fields[2]).children()[0]).click();
});
waitForElm('.js-editor .CodeMirror').then(() => {
const jsEditor = $('.js-editor .CodeMirror')[0].CodeMirror;
if (path[2]) {
jsEditor.setValue(path[2]);
} else {
jsEditor.setValue('')
}
const fields = $('._md-nav-bar-list').children();
$($(fields[3]).children()[0]).click();
});
waitForElm('.fields-editor .CodeMirror').then(() => {
const fieldsEditor = $('.fields-editor .CodeMirror')[0].CodeMirror;
if (path[3]) {
fieldsEditor.setValue(path[3]);
} else {
fieldsEditor.setValue('')
}
const fields = $('._md-nav-bar-list').children();
$($(fields[4]).children()[0]).click();
});
waitForElm('.field-data-editor .CodeMirror').then(() => {
const dataEditor = $('.field-data-editor .CodeMirror')[0].CodeMirror;
if (path[4]) {
dataEditor.setValue(path[4]);
} else {
dataEditor.setValue('')
}
$('.exit-code-editor').click()
});
}
function writeCode() {
var zip = new JSZip()
zip.file('widget.ini', ini)
codeArray = [];
var value = $('#widgets')[0].value
var widgetList = $('span[ng-show="!vm.editableWidgetNames[widget.id]"]')
widgetList.each((el) => {
if (widgetList[el].innerText === value) {
$($(widgetList[el]).parent()[0]).click()
}
})
$('.custom-event-list-editor-button-container > button').click();
waitForElm('.html-editor .CodeMirror').then(() => {
const htmlEditor = $('.html-editor .CodeMirror')[0].CodeMirror
//codeArray.push(htmlEditor.getValue())
zip.file('html.txt', htmlEditor.getValue())
const fields = $('._md-nav-bar-list').children();
$($(fields[1]).children()[0]).click();
});
waitForElm('.css-editor .CodeMirror').then(() => {
const cssEditor = $('.css-editor .CodeMirror')[0].CodeMirror;
//codeArray.push(cssEditor.getValue())
zip.file('css.txt', cssEditor.getValue())
const fields = $('._md-nav-bar-list').children();
$($(fields[2]).children()[0]).click();
});
waitForElm('.js-editor .CodeMirror').then(() => {
const jsEditor = $('.js-editor .CodeMirror')[0].CodeMirror;
//codeArray.push(jsEditor.getValue())
zip.file('js.txt', jsEditor.getValue())
const fields = $('._md-nav-bar-list').children();
$($(fields[3]).children()[0]).click();
});
waitForElm('.fields-editor .CodeMirror').then(() => {
const fieldsEditor = $('.fields-editor .CodeMirror')[0].CodeMirror;
//codeArray.push(jsEditor.getValue())
zip.file('fields.txt', fieldsEditor.getValue())
const fields = $('._md-nav-bar-list').children();
$($(fields[4]).children()[0]).click();
});
waitForElm('.field-data-editor .CodeMirror').then(() => {
const dataEditor = $('.field-data-editor .CodeMirror')[0].CodeMirror;
//codeArray.push(fieldsEditor.getValue())
zip.file('data.txt', dataEditor.getValue())
$('.exit-code-editor').click();
zip.generateAsync({ type: "blob" }).then(function (blob) {
saveAs(blob, `${value}.zip`);
});
});
}
function resetSession() {
$('#zip').val('')
$('#html').val('')
$('#css').val('')
$('#js').val('')
$('#fields').val('')
$('#data').val('')
$('.zip-select span').text('No file selected...')
$('.html-select span').text('No file selected...')
$('.css-select span').text('No file selected...')
$('.js-select span').text('No file selected...')
$('.fields-select span').text('No file selected...')
$('.data-select span').text('No file selected...')
codePath = []
}
////////////////////////////////////////////////////////////////////////////////
// ACTION SCRIPTS //
////////////////////////////////////////////////////////////////////////////////
waitForElm('#zip').then(() => {
codePath = []
$('#zip').on("change", function (evt) {
var files = evt.target.files;
for (var i = 0; i < files.length; i++) {
handleZip(files[i]);
}
});
});
waitForElm('#sigma-create').then(() => {
$('#sigma-create').click(() => {
$('#zip').val('')
$('.zip-select span').text('No file selected...')
handleCode(codePath)
});
});
waitForElm('#sigma-save').then(() => {
$('#sigma-save').click(() => {
writeCode()
});
});
////////////////////////////////////////////////////////////////////////////////
// EVENT LISTENERS //
////////////////////////////////////////////////////////////////////////////////
window.addEventListener("message", function (event) {
if (event.data[1] === 'zip') {
codePath = event.data[0];
}
else if (event.data[1] === 'html') {
codePath[0] = event.data[0];
}
else if (event.data[1] === 'css') {
codePath[1] = event.data[0];
}
else if (event.data[1] === 'js') {
codePath[2] = event.data[0];
}
else if (event.data[1] === 'fields') {
codePath[3] = event.data[0];
}
else if (event.data[1] === 'data') {
codePath[4] = event.data[0];
}
});
////////////////////////////////////////////////////////////////////////////////
// HTML SOURCES //
////////////////////////////////////////////////////////////////////////////////
const dialog = `
<div class="sigma-extension-dialog">
<div class="md-dialog-container sigma-dialog-container" tabindex="-1" md-theme="default"
style="top: 0px; height: 961px;">
<div class="md-dialog-focus-trap" tabindex="0"></div>
<md-dialog class="sigma-dialog overlay-editor__session-data-dialog__root _md md-default-theme md-transition-in"
md-theme="default" role="dialog" tabindex="-1" aria-describedby="dialogContent_111" style="">
<div class="title-area">
<h3>Manual upload</h3>
<p>
Unfortunately, the ZIP you uploaded is not supported by this
extension. Do not fret, however! You can manually provide
the required files. Open the ZIP and locate files with names
that either contain or otherwise resemble <a>"html.txt"</a>, <a>"css.txt"</a>,
<a>"js.txt"</a>, and <a>"fields.txt"</a>. Refer to the instructions that
came with your widget to find these files or their equivalents.
</p>
<p>
Then, select each file in the respective box below, and click
"Create widget". Enjoy!
</p>
</div>
<div class="upload-area">
<div class="selection">
<p>Select widget HTML file</p>
<div class="html-select">
<span>No file selected...</span>
<label for="html" class="sigma-file-upload">
Upload
</label>
</div>
<input type="file" id="html" name="zip" accept=".txt,.html">
</div>
<div class="selection">
<p>Select widget CSS file</p>
<div class="css-select">
<span>No file selected...</span>
<label for="css" class="sigma-file-upload">
Upload
</label>
</div>
<input type="file" id="css" name="zip" accept=".txt,.css">
</div>
<div class="selection">
<p>Select widget JS file</p>
<div class="js-select">
<span>No file selected...</span>
<label for="js" class="sigma-file-upload">
Upload
</label>
</div>
<input type="file" id="js" name="zip" accept=".txt,.js">
</div>
<div class="selection">
<p>Select widget FIELDS file</p>
<div class="fields-select">
<span>No file selected...</span>
<label for="fields" class="sigma-file-upload">
Upload
</label>
</div>
<input type="file" id="fields" name="zip" accept=".txt,.json">
</div>
<div class="selection">
<p>Select widget DATA file</p>
<div class="data-select">
<span>No file selected...</span>
<label for="data" class="sigma-file-upload">
Upload
</label>
</div>
<input type="file" id="data" name="zip" accept=".txt,.json">
</div>
</div>
<ext-button id="sigma-create-unsupported">CREATE WIDGET</ext-button>
</md-dialog>
<div class="md-dialog-focus-trap" tabindex="0"></div>
</div>
</div>
`