-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUTILS.js
1568 lines (1566 loc) · 60.3 KB
/
UTILS.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
var Utils = (function () {
function Utils() {
}
// if the key was passed before, then doesn't execute the func
Utils.doOnce = function (func, key) {
if (this.doOnceFlags[key]) {
return;
}
func();
this.doOnceFlags[key] = true;
};
// returns true if the event is close to the original event by X pixels either vertically or horizontally.
// we only start dragging after X pixels so this allows us to know if we should start dragging yet.
Utils.areEventsNear = function (e1, e2, pixelCount) {
// by default, we wait 4 pixels before starting the drag
if (pixelCount === 0) {
return false;
}
var diffX = Math.abs(e1.clientX - e2.clientX);
var diffY = Math.abs(e1.clientY - e2.clientY);
return Math.max(diffX, diffY) <= pixelCount;
};
Utils.shallowCompare = function (arr1, arr2) {
// if both are missing, then they are the same
if (this.missing(arr1) && this.missing(arr2)) {
return true;
}
// if one is present, but other is missing, then then are different
if (this.missing(arr1) || this.missing(arr2)) {
return false;
}
if (arr1.length !== arr2.length) {
return false;
}
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
};
Utils.getNameOfClass = function (TheClass) {
var funcNameRegex = /function (.{1,})\(/;
var funcAsString = TheClass.toString();
var results = (funcNameRegex).exec(funcAsString);
return (results && results.length > 1) ? results[1] : "";
};
Utils.values = function (object) {
var result = [];
this.iterateObject(object, function (key, value) {
result.push(value);
});
return result;
};
Utils.getValueUsingField = function (data, field, fieldContainsDots) {
if (!field || !data) {
return;
}
// if no '.', then it's not a deep value
if (!fieldContainsDots) {
return data[field];
}
else {
// otherwise it is a deep value, so need to dig for it
var fields = field.split('.');
var currentObject = data;
for (var i = 0; i < fields.length; i++) {
currentObject = currentObject[fields[i]];
if (this.missing(currentObject)) {
return null;
}
}
return currentObject;
}
};
Utils.getScrollLeft = function (element, rtl) {
var scrollLeft = element.scrollLeft;
if (rtl) {
// Absolute value - for FF that reports RTL scrolls in negative numbers
scrollLeft = Math.abs(scrollLeft);
// Get Chrome and Safari to return the same value as well
if (this.isBrowserSafari() || this.isBrowserChrome()) {
scrollLeft = element.scrollWidth - element.clientWidth - scrollLeft;
}
}
return scrollLeft;
};
Utils.cleanNumber = function (value) {
if (typeof value === 'string') {
value = parseInt(value);
}
if (typeof value === 'number') {
value = Math.floor(value);
}
else {
value = null;
}
return value;
};
Utils.setScrollLeft = function (element, value, rtl) {
if (rtl) {
// Chrome and Safari when doing RTL have the END position of the scroll as zero, not the start
if (this.isBrowserSafari() || this.isBrowserChrome()) {
value = element.scrollWidth - element.clientWidth - value;
}
// Firefox uses negative numbers when doing RTL scrolling
if (this.isBrowserFirefox()) {
value *= -1;
}
}
element.scrollLeft = value;
};
Utils.iterateNamedNodeMap = function (map, callback) {
if (!map) {
return;
}
for (var i = 0; i < map.length; i++) {
var attr = map[i];
callback(attr.name, attr.value);
}
};
Utils.iterateObject = function (object, callback) {
if (this.missing(object)) {
return;
}
if (Array.isArray(object)) {
object.forEach(function (value, index) {
callback(index + '', value);
});
}
else {
var keys = Object.keys(object);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = object[key];
callback(key, value);
}
}
};
Utils.cloneObject = function (object) {
var copy = {};
var keys = Object.keys(object);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = object[key];
copy[key] = value;
}
return copy;
};
Utils.map = function (array, callback) {
var result = [];
for (var i = 0; i < array.length; i++) {
var item = array[i];
var mappedItem = callback(item);
result.push(mappedItem);
}
return result;
};
Utils.mapObject = function (object, callback) {
var result = [];
Utils.iterateObject(object, function (key, value) {
result.push(callback(value));
});
return result;
};
Utils.forEach = function (array, callback) {
if (!array) {
return;
}
for (var i = 0; i < array.length; i++) {
var value = array[i];
callback(value, i);
}
};
Utils.filter = function (array, callback) {
var result = [];
array.forEach(function (item) {
if (callback(item)) {
result.push(item);
}
});
return result;
};
Utils.getAllKeysInObjects = function (objects) {
var allValues = {};
objects.forEach(function (obj) {
if (obj) {
Object.keys(obj).forEach(function (key) { return allValues[key] = null; });
}
});
return Object.keys(allValues);
};
Utils.mergeDeep = function (dest, source) {
if (this.exists(source)) {
this.iterateObject(source, function (key, newValue) {
var oldValue = dest[key];
if (oldValue === newValue) {
return;
}
if (typeof oldValue === 'object' && typeof newValue === 'object') {
Utils.mergeDeep(oldValue, newValue);
}
else {
dest[key] = newValue;
}
});
}
};
Utils.assign = function (object) {
var _this = this;
var sources = [];
for (var _i = 1; _i < arguments.length; _i++) {
sources[_i - 1] = arguments[_i];
}
sources.forEach(function (source) {
if (_this.exists(source)) {
_this.iterateObject(source, function (key, value) {
object[key] = value;
});
}
});
return object;
};
Utils.parseYyyyMmDdToDate = function (yyyyMmDd, separator) {
try {
if (!yyyyMmDd)
return null;
if (yyyyMmDd.indexOf(separator) === -1)
return null;
var fields = yyyyMmDd.split(separator);
if (fields.length != 3)
return null;
return new Date(Number(fields[0]), Number(fields[1]) - 1, Number(fields[2]));
}
catch (e) {
return null;
}
};
Utils.serializeDateToYyyyMmDd = function (date, separator) {
if (!date)
return null;
return date.getFullYear() + separator + Utils.pad(date.getMonth() + 1, 2) + separator + Utils.pad(date.getDate(), 2);
};
Utils.pad = function (num, totalStringSize) {
var asString = num + "";
while (asString.length < totalStringSize)
asString = "0" + asString;
return asString;
};
Utils.pushAll = function (target, source) {
if (this.missing(source) || this.missing(target)) {
return;
}
source.forEach(function (func) { return target.push(func); });
};
Utils.createArrayOfNumbers = function (first, last) {
var result = [];
for (var i = first; i <= last; i++) {
result.push(i);
}
return result;
};
Utils.getFunctionParameters = function (func) {
var fnStr = func.toString().replace(FUNCTION_STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(FUNCTION_ARGUMENT_NAMES);
if (result === null) {
return [];
}
else {
return result;
}
};
Utils.find = function (collection, predicate, value) {
if (collection === null || collection === undefined) {
return null;
}
if (!Array.isArray(collection)) {
var objToArray = this.values(collection);
return this.find(objToArray, predicate, value);
}
var collectionAsArray = collection;
var firstMatchingItem;
for (var i = 0; i < collectionAsArray.length; i++) {
var item = collectionAsArray[i];
if (typeof predicate === 'string') {
if (item[predicate] === value) {
firstMatchingItem = item;
break;
}
}
else {
var callback = predicate;
if (callback(item)) {
firstMatchingItem = item;
break;
}
}
}
return firstMatchingItem;
};
Utils.toStrings = function (array) {
return this.map(array, function (item) {
if (item === undefined || item === null || !item.toString) {
return null;
}
else {
return item.toString();
}
});
};
Utils.iterateArray = function (array, callback) {
for (var index = 0; index < array.length; index++) {
var value = array[index];
callback(value, index);
}
};
//Returns true if it is a DOM node
//taken from: http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object
Utils.isNode = function (o) {
return (typeof Node === "function" ? o instanceof Node :
o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
};
//Returns true if it is a DOM element
//taken from: http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object
Utils.isElement = function (o) {
return (typeof HTMLElement === "function" ? o instanceof HTMLElement :
o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string");
};
Utils.isNodeOrElement = function (o) {
return this.isNode(o) || this.isElement(o);
};
// makes a copy of a node list into a list
Utils.copyNodeList = function (nodeList) {
var childCount = nodeList ? nodeList.length : 0;
var res = [];
for (var i = 0; i < childCount; i++) {
res.push(nodeList[i]);
}
return res;
};
Utils.isEventFromPrintableCharacter = function (event) {
var pressedChar = String.fromCharCode(event.charCode);
// newline is an exception, as it counts as a printable character, but we don't
// want to start editing when it is pressed. without this check, if user is in chrome
// and editing a cell, and they press ctrl+enter, the cell stops editing, and then
// starts editing again with a blank value (two 'key down' events are fired). to
// test this, remove the line below, edit a cell in chrome and hit ctrl+enter while editing.
// https://ag-grid.atlassian.net/browse/AG-605
if (this.isKeyPressed(event, constants_1.Constants.KEY_NEW_LINE)) {
return false;
}
if (exports._.exists(event.key)) {
// modern browser will implement key, so we return if key is length 1, eg if it is 'a' for the
// a key, or '2' for the '2' key. non-printable characters have names, eg 'Enter' or 'Backspace'.
var printableCharacter = event.key.length === 1;
// IE11 & Edge treat the numpad del key differently - with numlock on we get "Del" for key,
// so this addition checks if its IE11/Edge and handles that specific case the same was as all other browers
var numpadDelWithNumlockOnForEdgeOrIe = Utils.isNumpadDelWithNumlockOnForEdgeOrIe(event);
return printableCharacter || numpadDelWithNumlockOnForEdgeOrIe;
}
else {
// otherwise, for older browsers, we test against a list of characters, which doesn't include
// accents for non-English, but don't care much, as most users are on modern browsers
return Utils.PRINTABLE_CHARACTERS.indexOf(pressedChar) >= 0;
}
};
//adds all type of change listeners to an element, intended to be a text field
Utils.addChangeListener = function (element, listener) {
element.addEventListener("changed", listener);
element.addEventListener("paste", listener);
element.addEventListener("input", listener);
// IE doesn't fire changed for special keys (eg delete, backspace), so need to
// listen for this further ones
element.addEventListener("keydown", listener);
element.addEventListener("keyup", listener);
};
//if value is undefined, null or blank, returns null, otherwise returns the value
Utils.makeNull = function (value) {
var valueNoType = value;
if (value === null || value === undefined || valueNoType === "") {
return null;
}
else {
return value;
}
};
Utils.missing = function (value) {
return !this.exists(value);
};
Utils.missingOrEmpty = function (value) {
return this.missing(value) || value.length === 0;
};
Utils.missingOrEmptyObject = function (value) {
return this.missing(value) || Object.keys(value).length === 0;
};
Utils.exists = function (value) {
if (value === null || value === undefined || value === '') {
return false;
}
else {
return true;
}
};
Utils.firstExistingValue = function () {
var values = [];
for (var _i = 0; _i < arguments.length; _i++) {
values[_i] = arguments[_i];
}
for (var i = 0; i < values.length; i++) {
var value = values[i];
if (exports._.exists(value))
return value;
}
return null;
};
Utils.anyExists = function (values) {
if (values) {
for (var i = 0; i < values.length; i++) {
if (this.exists(values[i])) {
return true;
}
}
}
return false;
};
Utils.existsAndNotEmpty = function (value) {
return this.exists(value) && value.length > 0;
};
Utils.removeAllChildren = function (node) {
if (node) {
while (node.hasChildNodes()) {
node.removeChild(node.lastChild);
}
}
};
Utils.removeElement = function (parent, cssSelector) {
this.removeFromParent(parent.querySelector(cssSelector));
};
Utils.removeFromParent = function (node) {
if (node && node.parentNode) {
node.parentNode.removeChild(node);
}
};
Utils.isVisible = function (element) {
return (element.offsetParent !== null);
};
/**
* loads the template and returns it as an element. makes up for no simple way in
* the dom api to load html directly, eg we cannot do this: document.createElement(template)
*/
Utils.loadTemplate = function (template) {
var tempDiv = document.createElement("div");
tempDiv.innerHTML = template;
return tempDiv.firstChild;
};
Utils.appendHtml = function (eContainer, htmlTemplate) {
if (eContainer.lastChild) {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
// we put the items at the start, so new items appear underneath old items,
// so when expanding/collapsing groups, the new rows don't go on top of the
// rows below that are moving our of the way
eContainer.insertAdjacentHTML('afterbegin', htmlTemplate);
}
else {
eContainer.innerHTML = htmlTemplate;
}
};
Utils.addOrRemoveCssClass = function (element, className, addOrRemove) {
if (addOrRemove) {
this.addCssClass(element, className);
}
else {
this.removeCssClass(element, className);
}
};
Utils.callIfPresent = function (func) {
if (func) {
func();
}
};
Utils.addCssClass = function (element, className) {
var _this = this;
if (!className || className.length === 0) {
return;
}
if (className.indexOf(' ') >= 0) {
className.split(' ').forEach(function (value) { return _this.addCssClass(element, value); });
return;
}
if (element.classList) {
if (!element.classList.contains(className)) {
element.classList.add(className);
}
}
else {
if (element.className && element.className.length > 0) {
var cssClasses = element.className.split(' ');
if (cssClasses.indexOf(className) < 0) {
cssClasses.push(className);
element.className = cssClasses.join(' ');
}
}
else {
element.className = className;
}
}
};
Utils.containsClass = function (element, className) {
if (element.classList) {
// for modern browsers
return element.classList.contains(className);
}
else if (element.className) {
// for older browsers, check against the string of class names
// if only one class, can check for exact match
var onlyClass = element.className === className;
// if many classes, check for class name, we have to pad with ' ' to stop other
// class names that are a substring of this class
var contains = element.className.indexOf(' ' + className + ' ') >= 0;
// the padding above then breaks when it's the first or last class names
var startsWithClass = element.className.indexOf(className + ' ') === 0;
var endsWithClass = element.className.lastIndexOf(' ' + className) === (element.className.length - className.length - 1);
return onlyClass || contains || startsWithClass || endsWithClass;
}
else {
// if item is not a node
return false;
}
};
Utils.getElementAttribute = function (element, attributeName) {
if (element.attributes) {
if (element.attributes[attributeName]) {
var attribute = element.attributes[attributeName];
return attribute.value;
}
else {
return null;
}
}
else {
return null;
}
};
Utils.offsetHeight = function (element) {
return element && element.clientHeight ? element.clientHeight : 0;
};
Utils.offsetWidth = function (element) {
return element && element.clientWidth ? element.clientWidth : 0;
};
Utils.sortNumberArray = function (numberArray) {
numberArray.sort(function (a, b) { return a - b; });
};
Utils.removeCssClass = function (element, className) {
if (element.classList) {
if (element.classList.contains(className)) {
element.classList.remove(className);
}
}
else {
if (element.className && element.className.length > 0) {
var cssClasses = element.className.split(' ');
if (cssClasses.indexOf(className) >= 0) {
// remove all instances of the item, not just the first, in case it's in more than once
while (cssClasses.indexOf(className) >= 0) {
cssClasses.splice(cssClasses.indexOf(className), 1);
}
element.className = cssClasses.join(' ');
}
}
}
};
Utils.removeRepeatsFromArray = function (array, object) {
if (!array) {
return;
}
for (var index = array.length - 2; index >= 0; index--) {
var thisOneMatches = array[index] === object;
var nextOneMatches = array[index + 1] === object;
if (thisOneMatches && nextOneMatches) {
array.splice(index + 1, 1);
}
}
};
Utils.removeFromArray = function (array, object) {
if (array.indexOf(object) >= 0) {
array.splice(array.indexOf(object), 1);
}
};
Utils.removeAllFromArray = function (array, toRemove) {
toRemove.forEach(function (item) {
if (array.indexOf(item) >= 0) {
array.splice(array.indexOf(item), 1);
}
});
};
Utils.insertIntoArray = function (array, object, toIndex) {
array.splice(toIndex, 0, object);
};
Utils.insertArrayIntoArray = function (dest, src, toIndex) {
if (this.missing(dest) || this.missing(src)) {
return;
}
// put items in backwards, otherwise inserted items end up in reverse order
for (var i = src.length - 1; i >= 0; i--) {
var item = src[i];
this.insertIntoArray(dest, item, toIndex);
}
};
Utils.moveInArray = function (array, objectsToMove, toIndex) {
var _this = this;
// first take out it items from the array
objectsToMove.forEach(function (obj) {
_this.removeFromArray(array, obj);
});
// now add the objects, in same order as provided to us, that means we start at the end
// as the objects will be pushed to the right as they are inserted
objectsToMove.slice().reverse().forEach(function (obj) {
_this.insertIntoArray(array, obj, toIndex);
});
};
Utils.defaultComparator = function (valueA, valueB, accentedCompare) {
if (accentedCompare === void 0) { accentedCompare = false; }
var valueAMissing = valueA === null || valueA === undefined;
var valueBMissing = valueB === null || valueB === undefined;
// this is for aggregations sum and avg, where the result can be a number that is wrapped.
// if we didn't do this, then the toString() value would be used, which would result in
// the strings getting used instead of the numbers.
if (valueA && valueA.toNumber) {
valueA = valueA.toNumber();
}
if (valueB && valueB.toNumber) {
valueB = valueB.toNumber();
}
if (valueAMissing && valueBMissing) {
return 0;
}
if (valueAMissing) {
return -1;
}
if (valueBMissing) {
return 1;
}
if (typeof valueA === "string") {
if (!accentedCompare) {
return doQuickCompare(valueA, valueB);
}
else {
try {
// using local compare also allows chinese comparisons
return valueA.localeCompare(valueB);
}
catch (e) {
// if something wrong with localeCompare, eg not supported
// by browser, then just continue with the quick one
return doQuickCompare(valueA, valueB);
}
}
}
if (valueA < valueB) {
return -1;
}
else if (valueA > valueB) {
return 1;
}
else {
return 0;
}
function doQuickCompare(a, b) {
return (a > b ? 1 : (a < b ? -1 : 0));
}
};
Utils.compareArrays = function (array1, array2) {
if (this.missing(array1) && this.missing(array2)) {
return true;
}
if (this.missing(array1) || this.missing(array2)) {
return false;
}
if (array1.length !== array2.length) {
return false;
}
for (var i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
return false;
}
}
return true;
};
Utils.ensureDomOrder = function (eContainer, eChild, eChildBefore) {
// if already in right order, do nothing
if (eChildBefore && eChildBefore.nextSibling === eChild) {
return;
}
if (eChildBefore) {
if (eChildBefore.nextSibling) {
// insert between the eRowBefore and the row after it
eContainer.insertBefore(eChild, eChildBefore.nextSibling);
}
else {
// if nextSibling is missing, means other row is at end, so just append new row at the end
eContainer.appendChild(eChild);
}
}
else {
// otherwise put at start
if (eContainer.firstChild) {
// insert it at the first location
eContainer.insertBefore(eChild, eContainer.firstChild);
}
}
};
Utils.insertWithDomOrder = function (eContainer, eChild, eChildBefore) {
if (eChildBefore) {
if (eChildBefore.nextSibling) {
// insert between the eRowBefore and the row after it
eContainer.insertBefore(eChild, eChildBefore.nextSibling);
}
else {
// if nextSibling is missing, means other row is at end, so just append new row at the end
eContainer.appendChild(eChild);
}
}
else {
if (eContainer.firstChild) {
// insert it at the first location
eContainer.insertBefore(eChild, eContainer.firstChild);
}
else {
// otherwise eContainer is empty, so just append it
eContainer.appendChild(eChild);
}
}
};
Utils.insertTemplateWithDomOrder = function (eContainer, htmlTemplate, eChildBefore) {
var res;
if (eChildBefore) {
// if previous element exists, just slot in after the previous element
eChildBefore.insertAdjacentHTML('afterend', htmlTemplate);
res = eChildBefore.nextSibling;
}
else {
if (eContainer.firstChild) {
// insert it at the first location
eContainer.insertAdjacentHTML('afterbegin', htmlTemplate);
}
else {
// otherwise eContainer is empty, so just append it
eContainer.innerHTML = htmlTemplate;
}
res = eContainer.firstChild;
}
return res;
};
Utils.every = function (items, callback) {
if (!items || items.length === 0) {
return true;
}
for (var i = 0; i < items.length; i++) {
if (!callback(items[i])) {
return false;
}
}
return true;
};
Utils.toStringOrNull = function (value) {
if (this.exists(value) && value.toString) {
return value.toString();
}
else {
return null;
}
};
Utils.formatWidth = function (width) {
if (typeof width === "number") {
return width + "px";
}
else {
return width;
}
};
Utils.formatNumberTwoDecimalPlacesAndCommas = function (value) {
if (typeof value !== 'number') {
return '';
}
// took this from: http://blog.tompawlak.org/number-currency-formatting-javascript
return (Math.round(value * 100) / 100).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
};
// the native method number.toLocaleString(undefined, {minimumFractionDigits: 0}) puts in decimal places in IE,
// so we use this method instead
Utils.formatNumberCommas = function (value) {
if (typeof value !== 'number') {
return '';
}
// took this from: http://blog.tompawlak.org/number-currency-formatting-javascript
return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
};
Utils.prependDC = function (parent, documentFragment) {
if (this.exists(parent.firstChild)) {
parent.insertBefore(documentFragment, parent.firstChild);
}
else {
parent.appendChild(documentFragment);
}
};
/**
* If icon provided, use this (either a string, or a function callback).
* if not, then use the default icon from the theme
*/
Utils.createIcon = function (iconName, gridOptionsWrapper, column) {
var iconContents = this.createIconNoSpan(iconName, gridOptionsWrapper, column);
if (iconContents.className.indexOf('ag-icon') > -1) {
return iconContents;
}
else {
var eResult = document.createElement('span');
eResult.appendChild(iconContents);
return eResult;
}
};
Utils.createIconNoSpan = function (iconName, gridOptionsWrapper, column) {
var userProvidedIcon;
// check col for icon first
if (column && column.getColDef().icons) {
userProvidedIcon = column.getColDef().icons[iconName];
}
// it not in col, try grid options
if (!userProvidedIcon && gridOptionsWrapper.getIcons()) {
userProvidedIcon = gridOptionsWrapper.getIcons()[iconName];
}
// now if user provided, use it
if (userProvidedIcon) {
var rendererResult = void 0;
if (typeof userProvidedIcon === 'function') {
rendererResult = userProvidedIcon();
}
else if (typeof userProvidedIcon === 'string') {
rendererResult = userProvidedIcon;
}
else {
throw 'icon from grid options needs to be a string or a function';
}
if (typeof rendererResult === 'string') {
return this.loadTemplate(rendererResult);
}
else if (this.isNodeOrElement(rendererResult)) {
return rendererResult;
}
else {
throw 'iconRenderer should return back a string or a dom object';
}
}
else {
var span = document.createElement('span');
var cssClass = this.iconNameClassMap[iconName];
if (!cssClass) {
throw new Error(iconName + " did not find class");
}
span.setAttribute("class", "ag-icon ag-icon-" + cssClass);
return span;
}
};
Utils.addStylesToElement = function (eElement, styles) {
var _this = this;
if (!styles) {
return;
}
Object.keys(styles).forEach(function (key) {
var keyCamelCase = _this.hyphenToCamelCase(key);
eElement.style[keyCamelCase] = styles[key];
});
};
Utils.isHorizontalScrollShowing = function (element) {
return element.clientWidth < element.scrollWidth;
};
Utils.isVerticalScrollShowing = function (element) {
return element.clientHeight < element.scrollHeight;
};
Utils.getMaxDivHeight = function () {
if (!document.body) {
return -1;
}
var res = 1000000;
// FF reports the height back but still renders blank after ~6M px
var testUpTo = navigator.userAgent.toLowerCase().match(/firefox/) ? 6000000 : 1000000000;
var div = this.loadTemplate("<div/>");
document.body.appendChild(div);
while (true) {
var test = res * 2;
div.style.height = test + 'px';
if (test > testUpTo || div.clientHeight !== test) {
break;
}
else {
res = test;
}
}
document.body.removeChild(div);
return res;
};
Utils.getScrollbarWidth = function () {
var outer = document.createElement("div");
outer.style.visibility = "hidden";
outer.style.width = "100px";
outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps
document.body.appendChild(outer);
var widthNoScroll = outer.offsetWidth;
// force scrollbars
outer.style.overflow = "scroll";
// add inner div
var inner = document.createElement("div");
inner.style.width = "100%";
outer.appendChild(inner);
var widthWithScroll = inner.offsetWidth;
// remove divs
outer.parentNode.removeChild(outer);
return widthNoScroll - widthWithScroll;
};
Utils.isKeyPressed = function (event, keyToCheck) {
var pressedKey = event.which || event.keyCode;
return pressedKey === keyToCheck;
};
Utils.setVisible = function (element, visible) {
this.addOrRemoveCssClass(element, 'ag-hidden', !visible);
};
Utils.setHidden = function (element, hidden) {
this.addOrRemoveCssClass(element, 'ag-visibility-hidden', hidden);
};
Utils.isBrowserIE = function () {
if (this.isIE === undefined) {
this.isIE = false || !!document.documentMode; // At least IE6
}
return this.isIE;
};
Utils.isBrowserEdge = function () {
if (this.isEdge === undefined) {
this.isEdge = !this.isBrowserIE() && !!window.StyleMedia;
}
return this.isEdge;
};
Utils.isBrowserSafari = function () {
if (this.isSafari === undefined) {
var anyWindow = window;
// taken from https://github.com/ag-grid/ag-grid/issues/550
this.isSafari = Object.prototype.toString.call(anyWindow.HTMLElement).indexOf('Constructor') > 0
|| (function (p) {
return p.toString() === "[object SafariRemoteNotification]";
})(!anyWindow.safari || anyWindow.safari.pushNotification);
}
return this.isSafari;
};
Utils.isBrowserChrome = function () {
if (this.isChrome === undefined) {
var anyWindow = window;
this.isChrome = !!anyWindow.chrome && !!anyWindow.chrome.webstore;
}
return this.isChrome;
};
Utils.isBrowserFirefox = function () {
if (this.isFirefox === undefined) {
var anyWindow = window;
this.isFirefox = typeof anyWindow.InstallTrigger !== 'undefined';
}
return this.isFirefox;
};
Utils.isUserAgentIPad = function () {
if (this.isIPad === undefined) {
// taken from https://davidwalsh.name/detect-ipad
this.isIPad = navigator.userAgent.match(/iPad|iPhone/i) != null;
}
return this.isIPad;
};
// srcElement is only available in IE. In all other browsers it is target
// http://stackoverflow.com/questions/5301643/how-can-i-make-event-srcelement-work-in-firefox-and-what-does-it-mean
Utils.getTarget = function (event) {
var eventNoType = event;
return eventNoType.target || eventNoType.srcElement;
};
Utils.isElementInEventPath = function (element, event) {
if (!event || !element) {
return false;
}
var path = exports._.getEventPath(event);
return path.indexOf(element) >= 0;
};
Utils.createEventPath = function (event) {
var res = [];
var pointer = exports._.getTarget(event);
while (pointer) {
res.push(pointer);
pointer = pointer.parentElement;
}
return res;
};
// firefox doesn't have event.path set, or any alternative to it, so we hack
// it in. this is needed as it's to late to work out the path when the item is
// removed from the dom. used by MouseEventService, where it works out if a click
// was from the current grid, or a detail grid (master / detail).
Utils.addAgGridEventPath = function (event) {
event.__agGridEventPath = this.getEventPath(event);
};