forked from 6pac/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slick.core.js
1007 lines (898 loc) · 27.6 KB
/
slick.core.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
/***
* Contains core SlickGrid classes.
* @module Core
* @namespace Slick
*/
(function (window) {
/***
* An event object for passing data to event handlers and letting them control propagation.
* <p>This is pretty much identical to how W3C and jQuery implement events.</p>
* @class EventData
* @constructor
*/
function EventData(event, args) {
this.event = event;
let nativeEvent = event;
let arguments_ = args;
let isPropagationStopped = false;
let isImmediatePropagationStopped = false;
let isDefaultPrevented = false;
let returnValues = [];
let returnValue = undefined;
// when we already have an event, we want to keep some of the event properties
// looping through some props is the only way to keep and sync these properties to the returned EventData
if (event) {
const eventProps = [
'altKey', 'ctrlKey', 'metaKey', 'shiftKey', 'key', 'keyCode',
'clientX', 'clientY', 'offsetX', 'offsetY', 'pageX', 'pageY',
'bubbles', 'type', 'which', 'x', 'y'
];
for (let key of eventProps) {
this[key] = event[key];
}
}
this.target = nativeEvent ? nativeEvent.target : undefined;
/***
* Stops event from propagating up the DOM tree.
* @method stopPropagation
*/
this.stopPropagation = function () {
isPropagationStopped = true;
if (nativeEvent) {
nativeEvent.stopPropagation();
}
};
/***
* Returns whether stopPropagation was called on this event object.
* @method isPropagationStopped
* @return {Boolean}
*/
this.isPropagationStopped = function () {
return isPropagationStopped;
};
/***
* Prevents the rest of the handlers from being executed.
* @method stopImmediatePropagation
*/
this.stopImmediatePropagation = function () {
isImmediatePropagationStopped = true;
if (nativeEvent) {
nativeEvent.stopImmediatePropagation();
}
};
/***
* Returns whether stopImmediatePropagation was called on this event object.\
* @method isImmediatePropagationStopped
* @return {Boolean}
*/
this.isImmediatePropagationStopped = function () {
return isImmediatePropagationStopped;
};
this.getNativeEvent = function() {
return nativeEvent;
}
this.preventDefault = function() {
if (nativeEvent) {
nativeEvent.preventDefault();
}
isDefaultPrevented = true;
}
this.isDefaultPrevented = function() {
if (nativeEvent) {
return nativeEvent.defaultPrevented;
}
return isDefaultPrevented;
}
this.addReturnValue = function(value) {
returnValues.push(value);
if(returnValue === undefined && value !== undefined) {
returnValue = value;
}
}
this.getReturnValue = function() {
return returnValue;
}
this.getArguments = function() {
return arguments_;
}
}
/***
* A simple publisher-subscriber implementation.
* @class Event
* @constructor
*/
function Event() {
var handlers = [];
/***
* Adds an event handler to be called when the event is fired.
* <p>Event handler will receive two arguments - an <code>EventData</code> and the <code>data</code>
* object the event was fired with.<p>
* @method subscribe
* @param fn {Function} Event handler.
*/
this.subscribe = function (fn) {
handlers.push(fn);
};
/***
* Removes an event handler added with <code>subscribe(fn)</code>.
* @method unsubscribe
* @param fn {Function} Event handler to be removed.
*/
this.unsubscribe = function (fn) {
for (var i = handlers.length - 1; i >= 0; i--) {
if (handlers[i] === fn) {
handlers.splice(i, 1);
}
}
};
/***
* Fires an event notifying all subscribers.
* @method notify
* @param args {Object} Additional data object to be passed to all handlers.
* @param e {EventData}
* Optional.
* An <code>EventData</code> object to be passed to all handlers.
* For DOM events, an existing W3C/jQuery event object can be passed in.
* @param scope {Object}
* Optional.
* The scope ("this") within which the handler will be executed.
* If not specified, the scope will be set to the <code>Event</code> instance.
*/
this.notify = function (args, e, scope) {
if (!(e instanceof EventData)) {
e = new EventData(e, args);
}
scope = scope || this;
for (var i = 0; i < handlers.length && !(e.isPropagationStopped() || e.isImmediatePropagationStopped()); i++) {
const returnValue = handlers[i].call(scope, e, args);
e.addReturnValue(returnValue);
}
return e;
};
}
function EventHandler() {
var handlers = [];
this.subscribe = function (event, handler) {
handlers.push({
event: event,
handler: handler
});
event.subscribe(handler);
return this; // allow chaining
};
this.unsubscribe = function (event, handler) {
var i = handlers.length;
while (i--) {
if (handlers[i].event === event &&
handlers[i].handler === handler) {
handlers.splice(i, 1);
event.unsubscribe(handler);
return;
}
}
return this; // allow chaining
};
this.unsubscribeAll = function () {
var i = handlers.length;
while (i--) {
handlers[i].event.unsubscribe(handlers[i].handler);
}
handlers = [];
return this; // allow chaining
};
}
/***
* A structure containing a range of cells.
* @class Range
* @constructor
* @param fromRow {Integer} Starting row.
* @param fromCell {Integer} Starting cell.
* @param toRow {Integer} Optional. Ending row. Defaults to <code>fromRow</code>.
* @param toCell {Integer} Optional. Ending cell. Defaults to <code>fromCell</code>.
*/
function Range(fromRow, fromCell, toRow, toCell) {
if (toRow === undefined && toCell === undefined) {
toRow = fromRow;
toCell = fromCell;
}
/***
* @property fromRow
* @type {Integer}
*/
this.fromRow = Math.min(fromRow, toRow);
/***
* @property fromCell
* @type {Integer}
*/
this.fromCell = Math.min(fromCell, toCell);
/***
* @property toRow
* @type {Integer}
*/
this.toRow = Math.max(fromRow, toRow);
/***
* @property toCell
* @type {Integer}
*/
this.toCell = Math.max(fromCell, toCell);
/***
* Returns whether a range represents a single row.
* @method isSingleRow
* @return {Boolean}
*/
this.isSingleRow = function () {
return this.fromRow == this.toRow;
};
/***
* Returns whether a range represents a single cell.
* @method isSingleCell
* @return {Boolean}
*/
this.isSingleCell = function () {
return this.fromRow == this.toRow && this.fromCell == this.toCell;
};
/***
* Returns whether a range contains a given cell.
* @method contains
* @param row {Integer}
* @param cell {Integer}
* @return {Boolean}
*/
this.contains = function (row, cell) {
return row >= this.fromRow && row <= this.toRow &&
cell >= this.fromCell && cell <= this.toCell;
};
/***
* Returns a readable representation of a range.
* @method toString
* @return {String}
*/
this.toString = function () {
if (this.isSingleCell()) {
return "(" + this.fromRow + ":" + this.fromCell + ")";
}
else {
return "(" + this.fromRow + ":" + this.fromCell + " - " + this.toRow + ":" + this.toCell + ")";
}
};
}
/***
* A base class that all special / non-data rows (like Group and GroupTotals) derive from.
* @class NonDataItem
* @constructor
*/
function NonDataItem() {
this.__nonDataRow = true;
}
/***
* Information about a group of rows.
* @class Group
* @extends Slick.NonDataItem
* @constructor
*/
function Group() {
this.__group = true;
/**
* Grouping level, starting with 0.
* @property level
* @type {Number}
*/
this.level = 0;
/***
* Number of rows in the group.
* @property count
* @type {Integer}
*/
this.count = 0;
/***
* Grouping value.
* @property value
* @type {Object}
*/
this.value = null;
/***
* Formatted display value of the group.
* @property title
* @type {String}
*/
this.title = null;
/***
* Whether a group is collapsed.
* @property collapsed
* @type {Boolean}
*/
this.collapsed = false;
/***
* Whether a group selection checkbox is checked.
* @property selectChecked
* @type {Boolean}
*/
this.selectChecked = false;
/***
* GroupTotals, if any.
* @property totals
* @type {GroupTotals}
*/
this.totals = null;
/**
* Rows that are part of the group.
* @property rows
* @type {Array}
*/
this.rows = [];
/**
* Sub-groups that are part of the group.
* @property groups
* @type {Array}
*/
this.groups = null;
/**
* A unique key used to identify the group. This key can be used in calls to DataView
* collapseGroup() or expandGroup().
* @property groupingKey
* @type {Object}
*/
this.groupingKey = null;
}
Group.prototype = new NonDataItem();
/***
* Compares two Group instances.
* @method equals
* @return {Boolean}
* @param group {Group} Group instance to compare to.
*/
Group.prototype.equals = function (group) {
return this.value === group.value &&
this.count === group.count &&
this.collapsed === group.collapsed &&
this.title === group.title;
};
/***
* Information about group totals.
* An instance of GroupTotals will be created for each totals row and passed to the aggregators
* so that they can store arbitrary data in it. That data can later be accessed by group totals
* formatters during the display.
* @class GroupTotals
* @extends Slick.NonDataItem
* @constructor
*/
function GroupTotals() {
this.__groupTotals = true;
/***
* Parent Group.
* @param group
* @type {Group}
*/
this.group = null;
/***
* Whether the totals have been fully initialized / calculated.
* Will be set to false for lazy-calculated group totals.
* @param initialized
* @type {Boolean}
*/
this.initialized = false;
}
GroupTotals.prototype = new NonDataItem();
/***
* A locking helper to track the active edit controller and ensure that only a single controller
* can be active at a time. This prevents a whole class of state and validation synchronization
* issues. An edit controller (such as SlickGrid) can query if an active edit is in progress
* and attempt a commit or cancel before proceeding.
* @class EditorLock
* @constructor
*/
function EditorLock() {
var activeEditController = null;
/***
* Returns true if a specified edit controller is active (has the edit lock).
* If the parameter is not specified, returns true if any edit controller is active.
* @method isActive
* @param editController {EditController}
* @return {Boolean}
*/
this.isActive = function (editController) {
return (editController ? activeEditController === editController : activeEditController !== null);
};
/***
* Sets the specified edit controller as the active edit controller (acquire edit lock).
* If another edit controller is already active, and exception will be throw new Error(.
* @method activate
* @param editController {EditController} edit controller acquiring the lock
*/
this.activate = function (editController) {
if (editController === activeEditController) { // already activated?
return;
}
if (activeEditController !== null) {
throw new Error("SlickGrid.EditorLock.activate: an editController is still active, can't activate another editController");
}
if (!editController.commitCurrentEdit) {
throw new Error("SlickGrid.EditorLock.activate: editController must implement .commitCurrentEdit()");
}
if (!editController.cancelCurrentEdit) {
throw new Error("SlickGrid.EditorLock.activate: editController must implement .cancelCurrentEdit()");
}
activeEditController = editController;
};
/***
* Unsets the specified edit controller as the active edit controller (release edit lock).
* If the specified edit controller is not the active one, an exception will be throw new Error(.
* @method deactivate
* @param editController {EditController} edit controller releasing the lock
*/
this.deactivate = function (editController) {
if (!activeEditController) {
return;
}
if (activeEditController !== editController) {
throw new Error("SlickGrid.EditorLock.deactivate: specified editController is not the currently active one");
}
activeEditController = null;
};
/***
* Attempts to commit the current edit by calling "commitCurrentEdit" method on the active edit
* controller and returns whether the commit attempt was successful (commit may fail due to validation
* errors, etc.). Edit controller's "commitCurrentEdit" must return true if the commit has succeeded
* and false otherwise. If no edit controller is active, returns true.
* @method commitCurrentEdit
* @return {Boolean}
*/
this.commitCurrentEdit = function () {
return (activeEditController ? activeEditController.commitCurrentEdit() : true);
};
/***
* Attempts to cancel the current edit by calling "cancelCurrentEdit" method on the active edit
* controller and returns whether the edit was successfully cancelled. If no edit controller is
* active, returns true.
* @method cancelCurrentEdit
* @return {Boolean}
*/
this.cancelCurrentEdit = function cancelCurrentEdit() {
return (activeEditController ? activeEditController.cancelCurrentEdit() : true);
};
}
function regexSanitizer(dirtyHtml) {
return dirtyHtml.replace(/(\b)(on[a-z]+)(\s*)=|javascript:([^>]*)[^>]*|(<\s*)(\/*)script([<>]*).*(<\s*)(\/*)script(>*)|(<)(\/*)(script|script defer)(.*)(>|>">)/gi, '');
}
function calculateAvailableSpace(element) {
let bottom = 0, top = 0, left = 0, right = 0;
const windowHeight = window.innerHeight || 0;
const windowWidth = window.innerWidth || 0;
const scrollPosition = windowScrollPosition();
const pageScrollTop = scrollPosition.top;
const pageScrollLeft = scrollPosition.left;
const elmOffset = offset(element);
if (elmOffset) {
const elementOffsetTop = elmOffset.top || 0;
const elementOffsetLeft = elmOffset.left || 0;
top = elementOffsetTop - pageScrollTop;
bottom = windowHeight - (elementOffsetTop - pageScrollTop);
left = elementOffsetLeft - pageScrollLeft;
right = windowWidth - (elementOffsetLeft - pageScrollLeft);
}
return { top, bottom, left, right };
}
/**
* Create a DOM Element with any optional attributes or properties.
* It will only accept valid DOM element properties that `createElement` would accept.
* For example: `createDomElement('div', { className: 'my-css-class' })`,
* for style or dataset you need to use nested object `{ style: { display: 'none' }}
* The last argument is to optionally append the created element to a parent container element.
* @param {String} tagName - html tag
* @param {Object} options - element properties
* @param {[HTMLElement]} appendToParent - parent element to append to
*/
function createDomElement(tagName, elementOptions, appendToParent) {
const elm = document.createElement(tagName);
if (elementOptions) {
Object.keys(elementOptions).forEach((elmOptionKey) => {
const elmValue = elementOptions[elmOptionKey];
if (typeof elmValue === 'object') {
Object.assign(elm[elmOptionKey], elmValue);
} else {
elm[elmOptionKey] = (elementOptions)[elmOptionKey];
}
});
}
if (appendToParent && appendToParent.appendChild) {
appendToParent.appendChild(elm);
}
return elm;
}
/**
* Debounce to delay JS callback execution, a wait of (-1) could be provided to execute callback without delay.
* @param {Function} callback - callback method to execute
* @param {Number} wait - delay to wait before execution or -1 delay
*/
function debounce(callback, wait) {
let timeoutId = null;
return (...args) => {
if (wait >= 0) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => callback.apply(null, args), wait);
} else {
callback.apply(null);
}
};
}
function emptyElement(element) {
if (element && element.firstChild) {
while (element.firstChild) {
if (element.lastChild) {
element.removeChild(element.lastChild);
}
}
}
return element;
}
function innerSize(elm, type) {
let size = 0;
if (elm) {
const clientSize = type === 'height' ? 'clientHeight' : 'clientWidth';
const sides = type === 'height' ? ['top', 'bottom'] : ['left', 'right'];
size = elm[clientSize];
for (const side of sides) {
const sideSize = (parseFloat(getElementProp(elm, `padding-${side}`)) || 0);
size -= sideSize;
}
}
return size;
}
function getElementProp(elm, property) {
if (elm && elm.getComputedStyle) {
return window.getComputedStyle(elm, null).getPropertyValue(property);
}
return null;
}
function isEmptyObject(obj) {
if (obj === null || obj === undefined) {
return true;
}
return Object.entries(obj).length === 0;
}
function noop() { }
function offset(el) {
if (!el || !el.getBoundingClientRect) {
return undefined;
}
const box = el.getBoundingClientRect();
const docElem = document.documentElement;
return {
top: box.top + window.pageYOffset - docElem.clientTop,
left: box.left + window.pageXOffset - docElem.clientLeft
};
}
function windowScrollPosition() {
return {
left: window.pageXOffset || document.documentElement.scrollLeft || 0,
top: window.pageYOffset || document.documentElement.scrollTop || 0,
};
}
function width(el, value) {
if (!el || !el.getBoundingClientRect) return;
if (value === undefined) {
return el.getBoundingClientRect().width
}
setStyleSize(el, "width", value);
}
function height(el, value) {
if (!el) return;
if (value === undefined) {
return el.getBoundingClientRect().height;
}
setStyleSize(el, "height", value);
}
function setStyleSize(el, style, val) {
if (typeof val === 'function') {
val = val();
} else if (typeof val === 'string') {
el.style[style] = val;
} else {
el.style[style] = val + 'px';
}
}
function contains(parent, child) {
if (!parent || !child) {
return false;
}
const parentList = parents(child);
return !parentList.every(function (p) {
if(parent == p) {
return false;
}
return true;
});
}
function isHidden(el) {
return el.offsetWidth === 0 && el.offsetHeight === 0;
}
function parents(el, selector) {
const parents = [];
const visible = selector == ":visible";
const hidden = selector == ":hidden";
while ((el = el.parentNode) && el !== document) {
if (hidden) {
if(isHidden(el)) {
parents.push(el);
}
} else if (visible) {
if(!isHidden(el)) {
parents.push(el);
}
} else if (!selector || el.matches(selector)) {
parents.push(el);
}
}
return parents;
}
function toFloat(value) {
var x = parseFloat(value);
if (isNaN(x)) {
return 0;
}
return x;
}
function show(el, type) {
type = type ? type : "";
if (Array.isArray(el)) {
el.forEach(function (e) {
e.style.display = type;
})
} else {
el.style.display = type;
}
}
function hide(el) {
if (Array.isArray(el)) {
el.forEach(function (e) {
e.style.display = "none";
});
} else {
el.style.display = "none";
}
}
function slideUp(el, callback) {
return slideAnimation(el, 'slideUp', callback);
}
function slideDown(el, callback) {
return slideAnimation(el, 'slideDown', callback);
}
function slideAnimation(el, slideDirection, callback) {
if (window.jQuery !== undefined) {
window.jQuery(el)[slideDirection]("fast", callback);
return;
}
(slideDirection === 'slideUp') ? hide(el) : show(el);
callback();
}
// jQuery's extend
var getProto = Object.getPrototypeOf;
var class2type = {};
var toString = class2type.toString;
var hasOwn = class2type.hasOwnProperty;
var fnToString = hasOwn.toString;
var ObjectFunctionString = fnToString.call( Object );
function isFunction( obj ) {
return typeof obj === "function" && typeof obj.nodeType !== "number" &&
typeof obj.item !== "function";
}
function isPlainObject( obj ) {
var proto, Ctor;
if ( !obj || toString.call( obj ) !== "[object Object]" ) {
return false;
}
proto = getProto( obj );
if ( !proto ) {
return true;
}
Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;
return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
}
function extend() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[ 0 ],
i = 1,
length = arguments.length,
deep = false;
if (typeof target === "boolean") {
deep = target;
target = arguments[ i ] || {};
i++;
} else {
target = target || {}
}
if (typeof target !== "object" && !isFunction(target)) {
target = {};
}
if (i === length) {
target = this;
i--;
}
for ( ; i < length; i++ ) {
if ( ( options = arguments[ i ] ) != null ) {
for ( name in options ) {
copy = options[ name ];
if ( name === "__proto__" || target === copy ) {
continue;
}
if ( deep && copy && ( isPlainObject( copy ) ||
( copyIsArray = Array.isArray( copy ) ) ) ) {
src = target[ name ];
if ( copyIsArray && !Array.isArray( src ) ) {
clone = [];
} else if ( !copyIsArray && !isPlainObject( src ) ) {
clone = {};
} else {
clone = src;
}
copyIsArray = false;
target[ name ] = extend( deep, clone, copy );
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
return target;
}
/**
* A simple binding event service to keep track of all JavaScript events with callback listeners,
* it allows us to unbind event(s) and their listener(s) by calling a simple unbind method call.
* Unbinding is a necessary step to make sure that all event listeners are removed to avoid memory leaks when destroing the grid
*/
function BindingEventService() {
this.boundedEvents = [];
this.destroy = function () {
this.unbindAll();
this.boundedEvents = [];
}
/** Bind an event listener to any element */
this.bind = function (element, eventName, listener, options) {
element.addEventListener(eventName, listener, options);
this.boundedEvents.push({ element: element, eventName, listener });
}
/** Unbind all will remove every every event handlers that were bounded earlier */
this.unbind = function (element, eventName, listener) {
if (element && element.removeEventListener) {
element.removeEventListener(eventName, listener);
}
}
this.unbindByEventName = function (element, eventName) {
const boundedEvent = this.boundedEvents.find(e => e.element === element && e.eventName === eventName);
if (boundedEvent) {
this.unbind(boundedEvent.element, boundedEvent.eventName, boundedEvent.listener);
}
}
/** Unbind all will remove every every event handlers that were bounded earlier */
this.unbindAll = function () {
while (this.boundedEvents.length > 0) {
const boundedEvent = this.boundedEvents.pop();
const { element, eventName, listener } = boundedEvent;
this.unbind(element, eventName, listener);
}
}
}
// export Slick namespace on both global & window objects
window.Slick = {
"Event": Event,
"EventData": EventData,
"EventHandler": EventHandler,
"Range": Range,
"NonDataRow": NonDataItem,
"Group": Group,
"GroupTotals": GroupTotals,
"RegexSanitizer": regexSanitizer,
"EditorLock": EditorLock,
"BindingEventService": BindingEventService,
"Utils": {
"debounce": debounce,
"extend": extend,
"calculateAvailableSpace": calculateAvailableSpace,
"createDomElement": createDomElement,
"emptyElement": emptyElement,
"innerSize": innerSize,
"isEmptyObject": isEmptyObject,
"noop": noop,
"offset": offset,
"height": height,
"width": width,
"setStyleSize": setStyleSize,
"contains": contains,
"toFloat": toFloat,
"parents": parents,
"show": show,
"hide": hide,
"slideUp": slideUp,
"slideDown": slideDown,
"storage": {
// https://stackoverflow.com/questions/29222027/vanilla-alternative-to-jquery-data-function-any-native-javascript-alternati
_storage: new WeakMap(),
put: function (element, key, obj) {
if (!this._storage.has(element)) {
this._storage.set(element, new Map());
}
this._storage.get(element).set(key, obj);
},
get: function (element, key) {
const el = this._storage.get(element);
if (el) {
return el.get(key);
}
return null;
},
remove: function (element, key) {
var ret = this._storage.get(element).delete(key);
if (!this._storage.get(element).size === 0) {
this._storage.delete(element);
}
return ret;
}
}
},
/***
* A global singleton editor lock.
* @class GlobalEditorLock
* @static
* @constructor
*/
"GlobalEditorLock": new EditorLock(),
"keyCode": {
SPACE: 8,
BACKSPACE: 8,
DELETE: 46,
DOWN: 40,
END: 35,
ENTER: 13,
ESCAPE: 27,
HOME: 36,
INSERT: 45,
LEFT: 37,
PAGE_DOWN: 34,
PAGE_UP: 33,
RIGHT: 39,
TAB: 9,
UP: 38,
A: 65
},
"preClickClassName": "slick-edit-preclick",
"GridAutosizeColsMode": {
None: 'NOA',
LegacyOff: 'LOF',
LegacyForceFit: 'LFF',
IgnoreViewport: 'IGV',
FitColsToViewport: 'FCV',
FitViewportToCols: 'FVC'
},
"ColAutosizeMode": {
Locked: 'LCK',
Guide: 'GUI',
Content: 'CON',
ContentExpandOnly: 'CXO',
ContentIntelligent: 'CTI'
},
"RowSelectionMode": {
FirstRow: 'FS1',
FirstNRows: 'FSN',
AllRows: 'ALL',
LastRow: 'LS1'
},
"ValueFilterMode": {
None: 'NONE',
DeDuplicate: 'DEDP',
GetGreatestAndSub: 'GR8T',
GetLongestTextAndSub: 'LNSB',
GetLongestText: 'LNSC'
},
"WidthEvalMode": {
Auto: 'AUTO',
TextOnly: 'CANV',
HTML: 'HTML'
}
}