UNPKG

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