2020-02-15 05:24:28 +00:00
|
|
|
import { DefaultIterableDiffer } from '@angular/core';
|
|
|
|
|
|
2020-02-19 23:41:30 +00:00
|
|
|
export interface MovedRecord {
|
|
|
|
|
currentIndex: number;
|
|
|
|
|
previousIndex: number;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 18:54:37 +00:00
|
|
|
export const diff = <T>(
|
|
|
|
|
differ: DefaultIterableDiffer<T>,
|
|
|
|
|
a: T[],
|
|
|
|
|
b: T[]
|
|
|
|
|
): {
|
|
|
|
|
newItems: T[];
|
|
|
|
|
removedItems: T[];
|
|
|
|
|
movedItems: T[];
|
|
|
|
|
} => {
|
2020-02-15 05:24:28 +00:00
|
|
|
differ.diff(a);
|
|
|
|
|
differ.diff(b);
|
|
|
|
|
|
2020-03-20 18:54:37 +00:00
|
|
|
const alreadySet: boolean[] = [];
|
2020-03-05 22:42:21 +00:00
|
|
|
const movedItems: T[] = [];
|
2020-02-20 01:12:50 +00:00
|
|
|
|
2020-02-19 23:41:30 +00:00
|
|
|
// We first have to set the moved items to their correct positions.
|
|
|
|
|
// Keep in mind that the track by function may not guarantee
|
|
|
|
|
// that we haven't changed any of the items' props.
|
|
|
|
|
differ.forEachMovedItem(record => {
|
2020-03-20 18:54:37 +00:00
|
|
|
if (record.currentIndex === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (record.previousIndex === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-21 03:08:02 +00:00
|
|
|
// We want to preserve the reference so that a default
|
|
|
|
|
// track by function used by the CDK, for instance, can
|
|
|
|
|
// recognize that this item's identity hasn't changed.
|
|
|
|
|
// At the same time, since we don't have the guarantee
|
|
|
|
|
// that we haven't already set the previousIndex while
|
|
|
|
|
// iterating, we need to check that. If we have, we assign
|
|
|
|
|
// this array item to a new object. We don't want to risk
|
|
|
|
|
// changing the properties of an object we'll use in the future.
|
|
|
|
|
if (!alreadySet[record.previousIndex]) {
|
|
|
|
|
a[record.currentIndex] = a[record.previousIndex];
|
|
|
|
|
} else {
|
|
|
|
|
a[record.currentIndex] = {} as T;
|
|
|
|
|
}
|
|
|
|
|
Object.keys(b[record.currentIndex]).forEach(prop => {
|
2020-03-20 18:54:37 +00:00
|
|
|
// TypeScript's type inference didn't follow the check from above.
|
|
|
|
|
if (record.currentIndex === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-21 03:08:02 +00:00
|
|
|
a[record.currentIndex][prop] = b[record.currentIndex][prop];
|
|
|
|
|
});
|
|
|
|
|
if (!alreadySet[record.previousIndex]) {
|
2020-03-20 18:54:37 +00:00
|
|
|
// tslint:disable-next-line: no-non-null-assertion
|
|
|
|
|
a[record.previousIndex] = null!;
|
2020-02-21 03:08:02 +00:00
|
|
|
}
|
2020-02-20 01:12:50 +00:00
|
|
|
alreadySet[record.currentIndex] = true;
|
2020-03-05 22:42:21 +00:00
|
|
|
movedItems.push(a[record.currentIndex]);
|
2020-02-15 05:24:28 +00:00
|
|
|
});
|
|
|
|
|
|
2020-02-19 23:41:30 +00:00
|
|
|
// Now we can set the new items and remove the deleted ones.
|
2020-02-15 05:24:28 +00:00
|
|
|
const newItems: T[] = [];
|
2020-03-09 19:19:28 +00:00
|
|
|
const removedItems: T[] = [];
|
2020-02-21 02:45:53 +00:00
|
|
|
differ.forEachAddedItem(record => {
|
2020-02-19 23:41:30 +00:00
|
|
|
if (record.currentIndex !== null && record.previousIndex === null) {
|
|
|
|
|
a[record.currentIndex] = record.item;
|
|
|
|
|
alreadySet[record.currentIndex] = true;
|
2020-02-20 01:12:50 +00:00
|
|
|
newItems.push(record.item);
|
2020-02-19 23:41:30 +00:00
|
|
|
}
|
2020-02-21 02:45:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
differ.forEachRemovedItem(record => {
|
2020-03-20 18:54:37 +00:00
|
|
|
if (record.previousIndex === null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-02-20 01:12:50 +00:00
|
|
|
if (record.currentIndex === null && !alreadySet[record.previousIndex]) {
|
2020-03-20 18:54:37 +00:00
|
|
|
// tslint:disable-next-line: no-non-null-assertion
|
|
|
|
|
a[record.previousIndex] = null!;
|
2020-02-15 05:24:28 +00:00
|
|
|
}
|
2020-03-09 19:19:28 +00:00
|
|
|
removedItems.push(record.item);
|
2020-02-15 05:24:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (let i = a.length - 1; i >= 0; i--) {
|
2020-02-19 23:41:30 +00:00
|
|
|
if (a[i] === null) {
|
|
|
|
|
a.splice(i, 1);
|
2020-02-15 05:24:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 22:42:21 +00:00
|
|
|
return { newItems, removedItems, movedItems };
|
2020-02-15 05:24:28 +00:00
|
|
|
};
|