-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
1733 lines (1405 loc) · 54.9 KB
/
Utils.js
File metadata and controls
1733 lines (1405 loc) · 54.9 KB
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
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** Utils
//*****************************************************************************/
/**
* Common functions and utilities used by the webcontrols
*
******************************************************************************/
javaxt.dhtml.utils = {
//**************************************************************************
//** get
//**************************************************************************
/** Used to execute an HTTP GET request. Example:
<pre>
get(url + "?filter=" + encodeURIComponent(filter), {
success: function(text){
var arr = JSON.parse(text).records;
},
failure: function(request){
alert(request.status);
}
});
</pre>
* @param url Required.
* @param config Optional. Common config settings include a "success"
* callback function, "failure" callback function, and "payload". Note that
* if a payload is given, an HTTP POST request will be executed. See the
* http() method for a full range of options.
*/
get: function(url, config){
if (!config) config = {};
if (config.payload!=null){ //convert to post request
var payload = config.payload;
delete config.payload;
return javaxt.dhtml.utils.post(url, payload, config);
}
var settings = {
method: "GET",
payload: null
};
javaxt.dhtml.utils.merge(settings, config);
return javaxt.dhtml.utils.http(url, settings);
},
//**************************************************************************
//** post
//**************************************************************************
/** Used to execute an HTTP POST request.
*/
post: function(url, payload, config){
var settings;
if (payload.payload){
settings = payload;
settings.method = "POST";
}
else{
var settings = {
method: "POST",
payload: payload
};
javaxt.dhtml.utils.merge(settings, config);
}
return javaxt.dhtml.utils.http(url, settings);
},
//**************************************************************************
//** delete
//**************************************************************************
/** Used to execute an HTTP DELETE request.
*/
delete: function(url, config){
if (!config) config = {};
config.method = "DELETE";
return javaxt.dhtml.utils.http(url, config);
},
//**************************************************************************
//** http
//**************************************************************************
/** Used to execute an HTTP request.
*/
http: function(url, config){
if (!config) config = {
method: "GET"
};
var cache = false; //no caching by default!
if (config.cache){
if (config.cache==true) cache = true;
}
if (!cache){
if (url.indexOf("?")==-1) url += "?";
url += "&_=" + new Date().getTime();
}
var method = config.method;
var success = config.success;
var scope = config.scope;
var async = true;
if (config.async){
if (config.async!=false) async = true;
}
var failure = config.failure;
if (typeof failure === "undefined") failure = function(request){
if (request.status!==0){
alert(request);
}
};
var request = new XMLHttpRequest();
if (config.username && config.password){
request.open(method, url, async, config.username, config.password);
request.setRequestHeader("Authorization", "Basic " + btoa(config.username + ":" + config.password)); //<-- Needed to add this sometime in mid 2018...
}
else{
request.open(method, url, async);
}
if (!cache) request.setRequestHeader("Cache-Control", "no-cache, no-transform");
if (config.contentType){ //Example: 'application/x-www-form-urlencoded'
request.setRequestHeader("Content-Type", config.contentType);
}
request.onreadystatechange = function(){
if (request.readyState === 4) {
if (request.status>=200 && request.status<300){
if (success) success.apply(scope, [request.responseText, request.responseXML, request.responseURL, request]);
}
else{
if (failure) failure.apply(scope, [request]);
}
if (config.finally) config.finally.apply(scope, [request]);
}
};
if (config.payload){
var payload = config.payload;
//Stringify the payload as needed
if (javaxt.dhtml.utils.isArray(payload)){
payload = JSON.stringify(payload);
}
else{
if (payload != null && typeof payload == 'object' && !(payload instanceof FormData)){
payload = JSON.stringify(payload);
}
}
request.send(payload);
}
else request.send();
return request;
},
//**************************************************************************
//** getParameter
//**************************************************************************
/** Returns the value of a given parameter name in a URL querystring
* @param name Parameter name
* @param url URL (e.g. window.location.href)
*/
getParameter: function(name, url){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
if (!url) url = window.location.href;
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec(url);
if (results == null) return "";
else return results[1];
},
//**************************************************************************
//** merge
//**************************************************************************
/** Used to merge properties from one json object into another. Credit:
* https://github.com/stevenleadbeater/JSONT/blob/master/JSONT.js
*/
merge: function(settings, defaults) {
var merge = function(settings, defaults) {
if (settings==null) return;
//Check if the settings is an array. Do not merge arrays!
if (javaxt.dhtml.utils.isArray(settings)){
return;
}
for (var p in defaults) {
if (defaults.hasOwnProperty(p) && typeof settings[p] !== "undefined") {
if (p!=0) //<--Added this as a bug fix
merge(settings[p], defaults[p]);
}
else {
settings[p] = defaults[p];
}
}
};
merge(settings, defaults);
return settings;
},
//**************************************************************************
//** clone
//**************************************************************************
/** Used to clone a json object
*/
clone: function(obj){
return javaxt.dhtml.utils.merge({}, obj);
},
//**************************************************************************
//** isDirty
//**************************************************************************
/** Returns true if the given json object differs from the original.
*/
isDirty: function(obj, org){
var isEmpty = javaxt.dhtml.utils.isEmpty;
var a = isEmpty(obj);
var b = isEmpty(org);
if ((a==true && b==false) || (b==true && a==false)) return true;
var d = javaxt.dhtml.utils.diff(obj, org);
return !isEmpty(d);
},
//**************************************************************************
//** diff
//**************************************************************************
/** Used to compare 2 json objects. Returns a json object with differences.
* Credit: https://stackoverflow.com/a/13389935/
*/
diff: function(obj1, obj2){
var isEmpty = javaxt.dhtml.utils.isEmpty;
var merge = javaxt.dhtml.utils.merge;
var diff = function(obj1, obj2){
var ret = {},rett;
for (var i in obj2) {
rett = {};
if (typeof obj2[i] === 'object'){
if (obj1.hasOwnProperty(i)){
rett = diff(obj1[i], obj2[i]);
if (!isEmpty(rett) ){
ret[i]= rett;
}
}
else{
ret[i] = obj2[i];
}
}
else{
if (!obj1 || !obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
ret[i] = obj2[i];
}
}
}
return ret;
};
var d1 = diff(obj1, obj2);
var d2 = diff(obj2, obj1);
return merge(d1,d2);
},
//**************************************************************************
//** isEmpty
//**************************************************************************
/** Returns true if the given json object has no key/value pairs.
*/
isEmpty: function(obj){
return JSON.stringify(obj) === "{}";
},
//**************************************************************************
//** isArray
//**************************************************************************
/** Used to check whether a given object is an array. Note that this check
* does not use the "instanceof Array" approach because of issues with
* frames.
*/
isArray: function(obj){
return (Object.prototype.toString.call(obj)==='[object Array]');
},
//**************************************************************************
//** isString
//**************************************************************************
/** Return true if a given object is a string.
*/
isString: function(obj){
return (typeof obj === "string"); // || obj instanceof String)
},
//**************************************************************************
//** isNumber
//**************************************************************************
/** Return true if a given object is a number or can be parsed into a number.
*/
isNumber: function(n) {
if (typeof n === "number") return true;
if (typeof n !== "string") n = ""+n;
return !isNaN(parseFloat(n)) && !isNaN(n - 0);
},
//**************************************************************************
//** isDate
//**************************************************************************
/** Return true if a given object can be parsed into a date. Returns false
* if the object is a number (e.g. "3", "1.2")
*/
isDate: function(d) {
//Don't pass numbers to Date.parse
if (typeof d === "string" || typeof d === "number"){
var n = (d+"").replace(/[^-+0-9,.]+/g,"");
if (d===n){
return false;
}
}
return !isNaN(Date.parse(d));
},
//**************************************************************************
//** isElement
//**************************************************************************
/** Return true if a given object is a DOM element.
*/
isElement: function(obj){
var b = (obj instanceof Element); //should work with 99% of the time
//Special case for Firefox for DOM elements created in an iFrame (e.g.
//JavaXT themes demo)
if (!b && (navigator.userAgent.indexOf("Firefox") > -1)){
//If the object resembles a node, maybe it is a node? Not foolproof
//but better than nothing...
b = (typeof obj == "object" &&
"nodeType" in obj &&
obj.nodeType === 1 &&
obj.cloneNode);
}
return b;
},
//**************************************************************************
//** setStyle
//**************************************************************************
/** Used to set the style for a given element, replacing whatever style was
* there before.
* @param el DOM element.
* @param style If a string is provided, assumes that the string represents
* a CSS class name update "class" attribute of given element. If a JSON
* object is provided, will assign the key/value pairs to the "style"
* attribute of the node.
*/
setStyle: function(el, style){
if (el===null || el===0) return;
if (style===null) return;
//Special case for iScroll
if (typeof IScroll !== 'undefined'){
if (el instanceof IScroll){
var indicators = el.indicators;
if (indicators){
var indicatorClass = "iScrollIndicator";
if (style.indicator) indicatorClass = style.indicator;
for (var i=0; i<indicators.length; i++){
var indicator = indicators[i].indicator;
indicator.className = indicatorClass;
var scrollbar = indicator.parentNode;
if (scrollbar.className.indexOf("iScrollVerticalScrollbar")){
if (style.verticalScrollbar){
scrollbar.className = scrollbar.className.replace("iScrollVerticalScrollbar", style.verticalScrollbar);
}
}
else{
if (style.horizontalScrollbar){
scrollbar.className = scrollbar.className.replace("iScrollHorizontalScrollbar", style.horizontalScrollbar);
}
}
}
}
return;
}
}
//el.style = '';
el.removeAttribute("style");
if (javaxt.dhtml.utils.isString(style)){
el.className = style;
}
else{
for (var key in style){
if (style.hasOwnProperty(key)){
var val = style[key];
if (key==="content"){
el.innerHTML = val;
}
else{
el.style[key] = val;
}
}
}
}
},
//**************************************************************************
//** addStyle
//**************************************************************************
/** Used to add style to a given element.
* @param el DOM element.
* @param style If a string is provided, assumes that the string represents
* a CSS class name update "class" attribute of given element. If a JSON
* object is provided, will assign the key/value pairs to the "style"
* attribute of the node.
*/
addStyle: function(el, style){
if (el===null || el===0) return;
if (style===null) return;
if (javaxt.dhtml.utils.isString(style)){
style = style.replace(/^\s*/, "").replace(/\s*$/, "");
if (el.hasAttribute("class")){
var arr = el.className.split(" ");
for (var i=0; i<arr.length; i++){
var className = arr[i].replace(/^\s*/, "").replace(/\s*$/, "");
if (className===style) return;
}
el.className += " " + style;
}
else{
el.className = style;
}
}
else{
for (var key in style){
var val = style[key];
el.style[key] = val;
if (key==="transform"){
el.style["-webkit-" +key] = val;
}
}
}
},
//**************************************************************************
//** hasStyleRule
//**************************************************************************
/** Returns true if there is a style rule defined for a given selector.
* @param selector CSS selector (e.g. ".deleteIcon", "h2", "#mid")
*/
hasStyleRule: function(selector) {
var hasRule = function(selector, rules){
if (!rules) return false;
for (var i=0; i<rules.length; i++) {
var rule = rules[i];
if (rule.selectorText){
var arr = rule.selectorText.split(',');
for (var j=0; j<arr.length; j++){
if (arr[j].indexOf(selector) !== -1){
var txt = trim(arr[j]);
if (txt===selector){
return true;
}
else{
var colIdx = txt.indexOf(":");
if (colIdx !== -1){
txt = trim(txt.substring(0, colIdx));
if (txt===selector){
return true;
}
}
}
}
}
}
}
return false;
};
var trim = function(str){
return str.replace(/^\s*/, "").replace(/\s*$/, "");
};
for (var i=0; i<document.styleSheets.length; i++){
var rules;
try{
rules = document.styleSheets[i].rules || document.styleSheets[i].cssRules;
if (hasRule(selector, rules)){
return true;
}
}
catch(e){
//Security error, typically occurs when running on the local file system (vs web server)
}
var imports = document.styleSheets[i].imports;
if (imports){
for (var j=0; j<imports.length; j++){
rules = imports[j].rules || imports[j].cssRules;
if (hasRule(selector, rules)) return true;
}
}
}
return false;
},
//**************************************************************************
//** addNoSelectRule
//**************************************************************************
/** Inserts the "javaxt-noselect" class into the document if it is not
* present.
*/
addNoSelectRule: function(){
var hasStyleRule = javaxt.dhtml.utils.hasStyleRule;
if (!hasStyleRule(".javaxt-noselect")){
var head = document.head || document.getElementsByTagName('head')[0];
var sheet = document.createElement('style');
sheet.innerHTML = ".javaxt-noselect {\n";
var arr = ["-webkit-","-moz-","-o-","-ms-","-khtml-",""];
for (var i=0; i<arr.length; i++){
sheet.innerHTML += arr[i] + "user-select: none;\n";
}
sheet.innerHTML += "}";
head.appendChild(sheet);
}
},
//**************************************************************************
//** createElement
//**************************************************************************
/** Used to create a DOM element
* @param type Node type (string). Example "div". This field is required.
* @param obj Optional. If a DOM element is provided, will append the newly
* created node into the element. Otherwise, will assume that the object is
* a style.
* @param style Optional. If a string is provided, assumes that the string
* represents a CSS class name update "class" attribute of the newly created
* node. If a JSON object is provided, will assign the key/value pairs to
* the "style" attribute.
*/
createElement: function(type, obj, style){
var setStyle = javaxt.dhtml.utils.setStyle;
var el = document.createElement(type);
if (obj){
if (javaxt.dhtml.utils.isElement(obj)){
setStyle(el, style);
obj.appendChild(el);
}
else{
//Shift args (2nd arg might be a style)
if (!style) style = obj;
setStyle(el, style);
}
}
else{
setStyle(el, style);
}
return el;
},
//**************************************************************************
//** createTable
//**************************************************************************
/** Used to create a table element.
* @param parent DOM element to append to (optional)
* @returns Table element (DOM object) with custom methods
*/
createTable: function(parent){
var createElement = javaxt.dhtml.utils.createElement;
var table = createElement('table', parent, {
width: "100%",
height: "100%",
margin: 0,
padding: 0,
borderCollapse: "collapse"
});
table.cellSpacing = 0;
table.cellPadding = 0;
var tbody = createElement('tbody', table);
table.addRow = function(style){
var tr = createElement("tr", tbody, style);
tr.addColumn = function(style){
return createElement("td", tr, style);
};
return tr;
};
table.removeRow = function(tr){
if (!tr) return;
tbody.removeChild(tr);
};
table.getRows = function(){
return tbody.childNodes;
};
table.clear = function(){
tbody.innerHTML = "";
};
if (parent) parent.appendChild(table);
return table;
},
//**************************************************************************
//** createClipboard
//**************************************************************************
/** Used to create a hidden clipboard in a given parent. Text and other data
* can be inserted into the clipboard via the insert() method on the DOM
* object returned by this method. Once data is inserted into the clipboard,
* clients can retrieve the data via the browser "paste" event (e.g. ctrl+v
* on windows).
* @param parent DOM element used to hold the clipboard (required)
* @returns DOM object (dov) with a custom insert() method
*/
createClipboard: function(parent){
var createElement = javaxt.dhtml.utils.createElement;
var clipboard = createElement("textarea");
clipboard.insert = function(str){
clipboard.value = str;
clipboard.select();
clipboard.setSelectionRange(0, 99999);
document.execCommand('copy');
};
var clipboardDiv = createElement("div", parent, {
position: "absolute",
left: "-9999px",
width: "0px",
height: "0px"
});
clipboardDiv.appendChild(clipboard);
return clipboard;
},
//**************************************************************************
//** getSuggestedColumnWidths
//**************************************************************************
/** Used to analyze a given dataset and suggest column widths for a table or
* a datagrid
* @param records A two-dimensional array representing rows and columns
* @param pixelsPerChar Approximate, average width of a character
* @param maxWidth Optional. The available width for the table/grid control
* @returns JSON object with various stats and suggestedWidths
*/
getSuggestedColumnWidths: function(records, pixelsPerChar, maxWidth){
var widths = [];
var zscores = [];
var totalWidth = 0;
var headerWidth = 0;
var suggestedWidths = [];
var columns = records[0];
if (columns.length>1){
for (var i=0; i<columns.length; i++){
var len = 0;
var column = columns[i];
if (column!=null) len = (column+"").length;
widths.push(len);
headerWidth+=len*pixelsPerChar;
}
for (var i=0; i<records.length; i++){
var record = records[i];
for (var j=0; j<record.length; j++){
var rec = record[j];
var len = 0;
if (rec!=null){
var str = rec+"";
var r = str.indexOf("\r");
var n = str.indexOf("\n");
if (r==-1){
if (n>-1) str = str.substring(0, n);
}
else{
if (n>-1){
str = str.substring(0, Math.min(r,n));
}
else str = str.substring(0, r);
}
len = str.length*pixelsPerChar;
}
widths[j] = Math.max(widths[j], len);
}
}
//Get total width
for (var i=0; i<widths.length; i++){
totalWidth += widths[i];
}
//Check if any columns are super wide (or very narrow) using z-scores
var outliers = [];
zscores = javaxt.dhtml.utils.getZScores(widths, true);
for (var i=0; i<zscores.length; i++){
if (zscores[i]>1) outliers.push(i);
}
//Compute suggestedWidths. If we have one column that's really wide,
//we'll make it 100% and use pixels for the other columns. Otherwise,
//we'll use percentages for everything
var usePercentages = true;
if (outliers.length===1){
var outlier = outliers[0];
var outlierWidth = widths[outlier];
//Check if the outlier is really wider than all the other fields
var updateOutlier = true;
for (var i=0; i<widths.length; i++){
if (i===outlier) continue;
if (widths[i]>outlierWidth){
updateOutlier = false;
break;
}
}
//Check if the outlier should be converted to 100% width
if (updateOutlier){
maxWidth = parseFloat(maxWidth+"");
if (!isNaN(maxWidth)){
if (totalWidth-outlierWidth>maxWidth){
//If all the other columns add up to more than the
//available area, don't use 100%
updateOutlier = false;
}
else{
//If the column were to be set to 100%, compute the
//max width that would be allotted. If the computed
//width is less than one of the other columns, don't
//use 100%
var r = maxWidth-(totalWidth-outlierWidth);
for (var i=0; i<widths.length; i++){
if (i===outlier) continue;
if (widths[i]>r){
updateOutlier = false;
break;
}
}
}
}
}
//Set outlier to 100% width and use pixels for the other columns
if (updateOutlier){
usePercentages = false;
for (var i=0; i<widths.length; i++){
var colWidth = i==outlier ? "100%" : widths[i] + "px";
suggestedWidths.push(colWidth);
}
}
}
if (usePercentages) {
//Use percentages for all the fields
for (var i=0; i<widths.length; i++){
var colWidth = ((widths[i]/totalWidth)*100)+"%";
suggestedWidths.push(colWidth);
}
}
}
else{
widths.push(1);
totalWidth = 1;
suggestedWidths.push("100%");
}
return {
widths: widths,
zscores: zscores,
totalWidth: totalWidth,
headerWidth: headerWidth,
suggestedWidths: suggestedWidths
};
},
//**************************************************************************
//** onRender
//**************************************************************************
/** Used to check whether DOM element has been added to the document. Calls
* a callback if it exists or when it is added.
*/
onRender: function(el, callback){
var w = el.offsetWidth;
if (w===0 || isNaN(w)){
var timer;
var checkWidth = function(){
var w = el.offsetWidth;
if (w===0 || isNaN(w)){
timer = setTimeout(checkWidth, 100);
}
else{
clearTimeout(timer);
if (callback) callback.apply(el, [el]);
}
};
timer = setTimeout(checkWidth, 100);
}
else{
if (callback) callback.apply(el, [el]);
}
},
//**************************************************************************
//** updateDOM
//**************************************************************************
/** Used to update the default befaviour of the browser to prevent things
* like right mouse click, scrolling beyond a page, and drag and drop.
*/
updateDOM: function(){
if (document.javaxt) return;
//Disable right-click context menu
document.oncontextmenu = function(e){
return false;
};
//Add logic to prevent touch devices like iPad from scrolling beyond the document
//http://stackoverflow.com/a/26853900
var firstMove;
window.addEventListener('touchstart', function (e) {
firstMove = true;
});
window.addEventListener('touchmove', function (e) {
if (firstMove) {
e.preventDefault();
firstMove = false;
}
});
//Watch for drag and drop events
if (!document.body) document.body = document.getElementsByTagName("body")[0];
var body = document.body;
body.addEventListener('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
return false;
}, false);
body.addEventListener('drop', function(e) {
e.stopPropagation();
e.preventDefault();
return false;
}, false);
document.javaxt = {};
},
//**************************************************************************
//** getRect
//**************************************************************************
/** Returns the geometry of a given element.
*/
getRect: function(el){
if (el.getBoundingClientRect){
return el.getBoundingClientRect();
}
else{
var x = 0;
var y = 0;
var w = el.offsetWidth;
var h = el.offsetHeight;
function isNumber(n){
return n === parseFloat(n);
}
var org = el;
do{
x += el.offsetLeft - el.scrollLeft;
y += el.offsetTop - el.scrollTop;
} while ( el = el.offsetParent );
el = org;
do{
if (isNumber(el.scrollLeft)) x -= el.scrollLeft;
if (isNumber(el.scrollTop)) y -= el.scrollTop;
} while ( el = el.parentNode );
return{
x: x,
y: y,
left: x,
right: x+w,
top: y,
bottom: y+h,
width: w,
height: h
};
}
},
//**************************************************************************
//** intersects
//**************************************************************************
/** Used to test whether two rectangles intersect.
*/