-
Notifications
You must be signed in to change notification settings - Fork 52
/
quicksettings.js
1638 lines (1456 loc) · 68.9 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 () {
////////////////////////////////////////////////////////////////////////////////
// region PRIVATE GENERIC UTILS
////////////////////////////////////////////////////////////////////////////////
var nextID = 0;
function getNextID() {
nextID++;
return "qs_" + nextID;
}
function createLabel(title, container) {
var label = createElement("div", null, "qs_label", container);
label.innerHTML = title;
return label;
}
function createInput(type, id, className, parent) {
var input = createElement("input", id, className, parent);
input.type = type;
return input;
}
function createElement(type, id, className, parent) {
var element = document.createElement(type);
if (!element) return;
element.id = id;
if (className) {
element.className = className;
}
if (parent) {
parent.appendChild(element);
}
return element;
}
function isIE() {
if (navigator.userAgent.indexOf("rv:11") != -1) {
return true;
}
if (navigator.userAgent.indexOf("MSIE") != -1) {
return true;
}
return false;
}
function isSafari() {
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;
}
function isEdge() {
var userAgent = navigator.userAgent.toLowerCase();
return userAgent.indexOf("edge") > -1;
}
// endregion
////////////////////////////////////////////////////////////////////////////////
// region 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;display:inline}.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;
}
// endregion
/**
*
* @alias module:QuickSettings
* @lends module:QuickSettings.prototype
*/
var QuickSettings = {
_version: "3.0.2",
_topZ: 1,
_panel: null,
_titleBar: null,
_content: null,
_startX: 0,
_startY: 0,
_hidden: false,
_collapsed: false,
_controls: null,
_keyCodeArray: new Array(),
_draggable: true,
_collapsible: true,
_globalChangeHandler: null,
////////////////////////////////////////////////////////////////////////////////
// region 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);
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region 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) {
if (this._controls[title].getValue) {
json[title] = this._controls[title].getValue();
}
}
if (asString) {
json = JSON.stringify(json);
}
return json;
},
/**
* Sets values of any controls from a JSON object or string. The JSON is one large object with title: value elements for each control you want to set.
* @param json {Object} A string or JS object containing the titles and values to set.
* @returns {module:QuickSettings}
*/
setValuesFromJSON: function (json) {
if (typeof json === "string") {
json = JSON.parse(json);
}
for (var title in json) {
if (this._controls[title] && this._controls[title].setValue) {
this._controls[title].setValue(json[title]);
}
}
return this;
},
/**
* Sets up the panel to save all of its values to local storage. This will also immediately try to read in any saved values from local storage, if they exist.
* So the method should be called after all controls are created on the panel.
* @param name {String} A unique name to store the values under in localStorage.
* @return {model:QuickSettings}
*/
saveInLocalStorage: function (name) {
this._localStorageName = name;
this._readFromLocalStorage(name);
return this;
},
/**
* Clears any saved values in local storage.
* @param name {String} The unique name in localStorage to clear.
* @return {module:QuickSettings}
*/
clearLocalStorage: function (name) {
localStorage.removeItem(name);
return this;
},
_saveInLocalStorage: function (name) {
localStorage.setItem(name, this.getValuesAsJSON(true));
},
_readFromLocalStorage: function (name) {
var str = localStorage.getItem(name);
if (str) {
this.setValuesFromJSON(str);
}
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region CREATION FUNCTIONS
////////////////////////////////////////////////////////////////////////////////
_createPanel: function (x, y, parent) {
this._panel = createElement("div", null, "qs_main", parent || document.body);
this._panel.style.zIndex = ++QuickSettings._topZ;
this.setPosition(x || 0, y || 0);
this._controls = {};
},
_createTitleBar: function (text) {
this._titleBar = createElement("div", null, "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 = createElement("div", null, "qs_content", this._panel);
},
_createContainer: function () {
var container = createElement("div", null, "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;
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region 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;
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region 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) {
document.removeEventListener("mousemove", this._drag);
document.removeEventListener("mouseup", this._endDrag);
event.preventDefault();
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region 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 (title) {
if (this._localStorageName) {
this._saveInLocalStorage(this._localStorageName);
}
if (this._globalChangeHandler) {
this._globalChangeHandler(title);
}
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region 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. More than one key may be set (allows use of keys on stylus as well as keyboard).
* @param key (accepts a string, e.g. 'a' or integer representing javascript character code)
* @returns {module:QuickSettings}
*/
setKey: function(key) {
if(Number.isInteger(key)) {
this._keyCodeArray.push(key);
}else{
this._keyCodeArray.push(key.toUpperCase().charCodeAt(0));
}
document.addEventListener("keyup", this._onKeyUp);
return this;
},
_onKeyUp: function(event) {
if(this._keyCodeArray.includes(event.keyCode)) {
this.toggleVisibility();
}
},
_doubleClickTitle: function () {
if (this._collapsible) {
this.toggleCollapsed();
}
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region 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 {String} 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;
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region GET/SET VALUES
////////////////////////////////////////////////////////////////////////////////
getValue: function (title) {
return this._controls[title].getValue();
},
setValue: function (title, value) {
this._controls[title].setValue(value);
this._callGCH(title);
return this;
},
// endregion
//==========================================================================================
//==========================================================================================
// CONTROL CREATION AND MANAGEMENT FUNCTIONS
//==========================================================================================
//==========================================================================================
////////////////////////////////////////////////////////////////////////////////
// region BOOLEAN
////////////////////////////////////////////////////////////////////////////////
/**
* 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 id = getNextID();
var label = createElement("label", null, "qs_checkbox_label", container);
label.textContent = title;
label.setAttribute("for", id);
var checkbox = createElement("label", null, "qs_checkbox", container);
checkbox.setAttribute("for", id);
var input = createInput("checkbox", id, null, checkbox);
input.checked = value;
var span = createElement("span", null, null, checkbox);
this._controls[title] = {
container: container,
control: input,
getValue: function () {
return this.control.checked;
},
setValue: function (value) {
this.control.checked = value;
if (callback) {
callback(value);
}
},
};
var self = this;
input.addEventListener("change", function () {
if (callback) {
callback(input.checked);
}
self._callGCH(title);
});
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;
});
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region BUTTON
////////////////////////////////////////////////////////////////////////////////
/**
* Adds a button to the panel.
* @param title {String} The title of the control.
* @param [callback] {Function} Callback function to be called when the button is clicked.
* @returns {module:QuickSettings}
*/
addButton: function (title, callback) {
var container = this._createContainer();
var button = createInput("button", getNextID(), "qs_button", container);
button.value = title;
this._controls[title] = {
container: container,
control: button
}
var self = this;
button.addEventListener("click", function () {
if (callback) {
callback(button);
}
self._callGCH(title);
});
return this;
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region COLOR
////////////////////////////////////////////////////////////////////////////////
/**
* Adds a color picker control. In some browsers this will just render as a text input field, but should still retain all other functionality.
* @param title {String} The title of this control.
* @param color {String} The initial color value for this control.
* @param [callback] {Function} Callback that will be called when the value of this control changes.
* @returns {module:QuickSettings}
*/
addColor: function (title, color, callback) {
if (isSafari() || isEdge() || isIE()) {
return this.addText(title, color, callback);
}
var container = this._createContainer();
var label = createLabel("<b>" + title + ":</b> " + color, container);
var id = getNextID();
var colorInput = createInput("color", id, "qs_color", container);
colorInput.value = color || "#ff0000";
var colorLabel = createElement("label", null, "qs_color_label", container);
colorLabel.setAttribute("for", id);
colorLabel.style.backgroundColor = colorInput.value;
this._controls[title] = {
container: container,
control: colorInput,
colorLabel: colorLabel,
label: label,
title: title,
getValue: function () {
return this.control.value;
},
setValue: function (value) {
this.control.value = value;
this.colorLabel.style.backgroundColor = colorInput.value;
this.label.innerHTML = "<b>" + this.title + ":</b> " + this.control.value;
if (callback) {
callback(value);
}
}
};
var self = this;
colorInput.addEventListener("input", function () {
label.innerHTML = "<b>" + title + ":</b> " + colorInput.value;
colorLabel.style.backgroundColor = colorInput.value;
if (callback) {
callback(colorInput.value);
}
self._callGCH(title);
});
return this;
},
/**
* Adds a color picker control bound to an object. In some browsers this will just render as a text input field, but should still retain all other functionality.
* @param title {String} The title of this control.
* @param color {String} The initial color value for 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}
*/
bindColor: function (title, color, object) {
return this.addColor(title, color, function (value) {
object[title] = value;
});
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region DATE INPUT
////////////////////////////////////////////////////////////////////////////////
/**
* Adds a date input control. In some browsers this will just render as a text input field, but should still retain all other functionality.
* @param title {String} The title of the control.
* @param date {String|Date} A string in the format "YYYY-MM-DD" or a Date object.
* @param [callback] {Function} Callback function that will be called when the value of this control changes.
* @returns {*}
*/
addDate: function (title, date, callback) {
function _createDateString(date)
{
if (date instanceof Date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
if (month < 10) month = "0" + month;
var day = date.getDate();
if (day < 10) day = "0" + day;
return year + "-" + month + "-" + day;
}
else {
return date;
}
}
var dateStr = _createDateString(date);
if (isIE()) {
return this.addText(title, dateStr, callback);
}
var container = this._createContainer();
var label = createLabel("<b>" + title + "</b>", container);
var dateInput = createInput("date", getNextID(), "qs_text_input", container);
dateInput.value = dateStr || "";
this._controls[title] = {
container: container,
control: dateInput,
label: label,
getValue: function () {
return this.control.value;
},
setValue: function (date) {
var dateStr = _createDateString(date);
this.control.value = dateStr || "";
if (callback) {
callback(dateStr);
}
}
}
var self = this;
dateInput.addEventListener("input", function () {
if (callback) {
callback(dateInput.value);
}
self._callGCH(title);
});
return this;
},
/**
* Adds a date input control. In some browsers this will just render as a text input field, but should still retain all other functionality.
* @param title {String} The title of the control.
* @param date {String|Date} A string in the format "YYYY-MM-DD" or a Date object.
* @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 {*}
*/
bindDate: function (title, date, object) {
return this.addDate(title, date, function (value) {
object[title] = value;
});
},
// endregion
////////////////////////////////////////////////////////////////////////////////
// region DROPDOWN
////////////////////////////////////////////////////////////////////////////////
/**
* Adds a dropdown (select) control. Dropdown items can be strings ("one", "two", "three"), any other values that can be converted to strings (1, 2, 3), or an object that contains label and value properties ({label: "one", value: 77}).
* @param title {String} The title of the control.
* @param items {Array} An array of items.
* @param [callback] {Function} Callback function that will be called when a new option is chosen. Callback will be passed an object containing "index", "label", and "value" properties. If the selected item is a simple value, then label and value will be the same.
* @returns {module:QuickSettings}
*/
addDropDown: function (title, items, callback) {
var container = this._createContainer();
var label = createLabel("<b>" + title + "</b>", container);
var select = createElement("select", null, "qs_select", container);
for (var i = 0; i < items.length; i++) {
var option = createElement("option"),
item = items[i];
if (item.label) {
option.value = item.value;
option.innerText = item.label;
}
else {
option.label = item;
option.innerText = item;
}
select.add(option);
}
;
var self = this;
select.addEventListener("change", function () {
var index = select.selectedIndex,
options = select.options;
if (callback) {
callback({
index: index,
label: options[index].label,
value: items[index].value || items[index]
});
}
self._callGCH(title);
});
this._controls[title] = {
container: container,
control: select,
label: label,
getValue: function () {
var index = this.control.selectedIndex;
return {
index: index,
label: this.control.options[index].label,
value: items[index].value || items[index]
}
},
setValue: function (value) {
var index
if (value.index != null) {
index = value.index;
}
else {
index = value;
}
var options = this.control.options;
this.control.selectedIndex = index;
if (callback) {
callback({
index: index,
label: options[index].label,
value: items[index].value || items[index]