-
Notifications
You must be signed in to change notification settings - Fork 0
/
thanos-testing-0.0.2.js
1676 lines (1598 loc) · 79.6 KB
/
thanos-testing-0.0.2.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
'use strict';
// Thanos.js version 0.0.2.
// Modifications or additions to native functionality:
// 1) In .misc_polyfills: polyfills or fixes to IE.
void function () {
var h = {}
if ((typeof module === 'object') && module.exports) module.exports = h
if (window) window.helperjs = h
// Settings.
h.library_settings = {
misc_polyfills : true,
string_manipulation : true,
object_manipulation : true,
physical_control : true,
dom_position : true,
download : true,
gui_widgets : true,
dom_manipulation : true,
domain_and_directory : true,
graphics : true,
feature_detection : true,
cookie : true,
font_loading : true,
misc : true,
}
// Hmm...
//['map'].forEach(function (op) {
// NodeList.prototype[op] = HTMLCollection.prototype[op] = function (callback, thisArg) {
// return Array.prototype.slice.call(this)[op](callback, thisArg)
// }
//})
var addStyle = function (element, text) {
// Multiple element support.
if ((typeof element == 'object') && (element instanceof Array)) {element.forEach(function (currentElement) {addStyle(currentElement, text)}); return}
if (element.getAttribute('style') == null) {element.setAttribute('style', text); return}
element.setAttribute('style', element.getAttribute('style') + '; ' + text)
}
// <Miscellaneous browser fixes and polyfills. TAG: polyfills.>
if (h.library_settings.misc_polyfills) {
// Fix errors caused by console (or console.log) not being defined in certain browsers.
if (typeof console == 'undefined') console = {}
if (typeof console.log == 'undefined') console.log = function () {}
// IE .innerHTML shim/polyfill.
if (/(msie|trident)/i.test(navigator.userAgent)) {
void function () {
var innerhtmlGet = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'innerHTML').get
var innerhtmlSet = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'innerHTML').set
Object.defineProperty(HTMLElement.prototype, 'innerHTML', {
get: function () {return innerhtmlGet.call(this)},
set: function (html) {
var childNodes = this.childNodes
for (var curlen = childNodes.length, i = curlen; i > 0; i--) {
this.removeChild(childNodes[0])
}
innerhtmlSet.call(this, html)
}
})
} ()
}
// Fix for IE HTMLImageElement.start being unwritable.
if (/(msie|trident)/i.test(navigator.userAgent)) {
Object.defineProperty(HTMLImageElement.prototype, 'start', {writable: true})
}
window.requestAnimationFrame = function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (callback) {window.setTimeout(callback, 1000 / 60)}
} ()
}
// </Miscellaneous browser fixes and polyfills.>
// <String manipulation functions. TAG: string, TAG: string manipulation.>
if (h.library_settings.object_manipulation) {
h.string = {}
h.string.isAlphanumeric = function (string) {return string.search(/^[a-z0-9]+$/i) != -1}
}
// </String manipulation functions.>
// <Object manipulation functions. TAG: array, TAG: arrays, TAG: array manipulation, TAG: object manipulation, TAG: copy, TAG: test.>
if (h.library_settings.object_manipulation) {
h.Array = {}
h.Array.shuffle = function (arr) {
for (var i = arr.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1)); var x = arr[i]; arr[i] = arr[j]; arr[j] = x
}
return arr
}
}
// <Object manipulation functions.>
// <Physical control functions. TAG: mouse, TAG: keyboard, TAG: pointer, TAG: pinch, TAG: swipe.>
if (h.library_settings.physical_control) {
var getRightClick = h.getRightClick = function (evt) {return (evt.which) ? (evt.which == 3) : ((evt.button) ? (evt.button == 2) : false)}
h.getWheelData = function (evt) {return evt.detail ? evt.detail * -1 : evt.wheelDelta / 40}
h.getKeyCodeString = function (evt) {
var keyCode = evt.keyCode
var keyMap = {37: 'left', 38: 'up', 39: 'right', 40: 'down', 107: '+', 187: '+', 109: '-', 189: '-', 16: 'shift', 27: 'escape', 13: 'enter'}
if (typeof keyCode[keyMap] == 'undefined') {return String.fromCharCode(keyCode).toUpperCase()} else {return keyCode[keyMap]}
}
h.addPinchControls = function (init) {
var target = init.target, currentDistance = false, zoomX = undefined, zoomY = undefined
target.addEventListener('touchend' , endEffect)
target.addEventListener('touchcancel' , endEffect)
target.addEventListener('touchleave' , endEffect)
target.addEventListener('touchstart' , pinch)
target.addEventListener('touchmove' , pinch)
function endEffect (evt) {if (currentDistance != false) if (typeof init.endEffect != 'undefined') init.endEffect(evt)}
function pinch (evt) {
if (evt.touches.length < 2) {currentDistance = false; return}
if (evt.type == 'touchmove') {
if (currentDistance == false) {return} else {var lastDistance = currentDistance}
} else {
if ((typeof init.startCondition != 'undefined') && (init.startCondition (evt) === false)) return
}
var xy0 = getXY(evt.touches[0], target); var x0 = xy0[0]; var y0 = xy0[1]
var xy1 = getXY(evt.touches[1], target); var x1 = xy1[0]; var y1 = xy1[1]
if ((init.alwaysRecalculateZoomTarget === true) || (currentDistance == false)) {
zoomX = (x0 + x1) / 2, zoomY = (y0 + y1) / 2
}
currentDistance = Math.sqrt(Math.pow(Math.abs(x0 - x1), 2) + Math.pow(Math.abs(y0 - y1), 2))
if (evt.type == 'touchstart') {
if (typeof init.startEffect != 'undefined') init.startEffect(evt, x0, y0, x1, y1)
return
}
init.resultFunction(currentDistance / lastDistance, zoomX, zoomY)
if (typeof init.moveEffect != 'undefined') init.moveEffect(evt)
}
}
h.addSwipeControls = function (init) {
// N.B.: For PCs, use .preventDefault on dragstart. For mobile, use .preventDefault on touchmove.
if (typeof init.isMobile == 'undefined') init.isMobile = true
var target = init.target, initialX, initialY, currentX, currentY, swipeInEffect = false
target.addEventListener(init.isMobile ? 'touchstart' : 'mousedown', swipeStartEvent)
target.addEventListener(init.isMobile ? 'touchmove' : 'mousemove', swipeEvent)
target.addEventListener(init.isMobile ? 'touchend' : 'mouseup' , function (evt) {if (swipeInEffect == false) return; swipeEndEvent(evt)})
target.addEventListener(init.isMobile ? 'touchleave' : 'mouseout' , function (evt) {if (swipeInEffect == false) return; swipeEndEvent(evt)})
target.addEventListener(init.isMobile ? 'touchcancel' : 'blur' , function (evt) {if (swipeInEffect == false) return; swipeEnd(evt)})
function swipeEnd (evt) {
if (swipeInEffect == false) return
swipeInEffect = false
if (typeof init.endEffect != 'undefined') init.endEffect(evt)
}
function swipeStartEvent (evt) {
if ((typeof init.startCondition != 'undefined') && (init.startCondition(evt) == false)) return
if ((init.isMobile) && (evt.touches.length != 1)) return
var xy = getXY((init.isMobile) ? evt.touches[0] : evt, target); initialX = xy[0]; initialY = xy[1]
currentX = initialX; currentY = initialY
if (swipeInEffect == true) return
swipeInEffect = true
if (typeof init.startEffect != 'undefined') init.startEffect(evt)
}
function swipeEvent (evt) {
if (
((init.isMobile) && (evt.touches.length != 1)) ||
((typeof init.continueCondition != 'undefined') && (init.continueCondition(evt) === false))
) {swipeEnd(evt); return}
var xy = getXY((init.isMobile) ? evt.touches[0] : evt, target); currentX = xy[0]; currentY = xy[1]
if (typeof init.swipeMove != 'undefined') init.swipeMove({evt: evt, init: init, initialX: initialX, initialY: initialY, currentX: currentX, currentY: currentY, swipeInEffect: swipeInEffect})
}
function swipeEndEvent (evt) {
if ((typeof init.continueCondition != 'undefined') && (init.continueCondition(evt) === false)) {swipeEnd(evt); return}
if (currentX < initialX) {
if (typeof init.swipeLeft != 'undefined') init.swipeLeft(evt)
} else {
if ((currentX > initialX) && (typeof init.swipeRight != 'undefined')) init.swipeRight(evt)
}
if (currentY < initialY) {
if (typeof init.swipeDown != 'undefined') init.swipeDown(evt)
} else {
if ((currentY > initialY) && (typeof init.swipeUp != 'undefined')) init.swipeUp(evt)
}
swipeEnd(evt)
}
}
}
// </Physical control functions.>
// <Mouse/DOM object position functions. TAG: mouse, TAG: mouse position, TAG: positions, TAG: object positions, TAG: element positions.>
if (h.library_settings.dom_position) {
var getXY = h.getXY = function (evt, target) {
if (typeof target == 'undefined') target = evt.target
var rect = target.getBoundingClientRect(); return [evt.clientX - rect.left, evt.clientY - rect.top]
}
var getX = h.getX = function (evt, target) {
if (typeof target == 'undefined') target = evt.target
return evt.clientX - target.getBoundingClientRect().left
}
var getY = h.getY = function (evt, target) {
if (typeof target == 'undefined') target = evt.target
return evt.clientY - target.getBoundingClientRect().top
}
// Function to find the absolute position of a DOM object.
var findabspos = h.findabspos = function (obj, lastobj) {
var curleft = 0, curtop = 0, borderWidthTest = 0
if (typeof lastobj == 'undefined') lastobj = null
do {
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderLeftWidth)
if (!isNaN(borderWidthTest)) curleft += borderWidthTest
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderTopWidth)
if (!isNaN(borderWidthTest)) curtop += borderWidthTest
curleft += obj.offsetLeft
curtop += obj.offsetTop
if (obj.offsetParent == lastobj) return [curleft, curtop] // If offsetParent is lastobj (or null if lastobj is null), return the result.
obj = obj.offsetParent
} while (true)
}
// Function to find the absolute x-position of a DOM object.
h.findabsposX = function (obj, lastobj) {
var curleft = 0, borderWidthTest = 0
if (typeof lastobj == 'undefined') lastobj = null
do {
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderLeftWidth)
if (!isNaN(borderWidthTest)) curleft += borderWidthTest
curleft += obj.offsetLeft
if (obj.offsetParent == lastobj) return curleft // If offsetParent is lastobj (or null if lastobj is null), return the result.
obj = obj.offsetParent
} while (true)
}
// Function to find the absolute y-position of a DOM object.
h.findabsposY = function (obj, lastobj) {
var curtop = 0, borderWidthTest = 0
if (typeof lastobj == 'undefined') lastobj = null
do {
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderTopWidth)
if (!isNaN(borderWidthTest)) curtop += borderWidthTest
curtop += obj.offsetTop
if (obj.offsetParent == lastobj) return curtop // If offsetParent is lastobj (or null if lastobj is null), return the result.
obj = obj.offsetParent
} while (true)
}
// Function to find the absolute position of a DOM object, taking CSS scale transforms into account.
var findabsposZoom = h.findabsposZoom = function (obj, lastobj) {
var curleft = 0, curtop = 0, zoomLevelX = 0, zoomLevelY = 0, borderWidthTest = 0
if (typeof lastobj == 'undefined') lastobj = null
do {
if (obj.offsetParent == lastobj) {
zoomLevelX = 1; zoomLevelY = 1
} else {
zoomLevelX = getInheritedTransform(obj.offsetParent, {type: 'scale', xy: 'x'})
zoomLevelY = getInheritedTransform(obj.offsetParent, {type: 'scale', xy: 'y'})
}
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderLeftWidth)
if (!isNaN(borderWidthTest)) curleft += borderWidthTest * zoomLevelX
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderTopWidth)
if (!isNaN(borderWidthTest)) curtop += borderWidthTest * zoomLevelY
curleft += obj.offsetLeft * zoomLevelX
curtop += obj.offsetTop * zoomLevelY
if (obj.offsetParent == lastobj) return [curleft, curtop] // If offsetParent is lastobj (or null if lastobj is null), return the result.
obj = obj.offsetParent
} while (true)
}
// Function to find the absolute x-position of a DOM objet, taking CSS scale transforms into account.
var findabsposZoomX = h.findabsposZoomX = function (obj, lastobj) {
var curleft = 0, zoomLevelX = 0, borderWidthTest = 0
if (typeof lastobj == 'undefined') lastobj = null
do {
if (obj.offsetParent == lastobj) {
zoomLevelX = 1
} else {
zoomLevelX = getInheritedTransform(obj.offsetParent, {type: 'scale', xy: 'x'})
}
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderLeftWidth)
if (!isNaN(borderWidthTest)) curleft += borderWidthTest * zoomLevelX
curleft += obj.offsetLeft * zoomLevelX
if (obj.offsetParent == lastobj) return curleft // If offsetParent is lastobj (or null if lastobj is null), return the result.
obj = obj.offsetParent
} while (true)
}
// Function to find the absolute y-position of a DOM object, taking CSS scale transforms into account.
var findabsposZoomY = h.findabsposZoomY = function (obj, lastobj) {
var curtop = 0, zoomLevelY = 0, borderWidthTest = 0
if (typeof lastobj == 'undefined') lastobj = null
do {
if (obj.offsetParent == lastobj) {
zoomLevelY = 1
} else {
zoomLevelY = getInheritedTransform(obj.offsetParent, {type: 'scale', xy: 'y'})
}
borderWidthTest = parseFloat(window.getComputedStyle(obj).borderTopWidth)
if (!isNaN(borderWidthTest)) curtop += borderWidthTest * zoomLevelY
curtop += obj.offsetTop * zoomLevelY
if (obj.offsetParent == lastobj) return curtop // If offsetParent is lastobj (or null if lastobj is null), return the result.
obj = obj.offsetParent
} while (true)
}
// Example usage: getInheritedTransform(obj, {type: scale, xy: 'x'})
var getInheritedTransform = h.getInheritedTransform = function (obj, init) {
var transformType = init.type
var xy = init.xy
var transformString = ''
var transformArray = []
switch (transformType) {
case 'scale':
var scale = 1
while (true) {
transformString = getTransformString(obj)
if (transformString != false) {
transformArray = (transformString.slice(7, transformString.length - 6)).split(',')
switch (xy) {
case 'x': scale *= parseFloat(transformArray[0]); break
case 'y': scale *= parseFloat(transformArray[3]); break
}
}
var obj = obj.parentNode
if (obj.parentNode == null) break
}
return scale
}
function getTransformString (obj) {
var transformString = window.getComputedStyle(obj)['Transform']
if (typeof transformString == 'undefined') {transformString = window.getComputedStyle(obj)['msTransform']}
if (typeof transformString == 'undefined') {transformString = window.getComputedStyle(obj)['webkitTransform']}
if (typeof transformString == 'undefined') {transformString = window.getComputedStyle(obj)['MozTransform']}
if (typeof transformString == 'undefined') {transformString = window.getComputedStyle(obj)['OTransform']}
if ((typeof transformString == 'undefined') || (transformString == 'none')) return false
return transformString
}
}
}
// </Mouse/DOM object position functions.>
// <Ajax / download functions. TAG: ajax, TAG: xhr, TAG: download, TAG: downloads.>
if (h.library_settings.download) {
// Ajax functions.
h.ajax = function () {
var ajax = {}
function merge_objects (secondary, primary) {
if (typeof secondary == 'undefined') var primary = primary.primary, secondary = primary.secondary
var result = {}
for (var property in secondary) {result[property] = secondary[property]}
for (var property in primary) {result[property] = primary [property]}
return result
}
// Only ajax_queue.add, ajax_queue.group_add, ajax_queue.group_run, and ajax_queue.run are exposed.
var ajax_queue = ajax.queue = function () {
var ajax_queue = {}
var queue_list = {}
// Enqueue an ajax function: If the queue is locked, add it to the queue. Otherwise, run it.
// Parameters: .name and .data.
ajax_queue.enqueue = ajax_queue.add = function (init) {
var name = (typeof init.name != 'undefined') ? init.name : 'default'
if (typeof queue_list[name] == 'undefined') queue_list[name] = {locked: false, queue: []}
var queue = queue_list[name]
if (queue.locked) {
queue.queue_function.push(init.function)
queue.queue_data.push(init.data)
return
}
process(init)
}
// Process an entry.
function process (init) {
var name = init.name
var data = init.data
queue_list[name].locked = true
init.function.apply(null, init.data)
['success', 'error', 'callback'].forEach(function (func) {
init.data[func] = function () {
queue_list[name].locked = false
init.data[func].apply(null, Array.prototype.slice.call(arguments))
}
})
}
} ()
// A 'latch'?
ajax.create_accumulator = function (init) {
var default_function = init.function
var func_list = []
var run_list = []
var resolve_func = undefined
var test_counter = 0
var accumulator = {add: add, add_custom: add_custom, resolve: resolve, test: test}; return accumulator
function add () {func_list.push(default_function); run_list.push(Array.prototype.slice.call(arguments))}
function add_custom (init) {
var func = (typeof init.function != 'undefined') ? init.function : default_function
func_list.push(func); run_list.push(init.data)
}
function resolve (new_resolve_func) {
if (typeof new_resolve_func != 'undefined') resolve_func = new_resolve_func
test_counter += run_list.length
run_list.forEach(function (params, i) {func_list[i].apply(null, params)})
return accumulator
}
function test () {
test_counter -= 1
if (accumulator.test_progress) accumulator.test_progress(run_list.length - test_counter, run_list.length)
if (test_counter == 0 && resolve_func) resolve_func()
}
}
// Get data. Parameters:
// .plaintext / .send_data_as_plaintext, .is_asynchronous / .async, .charset, .data,
// .ignore_request_status, .response_type, .header_list, .file, .request_method,
// .error, .success.
ajax.get_data = function (params) {
// Convert request into GET or POST data.
var send_data_as_plaintext = params.send_data_as_plaintext || params.plaintext || false
var is_asynchronous = true
if (typeof params.is_asynchronous != 'undefined') is_asynchronous = params.is_asynchronous
if (typeof params.async != 'undefined') is_asynchronous = params.async
var charset = (typeof params.charset != 'undefined') ? params.charset : ''
var data = (typeof params.data != 'undefined') ? params.data : ''
var ignore_request_status = (typeof params.ignore_request_status != 'undefined') ? params.ignore_request_status : false
var response_type = (typeof params.response_type != 'undefined') ? params.response_type : undefined
var header_list = (typeof params.header_list != 'undefined') ? params.header_list : undefined
// Call the request function.
var http_request_result = make_request(params.file, data, send_data_as_plaintext, charset, is_asynchronous, response_type, header_list, params.request_method, params.upload)
var http_request = http_request_result['http_request']
if (is_asynchronous == false) return process_http_request()
http_request.onreadystatechange = function () {if (http_request.readyState == 4) process_http_request()}
return http_request
function process_http_request () {
var response_text = http_request.responseText
// If the request status isn't 200, send an error.
if ((http_request.status != 200) && (ignore_request_status == false)) {
response_text = {error: true, errormessage: response_text}
} else {
if ((send_data_as_plaintext != true) && (response_text != '')) {
// Send an error if the JSON text can't be parsed.
try {
response_text = JSON.parse(response_text)
} catch (err) {
response_text = {error: true, errormessage: response_text}
if (is_asynchronous == false) {return response_text} else {if (params.error) params.error(response_text); return}
}
}
}
// Send an error.
if (response_text == null || ((response_text != '') && (response_text.error == true))) {
if (typeof params.error != 'undefined') if (is_asynchronous == false) {return response_text} else {return params.error(response_text)}
return
}
if (is_asynchronous == false) return response_text
if (typeof params.success != 'undefined') params.success(response_text)
}
}
// Use XMLHttpRequest to get text data from a file.
ajax.get_text_data = function (params) {return get_data(merge_objects(params, {plaintext: true}))}
// Set text in a DOM element based on a filename (url).
ajax.set_innerhtml_from_url = function (params) {
return get_data(merge_objects(params, {plaintext: true, success: function (result) {obj.innerHTML = result; params.success()}}))
}
function make_request (url, data, send_data_as_plaintext, charset, is_asynchronous, response_type, header_list, request_method, upload_list) {
if (upload_list !== undefined) request_method = 'POST'
if ((typeof send_data_as_plaintext == 'undefined') || (send_data_as_plaintext !== true)) send_data_as_plaintext = false
if ((typeof is_asynchronous == 'undefined') || (is_asynchronous != false)) is_asynchronous = true
if (typeof charset == 'undefined' || charset == '') {charset = ''} else {charset = '; charset=' + charset}
var http_request = new XMLHttpRequest()
if (!http_request) {alert('Cannot create an XMLHTTP instance for some reason. Please try reloading the page.')}
if (typeof request_method == 'undefined') var request_method = ((data === null || data === undefined || data === '') ? 'GET' : 'POST')
if (request_method == 'GET') {
if (typeof data == 'string') {
// Add a '?' or '&' if the '?' doesn't already exist in the ending part of the url.
var altchar = (data[0] != '&') ? '&' : ''
var connecting_character = (url.indexOf('?') == -1) ? '?' : altchar
url += connecting_character + data
}
data = null
}
http_request.open(request_method, url, is_asynchronous)
if (typeof response_type != 'undefined') http_request.responseType = response_type
var content_type_is_set = header_list && header_list.some(function (header) {return header.name == 'Content-type'})
if (!content_type_is_set) {
if (send_data_as_plaintext === true) {
http_request.setRequestHeader('Content-type', 'text/plain' + charset)
} else if (typeof upload_list == 'undefined') {
http_request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded' + charset)
} else {
// Do nothing. Don't set a content type for uploads as that is already set when we create a FormData object.
}
}
if (send_data_as_plaintext === true && typeof http_request.overrideMimeType != 'undefined') http_request.overrideMimeType('text/plain; charset=x-user-defined')
if (header_list) {
header_list.forEach(function (header) {
http_request.setRequestHeader(header.name, header.content)
if (header.name == 'Content-type') http_request.overrideMimeType(header.content)
})
}
if (typeof upload_list != 'undefined') {
var other_data = (typeof data == 'string') ? h.getUrlVars(data) : {}
var data = new FormData()
for (var prop_name in other_data) {
data.append(prop_name, other_data[prop_name])
}
if (!(upload_list instanceof FileList)) {upload_list = [upload_list]} else {upload_list = Array.from(upload_list)}
upload_list.forEach(function (item) {data.append('file', item, item.name)})
}
http_request.send(data)
return {'http_request': http_request}
}
ajax.is_library_container = true
return ajax
} ()
h.get_data = h.ajax.get_data
}
// </Ajax / download functions>
// <DOM-widget functions. TAG: DOM, TAG: widgets, TAG: DOM widgets.>
if (h.library_settings.gui_widgets) {
h.sliderbar = function (init) {
var main = (typeof init.mainElement != 'undefined') ? init.mainElement : document.createElement('div')
Object.defineProperty(main, 'parent', {
get: function () {return parent},
set: function (newParent) {
if ((typeof newParent != 'object') || (!(newParent instanceof HTMLElement))) return
parent = newParent; newParent.appendChild(main); if (main.textbox) newParent.appendChild(main.textbox)
}
})
var parent = main.parent = init.parent
var backgroundStyle = init.backgroundStyle
var foregroundContainerStyle = init.foregroundContainerStyle
var foregroundStyle = init.foregroundStyle
var foregroundInverseStyle = init.foregroundInverseStyle
var backgroundClass = init.backgroundClass
var foregroundContainerClass = init.foregroundContainerClass
var foregroundClass = init.foregroundClass
var foregroundInverseClass = init.foregroundInverseClass
var backgroundBeyondMaxStyle = init.backgroundBeyondMaxStyle
var pivotSliceStyle = init.pivotSliceStyle
var pivotSliceClass = init.pivotSliceClass
var controlStyle = init.controlStyle
var controlImageSrc = init.controlImage
var controlClass = init.controlClass
var pointInitial = (typeof init.pointInitial != 'undefined') ? init.pointInitial : 0
var orientation = ((typeof init.orientation != 'undefined') && (init.orientation == 'vertical')) ? 'vertical' : 'horizontal'
var useTouchEvents = ((typeof init.useTouchEvents != 'undefined') && (init.useTouchEvents == true )) ? true : false
var useMouseEvents = ((typeof init.useMouseEvents == 'undefined') || (init.useMouseEvents == true )) ? true : false
main.startCondition = (typeof init.startCondition != 'undefined') ? init.startCondition : undefined
main.events = {update: ('events' in init) ? init.events.update : undefined}
main.pointMaximum = (typeof init.pointMaximum == 'number') ? init.pointMaximum : 100
main.pivotPoint = (typeof init.pivotPoint == 'number') ? init.pivotPoint : 0
main.pointUpperLimit = (typeof init.pointUpperLimit == 'number') ? init.pointUpperLimit : 100
main.textboxEnabled = init.textboxEnabled || false
main.controlUnitOffset = (typeof init.controlUnitOffset == 'number') ? init.controlUnitOffset : 0
main.cssUnitType = init.cssUnitType || 'px'
main.recalculateSize = (typeof init.recalculateSize != 'undefined') ? init.recalculateSize : true
var widthHeight = (orientation == 'horizontal') ? 'width' : 'height'
var leftTop = (orientation == 'horizontal') ? 'left' : 'top'
var pageXY = (orientation == 'horizontal') ? 'pageX' : 'pageY'
var orientationXY = (orientation == 'horizontal') ? 'x' : 'y'
var findabsposZoom = (orientation == 'horizontal') ? h.findabsposZoomX : h.findabsposZoomY
var pxToCssUnitTypeFixed = undefined
var zoomLevelFixed = undefined
// Add a linked textbox object if it is set.
if (main.textboxEnabled != true) {
var textbox = main.textbox = undefined
} else {
var textbox = main.textbox = document.createElement('div')
textbox.className = init.textboxClass || ''; addStyle(textbox, init.textboxStyle || '')
parent.appendChild(textbox)
if (typeof init.textboxPrefix != 'undefined') {
var textboxPrefix = document.createElement('div')
textboxPrefix.className = init.textboxPrefixClass || ''; addStyle(textboxPrefix, init.textboxPrefixStyle || '')
textboxPrefix.innerHTML = init.textboxPrefix || ''
textbox.appendChild(textboxPrefix)
}
var textboxNumber = document.createElement('input')
addStyle(textboxNumber, init.textboxNumberStyle || ''); textboxNumber.className = init.textboxNumberClass || ''
textboxNumber.type = 'text'
textboxNumber.readOnly = false
textboxNumber.addEventListener('input', textboxChange)
textboxNumber.addEventListener('keypress', textboxKeypress)
textbox.appendChild(textboxNumber)
if (typeof init.textboxSuffix != 'undefined') {
var textboxSuffix = document.createElement('div')
textboxSuffix.className = init.textboxSuffixClass || ''; addStyle(textboxSuffix, init.textboxSuffixStyle || '')
textboxSuffix.innerHTML = init.textboxSuffix || ''
textbox.appendChild(textboxSuffix)
}
}
// Set the main object (background) class and style. Don't override the original style with a blank if a new one isn't defined.
main.className = init.backgroundClass || ''; if (typeof backgroundStyle != 'undefined') addStyle(main, backgroundStyle)
// Create the 'backgroundBeyondMax' object and set its class and style.
if ((typeof backgroundBeyondMaxStyle != 'undefined') || (typeof backgroundBeyondMaxClass != 'undefined')) {
main.backgroundBeyondMax = document.createElement('div'); main.backgroundBeyondMax.className = init.backgroundBeyondMaxClass || ''; addStyle(main.backgroundBeyondMax, backgroundBeyondMaxStyle || '')
main.appendChild(main.backgroundBeyondMax)
}
// Create the foreground object and set its class and style.
main.foregroundContainer = document.createElement('div'); addStyle(main.foregroundContainer, 'position: relative; overflow: hidden; '+widthHeight+': 100%; line-height: 0')
main.foregroundContainer.className = foregroundContainerClass || ''; addStyle(main.foregroundContainer, foregroundContainerStyle || '')
main.appendChild(main.foregroundContainer)
main.foregroundContainer.style.pointerEvents = 'none'
main.foreground = document.createElement('div'); main.foreground.className = foregroundClass || ''; addStyle(main.foreground, foregroundStyle || '')
main.foregroundContainer.appendChild(main.foreground)
main.foreground.style.pointerEvents = 'none'
main.foregroundInverse = document.createElement('div'); main.foregroundInverse.className = foregroundInverseClass || ''; addStyle(main.foregroundInverse, foregroundInverseStyle || '')
main.foregroundContainer.appendChild(main.foregroundInverse)
main.foregroundInverse.style.pointerEvents = 'none'
// Create the control object and set its class and style.
var controlElementType = (typeof controlImageSrc != 'undefined') ? 'img' : 'div'
main.control = document.createElement(controlElementType)
main.control.style.pointerEvents = 'none'
main.control.className = controlClass || ''
addStyle(main.control, controlStyle || '')
if (typeof controlImageSrc != 'undefined') main.control.src = controlImageSrc
main.appendChild(main.control)
// Now make the pivot slice element.
if (typeof pivotSliceStyle != 'undefined') {
main.pivotSlice = document.createElement('div'); main.pivotSlice.className = pivotSliceClass || ''; addStyle(main.pivotSlice, pivotSliceStyle || '')
main.appendChild(main.pivotSlice)
}
main.update = function () {update(main)}
main.update()
var startscroll = false, startxy = 0, offsetxy = 0
if (useMouseEvents) {
main.addEventListener ('mousedown', mousedown)
main.addEventListener ('mouseover', mousemove)
main.addEventListener ('mousemove', mousemove)
document.addEventListener ('mouseup' , mouseupOrBlur)
window.addEventListener ('blur' , mouseupOrBlur)
window.addEventListener ('mouseout' , mouseout)
}
if (useTouchEvents) {
main.addEventListener ('touchstart' , touchstart)
main.addEventListener ('touchmove' , mousemove)
document.addEventListener ('touchend' , mouseupOrBlur)
window.addEventListener ('touchcancel', mouseupOrBlur)
window.addEventListener ('touchleave' , mouseupOrBlur)
}
if (main.textbox) {
document.addEventListener ((!useTouchEvents) ? 'mousedown' : 'touchstart', textboxBlur)
textboxNumber.addEventListener ((!useTouchEvents) ? 'click' : 'touchend', textboxFocus)
textboxNumber.addEventListener ('input' , textboxChange)
textboxNumber.addEventListener ('keypress', textboxKeypress)
}
main.updatePosition = function (pxc, zoomLevel) {
if (typeof pxc == 'undefined') pxc = pxToCssUnitType()
if (typeof zoomLevel == 'undefined') zoomLevel = calculateZoomLevel()
startxy = findabsposZoom(main) / (pxc * zoomLevel)
}
main.setPosition = function (newPosition, triggerUpdateEvent, pxc, evt) {
triggerUpdateEvent = triggerUpdateEvent === undefined ? true : triggerUpdateEvent
return setPosition(newPosition, triggerUpdateEvent, pxc, evt)
}
main.setPositionPercent = function (newPointValue, triggerUpdateEvent, evt) {
triggerUpdateEvent = triggerUpdateEvent === undefined ? true : triggerUpdateEvent
var newPosition = main.positionPhysicalMax * newPointValue / main.pointUpperLimit
main.setPosition(newPosition, triggerUpdateEvent, pxToCssUnitType(), evt)
}
main.getPositionPercent = function () {return (main.position / main.positionPhysicalMax * main.pointUpperLimit)}
main.destroy = function () {return destroy(main, useMouseEvents, useTouchEvents)}
//If MutationObserver is defined, call main.destroy when the object is removed from a parent element.
var MutationObserver = window.MutationObserver || window.WebkitMutationObserver
if (typeof MutationObserver != 'undefined') {
var observer = new MutationObserver(function (mutationList) {
for (var i = 0, curlenI = mutationList.length; i < curlenI; i++) {
var mutationItem = mutationList[i]
if (mutationItem.type != 'childList') return
for (var j = 0, curlenJ = mutationItem.removedNodes.length; j < curlenJ; j++) {
if (mutationItem.removedNodes[j] != main) continue
main.destroy(); observer.disconnect(); return
}
}
})
observer.observe(parent, {attributes: false, childList: true, subtree: false})
}
return main
function pxToCssUnitType () {
if (main.cssUnitType == 'px') return 1
if ((!main.recalculateSize) && (typeof pxToCssUnitTypeFixed != 'undefined')) return pxToCssUnitTypeFixed
var mydiv = document.createElement('div'); mydiv.style.visibility = 'hidden'; mydiv.style.width = '1' + main.cssUnitType
parent.appendChild(mydiv); var w = mydiv.getBoundingClientRect().width; parent.removeChild(mydiv)
if (!main.recalculateSize) pxToCssUnitTypeFixed = w
return w
}
function calculatePhysicalMax (pxc, zoomLevel) {
if (typeof zoomLevel == 'undefined') zoomLevel = calculateZoomLevel()
if (typeof pxc == 'undefined') pxc = pxToCssUnitType()
var style = window.getComputedStyle(main)
var boxSizing = (style.boxSizing != '') ? style.boxSizing : style.mozBoxSizing
if (boxSizing == 'border-box') {
var borderAndPaddingAdjustment = 0
} else {
var borderAndPaddingAdjustment = (orientation == 'horizontal'
? parseFloat(window.getComputedStyle(main).borderLeftWidth) + parseFloat(window.getComputedStyle(main).borderRightWidth)
: parseFloat(window.getComputedStyle(main).borderTopWidth) + parseFloat(window.getComputedStyle(main).borderBottomWidth)
)
}
borderAndPaddingAdjustment += (orientation == 'horizontal'
? parseFloat(window.getComputedStyle(main).paddingLeft) + parseFloat(window.getComputedStyle(main).paddingRight)
: parseFloat(window.getComputedStyle(main).paddingTop) + parseFloat(window.getComputedStyle(main).paddingBottom)
)
if (isNaN(borderAndPaddingAdjustment)) borderAndPaddingAdjustment = 0
var isNotBorderBox = (boxSizing != 'border-box') ? 1 : 0
return (main.getBoundingClientRect()[widthHeight] - main.control.getBoundingClientRect()[widthHeight]) / (pxc * zoomLevel) - borderAndPaddingAdjustment / pxc - main.controlUnitOffset * 2
}
function calculateZoomLevel () {
if ((!main.recalculateSize) && (typeof zoomLevelFixed != 'undefined')) return zoomLevelFixed
var zoomLevel = getInheritedTransform(main, {type: 'scale', xy: orientationXY})
if (!main.recalculateSize) zoomLevelFixed = zoomLevel
return zoomLevel
}
function textboxChange (evt) {
var curvalue = parseFloat(textboxNumber.value)
if ((isNaN(curvalue)) || (curvalue < 0)) curvalue = 0
if (curvalue > main.pointUpperLimit) curvalue = main.pointUpperLimit
main.setPositionByPointValue(curvalue)
}
function textboxKeypress (evt) {var keyCode = evt.keyCode; if (keyCode == 13) textboxNumber.blur()}
function textboxUpdateValue (pxc) {
if (typeof pxc == 'undefined') pxc = pxToCssUnitType()
textboxNumber.value = Math.round((main.pointUpperLimit * main.position) / calculatePhysicalMax(pxc))
}
function textboxBlur (evt) {
if (main.textboxEnabled == false) return
if (evt.currentTarget == textboxNumber) return
textboxNumber.blur()
}
function textboxFocus (evt) {
if (main.textboxEnabled == true) return
if (evt.currentTarget == textboxNumber) return
textboxNumber.focus()
}
function updateForegroundWidthHeight () {
main.foreground.style[widthHeight] = (((main.position + main.controlUnitOffset) >= 0) ? main.position : 0) + main.cssUnitType
}
function mousedown (evt) {
evt.preventDefault()
if (evt.target != evt.currentTarget) return
if (h.getRightClick(evt) || (main.startCondition && !main.startCondition(main)) || startscroll == true) return
var pxc = pxToCssUnitType()
var zoomLevel = calculateZoomLevel()
main.updatePosition(pxc, zoomLevel)
offsetxy = (main.control.getBoundingClientRect()[widthHeight] / 2) / (zoomLevel * pxc)
startscroll = true
mousemove(evt, pxc, zoomLevel)
}
function touchstart (evt) {mousemove(evt); mousedown(evt)}
function mousemove (evt, pxc, zoomLevel) {
if (evt.currentTarget == main) evt.preventDefault()
if (startscroll == false) return
if (typeof pxc == 'undefined') pxc = pxToCssUnitType()
if (typeof zoomLevel == 'undefined') zoomLevel = calculateZoomLevel()
var xy = (evt.changedTouches ? evt.changedTouches[0] : evt)[pageXY] / (zoomLevel * pxc)
var newPosition = xy - offsetxy - startxy - main.controlUnitOffset
main.setPosition(newPosition, true, pxc, evt)
}
function mouseout (evt) {
evt.preventDefault()
var coords = (evt.changedTouches ? evt.changedTouches[0] : evt)
var mouseX = coords.pageX, mouseY = coords.pageY
var windowScrollX = (typeof window.scrollX != 'undefined') ? window.scrollX : document.body.scrollLeft
var windowScrollY = (typeof window.scrollY != 'undefined') ? window.scrollY : document.body.scrollTop
if (((mouseY >= 0)) && ((mouseY <= window.innerHeight + windowScrollY)) && ((mouseX >= 0) && mouseX <= (window.innerWidth + windowScrollX))) return
mouseupOrBlur(evt)
}
function mouseupOrBlur (evt) {
if (startscroll == false) return
if (evt.target != main) {
main.events.update(main, evt)
} else {
mousemove(evt, pxToCssUnitType(), calculateZoomLevel())
}
startscroll = false
}
// Calculate the physical control position max,
// calculate the logical control position max,
// and set the initial physical control position.
function update (main) {
var pxc = pxToCssUnitType()
var zoomLevel = calculateZoomLevel()
main.positionPhysicalMax = calculatePhysicalMax(pxc, zoomLevel)
if (main.pointUpperLimit != 0) {
main.positionLogicalMax = main.positionPhysicalMax * (main.pointMaximum / main.pointUpperLimit)
}
if (typeof main.position != 'undefined' && main.positionLogicalMax != 0) {
var logicalPosition = main.position / main.positionLogicalMax
}
if (typeof main.position == 'undefined') {
if (main.pointUpperLimit != 0) {
main.position = main.positionPhysicalMax * (pointInitial / main.pointUpperLimit)
}
} else {
if (typeof main.positionLogicalMax != 'undefined' && typeof logicalPosition != 'undefined') {
main.position = logicalPosition * main.positionLogicalMax
}
}
// Set the control left/top position and the foreground width/height.
main.control.style[leftTop] = (main.position + main.controlUnitOffset) + main.cssUnitType
updateForegroundWidthHeight()
if ((typeof backgroundBeyondMax != 'undefined') || (typeof backgroundBeyondMaxClass != 'undefined')) {
main.backgroundBeyondMax.style[widthHeight] = (main.positionLogicalMax - main.positionLogicalMax) + main.cssUnitType
main.backgroundBeyondMax.style[leftTop] = main.positionLogicalMax + main.cssUnitType
}
if (typeof pivotSliceStyle != 'undefined') {
main.pivotStart = main.positionPhysicalMax * (main.pivotPoint / main.pointUpperLimit)
main.pivotEnd = main.position
main.pivotSlice.style[widthHeight] = Math.abs(main.pivotEnd - main.pivotStart) + main.cssUnitType
main.pivotSlice.style[leftTop] = ((main.pivotEnd > main.pivotStart) ? main.pivotStart : main.pivotEnd) + main.cssUnitType
}
if (main.textbox) textboxUpdateValue(pxc)
if (main.events.update) main.events.update(main)
}
function setPosition (newPosition, triggerUpdateEvent, pxc, evt) {
main.position = newPosition
main.positionLogicalMax = main.positionPhysicalMax * (main.pointMaximum / main.pointUpperLimit)
if (main.position > main.positionLogicalMax) {
main.position = main.positionLogicalMax
} else {
if (main.position < 0) main.position = 0
}
main.control.style[leftTop] = (main.position + main.controlUnitOffset) + main.cssUnitType
updateForegroundWidthHeight()
if (typeof pivotSliceStyle != 'undefined') {
main.pivotStart = main.positionPhysicalMax * (main.pivotPoint / main.pointUpperLimit)
main.pivotEnd = main.position
updateForegroundWidthHeight()
}
if (main.textboxEnabled == true) textboxUpdateValue(pxc)
if (triggerUpdateEvent && main.events.update) main.events.update(main, evt)
}
function destroy (main, useMouseEvents, useTouchEvents) {
if (useMouseEvents) {
main.removeEventListener ('mousedown', mousedown)
main.removeEventListener ('mouseover', mousemove)
main.removeEventListener ('mousemove', mousemove)
document.removeEventListener ('mouseup' , mouseupOrBlur)
window.removeEventListener ('blur' , mouseupOrBlur)
window.removeEventListener ('mouseout' , mouseout)
}
if (useTouchEvents) {
main.removeEventListener ('touchstart' , touchstart)
main.removeEventListener ('touchmove' , mousemove)
document.removeEventListener ('touchend' , mouseupOrBlur)
window.removeEventListener ('touchcancel', mouseupOrBlur)
window.removeEventListener ('touchleave' , mouseupOrBlur)
}
if (main.textbox) {
document.removeEventListener ((!useTouchEvents) ? 'mousedown' : 'touchstart', textboxBlur)
textboxNumber.removeEventListener ((!useTouchEvents) ? 'click' : 'touchend', textboxFocus)
textboxNumber.removeEventListener ('input' , textboxChange)
textboxNumber.removeEventListener ('keypress', textboxKeypress)
}
var currentParent = main.parentNode; if (currentParent != null) currentParent.removeChild(main)
}
}
}
// </DOM-widget functions.>
// <DOM manipulation functions. TAG: px, TAG: CSS, TAG: DOM, TAG: dom, TAG: style.>
if (h.library_settings.dom_manipulation) {
// <Extra DOM things -- including a duplicated isAttached. Should we rework all dom-specific functions to this model?>
// 'dom.create', 'dom.createEventSubscriber', and 'dom.databaseSyncSetter' are especially useful.
var dom = h.dom = {}
dom.style = {
set: function (element, propList) {for (var prop in propList) {element.style[prop] = propList[prop]}}
}
dom.enhanceNative = function (nativeToEnhanceList) {
var enhanceList = {
'addEventListener' : function () { // Allows an array of strings in addEventListener. Very non-standard!!!
var addEventListenerNative = HTMLElement.prototype.addEventListener
HTMLElement.prototype.addEventListener = function () {
var args = Array.prototype.slice.call(arguments)
if (typeof args[0] == 'string') {addEventListenerNative.apply(this, args); return}
if (Array.isArray(args[0])) {
var args0 = args[0]
args0.forEach(function (eventName) {args[0] = eventName; addEventListenerNative.apply(this, args)}, this)
}
}
}
}
if (nativeToEnhanceList == 'all') {enhanceList.forEach(function (func) {func()}); return}
if (!Array.isArray(nativeToEnhanceList)) nativeToEnhanceList = [nativeToEnhanceList];
nativeToEnhanceList.forEach(function (nativeToEnhance) {
if (nativeToEnhance in enhanceList) enhanceList[nativeToEnhance]()
})
}
dom.removeEventListeners = function (element) {
element.dom.eventListenerArgumentList.forEach(function (eventListenerArgs) {
element.removeEventListener.apply(element, eventListenerArgs)
})
element.dom.eventListenerArgumentList = []
}
dom.enableEventListenerTracking = function (element) {
element.addEventListenerOriginal = element.addEventListener
element.dom.eventListenerArgumentList = []
element.addEventListener = function () {
var args = Array.prototype.slice.call(arguments)
element.dom.eventListenerArgumentList.push(args)
element.addEventListenerOriginal.apply(element, args)
}
this.removeEventListenerOriginal = element.removeEventListener
this.removeEventListener = function () {
var args = Array.prototype.slice.call(arguments)
element.dom.eventListenerArgumentList.some(function (eventListenerArgs) {
if (eventListenerArgs.length != args.length) return
return args.every(function (param, i) {return (param === eventListenerArgs[i])})
})
element.removeEventListenerOriginal.apply(element, args)
}
}
dom.setWidthLikeDiv = function (input) {dom.matchElementDimensions(input, 'width', 'inline-block', 'left', 'right', 'div')}
dom.setHeightLikeDiv = function (input) {dom.matchElementDimensions(input, 'height', 'block', 'top', 'bottom', 'div')}
dom.matchElementDimensions = function (input, dimension, tempDisplayType, minEdge, maxEdge, temporaryElementType) {
function capitalize (obj) {return obj.toLowerCase().replace(/^[a-z]|\s[a-z]/g, conv); function conv () {return arguments[0].toUpperCase()}}
var dimensionC = capitalize(dimension), minEdgeC = capitalize(minEdge), maxEdgeC = capitalize(maxEdge)
input.style[dimension] = ''
var s = window.getComputedStyle(input); var cousin = document.createElement(temporaryElementType)
cousin.style.display = tempDisplayType
new Array('paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom', 'fontFamily', 'fontSize', 'fontWeight', 'letterSpacing', 'fontKerning', 'lineHeight', 'textIndent').forEach(function (p) {cousin.style[p] = s[p]})
// 1) Converts end-of-line to '<br/> '.
// 2) Any empty strings become 'nbsp;'.
// 3) Converts any trailing spaces into sets of ' '.
cousin.innerHTML = input.value.replace(/[\n\r]/g, '<br/> ').replace(/^$/, ' ').replace(/ +$/, function(count) {return ' '.repeat(count.length)})
input.parentNode.insertBefore(cousin, input)
input.style[dimension] = (
cousin['client' + dimensionC] +
parseFloat(s['border' + minEdgeC + 'Width']) + parseFloat(s['border' + minEdgeC + 'Width']) +
parseFloat(s['margin' + minEdgeC]) + parseFloat(s['margin' + minEdgeC])
) + 'px'
cousin.parentNode.removeChild(cousin)
}
dom.isAttached = function (obj) {
while (true) {
obj = obj.parentNode
if (obj == document.documentElement) return true
if (obj == null) return false
}
}
dom.getNodeIndex = function (element) {var index = -1; do {element = element.previousSibling; index++} while (element != null); return index}
dom.appendChildren = function (init) {var parent = init.parent, children = init.children; children.forEach(function (child) {parent.appendChild(child)})}
dom.detachAll = function (parent, recursive) {
Array.prototype.slice.call(parent.childNodes).forEach(function (child) {
parent.removeChild(child); if (recursive) dom.detachAll(child, recursive)
})
}
dom.detachAllOtherSiblings = function (exceptedChildren, recursive) {
if (!Array.isArray(exceptedChildren)) exceptedChildren = [exceptedChildren]
var parent = exceptedChildren[0].parentNode
Array.prototype.slice.call(parent.childNodes).forEach(function (child) {
if (exceptedChildren.find(function (testchild) {return testchild == child})) return
parent.removeChild(child); if (recursive) dom.detachAll(child, recursive)
})
}
dom.detachAllRecursive = function (parent) {return dom.detachAll(parent, true)}
dom.detachAllOtherSiblingsRecursive = function (exceptedChildren, parent) {return dom.detachAllOtherSiblings(exceptedChildren, parent, true)}
dom.onRemoved = function (element, callback) {
var MutationObserver = window.MutationObserver || window.WebkitMutationObserver
var observer = new MutationObserver(function () {
if (!isAttached(element)) {callback(element); observer.disconnect()}
function isAttached (element) {
while (true) {
element = element.parentNode
if (element == document.documentElement) return true
if (element == null) return false
}
}
})
observer.observe(document, {childList: true, subtree: true})
return observer
}