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