1 | /*
|
2 | Copyright (c) 2019-present NAVER Corp.
|
3 | name: @egjs/children-differ
|
4 | license: MIT
|
5 | author: NAVER Corp.
|
6 | repository: https://github.com/naver/egjs-children-differ
|
7 | version: 1.0.1
|
8 | */
|
9 | (function (global, factory) {
|
10 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
11 | typeof define === 'function' && define.amd ? define(factory) :
|
12 | (global = global || self, (global.eg = global.eg || {}, global.eg.ChildrenDiffer = factory()));
|
13 | }(this, function () { 'use strict';
|
14 |
|
15 | /*! *****************************************************************************
|
16 | Copyright (c) Microsoft Corporation. All rights reserved.
|
17 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
18 | this file except in compliance with the License. You may obtain a copy of the
|
19 | License at http://www.apache.org/licenses/LICENSE-2.0
|
20 |
|
21 | THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
22 | KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
23 | WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
24 | MERCHANTABLITY OR NON-INFRINGEMENT.
|
25 |
|
26 | See the Apache Version 2.0 License for specific language governing permissions
|
27 | and limitations under the License.
|
28 | ***************************************************************************** */
|
29 |
|
30 | /* global Reflect, Promise */
|
31 | var extendStatics = function (d, b) {
|
32 | extendStatics = Object.setPrototypeOf || {
|
33 | __proto__: []
|
34 | } instanceof Array && function (d, b) {
|
35 | d.__proto__ = b;
|
36 | } || function (d, b) {
|
37 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
38 | };
|
39 |
|
40 | return extendStatics(d, b);
|
41 | };
|
42 |
|
43 | function __extends(d, b) {
|
44 | extendStatics(d, b);
|
45 |
|
46 | function __() {
|
47 | this.constructor = d;
|
48 | }
|
49 |
|
50 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
51 | }
|
52 |
|
53 | /*
|
54 | Copyright (c) 2019-present NAVER Corp.
|
55 | name: @egjs/list-differ
|
56 | license: MIT
|
57 | author: NAVER Corp.
|
58 | repository: https://github.com/naver/egjs-list-differ
|
59 | version: 1.0.0
|
60 | */
|
61 |
|
62 | /*
|
63 | egjs-list-differ
|
64 | Copyright (c) 2019-present NAVER Corp.
|
65 | MIT license
|
66 | */
|
67 | var PolyMap =
|
68 | /*#__PURE__*/
|
69 | function () {
|
70 | function PolyMap() {
|
71 | this.keys = [];
|
72 | this.values = [];
|
73 | }
|
74 |
|
75 | var __proto = PolyMap.prototype;
|
76 |
|
77 | __proto.get = function (key) {
|
78 | return this.values[this.keys.indexOf(key)];
|
79 | };
|
80 |
|
81 | __proto.set = function (key, value) {
|
82 | var keys = this.keys;
|
83 | var values = this.values;
|
84 | var prevIndex = keys.indexOf(key);
|
85 | var index = prevIndex === -1 ? keys.length : prevIndex;
|
86 | keys[index] = key;
|
87 | values[index] = value;
|
88 | };
|
89 |
|
90 | return PolyMap;
|
91 | }();
|
92 | /*
|
93 | egjs-list-differ
|
94 | Copyright (c) 2019-present NAVER Corp.
|
95 | MIT license
|
96 | */
|
97 |
|
98 |
|
99 | var HashMap =
|
100 | /*#__PURE__*/
|
101 | function () {
|
102 | function HashMap() {
|
103 | this.object = {};
|
104 | }
|
105 |
|
106 | var __proto = HashMap.prototype;
|
107 |
|
108 | __proto.get = function (key) {
|
109 | return this.object[key];
|
110 | };
|
111 |
|
112 | __proto.set = function (key, value) {
|
113 | this.object[key] = value;
|
114 | };
|
115 |
|
116 | return HashMap;
|
117 | }();
|
118 | /*
|
119 | egjs-list-differ
|
120 | Copyright (c) 2019-present NAVER Corp.
|
121 | MIT license
|
122 | */
|
123 |
|
124 |
|
125 | var SUPPORT_MAP = typeof Map === "function";
|
126 | /*
|
127 | egjs-list-differ
|
128 | Copyright (c) 2019-present NAVER Corp.
|
129 | MIT license
|
130 | */
|
131 |
|
132 | var Link =
|
133 | /*#__PURE__*/
|
134 | function () {
|
135 | function Link() {}
|
136 |
|
137 | var __proto = Link.prototype;
|
138 |
|
139 | __proto.connect = function (prevLink, nextLink) {
|
140 | this.prev = prevLink;
|
141 | this.next = nextLink;
|
142 | prevLink && (prevLink.next = this);
|
143 | nextLink && (nextLink.prev = this);
|
144 | };
|
145 |
|
146 | __proto.disconnect = function () {
|
147 | // In double linked list, diconnect the interconnected relationship.
|
148 | var prevLink = this.prev;
|
149 | var nextLink = this.next;
|
150 | prevLink && (prevLink.next = nextLink);
|
151 | nextLink && (nextLink.prev = prevLink);
|
152 | };
|
153 |
|
154 | __proto.getIndex = function () {
|
155 | var link = this;
|
156 | var index = -1;
|
157 |
|
158 | while (link) {
|
159 | link = link.prev;
|
160 | ++index;
|
161 | }
|
162 |
|
163 | return index;
|
164 | };
|
165 |
|
166 | return Link;
|
167 | }();
|
168 | /*
|
169 | egjs-list-differ
|
170 | Copyright (c) 2019-present NAVER Corp.
|
171 | MIT license
|
172 | */
|
173 |
|
174 |
|
175 | function orderChanged(changed, fixed) {
|
176 | // It is roughly in the order of these examples.
|
177 | // 4, 6, 0, 2, 1, 3, 5, 7
|
178 | var fromLinks = []; // 0, 1, 2, 3, 4, 5, 6, 7
|
179 |
|
180 | var toLinks = [];
|
181 | changed.forEach(function (_a) {
|
182 | var from = _a[0],
|
183 | to = _a[1];
|
184 | var link = new Link();
|
185 | fromLinks[from] = link;
|
186 | toLinks[to] = link;
|
187 | }); // `fromLinks` are connected to each other by double linked list.
|
188 |
|
189 | fromLinks.forEach(function (link, i) {
|
190 | link.connect(fromLinks[i - 1]);
|
191 | });
|
192 | return changed.filter(function (_, i) {
|
193 | return !fixed[i];
|
194 | }).map(function (_a, i) {
|
195 | var from = _a[0],
|
196 | to = _a[1];
|
197 |
|
198 | if (from === to) {
|
199 | return [0, 0];
|
200 | }
|
201 |
|
202 | var fromLink = fromLinks[from];
|
203 | var toLink = toLinks[to - 1];
|
204 | var fromIndex = fromLink.getIndex(); // Disconnect the link connected to `fromLink`.
|
205 |
|
206 | fromLink.disconnect(); // Connect `fromLink` to the right of `toLink`.
|
207 |
|
208 | if (!toLink) {
|
209 | fromLink.connect(undefined, fromLinks[0]);
|
210 | } else {
|
211 | fromLink.connect(toLink, toLink.next);
|
212 | }
|
213 |
|
214 | var toIndex = fromLink.getIndex();
|
215 | return [fromIndex, toIndex];
|
216 | });
|
217 | }
|
218 |
|
219 | var Result =
|
220 | /*#__PURE__*/
|
221 | function () {
|
222 | function Result(prevList, list, added, removed, changed, maintained, changedBeforeAdded, fixed) {
|
223 | this.prevList = prevList;
|
224 | this.list = list;
|
225 | this.added = added;
|
226 | this.removed = removed;
|
227 | this.changed = changed;
|
228 | this.maintained = maintained;
|
229 | this.changedBeforeAdded = changedBeforeAdded;
|
230 | this.fixed = fixed;
|
231 | }
|
232 |
|
233 | var __proto = Result.prototype;
|
234 | Object.defineProperty(__proto, "ordered", {
|
235 | get: function () {
|
236 | if (!this.cacheOrdered) {
|
237 | this.caculateOrdered();
|
238 | }
|
239 |
|
240 | return this.cacheOrdered;
|
241 | },
|
242 | enumerable: true,
|
243 | configurable: true
|
244 | });
|
245 | Object.defineProperty(__proto, "pureChanged", {
|
246 | get: function () {
|
247 | if (!this.cachePureChanged) {
|
248 | this.caculateOrdered();
|
249 | }
|
250 |
|
251 | return this.cachePureChanged;
|
252 | },
|
253 | enumerable: true,
|
254 | configurable: true
|
255 | });
|
256 |
|
257 | __proto.caculateOrdered = function () {
|
258 | var ordered = orderChanged(this.changedBeforeAdded, this.fixed);
|
259 | var changed = this.changed;
|
260 | var pureChanged = [];
|
261 | this.cacheOrdered = ordered.filter(function (_a, i) {
|
262 | var from = _a[0],
|
263 | to = _a[1];
|
264 | var _b = changed[i],
|
265 | fromBefore = _b[0],
|
266 | toBefore = _b[1];
|
267 |
|
268 | if (from !== to) {
|
269 | pureChanged.push([fromBefore, toBefore]);
|
270 | return true;
|
271 | }
|
272 | });
|
273 | this.cachePureChanged = pureChanged;
|
274 | };
|
275 |
|
276 | return Result;
|
277 | }();
|
278 | /**
|
279 | *
|
280 | * @memberof eg.ListDiffer
|
281 | * @static
|
282 | * @function
|
283 | * @param - Previous List <ko> 이전 목록 </ko>
|
284 | * @param - List to Update <ko> 업데이트 할 목록 </ko>
|
285 | * @param - This callback function returns the key of the item. <ko> 아이템의 키를 반환하는 콜백 함수입니다.</ko>
|
286 | * @return - Returns the diff between `prevList` and `list` <ko> `prevList`와 `list`의 다른 점을 반환한다.</ko>
|
287 | * @example
|
288 | * import { diff } from "@egjs/list-differ";
|
289 | * // script => eg.ListDiffer.diff
|
290 | * const result = diff([0, 1, 2, 3, 4, 5], [7, 8, 0, 4, 3, 6, 2, 1], e => e);
|
291 | * // List before update
|
292 | * // [1, 2, 3, 4, 5]
|
293 | * console.log(result.prevList);
|
294 | * // Updated list
|
295 | * // [4, 3, 6, 2, 1]
|
296 | * console.log(result.list);
|
297 | * // Index array of values added to `list`
|
298 | * // [0, 1, 5]
|
299 | * console.log(result.added);
|
300 | * // Index array of values removed in `prevList`
|
301 | * // [5]
|
302 | * console.log(result.removed);
|
303 | * // An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`
|
304 | * // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
|
305 | * console.log(result.changed);
|
306 | * // The subset of `changed` and an array of index pairs that moved data directly. Indicate an array of absolute index pairs of `ordered`.(Formatted by: Array<[index of prevList, index of list]>)
|
307 | * // [[4, 3], [3, 4], [2, 6]]
|
308 | * console.log(result.pureChanged);
|
309 | * // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
|
310 | * // [[4, 1], [4, 2], [4, 3]]
|
311 | * console.log(result.ordered);
|
312 | * // An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved
|
313 | * // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
|
314 | * console.log(result.maintained);
|
315 | */
|
316 |
|
317 |
|
318 | function diff(prevList, list, findKeyCallback) {
|
319 | var mapClass = SUPPORT_MAP ? Map : findKeyCallback ? HashMap : PolyMap;
|
320 |
|
321 | var callback = findKeyCallback || function (e) {
|
322 | return e;
|
323 | };
|
324 |
|
325 | var added = [];
|
326 | var removed = [];
|
327 | var maintained = [];
|
328 | var prevKeys = prevList.map(callback);
|
329 | var keys = list.map(callback);
|
330 | var prevKeyMap = new mapClass();
|
331 | var keyMap = new mapClass();
|
332 | var changedBeforeAdded = [];
|
333 | var fixed = [];
|
334 | var removedMap = {};
|
335 | var changed = [];
|
336 | var addedCount = 0;
|
337 | var removedCount = 0; // Add prevKeys and keys to the hashmap.
|
338 |
|
339 | prevKeys.forEach(function (key, prevListIndex) {
|
340 | prevKeyMap.set(key, prevListIndex);
|
341 | });
|
342 | keys.forEach(function (key, listIndex) {
|
343 | keyMap.set(key, listIndex);
|
344 | }); // Compare `prevKeys` and `keys` and add them to `removed` if they are not in `keys`.
|
345 |
|
346 | prevKeys.forEach(function (key, prevListIndex) {
|
347 | var listIndex = keyMap.get(key); // In prevList, but not in list, it is removed.
|
348 |
|
349 | if (typeof listIndex === "undefined") {
|
350 | ++removedCount;
|
351 | removed.push(prevListIndex);
|
352 | } else {
|
353 | removedMap[listIndex] = removedCount;
|
354 | }
|
355 | }); // Compare `prevKeys` and `keys` and add them to `added` if they are not in `prevKeys`.
|
356 |
|
357 | keys.forEach(function (key, listIndex) {
|
358 | var prevListIndex = prevKeyMap.get(key); // In list, but not in prevList, it is added.
|
359 |
|
360 | if (typeof prevListIndex === "undefined") {
|
361 | added.push(listIndex);
|
362 | ++addedCount;
|
363 | } else {
|
364 | maintained.push([prevListIndex, listIndex]);
|
365 | removedCount = removedMap[listIndex] || 0;
|
366 | changedBeforeAdded.push([prevListIndex - removedCount, listIndex - addedCount]);
|
367 | fixed.push(listIndex === prevListIndex);
|
368 |
|
369 | if (prevListIndex !== listIndex) {
|
370 | changed.push([prevListIndex, listIndex]);
|
371 | }
|
372 | }
|
373 | }); // Sort by ascending order of 'to(list's index).
|
374 |
|
375 | removed.reverse();
|
376 | return new Result(prevList, list, added, removed, changed, maintained, changedBeforeAdded, fixed);
|
377 | }
|
378 | /**
|
379 | * A module that checks diff when values are added, removed, or changed in an array.
|
380 | * @ko 배열 또는 오브젝트에서 값이 추가되거나 삭제되거나 순서가 변경사항을 체크하는 모듈입니다.
|
381 | * @memberof eg
|
382 | */
|
383 |
|
384 |
|
385 | var ListDiffer =
|
386 | /*#__PURE__*/
|
387 | function () {
|
388 | /**
|
389 | * @param - Initializing Data Array. <ko> 초기 설정할 데이터 배열.</ko>
|
390 | * @param - This callback function returns the key of the item. <ko> 아이템의 키를 반환하는 콜백 함수입니다.</ko>
|
391 | * @example
|
392 | * import ListDiffer from "@egjs/list-differ";
|
393 | * // script => eg.ListDiffer
|
394 | * const differ = new ListDiffer([0, 1, 2, 3, 4, 5], e => e);
|
395 | * const result = differ.update([7, 8, 0, 4, 3, 6, 2, 1]);
|
396 | * // List before update
|
397 | * // [1, 2, 3, 4, 5]
|
398 | * console.log(result.prevList);
|
399 | * // Updated list
|
400 | * // [4, 3, 6, 2, 1]
|
401 | * console.log(result.list);
|
402 | * // Index array of values added to `list`.
|
403 | * // [0, 1, 5]
|
404 | * console.log(result.added);
|
405 | * // Index array of values removed in `prevList`.
|
406 | * // [5]
|
407 | * console.log(result.removed);
|
408 | * // An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`.
|
409 | * // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
|
410 | * console.log(result.changed);
|
411 | * // The subset of `changed` and an array of index pairs that moved data directly. Indicate an array of absolute index pairs of `ordered`.(Formatted by: Array<[index of prevList, index of list]>)
|
412 | * // [[4, 3], [3, 4], [2, 6]]
|
413 | * console.log(result.pureChanged);
|
414 | * // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
|
415 | * // [[4, 1], [4, 2], [4, 3]]
|
416 | * console.log(result.ordered);
|
417 | * // An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved.
|
418 | * // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
|
419 | * console.log(result.maintained);
|
420 | */
|
421 | function ListDiffer(list, findKeyCallback) {
|
422 | if (list === void 0) {
|
423 | list = [];
|
424 | }
|
425 |
|
426 | this.findKeyCallback = findKeyCallback;
|
427 | this.list = [].slice.call(list);
|
428 | }
|
429 | /**
|
430 | * Update list.
|
431 | * @ko 리스트를 업데이트를 합니다.
|
432 | * @param - List to update <ko> 업데이트할 리스트 </ko>
|
433 | * @return - Returns the results of an update from `prevList` to `list`.<ko> `prevList`에서 `list`로 업데이트한 결과를 반환한다. </ko>
|
434 | */
|
435 |
|
436 |
|
437 | var __proto = ListDiffer.prototype;
|
438 |
|
439 | __proto.update = function (list) {
|
440 | var newData = [].slice.call(list);
|
441 | var result = diff(this.list, newData, this.findKeyCallback);
|
442 | this.list = newData;
|
443 | return result;
|
444 | };
|
445 |
|
446 | return ListDiffer;
|
447 | }();
|
448 |
|
449 | /*
|
450 | egjs-children-differ
|
451 | Copyright (c) 2019-present NAVER Corp.
|
452 | MIT license
|
453 | */
|
454 | var findKeyCallback = typeof Map === "function" ? undefined : function () {
|
455 | var childrenCount = 0;
|
456 | return function (el) {
|
457 | return el.__DIFF_KEY__ || (el.__DIFF_KEY__ = ++childrenCount);
|
458 | };
|
459 | }();
|
460 |
|
461 | /**
|
462 | * A module that checks diff when child are added, removed, or changed .
|
463 | * @ko 자식 노드들에서 자식 노드가 추가되거나 삭제되거나 순서가 변경된 사항을 체크하는 모듈입니다.
|
464 | * @memberof eg
|
465 | * @extends eg.ListDiffer
|
466 | */
|
467 |
|
468 | var ChildrenDiffer =
|
469 | /*#__PURE__*/
|
470 | function (_super) {
|
471 | __extends(ChildrenDiffer, _super);
|
472 | /**
|
473 | * @param - Initializing Children <ko> 초기 설정할 자식 노드들</ko>
|
474 | */
|
475 |
|
476 |
|
477 | function ChildrenDiffer(list) {
|
478 | if (list === void 0) {
|
479 | list = [];
|
480 | }
|
481 |
|
482 | return _super.call(this, list, findKeyCallback) || this;
|
483 | }
|
484 |
|
485 | return ChildrenDiffer;
|
486 | }(ListDiffer);
|
487 |
|
488 | /*
|
489 | egjs-children-differ
|
490 | Copyright (c) 2019-present NAVER Corp.
|
491 | MIT license
|
492 | */
|
493 | /**
|
494 | *
|
495 | * @memberof eg.ChildrenDiffer
|
496 | * @static
|
497 | * @function
|
498 | * @param - Previous List <ko> 이전 목록 </ko>
|
499 | * @param - List to Update <ko> 업데이트 할 목록 </ko>
|
500 | * @return - Returns the diff between `prevList` and `list` <ko> `prevList`와 `list`의 다른 점을 반환한다.</ko>
|
501 | * @example
|
502 | * import { diff } from "@egjs/children-differ";
|
503 | * // script => eg.ChildrenDiffer.diff
|
504 | * const result = diff([0, 1, 2, 3, 4, 5], [7, 8, 0, 4, 3, 6, 2, 1]);
|
505 | * // List before update
|
506 | * // [1, 2, 3, 4, 5]
|
507 | * console.log(result.prevList);
|
508 | * // Updated list
|
509 | * // [4, 3, 6, 2, 1]
|
510 | * console.log(result.list);
|
511 | * // Index array of values added to `list`
|
512 | * // [0, 1, 5]
|
513 | * console.log(result.added);
|
514 | * // Index array of values removed in `prevList`
|
515 | * // [5]
|
516 | * console.log(result.removed);
|
517 | * // An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`
|
518 | * // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
|
519 | * console.log(result.changed);
|
520 | * // The subset of `changed` and an array of index pairs that moved data directly. Indicate an array of absolute index pairs of `ordered`.(Formatted by: Array<[index of prevList, index of list]>)
|
521 | * // [[4, 3], [3, 4], [2, 6]]
|
522 | * console.log(result.pureChanged);
|
523 | * // An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
|
524 | * // [[4, 1], [4, 2], [4, 3]]
|
525 | * console.log(result.ordered);
|
526 | * // An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved
|
527 | * // [[0, 2], [4, 3], [3, 4], [2, 6], [1, 7]]
|
528 | * console.log(result.maintained);
|
529 | */
|
530 |
|
531 | function diff$1(prevList, list) {
|
532 | return diff(prevList, list, findKeyCallback);
|
533 | }
|
534 |
|
535 | /*
|
536 | egjs-children-differ
|
537 | Copyright (c) 2019-present NAVER Corp.
|
538 | MIT license
|
539 | */
|
540 |
|
541 | /*
|
542 | egjs-children-differ
|
543 | Copyright (c) 2019-present NAVER Corp.
|
544 | MIT license
|
545 | */
|
546 | ChildrenDiffer.diff = diff$1;
|
547 |
|
548 | return ChildrenDiffer;
|
549 |
|
550 | }));
|
551 | //# sourceMappingURL=children-differ.pkgd.js.map
|