-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksettings.js
1905 lines (1688 loc) · 62.8 KB
/
quicksettings.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module QuickSettings
*/
(function() {
////////////////////////////////////////////////////////////////////////////////
// PRIVATE/STATIC DATA AND FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
var cssInjected = false,
css = ".qs_main{background-color:#dddddd;text-align:left;position:absolute;width:200px;font:12px sans-serif;box-shadow:5px 5px 8px rgba(0,0,0,0.35);user-select:none;-webkit-user-select:none;color:#000000;border:none}.qs_content{background-color:#cccccc;overflow-y:auto}.qs_title_bar{background-color:#eeeeee;user-select:none;-webkit-user-select:none;cursor:pointer;padding:5px;font-weight:bold;border:none;color:#000000}.qs_container{margin:5px;padding:5px;background-color:#eeeeee;border:none;position:relative}.qs_container_selected{border:none;background-color:#ffffff}.qs_range{-webkit-appearance:none;-moz-appearance:none;width:100%;height:17px;padding:0;margin:0;background-color:transparent;border:none;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.qs_range:focus{outline:none;border:none}.qs_range::-webkit-slider-runnable-track{width:100%;height:15px;cursor:pointer;background:#cccccc;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0}.qs_range:focus::-webkit-slider-runnable-track{background:#cccccc}.qs_range::-webkit-slider-thumb{-webkit-appearance:none;height:15px;width:15px;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0;background:#999999;cursor:pointer;margin-top:0}.qs_range::-moz-range-track{width:100%;height:15px;cursor:pointer;background:#cccccc;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0}.qs_range::-moz-range-thumb{height:15px;width:15px;border:none;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0;background:#999999;cursor:pointer}.qs_range::-ms-track{width:100%;height:15px;cursor:pointer;visibility:hidden;background:transparent}.qs_range::-ms-thumb{height:15px;width:15px;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0;background:#999999;cursor:pointer;border:none}.qs_range::-ms-fill-lower{background:#cccccc;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0}.qs_range:focus::-ms-fill-lower{background:#cccccc}.qs_range::-ms-fill-upper{background:#cccccc;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0}.qs_range:focus::-ms-fill-upper{background:#cccccc}.qs_button{background-color:#f6f6f6;color:#000000;height:30px;border:1px solid #aaaaaa;font:12px sans-serif}.qs_button:active{background-color:#ffffff;border:1px solid #aaaaaa}.qs_button:focus{border:1px solid #aaaaaa;outline:none}.qs_checkbox{cursor:pointer}.qs_checkbox input{position:absolute;left:-99999px}.qs_checkbox span{height:16px;width:100%;display:block;text-indent:20px;background:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAALklEQVQ4T2OcOXPmfwYKACPIgLS0NLKMmDVrFsOoAaNhMJoOGBioFwZkZUWoJgApdFaxjUM1YwAAAABJRU5ErkJggg==') no-repeat}.qs_checkbox input:checked+span{background:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAvElEQVQ4T63Tyw2EIBAA0OFKBxBL40wDRovAUACcKc1IB1zZDAkG18GYZTmSmafzgTnnMgwchoDWGlJKheGcP3JtnPceCqCUAmttSZznuYtgchsXQrgC+77DNE0kUpPbmBOoJaBOIVQylnqWgAAeKhDve/AN+EaklJBzhhgjWRoJVGTbNjiOowAIret6a+4jYIwpX8aDwLIs74C2D0IIYIyVP6Gm898m9kbVm85ljHUTf16k4VUefkwDrxk+zoUEwCt0GbUAAAAASUVORK5CYII=') no-repeat}.qs_checkbox_label{position:absolute;top:7px;left:30px}.qs_label{margin-bottom:3px;user-select:none;-webkit-user-select:none;cursor:default;font:12px sans-serif}.qs_text_input{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;width:100%;padding:0 0 0 5px;height:24px;border:1px inset #ffffff;background-color:#ffffff;color:#000000;font-size:12px}.qs_text_input:focus{outline:none;background:#ffffff;border:1px inset #ffffff}.qs_select{background:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAp0lEQVRIS+2SsQ3FIAwF7RVYhA5mgQFhFuhYhJKWL0eKxI8SGylKZ0p4+OBsHGNM+HChAiS7qkgyBKrovaLeOxhjbgtxZ+cFtgelFMg5QwgBvPd/EO5sDbKAlBLUWo/8CjmL075zDmKMj6rEKbpCqBL9aqc4ZUQAhVbInBMQUXz5Vg/WfxOktXZsWWtZLds9uIqlqaH1NFV3jdhSJA47E1CAaE8ViYp+wGiWMZ/T+cgAAAAASUVORK5CYII=') no-repeat right #f6f6f6;-webkit-appearance:none;-moz-appearance:none;appearance:none;color:#000000;width:100%;height:24px;border:1px solid #aaaaaa;-webkit-border-radius:0;-moz-border-radius:0;border-radius:0;padding:0 5px;-moz-outline:none;font-size:14px}.qs_select option{font-size:14px}.qs_select::-ms-expand{display:none}.qs_select:focus{outline:none}.qs_number{height:24px}.qs_image{width:100%}.qs_progress{width:100%;height:15px;background-color:#cccccc;border:none;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.qs_progress_value{height:100%;background-color:#999999}.qs_textarea{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;resize:vertical;width:100%;padding:3px 5px;border:1px inset #ffffff;background-color:#ffffff;color:#000000;font-size:12px}.qs_textarea:focus{outline:none;background:#ffffff;border:1px inset #ffffff}.qs_color{position:absolute;left:-999999px}.qs_color_label{width:100%;height:20px;display:block;border:1px solid #aaaaaa;cursor:pointer;padding:0 0 0 5px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.qs_file_chooser{position:absolute;left:-999999px}.qs_file_chooser_label{background-color:#f6f6f6;color:#000000;height:30px;border:1px solid #aaaaaa;font:12px sans-serif;width:100%;display:block;cursor:pointer;padding:7px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}"; // will be injected with default css
function injectCSS() {
var styleTag = document.createElement("style");
styleTag.innerText = css;
document.head.appendChild(styleTag);
cssInjected = true;
}
////////////////////////////////////////////////////////////////////////////////
// MAIN MODULE DEFINITION
////////////////////////////////////////////////////////////////////////////////
/**
*
* @alias module:QuickSettings
* @lends module:QuickSettings.prototype
*/
var QuickSettings = {
_version: "2.1",
_topZ: 1,
_panel: null,
_titleBar: null,
_content: null,
_startX: 0,
_startY: 0,
_hidden: false,
_collapsed: false,
_controls: null,
_keyCode: -1,
_draggable: true,
_collapsible: true,
_snapToGrid: false,
_gridSize: 40,
_globalChangeHandler: null,
////////////////////////////////////////////////////////////////////////////////
// GENERAL INIT FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Static method. Causes QuickSettings to ignore its default styles and instead use whatever QuickSettings stylesheet is on the page. This must be called before creating any panel in order to have any effect.
* @static
*/
useExtStyleSheet: function() {
cssInjected = true;
},
/**
* Static method. Creates a new QuickSettings Panel
* @param x {Number} x position of panel (default 0)
* @param y {Number} y position of panel (default 0)
* @param title {String} title of panel (default "QuickSettings")
* @param parent {HTMLElement} parent element (default document.body)
* @returns {module:QuickSettings} New QuickSettings Panel
* @static
*/
create: function(x, y, title, parent) {
var obj = Object.create(this);
obj._init(x, y, title, parent);
return obj;
},
/**
* Destroys the panel, removing it from the document and nulling all properties.
*/
destroy: function() {
if(this._panel.parentElement) {
this._panel.parentElement.removeChild(this._panel);
}
for(var prop in this) {
this[prop] = null;
}
},
_init: function(x, y, title, parent) {
if(!cssInjected) {
injectCSS();
}
this._bindHandlers();
this._createPanel(x, y, parent);
this._createTitleBar(title || "QuickSettings");
this._createContent();
},
_bindHandlers: function() {
this._startDrag = this._startDrag.bind(this);
this._drag = this._drag.bind(this);
this._endDrag = this._endDrag.bind(this);
this._doubleClickTitle = this._doubleClickTitle.bind(this);
this._onKeyUp = this._onKeyUp.bind(this);
},
////////////////////////////////////////////////////////////////////////////////
// VALUE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Returns an object containing the titles and values of all user-interactive controls in this panel.
* @param asString {Boolean} If true, returns a JSON formatted string of these values.
* @returns {Object} An object or string containing the titles and values fo all user-interactive controls in this panel.
*/
getValuesAsJSON: function(asString) {
var json = {};
for(var title in this._controls) {
var control = this._controls[title];
switch(control.type) {
case "color":
case "date":
case "password":
case "text":
case "textarea":
case "time":
json[title] = control.control.value;
break;
case "number":
case "range":
json[title] = parseFloat(control.control.value);
break;
case "boolean":
json[title] = control.control.checked;
break;
case "fileChooser":
if(control.control.files) {
json[title] = control.control.files[0];
}
else {
json[title] = undefined;
}
break;
case "dropdown":
var select = control.control,
options = select.options,
index = select.selectedIndex,
option = options[index];
json[title] = option.label;
break;
}
}
if(asString) {
json = JSON.stringify(json);
}
return json;
},
////////////////////////////////////////////////////////////////////////////////
// CREATION FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
_createPanel: function(x, y, parent) {
this._panel = this._createElement("div", "qs_main", parent || document.body);
this._panel.style.zIndex = ++QuickSettings._topZ;
this.setPosition(x || 0, y || 0);
this._controls = {};
},
_createTitleBar: function(text) {
this._titleBar = this._createElement("div", "qs_title_bar", this._panel);
this._titleBar.textContent = text;
this._titleBar.addEventListener("mousedown", this._startDrag);
this._titleBar.addEventListener("dblclick", this._doubleClickTitle);
},
_createContent: function() {
this._content = this._createElement("div", "qs_content", this._panel);
},
_createElement: function(type, className, parent) {
var element = document.createElement(type);
if(!element) return;
if(className) {
element.className = className;
}
if(parent) {
parent.appendChild(element);
}
return element;
},
_createContainer: function() {
var container = this._createElement("div", "qs_container");
container.addEventListener("focus", function() {
this.className += " qs_container_selected";
}, true);
container.addEventListener("blur", function() {
var index = this.className.indexOf(" qs_container_selected");
if(index > -1) {
this.className = this.className.substr(0, index);
}
}, true);
this._content.appendChild(container);
return container;
},
_createLabel: function(title, container) {
var label = this._createElement("div", "qs_label", container);
label.innerHTML = title;
return label;
},
////////////////////////////////////////////////////////////////////////////////
// SIZE AND POSITION FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Positions the panel at the given location.
* @param x {Number} The x position.
* @param y {Number} The y position.
* @returns {module:QuickSettings}
*/
setPosition: function(x, y) {
this._panel.style.left = x + "px";
this._panel.style.top = Math.max(y, 0) + "px";
return this;
},
/**
* Sets the size of the panel.
* @param w {Number} The width of the panel.
* @param h {Number} The height of the panel.
* @returns {module:QuickSettings}
*/
setSize: function(w, h) {
this._panel.style.width = w + "px";
this._content.style.width = w + "px";
this._content.style.height = (h - this._titleBar.offsetHeight) + "px";
return this;
},
/**
* Sets the width of the panel.
* @param w {Number} The width of the panel.
* @returns {module:QuickSettings}
*/
setWidth: function(w) {
this._panel.style.width = w + "px";
this._content.style.width = w + "px";
return this;
},
/**
* Sets the height of the panel.
* @param h {Number} The height of the panel.
* @returns {module:QuickSettings}
*/
setHeight: function(h) {
this._content.style.height = (h - this._titleBar.offsetHeight) + "px";
return this;
},
////////////////////////////////////////////////////////////////////////////////
// DRAG AND DROP FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Sets whether or not the panel can be dragged.
* @param draggable {Boolean} Whether or not the panel can be dragged.
* @returns {module:QuickSettings}
*/
setDraggable: function(draggable) {
this._draggable = draggable;
if(this._draggable || this._collapsible) {
this._titleBar.style.cursor = "pointer";
}
else {
this._titleBar.style.cursor = "default";
}
return this;
},
_startDrag: function(event) {
if(this._draggable) {
this._panel.style.zIndex = ++QuickSettings._topZ;
document.addEventListener("mousemove", this._drag);
document.addEventListener("mouseup", this._endDrag);
this._startX = event.clientX;
this._startY = event.clientY;
}
event.preventDefault();
},
_drag: function(event) {
var x = parseInt(this._panel.style.left),
y = parseInt(this._panel.style.top),
mouseX = event.clientX,
mouseY = event.clientY;
this.setPosition(x + mouseX - this._startX, y + mouseY - this._startY);
this._startX = mouseX;
this._startY = mouseY;
event.preventDefault();
},
_endDrag: function(event) {
if(this._snapToGrid) {
var x = parseInt(this._panel.style.left),
y = parseInt(this._panel.style.top),
mouseX = event.clientX,
mouseY = event.clientY;
x = x + mouseX - this._startX;
y = y + mouseY - this._startY;
x = Math.round(x / this._gridSize) * this._gridSize;
y = Math.round(y / this._gridSize) * this._gridSize;
this.setPosition(x, y);
}
document.removeEventListener("mousemove", this._drag);
document.removeEventListener("mouseup", this._endDrag);
event.preventDefault();
},
/**
* Sets whether or not the panel will snap to a grid location when moved.
* @param snap {Boolean} Whether or not the panel will snap to a grid location when moved.
* @returns {module:QuickSettings}
*/
setSnapToGrid: function(snap) {
this._snapToGrid = snap;
return this;
},
/**
* Sets the size of the grid that the panel will snap to if snapping is set to true.
* @param size {Number} The size of the grid.
* @returns {module:QuickSettings}
*/
setGridSize: function(size) {
this._gridSize = size;
return this;
},
////////////////////////////////////////////////////////////////////////////////
// CHANGE HANDLER FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Sets a function that will be called whenever any value in the panel is changed.
* @param handler {Function}
* @returns {module:QuickSettings}
*/
setGlobalChangeHandler: function(handler) {
this._globalChangeHandler = handler;
return this;
},
_callGCH: function() {
if(this._globalChangeHandler) {
this._globalChangeHandler();
}
},
////////////////////////////////////////////////////////////////////////////////
// VISIBILITY FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Hides the panel.
* @returns {module:QuickSettings}
*/
hide: function() {
this._panel.style.visibility = "hidden";
this._hidden = true;
return this;
},
/**
* Shows the panel.
* @returns {module:QuickSettings}
*/
show: function() {
this._panel.style.visibility = "visible";
this._panel.style.zIndex = ++QuickSettings._topZ;
this._hidden = false;
return this;
},
/**
* Toggles the panel from hidden to visible and back.
* @returns {module:QuickSettings}
*/
toggleVisibility: function() {
if(this._hidden) {
this.show();
}
else {
this.hide();
}
return this;
},
/**
* Sets whether or not the panel will collapse and expand when the title is double clicked.
* @param collapsible {Boolean} Wheter or not the panel can collapse and expand.
* @returns {module:QuickSettings}
*/
setCollapsible: function(collapsible) {
this._collapsible = collapsible;
if(this._draggable || this._collapsible) {
this._titleBar.style.cursor = "pointer";
}
else {
this._titleBar.style.cursor = "default";
}
return this;
},
/**
* Collapses the panel showing only the title bar.
* @returns {module:QuickSettings}
*/
collapse: function() {
this._panel.removeChild(this._content);
this._collapsed = true;
return this;
},
/**
* If panel is collapsed, re-expands it.
* @returns {module:QuickSettings}
*/
expand: function() {
this._panel.appendChild(this._content);
this._collapsed = false;
return this;
},
/**
* Toggles the panel back and forth between collapsed and expanded states.
* @returns {module:QuickSettings}
*/
toggleCollapsed: function() {
if(this._collapsed) {
this.expand();
}
else {
this.collapse();
}
return this;
},
/**
* Sets a key that, when pressed, will show and hide the panel.
* @param char
* @returns {module:QuickSettings}
*/
setKey: function(char) {
this._keyCode = char.toUpperCase().charCodeAt(0);
document.body.addEventListener("keyup", this.onKeyUp);
return this;
},
_onKeyUp: function(event) {
if(event.keyCode === this._keyCode) {
this.toggleVisibility();
}
},
_doubleClickTitle: function() {
if(this._collapsible) {
this.toggleCollapsed();
}
},
////////////////////////////////////////////////////////////////////////////////
// CONTROL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
/**
* Removes a given control from the panel.
* @param title {String} The title of the control to remove.
* @returns {module:QuickSettings}
*/
removeControl: function(title) {
if(this._controls[title]){
var container = this._controls[title].container;
}
if(container && container.parentElement) {
container.parentElement.removeChild(container);
}
this._controls[title] = null;
return this;
},
/**
* Enables the given control.
* @param title {String} The title of the control to enable.
* @returns {module:QuickSettings}
*/
enableControl: function(title) {
if(this._controls[title]) {
this._controls[title].control.disabled = false;
}
return this;
},
/**
* Disables the given control.
* @param title {String} The title of the control to disable.
* @returns {module:QuickSettings}
*/
disableControl: function(title) {
if(this._controls[title]) {
this._controls[title].control.disabled = true;
}
return this;
},
/**
* Hides the given control.
* @param title {String} The title of the control to hide.
* @returns {module:QuickSettings}
*/
hideControl: function(title) {
if(this._controls[title]) {
this._controls[title].container.style.display = "none";
}
return this;
},
/**
* Shows the given control.
* @param title {String} The title of the control to show.
* @returns {module:QuickSettings}
*/
showControl: function(title) {
if(this._controls[title]) {
this._controls[title].container.style.display = "block";
}
return this;
},
/**
* Changes a specific style on the given component.
* @param title {String} The title of the control.
* @param style {String} The name of the style.
* @param value {Various} The new value of the style.
* @returns {module:QuickSettings}
*/
overrideStyle: function(title, style, value) {
if(this._controls[title]) {
this._controls[title].control.style[style] = value;
}
return this;
},
/**
* Hides the title label of a given control.
* @param title {String} The title of the control.
* @returns {module:QuickSettings}
*/
hideTitle: function(title) {
var label = this._controls[title].label;
if(label) {
label.style.display = "none";
}
return this;
},
/**
* Shows the title label of a given control.
* @param title {String} The title of the control.
* @returns {module:QuickSettings}
*/
showTitle: function(title) {
var label = this._controls[title].label;
if(label) {
label.style.display = "block";
}
return this;
},
/**
* Hides the title labels of all controls.
* @returns {module:QuickSettings}
*/
hideAllTitles: function() {
for(var title in this._controls) {
var label = this._controls[title].label;
if(label) {
label.style.display = "none";
}
}
return this;
},
/**
* Shows the title labels of all controls. Button and booleans have no title labels.
* @returns {module:QuickSettings}
*/
showAllTitles: function() {
for(var title in this._controls) {
var label = this._controls[title].label;
if(label) {
label.style.display = "block";
}
}
return this;
},
////////////////////////////////////////////////////////////////////////////////
// JSON PARSER
////////////////////////////////////////////////////////////////////////////////
/**
* Creates a new QuickSettings Panel from a JSON string or object.
* @param json {Object|String} The JSON string or object to parse.
* @param parent {HTMLElement} The parent element to attach the new panel to.
* @param scope {Object} The object to look for any callbacks on.
* @returns {module:QuickSettings}
*/
parse: function(json, parent, scope) {
if(typeof json === "string") {
json = JSON.parse(json);
}
var panel = QuickSettings.create(json.x, json.y, json.title, parent);
panel.setDraggable(json.draggable == null ? true : json.draggable);
panel.setCollapsible(json.collapsible == null ? true : json.collapsible);
panel.setGridSize(json.gridSize || 40);
panel.setSnapToGrid(json.snapToGrid == null ? false : json.snapToGrid);
if(json.width) {
panel.setWidth(json.width);
}
if(json.height) {
panel.setHeight(json.height);
}
scope = scope || {};
for(var i = 0; i < json.controls.length; i++) {
var control = json.controls[i];
switch(control.type) {
case "range":
panel.addRange(control.title, control.min || 0, control.max || 100, control.value || control.min || 0, control.step || 1, scope[control.callback]);
break;
case "number":
panel.addNumber(control.title, control.min || 0, control.max || 100, control.value || control.min || 0, control.step || 1, scope[control.callback]);
break;
case "boolean":
panel.addBoolean(control.title, control.value, scope[control.callback]);
break;
case "button":
panel.addButton(control.title, scope[control.callback]);
break;
case "color":
panel.addColor(control.title, control.value, scope[control.callback]);
break;
case "text":
panel.addText(control.title, control.value, scope[control.callback]);
break;
case "password":
panel.addPassword(control.title, control.value, scope[control.callback]);
break;
case "textarea":
case "textArea":
panel.addTextArea(control.title, control.value, scope[control.callback]);
break;
case "date":
panel.addDate(control.title, control.value, scope[control.callback]);
break;
case "time":
panel.addTime(control.title, control.value, scope[control.callback]);
break;
case "info":
panel.addHTML(control.title, control.value);
break;
case "dropdown":
case "dropDown":
panel.addDropDown(control.title, control.value, scope[control.callback]);
break;
case "image":
panel.addImage(control.title, control.value);
break;
case "progressbar":
case "progressBar":
panel.addProgressBar(control.title, control.max || 100, control.value || 0, control.valueDisplay);
break;
case "html":
panel.addHTML(control.title, control.value);
break;
case "filechooser":
case "fileChooser":
panel.addFileChooser(control.title, control.labelStr, control.filter, scope[control.callback]);
break;
}
}
return panel;
},
////////////////////////////////////////////////////////////////////////////////
// PLATFORM TESTS
////////////////////////////////////////////////////////////////////////////////
_isIE: function() {
if(navigator.userAgent.indexOf("rv:11") != -1) {
return true;
}
if(navigator.userAgent.indexOf("MSIE") != -1) {
return true;
}
return false;
},
_isSafari: function() {
var userAgent = navigator.userAgent.toLowerCase();
if(userAgent.indexOf("chrome") > -1 ||
userAgent.indexOf("firefox") > -1 ||
userAgent.indexOf("epiphany") > -1) {
return false;
}
if(userAgent.indexOf('safari/') > -1) {
return true;
}
return false;
},
_isEdge: function() {
var userAgent = navigator.userAgent.toLowerCase();
return userAgent.indexOf("edge") > -1;
},
//==========================================================================================
//==========================================================================================
// CONTROL CREATION AND MANAGEMENT FUNCTIONS
//==========================================================================================
//==========================================================================================
////////////////////////////////////////////////////////////////////////////////
// RANGE (SLIDER)
////////////////////////////////////////////////////////////////////////////////
/**
* Adds a range slider control.
* @param title {String} Title of the control.
* @param min {Number} Minimum value of control.
* @param max {Number} Maximum value of control.
* @param value {Number} Initial value of control.
* @param step {Number} Size of value increments.
* @param callback {Function} Callback function to call when control value changes.
* @returns {module:QuickSettings}
*/
addRange: function(title, min, max, value, step, callback) {
return this._addNumber("range", title, min, max, value, step, callback);
},
/**
* Adds a number control.
* @param title {String} Title of the control.
* @param min {Number} Minimum value of control.
* @param max {Number} Maximum value of control.
* @param value {Number} Initial value of control.
* @param step {Number} Size of value increments.
* @param callback {Function} Callback function to call when control value changes.
* @returns {module:QuickSettings}
*/
addNumber: function(title, min, max, value, step, callback) {
return this._addNumber("number", title, min, max, value, step, callback);
},
_addNumber: function(type, title, min, max, value, step, callback) {
var container = this._createContainer();
var label = this._createLabel("", container);
var className = type === "range" ? "qs_range" : "qs_text_input qs_number";
var input = this._createElement("input", className, container);
input.type = type;
input.id = title;
input.min = min || 0;
input.max = max || 100;
input.step = step || 1;
input.value = value || 0;
label.innerHTML = "<b>" + title + ":</b> " + input.value;
this._controls[title] = {
type: type,
container: container,
control: input,
label: label,
callback: callback
};
var eventName = "input";
if(type === "range" && this._isIE()) {
eventName = "change";
}
var self = this;
input.addEventListener(eventName, function() {
label.innerHTML = "<b>" + title + ":</b> " + input.value;
if(callback) {
callback(parseFloat(input.value));
}
self._callGCH();
});
return this;
},
/**
* Add a range slider control bound to an object.
* @param title {String} Title of the control.
* @param min {Number} Minimum value of control.
* @param max {Number} Maximum value of control.
* @param value {Number} Initial value of control.
* @param step {Number} Size of value increments.
* @param object {Object} Object the control is bound to. When the value of the control changes, a property on this object, with the name of the title of this control, will be set to the current value of this control.
* @returns {module:QuickSettings}
*/
bindRange: function(title, min, max, value, step, object) {
return this.addRange(title, min, max, value, step, function(value) {
object[title] = value;
});
},
/**
* Add a number control bound to an object.
* @param title {String} Title of the control.
* @param min {Number} Minimum value of control.
* @param max {Number} Maximum value of control.
* @param value {Number} Initial value of control.
* @param step {Number} Size of value increments.
* @param object {Object} Object the control is bound to. When the value of the control changes, a property on this object, with the name of the title of this control, will be set to the current value of this control.
* @returns {module:QuickSettings}
*/
bindNumber: function(title, min, max, value, step, object) {
return this.addNumber(title, min, max, value, step, function(value) {
object[title] = value;
});
},
/**
* Get the current value of a range control.
* @param title {Number} The title of the control to get the value for.
* @returns {Number}
*/
getRangeValue: function(title) {
return this.getNumberValue(title);
},
/**
* Gets the current value of a number control.
* @param title {Number} The title of the control to get the value for.
* @returns {Number}
*/
getNumberValue: function(title) {
return parseFloat(this._controls[title].control.value);
},
/**
* Sets the value of a range control.
* @param title {Number} The title of the control to set the value on.
* @param value {Number} The value to set.
* @returns {module:QuickSettings}
*/
setRangeValue: function(title, value) {
return this.setNumberValue(title, value);
},
/**
* Sets the value of a number control.
* @param title {Number} The title of the control to set the value on.
* @param value {Number} The value to set.
* @returns {module:QuickSettings}
*/
setNumberValue: function(title, value) {
var control = this._controls[title];
control.control.value = value;
control.label.innerHTML = "<b>" + title + ":</b> " + control.control.value;
if(control.callback) {
control.callback(parseFloat(control.control.value));
}
this._callGCH();
return this;
},
/**
* Sets the parameters of a range control.
* @param title {Number} The title of the control to set the parameters on.
* @param min {Number} The minimum value of the control.
* @param max {Number} The maximum value of the control.
* @param step {Number} Size of value increments.
* @returns {module:QuickSettings}
*/
setRangeParameters: function(title, min, max, step) {
return this.setNumberParameters(title, min, max, step);
},
/**
* Sets the parameters of a number control.
* @param title {Number} The title of the control to set the parameters on.
* @param min {Number} The minimum value of the control.
* @param max {Number} The maximum value of the control.
* @param step {Number} Size of value increments.
* @returns {module:QuickSettings}
*/
setNumberParameters: function(title, min, max, step) {
var control = this._controls[title];
control.control.min = min;
control.control.max = max;
control.control.step = step;
return this;
},
////////////////////////////////////////////////////////////////////////////////
// BOOLEAN (CHECKBOX)
////////////////////////////////////////////////////////////////////////////////
/**
* Adds a checkbox to the panel.
* @param title {String} The title of this control.
* @param value {Boolean} The initial value of this control.
* @param callback {Function} A callback function that will be called when the value of this control changes.
* @returns {module:QuickSettings}
*/
addBoolean: function(title, value, callback) {
var container = this._createContainer();
var label = this._createElement("label", "qs_checkbox_label", container);
label.textContent = title;
label.setAttribute("for", title);
var checkbox = this._createElement("label", "qs_checkbox", container);
checkbox.setAttribute("for", title);
var input = this._createElement("input", null, checkbox);
input.type = "checkbox";
input.id = title;
input.checked = value;
var span = this._createElement("span", null, checkbox);
this._controls[title] = {
type: "boolean",
container: container,
control: input,
callback: callback
};
var self = this;
input.addEventListener("change", function() {
if(callback) {
callback(input.checked);
}
self._callGCH();
});
return this;
},
/**
* Adds a checkbox to the panel, bound to an object
* @param title {String} The title of this control.
* @param value {Boolean} The initial value of this control.
* @param object {Object} Object the control is bound to. When the value of the control changes, a property on this object, with the name of the title of this control, will be set to the current value of this control.
* @returns {module:QuickSettings}
*/
bindBoolean: function(title, value, object) {
return this.addBoolean(title, value, function(value) {
object[title] = value;
});
},
/**
* Gets the current value of a boolean (checkbox) control.
* @param title {String} The title of the control to get the value for.
* @returns {Boolean}
*/
getBoolean: function(title) {
return this._controls[title].control.checked;
},