UNPKG

6.03 kBJavaScriptView Raw
1// ag-grid-react v28.1.0
2"use strict";
3Object.defineProperty(exports, "__esModule", { value: true });
4var ChangeDetectionStrategyType;
5(function (ChangeDetectionStrategyType) {
6 ChangeDetectionStrategyType["IdentityCheck"] = "IdentityCheck";
7 ChangeDetectionStrategyType["DeepValueCheck"] = "DeepValueCheck";
8 ChangeDetectionStrategyType["NoCheck"] = "NoCheck";
9})(ChangeDetectionStrategyType = exports.ChangeDetectionStrategyType || (exports.ChangeDetectionStrategyType = {}));
10var SimpleFunctionalStrategy = /** @class */ (function () {
11 function SimpleFunctionalStrategy(strategy) {
12 this.strategy = strategy;
13 }
14 SimpleFunctionalStrategy.prototype.areEqual = function (a, b) {
15 return this.strategy(a, b);
16 };
17 return SimpleFunctionalStrategy;
18}());
19var DeepValueStrategy = /** @class */ (function () {
20 function DeepValueStrategy() {
21 }
22 DeepValueStrategy.prototype.areEqual = function (a, b) {
23 return DeepValueStrategy.areEquivalent(DeepValueStrategy.copy(a), DeepValueStrategy.copy(b));
24 };
25 /*
26 * deeper object comparison - taken from https://stackoverflow.com/questions/1068834/object-comparison-in-javascript
27 */
28 DeepValueStrategy.unwrapStringOrNumber = function (obj) {
29 return obj instanceof Number || obj instanceof String ? obj.valueOf() : obj;
30 };
31 // sigh, here for ie compatibility
32 DeepValueStrategy.copy = function (value) {
33 if (!value) {
34 return value;
35 }
36 if (Array.isArray(value)) {
37 // shallow copy the array - this will typically be either rowData or columnDefs
38 var arrayCopy = [];
39 for (var i = 0; i < value.length; i++) {
40 arrayCopy.push(this.copy(value[i]));
41 }
42 return arrayCopy;
43 }
44 // for anything without keys (boolean, string etc).
45 // Object.keys - chrome will swallow them
46 if (typeof value !== "object") {
47 return value;
48 }
49 return [{}, value].reduce(function (r, o) {
50 Object.keys(o).forEach(function (k) {
51 r[k] = o[k];
52 });
53 return r;
54 }, {});
55 };
56 DeepValueStrategy.isNaN = function (value) {
57 if (Number.isNaN) {
58 return Number.isNaN(value);
59 }
60 // for ie11...
61 return typeof (value) === 'number' && isNaN(value);
62 };
63 /*
64 * slightly modified, but taken from https://stackoverflow.com/questions/1068834/object-comparison-in-javascript
65 *
66 * What we're trying to do here is determine if the property being checked has changed in _value_, not just in reference
67 *
68 * For eg, if a user updates the columnDefs via property binding, but the actual columns defs are the same before and
69 * after, then we don't want the grid to re-render
70 */
71 DeepValueStrategy.areEquivalent = function (a, b) {
72 a = DeepValueStrategy.unwrapStringOrNumber(a);
73 b = DeepValueStrategy.unwrapStringOrNumber(b);
74 if (a === b)
75 return true; //e.g. a and b both null
76 if (a === null || b === null || typeof a !== typeof b)
77 return false;
78 if (DeepValueStrategy.isNaN(a) && DeepValueStrategy.isNaN(b)) {
79 return true;
80 }
81 if (a instanceof Date) {
82 return b instanceof Date && a.valueOf() === b.valueOf();
83 }
84 if (typeof a === "function") {
85 // false to allow for callbacks to be reactive...
86 return false;
87 }
88 if (typeof a !== "object" ||
89 (a.$$typeof && a.$$typeof.toString() === "Symbol(react.element)")) {
90 return a == b; //for boolean, number, string, function, xml
91 }
92 if (Object.isFrozen(a) || Object.isFrozen(b)) {
93 return a === b;
94 }
95 var newA = a.areEquivPropertyTracking === undefined, newB = b.areEquivPropertyTracking === undefined;
96 try {
97 var prop = void 0;
98 if (newA) {
99 a.areEquivPropertyTracking = [];
100 }
101 else if (a.areEquivPropertyTracking.some(function (other) {
102 return other === b;
103 }))
104 return true;
105 if (newB) {
106 b.areEquivPropertyTracking = [];
107 }
108 else if (b.areEquivPropertyTracking.some(function (other) { return other === a; })) {
109 return true;
110 }
111 a.areEquivPropertyTracking.push(b);
112 b.areEquivPropertyTracking.push(a);
113 var tmp = {};
114 for (prop in a)
115 if (prop != "areEquivPropertyTracking") {
116 tmp[prop] = null;
117 }
118 for (prop in b)
119 if (prop != "areEquivPropertyTracking") {
120 tmp[prop] = null;
121 }
122 for (prop in tmp) {
123 if (!this.areEquivalent(a[prop], b[prop])) {
124 return false;
125 }
126 }
127 return true;
128 }
129 finally {
130 if (newA)
131 delete a.areEquivPropertyTracking;
132 if (newB)
133 delete b.areEquivPropertyTracking;
134 }
135 };
136 return DeepValueStrategy;
137}());
138var ChangeDetectionService = /** @class */ (function () {
139 function ChangeDetectionService() {
140 var _a;
141 this.strategyMap = (_a = {},
142 _a[ChangeDetectionStrategyType.DeepValueCheck] = new DeepValueStrategy(),
143 _a[ChangeDetectionStrategyType.IdentityCheck] = new SimpleFunctionalStrategy(function (a, b) { return a === b; }),
144 _a[ChangeDetectionStrategyType.NoCheck] = new SimpleFunctionalStrategy(function (a, b) { return false; }),
145 _a);
146 }
147 ChangeDetectionService.prototype.getStrategy = function (changeDetectionStrategy) {
148 return this.strategyMap[changeDetectionStrategy];
149 };
150 return ChangeDetectionService;
151}());
152exports.ChangeDetectionService = ChangeDetectionService;