@progress/kendo-react-treelist 5.20.0-dev.202310051816 → 5.20.0-dev.202310061256

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,250 +0,0 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
13
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
14
- if (ar || !(i in from)) {
15
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
16
- ar[i] = from[i];
17
- }
18
- }
19
- return to.concat(ar || Array.prototype.slice.call(from));
20
- };
21
- import { getNestedValue } from './index';
22
- import { filterBy as filterByCommon, orderBy as orderByCommon } from '@progress/kendo-react-data-tools';
23
- import { mapTree as mapTreeCommon, mapTreeItem as mapTreeItemCommon, extendDataItem as extendDataItemCommon, getItemPath as getItemPathCommon } from '@progress/kendo-react-common';
24
- /**
25
- * Orders the specified tree according to the provided sort descriptors.
26
- *
27
- * @param {T[]} data - The data that will be sorted.
28
- * @param {SortDescriptor[]} descriptors - The descriptors by which the data will be sorted.
29
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
30
- * @returns {T[]} - The sorted data.
31
- */
32
- export function orderBy(data, descriptors, subItemsField) {
33
- return orderByCommon(data, descriptors, subItemsField);
34
- }
35
- /**
36
- * Filters the provided data tree according to the specified `Array<FilterDescriptor|CompositeFilterDescriptor>`.
37
- *
38
- * @param {T[]} data - The data that will be filtered.
39
- * @param {FilterDescriptor[]|CompositeFilterDescriptor[]} descriptors - The filter criteria that will be applied.
40
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
41
- * @returns {T[]} - The filtered data.
42
- */
43
- export function filterBy(data, descriptors, subItemsField) {
44
- return filterByCommon(data, descriptors, subItemsField);
45
- }
46
- /**
47
- * Creates a new array with the results of calling the provided callback function
48
- * on every element in the provided data tree.
49
- *
50
- * @param {any[]} tree - The data tree.
51
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
52
- * @param {(value: any) => any} callback - The callback function.
53
- * @returns {any[]} - The new data tree.
54
- */
55
- export var mapTree = function (tree, subItemsField, callback) {
56
- return mapTreeCommon(tree, subItemsField, callback);
57
- };
58
- /**
59
- * Creates a new array with the results of calling the provided callback function
60
- * on the element which match the `level` in the provided data tree.
61
- *
62
- * @param {any[]} tree - The data tree.
63
- * @param {number[]} level - An array of indexes of each parent and current item in the data tree.
64
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
65
- * @param {(value: any) => any} callback - The callback function.
66
- * @returns {any[]} - The new data tree.
67
- */
68
- export var mapTreeItem = function (tree, level, subItemsField, callback) {
69
- return mapTreeItemCommon(tree, level, subItemsField, callback);
70
- };
71
- /**
72
- * @hidden
73
- */
74
- export function flatData(data, getChildren, itemMap) {
75
- var stack = [];
76
- var flatted = [];
77
- // we start from end as push() improves performance by up to 50%
78
- for (var i = data.length - 1; i >= 0; i--) {
79
- var root = data[i];
80
- stack.push({
81
- root: root,
82
- data: getChildren(root),
83
- level: [i],
84
- levelCount: data.length
85
- });
86
- }
87
- while (stack.length > 0) {
88
- var stackItem = stack.pop();
89
- flatted.push(__assign(__assign({ dataItem: stackItem.root, level: __spreadArray([], stackItem.level, true) }, itemMap(stackItem.root)), { levelCount: stackItem.levelCount }));
90
- if (stackItem.data.length) {
91
- for (var i = stackItem.data.length - 1; i >= 0; i--) {
92
- var root = stackItem.data[i];
93
- stack.push({
94
- root: root,
95
- data: getChildren(root),
96
- level: __spreadArray(__spreadArray([], stackItem.level, true), [i], false),
97
- levelCount: stackItem.data.length
98
- });
99
- }
100
- }
101
- }
102
- return flatted;
103
- }
104
- /**
105
- * Creates a flat data array from the passed tree dataset.
106
- *
107
- * @param {object[]} dataset - The source dataset of data items.
108
- * @param {string} expandField - The field which points to the expanded value of each data item.
109
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
110
- * @returns {object[]} - A collection of the generated data items that are in a flat structure.
111
- */
112
- export var treeToFlat = function (data, expandField, subItemsField) {
113
- var expanded = function (dataItem) { return Boolean(getNestedValue(expandField, dataItem)); };
114
- var hasChildren = function (dataItem) { return Boolean(getNestedValue(subItemsField, dataItem)); };
115
- var getChildren = function (dataItem) {
116
- var items = [];
117
- if (expanded(dataItem) && hasChildren(dataItem)) {
118
- items.push.apply(items, getNestedValue(subItemsField, dataItem));
119
- }
120
- return items;
121
- };
122
- var flatItems = function (dataItem, flatted, level) {
123
- flatted.push(__assign(__assign({}, dataItem), { level: level }));
124
- getChildren(dataItem).forEach(function (root) { return flatItems(root, flatted, level + 1); });
125
- };
126
- var flattedData = [];
127
- data.forEach(function (root) { return flatItems(root, flattedData, 0); });
128
- return flattedData;
129
- };
130
- /**
131
- * Creates a tree from the passed dataset.
132
- *
133
- * @param {object[]} dataset - The source dataset of data items.
134
- * @param {(item: object) => any} getId - A function which will return the id of the data item.
135
- * @param {(item: object) => any} getParentId - A function which will return the data item id of its parent data item.
136
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
137
- * @returns {object[]} - A collection of the generated data items that are structured in a tree.
138
- */
139
- export var createDataTree = function (dataset, getId, getParentId, subItemsField) {
140
- var hashTable = {}, dataTree = [], parentItem;
141
- dataset.forEach(function (dataItem) { return hashTable[getId(dataItem)] = __assign({}, dataItem); });
142
- dataset.forEach(function (dataItem) {
143
- parentItem = hashTable[getParentId(dataItem)];
144
- if (parentItem) {
145
- parentItem[subItemsField] = parentItem[subItemsField] || [];
146
- parentItem[subItemsField].push(hashTable[getId(dataItem)]);
147
- }
148
- else {
149
- dataTree.push(hashTable[getId(dataItem)]);
150
- }
151
- });
152
- return dataTree;
153
- };
154
- /**
155
- * Similar to the `Object.assign` function. Additionally, creates a new array for the subitems.
156
- *
157
- * @param {object} item - The source data item.
158
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
159
- * @param {object} propsToExtend - The props with which the source data item will be extended.
160
- * @returns {object} - The target data item.
161
- */
162
- export var extendDataItem = function (item, subItemsField, propsToExtend) {
163
- return extendDataItemCommon(item, subItemsField, propsToExtend);
164
- };
165
- /**
166
- * Removes the items from the passed `data` which match the passed `condition`.
167
- *
168
- * @param {any[]} data - The data tree.
169
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
170
- * @param {(item: object) => Boolean} condition - A function that will be executed for each data item
171
- * in the tree data and the items for which returns true will be removed.
172
- * @returns {any[]} - The new data tree.
173
- */
174
- export var removeItems = function (data, subItemsField, condition) {
175
- var _a;
176
- var newData = mapTree([(_a = {}, _a[subItemsField] = __spreadArray([], data, true), _a)], subItemsField, function (item) { return removeChild(item, condition, subItemsField); });
177
- return __spreadArray([], newData[0][subItemsField], true);
178
- };
179
- /**
180
- * @hidden
181
- */
182
- var removeChild = function (item, condition, subItemsField) {
183
- var _a;
184
- var subItems = item[subItemsField] || [];
185
- var childIndex = subItems.findIndex(condition);
186
- if (childIndex !== -1) {
187
- var newChildren = __spreadArray([], subItems, true);
188
- newChildren.splice(childIndex, 1);
189
- return __assign(__assign({}, item), (_a = {}, _a[subItemsField] = newChildren, _a));
190
- }
191
- return item;
192
- };
193
- /**
194
- * Changes the `subItems` collection of each data item which matches the passed `condition`.
195
- *
196
- * @param {any[]} data - The data tree.
197
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
198
- * @param {(item: object) => Boolean} condition - A function that will be executed for each data item and
199
- * will return `true` for items that have to change the subitems collection.
200
- * @param {(subItems: object[]) => object[]} change - A function which
201
- * has as a parameter the subitems collection of the matched items and which will return the new subitems collection.
202
- * @returns {any[]} - The new data tree.
203
- */
204
- export var modifySubItems = function (data, subItemsField, condition, change) {
205
- return mapTree(data, subItemsField, function (item) {
206
- var _a;
207
- return condition(item) ? __assign(__assign({}, item), (_a = {}, _a[subItemsField] = change(item[subItemsField] || []), _a)) :
208
- item;
209
- });
210
- };
211
- /**
212
- * Returns the data item path in the tree based on the level parameter.
213
- *
214
- * @param {any[]} tree - The data tree.
215
- * @param {number[]} level - The level of the target tree item.
216
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
217
- * @returns {any[]} - The path of the data item.
218
- */
219
- export var getItemPath = function (tree, level, subItemsField) {
220
- return getItemPathCommon(tree, level, subItemsField);
221
- };
222
- /**
223
- * Moves the targeted item in the tree to another position.
224
- *
225
- * @param {any[]} data - The data tree.
226
- * @param {number[]} target - The level of the target tree item which will be moved.
227
- * @param {number[] | null} destination - The level of the destination tree item where the target item will be moved in.
228
- * If it is null, the target item will be added at the root level.
229
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
230
- * @returns {any[]} - The new data tree.
231
- */
232
- export var moveTreeItem = function (data, target, destination, subItemsField) {
233
- var tree = __spreadArray([], data, true);
234
- var targetItemPath = getItemPath(tree, target, subItemsField);
235
- var targetItem = targetItemPath.pop();
236
- var targetParent = targetItemPath.pop();
237
- var targetItemIndex = target[target.length - 1];
238
- if (destination) {
239
- var destinationItemPath = getItemPath(tree, destination, subItemsField);
240
- var destinationItem = destinationItemPath.pop();
241
- (targetParent ? targetParent[subItemsField] : tree).splice(targetItemIndex, 1);
242
- destinationItem[subItemsField] = destinationItem[subItemsField] || [];
243
- destinationItem[subItemsField].push(targetItem);
244
- }
245
- else {
246
- (targetParent ? targetParent[subItemsField] : tree).splice(targetItemIndex, 1);
247
- tree.push(targetItem);
248
- }
249
- return tree;
250
- };
@@ -1,115 +0,0 @@
1
- import { SortDescriptor, FilterDescriptor, CompositeFilterDescriptor } from '@progress/kendo-data-query';
2
- import { DataItemWrapper } from '../interfaces/DataItemWrapper';
3
- /**
4
- * Orders the specified tree according to the provided sort descriptors.
5
- *
6
- * @param {T[]} data - The data that will be sorted.
7
- * @param {SortDescriptor[]} descriptors - The descriptors by which the data will be sorted.
8
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
9
- * @returns {T[]} - The sorted data.
10
- */
11
- export declare function orderBy(data: any[], descriptors: SortDescriptor[], subItemsField: string): any[];
12
- /**
13
- * Filters the provided data tree according to the specified `Array<FilterDescriptor|CompositeFilterDescriptor>`.
14
- *
15
- * @param {T[]} data - The data that will be filtered.
16
- * @param {FilterDescriptor[]|CompositeFilterDescriptor[]} descriptors - The filter criteria that will be applied.
17
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
18
- * @returns {T[]} - The filtered data.
19
- */
20
- export declare function filterBy(data: any[], descriptors: FilterDescriptor[] | CompositeFilterDescriptor[], subItemsField: string): any[];
21
- /**
22
- * Creates a new array with the results of calling the provided callback function
23
- * on every element in the provided data tree.
24
- *
25
- * @param {any[]} tree - The data tree.
26
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
27
- * @param {(value: any) => any} callback - The callback function.
28
- * @returns {any[]} - The new data tree.
29
- */
30
- export declare const mapTree: (tree: any[], subItemsField: string, callback: (value: any) => any) => any[];
31
- /**
32
- * Creates a new array with the results of calling the provided callback function
33
- * on the element which match the `level` in the provided data tree.
34
- *
35
- * @param {any[]} tree - The data tree.
36
- * @param {number[]} level - An array of indexes of each parent and current item in the data tree.
37
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
38
- * @param {(value: any) => any} callback - The callback function.
39
- * @returns {any[]} - The new data tree.
40
- */
41
- export declare const mapTreeItem: (tree: any[], level: number[], subItemsField: string, callback: (value: any) => any) => void;
42
- /**
43
- * @hidden
44
- */
45
- export declare function flatData(data: any[], getChildren: (dataItem: any) => any[], itemMap: (item: any) => any): DataItemWrapper[];
46
- /**
47
- * Creates a flat data array from the passed tree dataset.
48
- *
49
- * @param {object[]} dataset - The source dataset of data items.
50
- * @param {string} expandField - The field which points to the expanded value of each data item.
51
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
52
- * @returns {object[]} - A collection of the generated data items that are in a flat structure.
53
- */
54
- export declare const treeToFlat: (data: any[], expandField: string, subItemsField: string) => any[];
55
- /**
56
- * Creates a tree from the passed dataset.
57
- *
58
- * @param {object[]} dataset - The source dataset of data items.
59
- * @param {(item: object) => any} getId - A function which will return the id of the data item.
60
- * @param {(item: object) => any} getParentId - A function which will return the data item id of its parent data item.
61
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
62
- * @returns {object[]} - A collection of the generated data items that are structured in a tree.
63
- */
64
- export declare const createDataTree: (dataset: any[], getId: (item: any) => any, getParentId: (item: any) => any, subItemsField: string) => any[];
65
- /**
66
- * Similar to the `Object.assign` function. Additionally, creates a new array for the subitems.
67
- *
68
- * @param {object} item - The source data item.
69
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
70
- * @param {object} propsToExtend - The props with which the source data item will be extended.
71
- * @returns {object} - The target data item.
72
- */
73
- export declare const extendDataItem: (item: any, subItemsField: string, propsToExtend?: any) => any;
74
- /**
75
- * Removes the items from the passed `data` which match the passed `condition`.
76
- *
77
- * @param {any[]} data - The data tree.
78
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
79
- * @param {(item: object) => Boolean} condition - A function that will be executed for each data item
80
- * in the tree data and the items for which returns true will be removed.
81
- * @returns {any[]} - The new data tree.
82
- */
83
- export declare const removeItems: (data: any[], subItemsField: string, condition: (item: any) => boolean) => any[];
84
- /**
85
- * Changes the `subItems` collection of each data item which matches the passed `condition`.
86
- *
87
- * @param {any[]} data - The data tree.
88
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
89
- * @param {(item: object) => Boolean} condition - A function that will be executed for each data item and
90
- * will return `true` for items that have to change the subitems collection.
91
- * @param {(subItems: object[]) => object[]} change - A function which
92
- * has as a parameter the subitems collection of the matched items and which will return the new subitems collection.
93
- * @returns {any[]} - The new data tree.
94
- */
95
- export declare const modifySubItems: (data: any[], subItemsField: string, condition: (item: any) => boolean, change: (subItems: any[]) => any[]) => any[];
96
- /**
97
- * Returns the data item path in the tree based on the level parameter.
98
- *
99
- * @param {any[]} tree - The data tree.
100
- * @param {number[]} level - The level of the target tree item.
101
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
102
- * @returns {any[]} - The path of the data item.
103
- */
104
- export declare const getItemPath: (tree: any[], level: number[], subItemsField?: string) => any[];
105
- /**
106
- * Moves the targeted item in the tree to another position.
107
- *
108
- * @param {any[]} data - The data tree.
109
- * @param {number[]} target - The level of the target tree item which will be moved.
110
- * @param {number[] | null} destination - The level of the destination tree item where the target item will be moved in.
111
- * If it is null, the target item will be added at the root level.
112
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
113
- * @returns {any[]} - The new data tree.
114
- */
115
- export declare const moveTreeItem: (data: any[], target: number[], destination: number[] | null, subItemsField: string) => any[];
@@ -1,265 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
- if (ar || !(i in from)) {
16
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
- ar[i] = from[i];
18
- }
19
- }
20
- return to.concat(ar || Array.prototype.slice.call(from));
21
- };
22
- Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.moveTreeItem = exports.getItemPath = exports.modifySubItems = exports.removeItems = exports.extendDataItem = exports.createDataTree = exports.treeToFlat = exports.flatData = exports.mapTreeItem = exports.mapTree = exports.filterBy = exports.orderBy = void 0;
24
- var index_1 = require("./index");
25
- var kendo_react_data_tools_1 = require("@progress/kendo-react-data-tools");
26
- var kendo_react_common_1 = require("@progress/kendo-react-common");
27
- /**
28
- * Orders the specified tree according to the provided sort descriptors.
29
- *
30
- * @param {T[]} data - The data that will be sorted.
31
- * @param {SortDescriptor[]} descriptors - The descriptors by which the data will be sorted.
32
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
33
- * @returns {T[]} - The sorted data.
34
- */
35
- function orderBy(data, descriptors, subItemsField) {
36
- return (0, kendo_react_data_tools_1.orderBy)(data, descriptors, subItemsField);
37
- }
38
- exports.orderBy = orderBy;
39
- /**
40
- * Filters the provided data tree according to the specified `Array<FilterDescriptor|CompositeFilterDescriptor>`.
41
- *
42
- * @param {T[]} data - The data that will be filtered.
43
- * @param {FilterDescriptor[]|CompositeFilterDescriptor[]} descriptors - The filter criteria that will be applied.
44
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
45
- * @returns {T[]} - The filtered data.
46
- */
47
- function filterBy(data, descriptors, subItemsField) {
48
- return (0, kendo_react_data_tools_1.filterBy)(data, descriptors, subItemsField);
49
- }
50
- exports.filterBy = filterBy;
51
- /**
52
- * Creates a new array with the results of calling the provided callback function
53
- * on every element in the provided data tree.
54
- *
55
- * @param {any[]} tree - The data tree.
56
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
57
- * @param {(value: any) => any} callback - The callback function.
58
- * @returns {any[]} - The new data tree.
59
- */
60
- var mapTree = function (tree, subItemsField, callback) {
61
- return (0, kendo_react_common_1.mapTree)(tree, subItemsField, callback);
62
- };
63
- exports.mapTree = mapTree;
64
- /**
65
- * Creates a new array with the results of calling the provided callback function
66
- * on the element which match the `level` in the provided data tree.
67
- *
68
- * @param {any[]} tree - The data tree.
69
- * @param {number[]} level - An array of indexes of each parent and current item in the data tree.
70
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
71
- * @param {(value: any) => any} callback - The callback function.
72
- * @returns {any[]} - The new data tree.
73
- */
74
- var mapTreeItem = function (tree, level, subItemsField, callback) {
75
- return (0, kendo_react_common_1.mapTreeItem)(tree, level, subItemsField, callback);
76
- };
77
- exports.mapTreeItem = mapTreeItem;
78
- /**
79
- * @hidden
80
- */
81
- function flatData(data, getChildren, itemMap) {
82
- var stack = [];
83
- var flatted = [];
84
- // we start from end as push() improves performance by up to 50%
85
- for (var i = data.length - 1; i >= 0; i--) {
86
- var root = data[i];
87
- stack.push({
88
- root: root,
89
- data: getChildren(root),
90
- level: [i],
91
- levelCount: data.length
92
- });
93
- }
94
- while (stack.length > 0) {
95
- var stackItem = stack.pop();
96
- flatted.push(__assign(__assign({ dataItem: stackItem.root, level: __spreadArray([], stackItem.level, true) }, itemMap(stackItem.root)), { levelCount: stackItem.levelCount }));
97
- if (stackItem.data.length) {
98
- for (var i = stackItem.data.length - 1; i >= 0; i--) {
99
- var root = stackItem.data[i];
100
- stack.push({
101
- root: root,
102
- data: getChildren(root),
103
- level: __spreadArray(__spreadArray([], stackItem.level, true), [i], false),
104
- levelCount: stackItem.data.length
105
- });
106
- }
107
- }
108
- }
109
- return flatted;
110
- }
111
- exports.flatData = flatData;
112
- /**
113
- * Creates a flat data array from the passed tree dataset.
114
- *
115
- * @param {object[]} dataset - The source dataset of data items.
116
- * @param {string} expandField - The field which points to the expanded value of each data item.
117
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
118
- * @returns {object[]} - A collection of the generated data items that are in a flat structure.
119
- */
120
- var treeToFlat = function (data, expandField, subItemsField) {
121
- var expanded = function (dataItem) { return Boolean((0, index_1.getNestedValue)(expandField, dataItem)); };
122
- var hasChildren = function (dataItem) { return Boolean((0, index_1.getNestedValue)(subItemsField, dataItem)); };
123
- var getChildren = function (dataItem) {
124
- var items = [];
125
- if (expanded(dataItem) && hasChildren(dataItem)) {
126
- items.push.apply(items, (0, index_1.getNestedValue)(subItemsField, dataItem));
127
- }
128
- return items;
129
- };
130
- var flatItems = function (dataItem, flatted, level) {
131
- flatted.push(__assign(__assign({}, dataItem), { level: level }));
132
- getChildren(dataItem).forEach(function (root) { return flatItems(root, flatted, level + 1); });
133
- };
134
- var flattedData = [];
135
- data.forEach(function (root) { return flatItems(root, flattedData, 0); });
136
- return flattedData;
137
- };
138
- exports.treeToFlat = treeToFlat;
139
- /**
140
- * Creates a tree from the passed dataset.
141
- *
142
- * @param {object[]} dataset - The source dataset of data items.
143
- * @param {(item: object) => any} getId - A function which will return the id of the data item.
144
- * @param {(item: object) => any} getParentId - A function which will return the data item id of its parent data item.
145
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
146
- * @returns {object[]} - A collection of the generated data items that are structured in a tree.
147
- */
148
- var createDataTree = function (dataset, getId, getParentId, subItemsField) {
149
- var hashTable = {}, dataTree = [], parentItem;
150
- dataset.forEach(function (dataItem) { return hashTable[getId(dataItem)] = __assign({}, dataItem); });
151
- dataset.forEach(function (dataItem) {
152
- parentItem = hashTable[getParentId(dataItem)];
153
- if (parentItem) {
154
- parentItem[subItemsField] = parentItem[subItemsField] || [];
155
- parentItem[subItemsField].push(hashTable[getId(dataItem)]);
156
- }
157
- else {
158
- dataTree.push(hashTable[getId(dataItem)]);
159
- }
160
- });
161
- return dataTree;
162
- };
163
- exports.createDataTree = createDataTree;
164
- /**
165
- * Similar to the `Object.assign` function. Additionally, creates a new array for the subitems.
166
- *
167
- * @param {object} item - The source data item.
168
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
169
- * @param {object} propsToExtend - The props with which the source data item will be extended.
170
- * @returns {object} - The target data item.
171
- */
172
- var extendDataItem = function (item, subItemsField, propsToExtend) {
173
- return (0, kendo_react_common_1.extendDataItem)(item, subItemsField, propsToExtend);
174
- };
175
- exports.extendDataItem = extendDataItem;
176
- /**
177
- * Removes the items from the passed `data` which match the passed `condition`.
178
- *
179
- * @param {any[]} data - The data tree.
180
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
181
- * @param {(item: object) => Boolean} condition - A function that will be executed for each data item
182
- * in the tree data and the items for which returns true will be removed.
183
- * @returns {any[]} - The new data tree.
184
- */
185
- var removeItems = function (data, subItemsField, condition) {
186
- var _a;
187
- var newData = (0, exports.mapTree)([(_a = {}, _a[subItemsField] = __spreadArray([], data, true), _a)], subItemsField, function (item) { return removeChild(item, condition, subItemsField); });
188
- return __spreadArray([], newData[0][subItemsField], true);
189
- };
190
- exports.removeItems = removeItems;
191
- /**
192
- * @hidden
193
- */
194
- var removeChild = function (item, condition, subItemsField) {
195
- var _a;
196
- var subItems = item[subItemsField] || [];
197
- var childIndex = subItems.findIndex(condition);
198
- if (childIndex !== -1) {
199
- var newChildren = __spreadArray([], subItems, true);
200
- newChildren.splice(childIndex, 1);
201
- return __assign(__assign({}, item), (_a = {}, _a[subItemsField] = newChildren, _a));
202
- }
203
- return item;
204
- };
205
- /**
206
- * Changes the `subItems` collection of each data item which matches the passed `condition`.
207
- *
208
- * @param {any[]} data - The data tree.
209
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
210
- * @param {(item: object) => Boolean} condition - A function that will be executed for each data item and
211
- * will return `true` for items that have to change the subitems collection.
212
- * @param {(subItems: object[]) => object[]} change - A function which
213
- * has as a parameter the subitems collection of the matched items and which will return the new subitems collection.
214
- * @returns {any[]} - The new data tree.
215
- */
216
- var modifySubItems = function (data, subItemsField, condition, change) {
217
- return (0, exports.mapTree)(data, subItemsField, function (item) {
218
- var _a;
219
- return condition(item) ? __assign(__assign({}, item), (_a = {}, _a[subItemsField] = change(item[subItemsField] || []), _a)) :
220
- item;
221
- });
222
- };
223
- exports.modifySubItems = modifySubItems;
224
- /**
225
- * Returns the data item path in the tree based on the level parameter.
226
- *
227
- * @param {any[]} tree - The data tree.
228
- * @param {number[]} level - The level of the target tree item.
229
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
230
- * @returns {any[]} - The path of the data item.
231
- */
232
- var getItemPath = function (tree, level, subItemsField) {
233
- return (0, kendo_react_common_1.getItemPath)(tree, level, subItemsField);
234
- };
235
- exports.getItemPath = getItemPath;
236
- /**
237
- * Moves the targeted item in the tree to another position.
238
- *
239
- * @param {any[]} data - The data tree.
240
- * @param {number[]} target - The level of the target tree item which will be moved.
241
- * @param {number[] | null} destination - The level of the destination tree item where the target item will be moved in.
242
- * If it is null, the target item will be added at the root level.
243
- * @param {string} subItemsField - The field which points to the subitems collection of each data item.
244
- * @returns {any[]} - The new data tree.
245
- */
246
- var moveTreeItem = function (data, target, destination, subItemsField) {
247
- var tree = __spreadArray([], data, true);
248
- var targetItemPath = (0, exports.getItemPath)(tree, target, subItemsField);
249
- var targetItem = targetItemPath.pop();
250
- var targetParent = targetItemPath.pop();
251
- var targetItemIndex = target[target.length - 1];
252
- if (destination) {
253
- var destinationItemPath = (0, exports.getItemPath)(tree, destination, subItemsField);
254
- var destinationItem = destinationItemPath.pop();
255
- (targetParent ? targetParent[subItemsField] : tree).splice(targetItemIndex, 1);
256
- destinationItem[subItemsField] = destinationItem[subItemsField] || [];
257
- destinationItem[subItemsField].push(targetItem);
258
- }
259
- else {
260
- (targetParent ? targetParent[subItemsField] : tree).splice(targetItemIndex, 1);
261
- tree.push(targetItem);
262
- }
263
- return tree;
264
- };
265
- exports.moveTreeItem = moveTreeItem;