|
@@ -0,0 +1,501 @@
|
|
1
|
+var d3 = require('d3'),
|
|
2
|
+ $ = require('jquery'),
|
|
3
|
+ preview = require('./preview.js'),
|
|
4
|
+ options = require('./themeOptions.js');
|
|
5
|
+
|
|
6
|
+var editorIsActive = false;
|
|
7
|
+var themesJson = {};
|
|
8
|
+
|
|
9
|
+function updateTheme(options) {
|
|
10
|
+ if(options !== undefined){
|
|
11
|
+ if(options.backgroundImage !== '')
|
|
12
|
+ getImage(options);
|
|
13
|
+ preview.theme(options);
|
|
14
|
+ }
|
|
15
|
+ else{
|
|
16
|
+ preview.theme(d3.select(this.options[this.selectedIndex]).datum());
|
|
17
|
+ }
|
|
18
|
+}
|
|
19
|
+
|
|
20
|
+function getImage(theme) {
|
|
21
|
+ if (theme.backgroundImage) {
|
|
22
|
+ theme.backgroundImageFile = new Image();
|
|
23
|
+
|
|
24
|
+ theme.backgroundImageFile.onload = function(){
|
|
25
|
+ updateTheme(anonymousTheme());
|
|
26
|
+ };
|
|
27
|
+
|
|
28
|
+ theme.backgroundImageFile.src = '/settings/backgrounds/' + theme.backgroundImage;
|
|
29
|
+ }
|
|
30
|
+}
|
|
31
|
+
|
|
32
|
+function error(msg) {
|
|
33
|
+ if (msg.responseText) {
|
|
34
|
+ msg = msg.responseText;
|
|
35
|
+ }
|
|
36
|
+ if (typeof msg !== 'string') {
|
|
37
|
+ msg = JSON.stringify(msg);
|
|
38
|
+ }
|
|
39
|
+ if (!msg) {
|
|
40
|
+ msg = 'Unknown error';
|
|
41
|
+ }
|
|
42
|
+ d3.select('#loading-message').text('Loading...');
|
|
43
|
+ setClass('error', msg);
|
|
44
|
+}
|
|
45
|
+
|
|
46
|
+function setClass(cl, msg) {
|
|
47
|
+ d3.select('body').attr('class', cl || null);
|
|
48
|
+ d3.select('#error').text(msg || '');
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+function anonymousTheme(){
|
|
52
|
+ var output = {};
|
|
53
|
+ d3.selectAll('.options-attribute-input').each(function(d){
|
|
54
|
+ if(d.type == 'number'){
|
|
55
|
+ if(Number.isNaN(parseFloat(this.value)))
|
|
56
|
+ output[d.name] = null;
|
|
57
|
+ else
|
|
58
|
+ output[d.name] = parseFloat(this.value);
|
|
59
|
+ }
|
|
60
|
+ else{
|
|
61
|
+ output[d.name] = this.value;
|
|
62
|
+ }
|
|
63
|
+ });
|
|
64
|
+
|
|
65
|
+ return output;
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+function postTheme(type, data, cb){
|
|
69
|
+ var postData = {
|
|
70
|
+ type: type,
|
|
71
|
+ data: data
|
|
72
|
+ };
|
|
73
|
+
|
|
74
|
+ $.ajax({
|
|
75
|
+ url: '/api/themes',
|
|
76
|
+ type: 'POST',
|
|
77
|
+ data: JSON.stringify(postData),
|
|
78
|
+ contentType: 'application/json',
|
|
79
|
+ cache: false,
|
|
80
|
+ success: cb,
|
|
81
|
+ error: error
|
|
82
|
+ });
|
|
83
|
+}
|
|
84
|
+
|
|
85
|
+function saveChanges(){
|
|
86
|
+ var activeTheme = preview.theme();
|
|
87
|
+ var activeThemeName = d3.select('#input-theme').property('value');
|
|
88
|
+
|
|
89
|
+ function makeTheme(){
|
|
90
|
+ var file = {};
|
|
91
|
+
|
|
92
|
+ file.name = activeTheme.name;
|
|
93
|
+ file.currentName = activeThemeName;
|
|
94
|
+
|
|
95
|
+ d3.selectAll('.options-attribute-input').each(function(d){
|
|
96
|
+ if(!this.disabled){
|
|
97
|
+ if(this.getAttribute('type') == 'number')
|
|
98
|
+ file[d.name] = parseFloat(this.value);
|
|
99
|
+ else
|
|
100
|
+ file[d.name] = this.value;
|
|
101
|
+ }
|
|
102
|
+ });
|
|
103
|
+
|
|
104
|
+ return file;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ function reloadPage(){
|
|
108
|
+ var url = window.location.origin + window.location.pathname + '?t=' + activeTheme.name;
|
|
109
|
+ window.location = url;
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ var themeNames = [];
|
|
113
|
+ d3.selectAll('#input-theme option').each(function(d){
|
|
114
|
+ themeNames.push(d.name);
|
|
115
|
+ });
|
|
116
|
+ themeNames.splice(themeNames.length-1, 1);
|
|
117
|
+
|
|
118
|
+ var postData = makeTheme();
|
|
119
|
+
|
|
120
|
+ if(activeTheme.name == 'default' || activeTheme.name == '0' || activeTheme.name == ''){
|
|
121
|
+ window.alert('Please give your theme a name.');
|
|
122
|
+ }
|
|
123
|
+ else if(themeNames.indexOf(activeTheme.name) != -1 && activeThemeName == 'default'){
|
|
124
|
+ window.alert('That theme name already exists. Please choose a different name or select it to edit it.');
|
|
125
|
+ }
|
|
126
|
+ else if(themeNames.indexOf(activeThemeName) != -1){
|
|
127
|
+ var msg = 'Are you sure you want to override the options for "';
|
|
128
|
+ msg += activeThemeName + '"? This action is permanent and cannot be undone.';
|
|
129
|
+ if(window.confirm(msg)){
|
|
130
|
+ postTheme('UPDATE', postData, reloadPage);
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+ else{
|
|
134
|
+ postTheme('ADD', postData, reloadPage);
|
|
135
|
+ }
|
|
136
|
+}
|
|
137
|
+
|
|
138
|
+function deleteTheme(){
|
|
139
|
+ var activeThemeName = d3.select('#input-theme').property('value');
|
|
140
|
+
|
|
141
|
+ var msg = 'Are you sure you want to delete the theme "';
|
|
142
|
+ msg += activeThemeName + '"? This action is permanent and cannot be undone.';
|
|
143
|
+
|
|
144
|
+ if(window.confirm(msg)){
|
|
145
|
+ var postData = {name: activeThemeName};
|
|
146
|
+ postTheme('DELETE', postData, function(){
|
|
147
|
+ var url = window.location.origin + window.location.pathname;
|
|
148
|
+ window.location = url;
|
|
149
|
+ });
|
|
150
|
+ }
|
|
151
|
+}
|
|
152
|
+
|
|
153
|
+function refreshTheme(){
|
|
154
|
+ var theme = $.extend({name: preview.theme().name}, themesJson.default, themesJson[preview.theme().name]);
|
|
155
|
+ updateTheme(theme);
|
|
156
|
+ populateThemeFields();
|
|
157
|
+}
|
|
158
|
+
|
|
159
|
+function loadBkgndImages(cb){
|
|
160
|
+ $.ajax({
|
|
161
|
+ url: '/api/images',
|
|
162
|
+ type: 'GET',
|
|
163
|
+ cache: false,
|
|
164
|
+ success: function(data){
|
|
165
|
+ $('#attribute-backgroundImage').html('');
|
|
166
|
+
|
|
167
|
+ var bkgndImgSelect = d3.select('#attribute-backgroundImage');
|
|
168
|
+ for(var img of data){
|
|
169
|
+ bkgndImgSelect.append('option')
|
|
170
|
+ .attr('value', img)
|
|
171
|
+ .text(img);
|
|
172
|
+ }
|
|
173
|
+
|
|
174
|
+ if(cb !== undefined){
|
|
175
|
+ cb();
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ },
|
|
179
|
+ error: error
|
|
180
|
+ });
|
|
181
|
+}
|
|
182
|
+
|
|
183
|
+function uploadImage(){
|
|
184
|
+ // this = the file input calling the function
|
|
185
|
+ var img = this.files[0];
|
|
186
|
+
|
|
187
|
+ var confirmed = confirm('Are you sure you want to upload ' + img.name + '?');
|
|
188
|
+ if(confirmed){
|
|
189
|
+ var formData = new FormData();
|
|
190
|
+ formData.append('img', img);
|
|
191
|
+
|
|
192
|
+ setClass('loading');
|
|
193
|
+
|
|
194
|
+ $.ajax({
|
|
195
|
+ url: '/api/images',
|
|
196
|
+ type: 'POST',
|
|
197
|
+ data: formData,
|
|
198
|
+ contentType: false,
|
|
199
|
+ cache: false,
|
|
200
|
+ processData: false,
|
|
201
|
+ success: function(){
|
|
202
|
+ var setImg = function(){
|
|
203
|
+ var input = d3.select('#container-backgroundImage').select('.options-attribute-input');
|
|
204
|
+ input.property('value', img.name).attr('data-value', img.name);
|
|
205
|
+
|
|
206
|
+ updateTheme(anonymousTheme());
|
|
207
|
+
|
|
208
|
+ setClass(null);
|
|
209
|
+ };
|
|
210
|
+
|
|
211
|
+ loadBkgndImages(setImg);
|
|
212
|
+ },
|
|
213
|
+ error: error
|
|
214
|
+ });
|
|
215
|
+ }
|
|
216
|
+}
|
|
217
|
+
|
|
218
|
+function camelToTitle(string){
|
|
219
|
+ var conversion = string.replace( /([A-Z])/g, ' $1' );
|
|
220
|
+ var title = conversion.charAt(0).toUpperCase() + conversion.slice(1);
|
|
221
|
+ return title;
|
|
222
|
+}
|
|
223
|
+
|
|
224
|
+function queryParser(query){
|
|
225
|
+ query = query.substring(1);
|
|
226
|
+ let query_string = {};
|
|
227
|
+ const vars = query.split('&');
|
|
228
|
+ for (var i=0;i<vars.length;i++) {
|
|
229
|
+ var pair = vars[i].split('=');
|
|
230
|
+ if (typeof query_string[pair[0]] === 'undefined') {
|
|
231
|
+ query_string[pair[0]] = decodeURIComponent(pair[1]);
|
|
232
|
+ } else if (typeof query_string[pair[0]] === 'string') {
|
|
233
|
+ const arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
|
|
234
|
+ query_string[pair[0]] = arr;
|
|
235
|
+ } else {
|
|
236
|
+ query_string[pair[0]].push(decodeURIComponent(pair[1]));
|
|
237
|
+ }
|
|
238
|
+ }
|
|
239
|
+
|
|
240
|
+ if (Object.keys(query_string).includes(''))
|
|
241
|
+ return {};
|
|
242
|
+ else
|
|
243
|
+ return query_string;
|
|
244
|
+};
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+function isEditor(){
|
|
248
|
+ return document.querySelector('#themeEditor') !== null;
|
|
249
|
+}
|
|
250
|
+
|
|
251
|
+function isActive(){
|
|
252
|
+ return editorIsActive;
|
|
253
|
+}
|
|
254
|
+
|
|
255
|
+function initializeThemes(themes){
|
|
256
|
+ for (var key in themes) {
|
|
257
|
+ themesJson[key] = $.extend({}, themes[key]);
|
|
258
|
+ if('foregroundColor' in themesJson[key]){
|
|
259
|
+ themesJson[key].captionColor = themesJson[key].foregroundColor;
|
|
260
|
+ themesJson[key].waveColor = themesJson[key].foregroundColor;
|
|
261
|
+ }
|
|
262
|
+ }
|
|
263
|
+}
|
|
264
|
+
|
|
265
|
+function initializePreview(){
|
|
266
|
+ var container = $('#preview');
|
|
267
|
+ var top = $(container).offset().top;
|
|
268
|
+ $(window).scroll(function() {
|
|
269
|
+ var y = $(this).scrollTop();
|
|
270
|
+ if (y >= top){
|
|
271
|
+ $(container).addClass('sticky');
|
|
272
|
+ }
|
|
273
|
+ else{
|
|
274
|
+ $(container).removeClass('sticky');
|
|
275
|
+ }
|
|
276
|
+ });
|
|
277
|
+
|
|
278
|
+ preview.redraw();
|
|
279
|
+}
|
|
280
|
+
|
|
281
|
+function toggleSection(d){
|
|
282
|
+ var name;
|
|
283
|
+
|
|
284
|
+ if(typeof(d) == 'object')
|
|
285
|
+ name = d.name;
|
|
286
|
+ else
|
|
287
|
+ name = d;
|
|
288
|
+
|
|
289
|
+ $('#section-options-'+ name).slideToggle(500);
|
|
290
|
+ $('#section-toggle-' + name).toggleClass('toggled');
|
|
291
|
+}
|
|
292
|
+
|
|
293
|
+function initialize(){
|
|
294
|
+ var container = d3.select('#options');
|
|
295
|
+
|
|
296
|
+ // Add Option Sections
|
|
297
|
+ var sections = container.selectAll('.section').data(options)
|
|
298
|
+ .enter()
|
|
299
|
+ .append('div')
|
|
300
|
+ .attr('class', 'section');
|
|
301
|
+
|
|
302
|
+ var headers = sections.append('div')
|
|
303
|
+ .attr('class', 'section-header')
|
|
304
|
+ .on('click', toggleSection);
|
|
305
|
+
|
|
306
|
+ // Add Section Toggles
|
|
307
|
+ headers.append('svg')
|
|
308
|
+ .attr('id', function(d){return 'section-toggle-' + d.name;})
|
|
309
|
+ .attr('class', 'section-toggle')
|
|
310
|
+ .attr('viewBox', '0 0 24 24')
|
|
311
|
+ .append('path')
|
|
312
|
+ .attr('d', function(){
|
|
313
|
+ var path = 'M12,13.7c-0.5,0-0.9-0.2-1.2-0.5L0.5,2.9c-0.7-0.7-0.7-1.8,0-2.4C0.8,0.2,1.3,0,1.7,0h20.6C23.3,0,' +
|
|
314
|
+ '24,0.8,24,1.7c0,0.4-0.2,0.9-0.5,1.2L13.2,13.2C12.9,13.6,12.5,13.7,12,13.7z';
|
|
315
|
+ return path;
|
|
316
|
+ });
|
|
317
|
+
|
|
318
|
+ // Add Section Titles
|
|
319
|
+ headers.append('h4')
|
|
320
|
+ .attr('class', 'section-title')
|
|
321
|
+ .text(function(d){return d.name;});
|
|
322
|
+
|
|
323
|
+ var attributesContainer = sections.append('div')
|
|
324
|
+ .attr('class', 'section-options')
|
|
325
|
+ .attr('id', function(d){return 'section-options-' + d.name;})
|
|
326
|
+ .style('display', 'none');
|
|
327
|
+
|
|
328
|
+ // Add Option Container
|
|
329
|
+ var attributes = attributesContainer.selectAll('.options-attribute')
|
|
330
|
+ .data(function(d){return d.options;})
|
|
331
|
+ .enter()
|
|
332
|
+ .append('div')
|
|
333
|
+ .attr('class','options-attribute')
|
|
334
|
+ .attr('id', function(d){return 'container-' + d.name;})
|
|
335
|
+ .attr('data-type', function(d){return d.type;});
|
|
336
|
+
|
|
337
|
+ // Add Enable Checkboxes
|
|
338
|
+ attributes.append('input')
|
|
339
|
+ .attr('id', function(d){return 'enable-' + d.name;})
|
|
340
|
+ .attr('class', 'options-checkbox options-attribute-child')
|
|
341
|
+ .attr('name', function(d){return 'enable-' + d.name;})
|
|
342
|
+ .attr('value', 'true')
|
|
343
|
+ .attr('type', 'checkbox')
|
|
344
|
+ .property('checked', true)
|
|
345
|
+ .on('click', function(d){
|
|
346
|
+ var input = d3.select('#attribute-' + d.name);
|
|
347
|
+ if(this.checked){
|
|
348
|
+ input.property('disabled', false);
|
|
349
|
+ input.property('value', input.attr('data-value'));
|
|
350
|
+ input.attr('value', input.attr('data-value'));
|
|
351
|
+ updateTheme(anonymousTheme());
|
|
352
|
+ }
|
|
353
|
+ else{
|
|
354
|
+ input.property('value', themesJson.default[d.name]);
|
|
355
|
+ input.attr('value', themesJson.default[d.name]);
|
|
356
|
+ input.property('disabled', true);
|
|
357
|
+ updateTheme(anonymousTheme());
|
|
358
|
+ }
|
|
359
|
+ });
|
|
360
|
+
|
|
361
|
+ // Add Option Labels
|
|
362
|
+ attributes.append('label')
|
|
363
|
+ .attr('for', function(d){return 'attribute-' + d.name;})
|
|
364
|
+ .attr('class', 'options-attribute-child')
|
|
365
|
+ .text(function(d){return camelToTitle(d.name);});
|
|
366
|
+
|
|
367
|
+ // Add Help Text Icons
|
|
368
|
+ attributes.append('i')
|
|
369
|
+ .attr('id', function(d){return 'help-' + d.name;})
|
|
370
|
+ .attr('class', 'attribute-help options-attribute-child fa fa-question')
|
|
371
|
+ .attr('title', function(d){return d.help;});
|
|
372
|
+
|
|
373
|
+ // Add Inputs For Text Fields
|
|
374
|
+ sections.selectAll('.options-attribute:not([data-type="select"])')
|
|
375
|
+ .append('input')
|
|
376
|
+ .attr('id', function(d){return 'attribute-' + d.name;})
|
|
377
|
+ .attr('class', 'options-attribute-input options-attribute-child input-text')
|
|
378
|
+ .attr('name', function(d){return d.name;})
|
|
379
|
+ .attr('type', function(d){return d.type;})
|
|
380
|
+ .on('input', function(){
|
|
381
|
+ this.setAttribute('data-value', this.value);
|
|
382
|
+ updateTheme(anonymousTheme());
|
|
383
|
+ });
|
|
384
|
+
|
|
385
|
+ // Add Inputs For Select Fields
|
|
386
|
+ sections.selectAll('.options-attribute[data-type="select"]')
|
|
387
|
+ .append('select')
|
|
388
|
+ .attr('id', function(d){return 'attribute-' + d.name;})
|
|
389
|
+ .attr('class', 'options-attribute-input options-attribute-child input-select')
|
|
390
|
+ .attr('name', function(d){return d.name;})
|
|
391
|
+ .attr('type', function(d){return d.type;})
|
|
392
|
+ .on('input', function(){updateTheme(anonymousTheme());})
|
|
393
|
+ .selectAll('options')
|
|
394
|
+ .data(function(d){return d.options;})
|
|
395
|
+ .enter()
|
|
396
|
+ .append('option')
|
|
397
|
+ .attr('value', function(d){return d;})
|
|
398
|
+ .text(function(d){return camelToTitle(d);});
|
|
399
|
+
|
|
400
|
+ // Add Section Notes
|
|
401
|
+ attributesContainer.append('p')
|
|
402
|
+ .attr('class', 'options-note note')
|
|
403
|
+ .text(function(d){return d.note;});
|
|
404
|
+
|
|
405
|
+ // Add "New..." option to theme select
|
|
406
|
+ d3.select('#input-theme')
|
|
407
|
+ .append('option')
|
|
408
|
+ .data([themesJson.default])
|
|
409
|
+ .attr('value', 'default')
|
|
410
|
+ .text('New...');
|
|
411
|
+
|
|
412
|
+ // Add clickHandler for Save Button
|
|
413
|
+ d3.select('#saveChanges')
|
|
414
|
+ .on('click', saveChanges);
|
|
415
|
+
|
|
416
|
+ // Add clickHandler for Delete Button
|
|
417
|
+ d3.select('#deleteTheme')
|
|
418
|
+ .on('click', deleteTheme);
|
|
419
|
+
|
|
420
|
+ // Add clickHandler for Refresh Button
|
|
421
|
+ d3.select('#refreshTheme')
|
|
422
|
+ .on('click', refreshTheme);
|
|
423
|
+
|
|
424
|
+ // Background Images Populate and Add Uploader
|
|
425
|
+ loadBkgndImages(populateThemeFields);
|
|
426
|
+ var bkgndImgContainer = d3.select('#container-backgroundImage');
|
|
427
|
+ bkgndImgContainer.append('label')
|
|
428
|
+ .attr('for', 'imgUploader')
|
|
429
|
+ .attr('id', 'imgUploader-label')
|
|
430
|
+ .attr('class', 'button')
|
|
431
|
+ .append('i')
|
|
432
|
+ .attr('class', 'fa fa-upload');
|
|
433
|
+ bkgndImgContainer.append('input')
|
|
434
|
+ .attr('type', 'file')
|
|
435
|
+ .attr('id', 'imgUploader')
|
|
436
|
+ .attr('name', 'imgUploader')
|
|
437
|
+ .on('change', uploadImage);
|
|
438
|
+
|
|
439
|
+ toggleSection('Metadata');
|
|
440
|
+
|
|
441
|
+ // Active url theme
|
|
442
|
+ var selectedTheme = queryParser(window.location.search);
|
|
443
|
+ if('t' in selectedTheme && selectedTheme.t in themesJson){
|
|
444
|
+ d3.select('#input-theme')
|
|
445
|
+ .property('value', selectedTheme.t)
|
|
446
|
+ .dispatch('change');
|
|
447
|
+ }
|
|
448
|
+
|
|
449
|
+ populateThemeFields();
|
|
450
|
+
|
|
451
|
+ initializePreview();
|
|
452
|
+ window.addEventListener('resize', function(){
|
|
453
|
+ preview.redraw();
|
|
454
|
+ });
|
|
455
|
+
|
|
456
|
+ d3.select('#toggle-more-instructions').on('click', function(){
|
|
457
|
+ $('#more-instructions').slideToggle(500);
|
|
458
|
+ })
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+ // Toggle the editor container to loaded
|
|
462
|
+ editorIsActive = true;
|
|
463
|
+}
|
|
464
|
+
|
|
465
|
+function populateThemeFields(){
|
|
466
|
+ var activeTheme = preview.theme();
|
|
467
|
+ var themeJson = activeTheme.name !== undefined && activeTheme.name in themesJson ?
|
|
468
|
+ themesJson[activeTheme.name] :
|
|
469
|
+ themesJson.default;
|
|
470
|
+
|
|
471
|
+ d3.selectAll('.options-attribute').each(function(d){
|
|
472
|
+ var input = d3.select(this).select('.options-attribute-input');
|
|
473
|
+
|
|
474
|
+ var activeValue = d.name in activeTheme ? activeTheme[d.name] : '';
|
|
475
|
+ input.property('value', activeValue).attr('data-value', activeValue);
|
|
476
|
+
|
|
477
|
+ if(d.name == 'name'){
|
|
478
|
+ d3.select('#enable-'+d.name).property('checked', true);
|
|
479
|
+ input.property('disabled', false);
|
|
480
|
+ }
|
|
481
|
+ else if(activeTheme.name == undefined || !(d.name in themeJson)){
|
|
482
|
+ d3.select('#enable-'+d.name).property('checked', false);
|
|
483
|
+ input.property('disabled', true);
|
|
484
|
+ }
|
|
485
|
+ else{
|
|
486
|
+ d3.select('#enable-'+d.name).property('checked', true);
|
|
487
|
+ input.property('disabled', false);
|
|
488
|
+ }
|
|
489
|
+
|
|
490
|
+ });
|
|
491
|
+}
|
|
492
|
+
|
|
493
|
+window.themesJson = themesJson;
|
|
494
|
+
|
|
495
|
+module.exports = {
|
|
496
|
+ isEditor,
|
|
497
|
+ isActive,
|
|
498
|
+ initialize,
|
|
499
|
+ initializeThemes,
|
|
500
|
+ populateThemeFields
|
|
501
|
+};
|