aio-entity 1.0.5 → 2.0.1

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.
Files changed (3) hide show
  1. package/index.d.ts +124 -26
  2. package/index.js +236 -138
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -24,6 +24,7 @@ export type I_useEntity<T> = {
24
24
  onChange?: (data: T[]) => void;
25
25
  };
26
26
  export declare const useEntity: <T>(p: I_useEntity<T>) => {
27
+ methods: EntityClass<T>;
27
28
  data: T[];
28
29
  changeData: (data: T[]) => Promise<boolean>;
29
30
  id: {
@@ -38,28 +39,18 @@ export declare const useEntity: <T>(p: I_useEntity<T>) => {
38
39
  mergeToNewRow: (rowIds: (string | number)[], newRow: T, newRowParentId?: string | number) => Promise<boolean>;
39
40
  moveTo: (fromIds: (string | number)[], toId: string | number) => Promise<boolean>;
40
41
  editChilds: (rows: T[], callback: (row: T) => T) => Promise<boolean>;
42
+ changeOrderByDir: (row: T, dir: 1 | -1) => Promise<boolean>;
41
43
  duplicate: (row: T) => Promise<string | number | false>;
42
- filterRows: (p: {
43
- rows: T[];
44
- getResult: (row: T, detail: I_detail) => boolean;
45
- }, parentIds?: (string | number)[]) => T[];
46
- search: (p: {
47
- rows: T[];
48
- getResult: (row: T, detail: I_detail) => boolean;
49
- }) => T[];
44
+ filterRows: (rows: T[], getResult: (row: T, detail: I_detail) => boolean, parentIds?: (string | number)[]) => T[];
45
+ search: (rows: T[], getResult: (row: T, detail: I_detail) => boolean) => T[];
50
46
  getLeafs: (rows: T[]) => T[];
51
47
  getSortedData: (rows: T[], sorts: I_sort<T>[]) => T[];
52
48
  sort: (params: {
53
49
  sorts: I_sort<T>[];
54
50
  row?: T;
55
51
  }) => Promise<false | T[]>;
56
- getNodes: <R = any>(params: {
57
- rows: T[];
58
- getValue: (row: T, detail: I_detail) => false | R;
59
- }) => R[];
60
- getRowAndParentsById: (p: {
61
- rowId: string | number;
62
- }) => {
52
+ getNodes: <R = any>(data: T[], getValue: (row: T, detail: I_detail) => false | R) => R[];
53
+ getRowAndParentsById: (rowId: string | number) => {
63
54
  row?: T;
64
55
  parents: T[];
65
56
  };
@@ -70,18 +61,69 @@ export declare const useEntity: <T>(p: I_useEntity<T>) => {
70
61
  get: () => any;
71
62
  set: () => any;
72
63
  };
73
- getReport: (p: {
74
- rows: T[];
75
- getValue: (row: T, detail: I_detail) => {
76
- [key: string]: any[] | number;
77
- } | false | undefined;
78
- getGroup?: (row: T) => string[];
79
- }) => any;
80
- getParentByRowId: (p: {
81
- rowId: any;
82
- rows: T[];
83
- }) => T;
64
+ getReport: (rows: T[], getValue: (row: T, detail: I_detail) => false | {
65
+ [key: string]: number | any[];
66
+ }, getGroup?: (row: T) => string[]) => any;
67
+ getParentByRowId: (data: T[], rowId: string | number) => T;
84
68
  };
69
+ type I_EntityClass<T> = {
70
+ childs?: {
71
+ get: (row: T) => (T[] | void);
72
+ set: (row: T, childs: T[] | undefined) => T;
73
+ };
74
+ id: {
75
+ get: (row: T) => string | number;
76
+ set: (row: T) => T;
77
+ };
78
+ };
79
+ declare class EntityClass<T> {
80
+ params: I_EntityClass<T>;
81
+ constructor(p: I_EntityClass<T>);
82
+ private getRowAndParentsById_req;
83
+ private addToParent;
84
+ private editByParent;
85
+ private removeChild;
86
+ private walk;
87
+ private editChilds_req;
88
+ private replace;
89
+ filterRows: (rows: T[], getResult: (row: T, detail: I_detail) => boolean, parentIds?: (string | number)[]) => T[];
90
+ updateReport: (values: any, groups: string[], dic: any) => void;
91
+ getReport: (rows: T[], getValue: (row: T, detail: I_detail) => false | {
92
+ [key: string]: number | any[];
93
+ }, getGroup?: (row: T) => string[]) => any;
94
+ add: (data: T[], row: T, parentId?: string | number) => {
95
+ data: T[];
96
+ addedId?: string | number;
97
+ };
98
+ editRoot: (rows: T[], row: T) => T[];
99
+ edit: (data: T[], row: T) => T[];
100
+ remove: (data: T[], rowId: string | number) => T[];
101
+ removeByIds: (data: T[], rowIds: (string | number)[]) => T[];
102
+ mergeToExistRow: (data: T[], rowIds: (string | number)[], parentId: string | number) => T[];
103
+ mergeToNewRow: (data: T[], rowIds: (string | number)[], newRow: T, newRowParentId?: string | number) => T[];
104
+ getNodes: <R = any>(data: T[], getValue: (row: T, detail: I_detail) => false | R) => R[];
105
+ getChilds: (row: T) => void | T[];
106
+ getRowAndParentsById: (data: T[], rowId: string | number) => {
107
+ row?: T;
108
+ parents: T[];
109
+ };
110
+ getRowsByIds: (data: T[], rowIds: (string | number)[]) => T[];
111
+ getParentByRowId: (data: T[], rowId: string | number) => T | undefined;
112
+ private moveToUnit;
113
+ moveTo: (data: T[], fromIds: (string | number)[], toId: string | number) => T[];
114
+ editChilds: (rows: T[], callback: (row: T) => T) => T[];
115
+ search: (rows: T[], getResult: (row: T, detail: I_detail) => boolean) => T[];
116
+ getParentById: (rows: T[], rowId: string | number) => false | T;
117
+ replaceById: (rows: T[], fromId: string | number, toId: string | number) => T[];
118
+ changeOrderByDir: (rows: T[], row: T, dir: 1 | -1) => T[];
119
+ changeOrderByIndex: (rows: T[], parent: T, fromIndex: number, toIndex: number) => T[];
120
+ duplicate: (data: T[], row: T) => {
121
+ data: T[];
122
+ addedId?: string | number;
123
+ };
124
+ getLeafs: (rows: T[]) => T[];
125
+ getSortedData: (rows: T[], sorts: I_sort<T>[]) => T[];
126
+ }
85
127
  export declare const useFilter: <T, F>(p: {
86
128
  getFilteredRows: (rows: T[], filter: F) => T[];
87
129
  getDefaultFilter: () => F;
@@ -95,4 +137,60 @@ export declare const useFilter: <T, F>(p: {
95
137
  updateFilteredRows: (rows: T[], pFilter?: F) => void;
96
138
  report: any;
97
139
  };
140
+ export declare const useEntityFilter: <T, F>(p: {
141
+ entity: {
142
+ methods: EntityClass<T>;
143
+ data: T[];
144
+ changeData: (data: T[]) => Promise<boolean>;
145
+ id: {
146
+ get: (row: T) => string | number;
147
+ set: (row: T) => T;
148
+ };
149
+ add: (row: T, parentId?: string | number) => Promise<number | string | false>;
150
+ edit: (row: T) => Promise<boolean>;
151
+ remove: (rowId: string | number) => Promise<boolean>;
152
+ removeByIds: (rowIds: (string | number)[]) => Promise<boolean>;
153
+ mergeToExistRow: (rowIds: (string | number)[], parentId: string | number) => Promise<boolean>;
154
+ mergeToNewRow: (rowIds: (string | number)[], newRow: T, newRowParentId?: string | number) => Promise<boolean>;
155
+ moveTo: (fromIds: (string | number)[], toId: string | number) => Promise<boolean>;
156
+ editChilds: (rows: T[], callback: (row: T) => T) => Promise<boolean>;
157
+ changeOrderByDir: (row: T, dir: 1 | -1) => Promise<boolean>;
158
+ duplicate: (row: T) => Promise<string | number | false>;
159
+ filterRows: (rows: T[], getResult: (row: T, detail: I_detail) => boolean, parentIds?: (string | number)[]) => T[];
160
+ search: (rows: T[], getResult: (row: T, detail: I_detail) => boolean) => T[];
161
+ getLeafs: (rows: T[]) => T[];
162
+ getSortedData: (rows: T[], sorts: I_sort<T>[]) => T[];
163
+ sort: (params: {
164
+ sorts: I_sort<T>[];
165
+ row?: T;
166
+ }) => Promise<false | T[]>;
167
+ getNodes: <R = any>(data: T[], getValue: (row: T, detail: I_detail) => false | R) => R[];
168
+ getRowAndParentsById: (rowId: string | number) => {
169
+ row?: T;
170
+ parents: T[];
171
+ };
172
+ childs: {
173
+ get: (row: T) => void | T[];
174
+ set: (row: T, childs: T[]) => T;
175
+ } | {
176
+ get: () => any;
177
+ set: () => any;
178
+ };
179
+ getReport: (rows: T[], getValue: (row: T, detail: I_detail) => false | {
180
+ [key: string]: number | any[];
181
+ }, getGroup?: (row: T) => string[]) => any;
182
+ getParentByRowId: (data: T[], rowId: string | number) => T;
183
+ };
184
+ defaultFilter: F;
185
+ getResult: (row: T, detail: I_detail, filter: F) => boolean;
186
+ getReport?: (row: T, detail: I_detail, filter: F) => false | {
187
+ [key: string]: number | any[];
188
+ };
189
+ defaultReport?: any;
190
+ }) => {
191
+ filter: F;
192
+ changeFilter: (p: Partial<F>) => void;
193
+ data: T[];
194
+ report: any;
195
+ };
98
196
  export {};
package/index.js CHANGED
@@ -33,31 +33,33 @@ export const useEntity = (p) => {
33
33
  return res;
34
34
  });
35
35
  return {
36
+ methods: entity,
36
37
  data, changeData, id: p.id,
37
38
  add: (row, parentId) => __awaiter(void 0, void 0, void 0, function* () {
38
- const { rows, addedId } = entity.add({ row, parentId, rows: dataRef.current });
39
+ const { data, addedId } = entity.add(dataRef.current, row, parentId);
39
40
  if (addedId === undefined) {
40
41
  return false;
41
42
  }
42
- const res = yield changeData(rows);
43
+ const res = yield changeData(data);
43
44
  if (!res) {
44
45
  return false;
45
46
  }
46
47
  return addedId;
47
48
  }),
48
- edit: (row) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.edit({ row, rows: dataRef.current })); }),
49
- remove: (rowId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.remove({ rowId, data: dataRef.current })); }),
50
- removeByIds: (rowIds) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.removeByIds({ rowIds, data: dataRef.current })); }),
51
- mergeToExistRow: (rowIds, parentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToExistRow({ rowIds, rows: dataRef.current, parentId })); }),
52
- mergeToNewRow: (rowIds, newRow, newRowParentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToNewRow({ rowIds, data: dataRef.current, newRow, newRowParentId })); }),
53
- moveTo: (fromIds, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo({ fromIds, toId, data: dataRef.current })); }),
54
- editChilds: (rows, callback) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.editChilds({ rows, callback })); }),
49
+ edit: (row) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.edit(dataRef.current, row)); }),
50
+ remove: (rowId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.remove(dataRef.current, rowId)); }),
51
+ removeByIds: (rowIds) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.removeByIds(dataRef.current, rowIds)); }),
52
+ mergeToExistRow: (rowIds, parentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToExistRow(dataRef.current, rowIds, parentId)); }),
53
+ mergeToNewRow: (rowIds, newRow, newRowParentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToNewRow(dataRef.current, rowIds, newRow, newRowParentId)); }),
54
+ moveTo: (fromIds, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo(dataRef.current, fromIds, toId)); }),
55
+ editChilds: (rows, callback) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.editChilds(rows, callback)); }),
56
+ changeOrderByDir: (row, dir) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.changeOrderByDir(dataRef.current, row, dir)); }),
55
57
  duplicate: (row) => __awaiter(void 0, void 0, void 0, function* () {
56
- const { rows, addedId } = entity.duplicate({ rows: dataRef.current, row });
58
+ const { data, addedId } = entity.duplicate(dataRef.current, row);
57
59
  if (addedId === undefined) {
58
60
  return false;
59
61
  }
60
- const res = yield changeData(rows);
62
+ const res = yield changeData(data);
61
63
  if (!res) {
62
64
  return false;
63
65
  }
@@ -68,8 +70,7 @@ export const useEntity = (p) => {
68
70
  getLeafs: (rows) => entity.getLeafs(rows),
69
71
  getSortedData: entity.getSortedData,
70
72
  sort: (params) => __awaiter(void 0, void 0, void 0, function* () {
71
- const { childs: c } = p;
72
- const { row, sorts } = params;
73
+ const { childs: c } = p, { row, sorts } = params;
73
74
  let newData = dataRef.current;
74
75
  if (row) {
75
76
  if (!c) {
@@ -81,7 +82,7 @@ export const useEntity = (p) => {
81
82
  }
82
83
  const newChilds = entity.getSortedData(childs, sorts);
83
84
  const newRow = c.set(row, newChilds);
84
- newData = entity.edit({ rows: dataRef.current, row: newRow });
85
+ newData = entity.edit(dataRef.current, newRow);
85
86
  }
86
87
  else {
87
88
  newData = entity.getSortedData(dataRef.current, sorts);
@@ -90,9 +91,9 @@ export const useEntity = (p) => {
90
91
  return res ? newData : false;
91
92
  }),
92
93
  getNodes: entity.getNodes,
93
- getRowAndParentsById: (p) => entity.getRowAndParentsById(Object.assign(Object.assign({}, p), { rows: dataRef.current })),
94
+ getRowAndParentsById: (rowId) => entity.getRowAndParentsById(dataRef.current, rowId),
94
95
  childs: p.childs || { get: () => undefined, set: () => { return undefined; } },
95
- getReport: (p) => entity.getReport({ rows: p.rows, getValue: p.getValue, getGroup: p.getGroup }),
96
+ getReport: (rows, getValue, getGroup) => entity.getReport(rows, getValue, getGroup),
96
97
  getParentByRowId: entity.getParentByRowId
97
98
  };
98
99
  };
@@ -115,34 +116,30 @@ class EntityClass {
115
116
  }
116
117
  }
117
118
  };
118
- this.addToParent = (p) => {
119
+ this.addToParent = (parentRow, row) => {
119
120
  const { childs: c } = this.params;
120
- const { parentRow, row } = p;
121
121
  if (!c) {
122
122
  return row;
123
123
  }
124
- const childs = c.get(parentRow) || [];
124
+ const childs = c.get(parentRow);
125
125
  if (!childs) {
126
126
  return row;
127
127
  }
128
128
  return c.set(parentRow, [...childs, row]) || row;
129
129
  };
130
- this.editByParent = (p) => {
131
- const { parentRow, row } = p;
130
+ this.editByParent = (parentRow, row) => {
132
131
  const { childs: c, id } = this.params;
133
132
  if (!c) {
134
133
  return parentRow;
135
134
  }
136
- const rowId = id.get(row);
137
- const childs = c.get(parentRow);
135
+ const rowId = id.get(row), childs = c.get(parentRow);
138
136
  if (!childs) {
139
137
  return parentRow;
140
138
  }
141
139
  const newChilds = childs.map((o) => id.get(o) === rowId ? row : o);
142
140
  return c.set(parentRow, newChilds) || parentRow;
143
141
  };
144
- this.removeChild = (p) => {
145
- const { parentRow, rowId, data } = p;
142
+ this.removeChild = (data, parentRow, rowId) => {
146
143
  const { childs: c, id } = this.params;
147
144
  if (!c) {
148
145
  return data;
@@ -156,7 +153,7 @@ class EntityClass {
156
153
  if (!res) {
157
154
  return data;
158
155
  }
159
- return this.edit({ row: res, rows: data });
156
+ return this.edit(data, res);
160
157
  };
161
158
  this.walk = (p) => {
162
159
  const { childs: c } = this.params;
@@ -189,7 +186,7 @@ class EntityClass {
189
186
  }
190
187
  for (let i = 0; i < data.length; i++) {
191
188
  const row = data[i];
192
- result.rows = this.edit({ row: callback(row), rows: result.rows });
189
+ result.rows = this.edit(result.rows, callback(row));
193
190
  const childs = c.get(row);
194
191
  if (!childs || !childs.length) {
195
192
  continue;
@@ -197,21 +194,74 @@ class EntityClass {
197
194
  this.editChilds_req(childs, callback, result);
198
195
  }
199
196
  };
200
- this.filterRows = (p, parentIds = []) => {
197
+ this.replace = (rows, fromId, toId, helper) => {
198
+ const { childs: c, id } = this.params;
199
+ if (fromId === toId) {
200
+ return rows;
201
+ }
202
+ if (c) {
203
+ let parentChilds = [];
204
+ let parent = helper && helper.parent ? helper.parent : this.getParentById(rows, fromId);
205
+ if (!parent) {
206
+ return rows;
207
+ }
208
+ if (helper && helper.parentChilds) {
209
+ parentChilds = helper.parentChilds;
210
+ }
211
+ else {
212
+ const res = c.get(parent);
213
+ if (!res) {
214
+ return rows;
215
+ }
216
+ parentChilds = res;
217
+ }
218
+ const { fromIndex = parentChilds.findIndex((o) => id.get(o) === fromId), toIndex = parentChilds.findIndex((o) => id.get(o) === toId) } = helper || {};
219
+ if (fromIndex === -1 || toIndex === -1) {
220
+ return rows;
221
+ }
222
+ const newParentChilds = parentChilds.map((o, i) => {
223
+ if (i === fromIndex) {
224
+ return parentChilds[toIndex];
225
+ }
226
+ if (i === toIndex) {
227
+ return parentChilds[fromIndex];
228
+ }
229
+ return o;
230
+ });
231
+ const newParent = c.set(parent, newParentChilds);
232
+ return this.edit(rows, newParent);
233
+ }
234
+ else {
235
+ const { fromIndex = rows.findIndex((o) => id.get(o) === fromId), toIndex = rows.findIndex((o) => id.get(o) === toId) } = helper || {};
236
+ if (fromIndex === -1 || toIndex === -1) {
237
+ return rows;
238
+ }
239
+ return rows.map((o, i) => {
240
+ if (i === fromIndex) {
241
+ return rows[toIndex];
242
+ }
243
+ if (i === toIndex) {
244
+ return rows[fromIndex];
245
+ }
246
+ return o;
247
+ });
248
+ }
249
+ };
250
+ this.filterRows = (rows, getResult, parentIds) => {
201
251
  const { childs: c, id } = this.params;
202
252
  if (!c) {
203
- return p.rows.filter((o) => p.getResult(o, { _parentIds: [], _childsLength: undefined, _isLeaf: false }));
253
+ return rows.filter((o) => getResult(o, { _parentIds: [], _childsLength: undefined, _isLeaf: false }));
204
254
  }
205
- const res = p.rows.filter((o) => {
255
+ const res = rows.filter((o) => {
206
256
  const childs = c.get(o);
207
257
  const childsLength = childs ? childs.length : undefined;
208
- return p.getResult(o, { _childsLength: childsLength, _isLeaf: childsLength === undefined, _parentIds: parentIds });
258
+ return getResult(o, { _childsLength: childsLength, _isLeaf: childsLength === undefined, _parentIds: parentIds || [] });
209
259
  });
210
260
  return res.map((row) => {
211
261
  const childs = c ? c.get(row) : undefined;
212
262
  if (childs && c) {
213
263
  const rowId = id.get(row);
214
- const newChilds = this.filterRows({ rows: childs, getResult: p.getResult }, parentIds.concat(rowId));
264
+ const newChilds = this.filterRows(childs, getResult, (parentIds || []).concat(rowId));
215
265
  return c.set(row, newChilds);
216
266
  }
217
267
  else {
@@ -244,94 +294,73 @@ class EntityClass {
244
294
  }
245
295
  }
246
296
  };
247
- this.getReport = (p) => {
297
+ this.getReport = (rows, getValue, getGroup) => {
248
298
  const dic = {};
249
- const rows = p.rows;
250
- this.getNodes({
251
- rows,
252
- getValue: (o, detail) => {
253
- const values = p.getValue(o, detail);
254
- if (values === false || values === undefined) {
255
- return values;
256
- }
257
- const groups = p.getGroup ? p.getGroup(o) : [];
258
- this.updateReport(values, groups, dic);
299
+ this.getNodes(rows, (o, detail) => {
300
+ const values = getValue(o, detail);
301
+ if (values === false || values === undefined) {
302
+ return values;
259
303
  }
304
+ const groups = getGroup ? getGroup(o) : [];
305
+ this.updateReport(values, groups, dic);
260
306
  });
261
307
  return dic;
262
308
  };
263
- this.add = (p) => {
264
- const { rows, row, parentId } = p;
309
+ this.add = (data, row, parentId) => {
265
310
  const newRow = this.params.id.set(row);
266
311
  const addedId = this.params.id.get(newRow);
267
312
  if (parentId !== undefined) {
268
- const res = this.getRowAndParentsById({ rowId: parentId, rows });
313
+ const res = this.getRowAndParentsById(data, parentId);
269
314
  if (!res.row) {
270
- return { rows };
315
+ return { data };
271
316
  }
272
- const res1 = this.addToParent({ parentRow: res.row, row: newRow });
273
- const newRows = this.edit({ row: res1, rows });
274
- return { rows: newRows, addedId };
317
+ const res1 = this.addToParent(res.row, newRow);
318
+ const newData = this.edit(data, res1);
319
+ return { data: newData, addedId };
275
320
  }
276
321
  else {
277
- const newRows = [...rows, newRow];
278
- return { rows: newRows, addedId };
322
+ return { data: [...data, newRow], addedId };
279
323
  }
280
324
  };
281
- this.editRoot = (rows, row) => {
282
- const { id } = this.params;
283
- return rows.map((o) => id.get(o) === id.get(row) ? row : o);
284
- };
285
- this.edit = (p) => {
325
+ this.editRoot = (rows, row) => rows.map((o) => this.params.id.get(o) === this.params.id.get(row) ? row : o);
326
+ this.edit = (data, row) => {
286
327
  const { childs: c } = this.params;
287
- const { row, rows } = p;
288
328
  const rowId = this.params.id.get(row);
289
329
  if (!!c) {
290
- let parentRow = this.getParentByRowId({ rowId, rows });
291
- if (parentRow) {
292
- parentRow = this.editByParent({ parentRow, row });
293
- return this.edit({ row: parentRow, rows: p.rows });
294
- }
295
- else {
296
- const res = this.editRoot(rows, row);
297
- console.log(res);
298
- return res;
299
- }
330
+ let parentRow = this.getParentByRowId(data, rowId);
331
+ return parentRow ? this.edit(data, this.editByParent(parentRow, row)) : this.editRoot(data, row);
300
332
  }
301
333
  else {
302
- return this.editRoot(rows, row);
334
+ return this.editRoot(data, row);
303
335
  }
304
336
  };
305
- this.remove = (p) => {
337
+ this.remove = (data, rowId) => {
306
338
  const { id } = this.params;
307
- const { rowId, data } = p;
308
- const parentResult = this.getParentByRowId({ rowId, rows: data });
339
+ const parentResult = this.getParentByRowId(data, rowId);
309
340
  if (parentResult) {
310
- return this.removeChild({ parentRow: parentResult, rowId, data });
341
+ return this.removeChild(data, parentResult, rowId);
311
342
  }
312
343
  else {
313
344
  return data.filter((o) => id.get(o) !== rowId);
314
345
  }
315
346
  };
316
- this.removeByIds = (p) => {
317
- const { rowIds, data } = p;
347
+ this.removeByIds = (data, rowIds) => {
318
348
  let tempRows = [...data];
319
349
  for (let i = 0; i < rowIds.length; i++) {
320
- const rowId = rowIds[i];
321
- tempRows = this.remove({ rowId, data: tempRows });
350
+ tempRows = this.remove(tempRows, rowIds[i]);
322
351
  }
323
352
  return tempRows;
324
353
  };
325
- this.mergeToExistRow = (p) => {
326
- let rows = p.rows;
354
+ this.mergeToExistRow = (data, rowIds, parentId) => {
355
+ let rows = data;
327
356
  if (!this.params.childs) {
328
- return rows;
357
+ return data;
329
358
  }
330
- const { row: parentRow } = this.getRowAndParentsById({ rows: p.rows, rowId: p.parentId });
359
+ const { row: parentRow } = this.getRowAndParentsById(data, parentId);
331
360
  if (!parentRow) {
332
361
  return rows;
333
362
  }
334
- const newChilds = this.getRowsByIds({ rows: p.rows, rowIds: p.rowIds });
363
+ const newChilds = this.getRowsByIds(data, rowIds);
335
364
  if (!newChilds.length) {
336
365
  return rows;
337
366
  }
@@ -339,46 +368,43 @@ class EntityClass {
339
368
  if (!parentChilds) {
340
369
  return rows;
341
370
  }
342
- rows = this.removeByIds({ data: rows, rowIds: p.rowIds });
371
+ rows = this.removeByIds(rows, rowIds);
343
372
  const newParentChilds = [...parentChilds, ...newChilds];
344
373
  const newParentRow = this.params.childs.set(parentRow, newParentChilds);
345
- return this.edit({ rows, row: newParentRow });
374
+ return this.edit(rows, newParentRow);
346
375
  };
347
- this.mergeToNewRow = (p) => {
348
- const { rows, addedId } = this.add({ rows: p.data, row: p.newRow, parentId: p.newRowParentId });
349
- if (addedId === undefined) {
350
- return rows;
351
- }
352
- return this.mergeToExistRow({ rows, rowIds: p.rowIds, parentId: addedId });
376
+ this.mergeToNewRow = (data, rowIds, newRow, newRowParentId) => {
377
+ const addRes = this.add(data, newRow, newRowParentId);
378
+ return addRes.addedId === undefined ? data : this.mergeToExistRow(data, rowIds, addRes.addedId);
353
379
  };
354
- this.getNodes = (params) => {
380
+ this.getNodes = (data, getValue) => {
355
381
  const result = [];
356
- this.walk({ childs: params.rows, getValue: params.getValue, result, parentIds: [] });
382
+ this.walk({ childs: data, getValue, result, parentIds: [] });
357
383
  return result;
358
384
  };
359
385
  this.getChilds = (row) => !this.params.childs ? undefined : this.params.childs.get(row);
360
- this.getRowAndParentsById = (p) => {
386
+ this.getRowAndParentsById = (data, rowId) => {
361
387
  let result = { parents: [] };
362
- this.getRowAndParentsById_req(p.rows, p.rowId, [], result);
388
+ this.getRowAndParentsById_req(data, rowId, [], result);
363
389
  return result;
364
390
  };
365
- this.getRowsByIds = (p) => {
391
+ this.getRowsByIds = (data, rowIds) => {
366
392
  const res = [];
367
393
  const usedIds = [];
368
- for (let i = 0; i < p.rowIds.length; i++) {
369
- const id = p.rowIds[i];
394
+ for (let i = 0; i < rowIds.length; i++) {
395
+ const id = rowIds[i];
370
396
  if (usedIds.indexOf(id) !== -1) {
371
397
  continue;
372
398
  }
373
- const { row } = this.getRowAndParentsById({ rowId: id, rows: p.rows });
399
+ const { row } = this.getRowAndParentsById(data, id);
374
400
  if (row) {
375
401
  res.push(row);
376
402
  }
377
403
  }
378
404
  return res;
379
405
  };
380
- this.getParentByRowId = (p) => {
381
- const res = this.getRowAndParentsById(p);
406
+ this.getParentByRowId = (data, rowId) => {
407
+ const res = this.getRowAndParentsById(data, rowId);
382
408
  if (!res.row) {
383
409
  return;
384
410
  }
@@ -388,71 +414,116 @@ class EntityClass {
388
414
  }
389
415
  return parents[parents.length - 1];
390
416
  };
391
- this.moveToUnit = (p) => {
392
- const { fromId, toId, rows } = p;
393
- let { row: toRow } = this.getRowAndParentsById({ rowId: toId, rows });
417
+ this.moveToUnit = (data, fromId, toId) => {
418
+ let { row: toRow } = this.getRowAndParentsById(data, toId);
394
419
  if (!toRow) {
395
- return p.rows;
420
+ return data;
396
421
  }
397
- let { row: fromRow } = this.getRowAndParentsById({ rowId: fromId, rows });
422
+ let { row: fromRow } = this.getRowAndParentsById(data, fromId);
398
423
  if (!fromRow) {
399
- return p.rows;
424
+ return data;
400
425
  }
401
- const rowsAfterRemove = this.remove({ rowId: fromId, data: rows });
426
+ const rowsAfterRemove = this.remove(data, fromId);
402
427
  //اگر بروز نشه مشکل بوجود میاد
403
- toRow = this.getRowAndParentsById({ rowId: toId, rows: rowsAfterRemove }).row;
428
+ toRow = this.getRowAndParentsById(rowsAfterRemove, toId).row;
404
429
  if (!toRow) {
405
- return p.rows;
430
+ return data;
406
431
  }
407
- const newToRow = this.addToParent({ parentRow: toRow, row: fromRow });
408
- const res = this.edit({ row: newToRow, rows: rowsAfterRemove });
432
+ const newToRow = this.addToParent(toRow, fromRow);
433
+ const res = this.edit(rowsAfterRemove, newToRow);
409
434
  return res;
410
435
  };
411
- this.moveTo = (p) => {
412
- const { fromIds, toId } = p;
413
- let rows = p.data;
414
- for (let i = 0; i < p.fromIds.length; i++) {
415
- const fromId = fromIds[i];
416
- rows = this.moveToUnit({ fromId, toId, rows });
436
+ this.moveTo = (data, fromIds, toId) => {
437
+ let rows = data;
438
+ for (let i = 0; i < fromIds.length; i++) {
439
+ rows = this.moveToUnit(rows, fromIds[i], toId);
417
440
  }
418
441
  return rows;
419
442
  };
420
- this.editChilds = (p) => {
421
- const { rows, callback } = p;
443
+ this.editChilds = (rows, callback) => {
422
444
  const result = { rows };
423
445
  this.editChilds_req(rows, callback, result);
424
446
  return result.rows;
425
447
  };
426
- this.search = (p) => {
448
+ this.search = (rows, getResult) => {
427
449
  const { id } = this.params;
428
450
  const dic = {};
429
- this.getNodes({
430
- rows: p.rows,
431
- getValue: (row, detail) => {
432
- const rowId = id.get(row);
433
- const result = p.getResult(row, detail);
434
- if (!result) {
435
- return;
436
- }
437
- const ids = [...detail._parentIds, rowId];
438
- for (let i = 0; i < ids.length; i++) {
439
- dic['a' + ids[i]] = true;
440
- }
451
+ this.getNodes(rows, (row, detail) => {
452
+ const result = getResult(row, detail);
453
+ if (!result) {
454
+ return;
455
+ }
456
+ const ids = [...detail._parentIds, id.get(row)];
457
+ for (let i = 0; i < ids.length; i++) {
458
+ dic['a' + ids[i]] = true;
441
459
  }
442
460
  });
443
- return this.filterRows({ rows: p.rows, getResult: (row) => !!dic['a' + id.get(row)] });
461
+ return this.filterRows(rows, (row) => !!dic['a' + id.get(row)]);
462
+ };
463
+ this.getParentById = (rows, rowId) => {
464
+ const { parents } = this.getRowAndParentsById(rows, rowId);
465
+ if (!parents.length) {
466
+ return false;
467
+ }
468
+ return parents[parents.length - 1];
469
+ };
470
+ this.replaceById = (rows, fromId, toId) => this.replace(rows, fromId, toId);
471
+ this.changeOrderByDir = (rows, row, dir) => {
472
+ const { childs: c, id } = this.params;
473
+ const rowId = id.get(row);
474
+ let list = [];
475
+ let parentRow;
476
+ if (c) {
477
+ const res = this.getParentById(rows, rowId);
478
+ if (!res) {
479
+ return rows;
480
+ }
481
+ parentRow = res;
482
+ const parentChilds = c.get(parentRow);
483
+ if (!parentChilds) {
484
+ return rows;
485
+ }
486
+ list = parentChilds;
487
+ }
488
+ else {
489
+ list = rows;
490
+ }
491
+ const fromIndex = list.findIndex((o) => id.get(o) === rowId);
492
+ if (dir === 1 && fromIndex === list.length - 1) {
493
+ return rows;
494
+ }
495
+ if (dir === -1 && fromIndex === 0) {
496
+ return rows;
497
+ }
498
+ const toIndex = fromIndex + dir;
499
+ const from = list[fromIndex], to = list[toIndex];
500
+ if (!from || !to) {
501
+ return rows;
502
+ }
503
+ const fromId = id.get(from), toId = id.get(to);
504
+ return this.replace(rows, fromId, toId, { parent: parentRow, parentChilds: list, fromIndex, toIndex });
505
+ };
506
+ this.changeOrderByIndex = (rows, parent, fromIndex, toIndex) => {
507
+ const { childs: c, id } = this.params;
508
+ let parentChilds = c ? c.get(parent) : rows;
509
+ if (!parentChilds) {
510
+ return rows;
511
+ }
512
+ const from = parentChilds[fromIndex];
513
+ const to = parentChilds[toIndex];
514
+ const fromId = id.get(from), toId = id.get(to);
515
+ return this.replace(rows, fromId, toId, { parent, parentChilds, fromIndex, toIndex });
444
516
  };
445
- this.duplicate = (p) => {
517
+ this.duplicate = (data, row) => {
446
518
  const { id } = this.params;
447
- const { rows, row } = p;
448
519
  const rowId = id.get(row);
449
- const parent = this.getParentByRowId({ rowId, rows });
520
+ const parent = this.getParentByRowId(data, rowId);
450
521
  const parentId = parent ? id.get(parent) : undefined;
451
522
  const newRow = id.set(row);
452
- return this.add({ row: newRow, parentId, rows });
523
+ return this.add(data, newRow, parentId);
453
524
  };
454
525
  this.getLeafs = (rows) => {
455
- const res = this.getReport({ rows, getValue: (o, detail) => detail._isLeaf ? { rows: [o] } : undefined, });
526
+ const res = this.getReport(rows, (o, detail) => detail._isLeaf ? { rows: [o] } : undefined);
456
527
  return res.rows || [];
457
528
  };
458
529
  this.getSortedData = (rows, sorts) => {
@@ -497,3 +568,30 @@ export const useFilter = (p) => {
497
568
  useEffect(() => { updateFilteredRows(p.rows); }, [filter, p.rows]);
498
569
  return { filter, changeFilter, data: filteredDataRef.current, updateFilteredRows, report };
499
570
  };
571
+ export const useEntityFilter = (p) => {
572
+ const [filter, setFilter] = useState(p.defaultFilter);
573
+ const filterRef = useRef(filter);
574
+ filterRef.current = filter;
575
+ const [data, setData] = useState([]);
576
+ const [report, setReport] = useState(p.defaultReport);
577
+ const filteredDataRef = useRef([]);
578
+ filteredDataRef.current = data;
579
+ const changeFilter = (p) => setFilter(Object.assign(Object.assign({}, filter), p));
580
+ const updateFilteredRows = (rows) => {
581
+ Debounce(() => {
582
+ const res = p.entity.search(rows, (row, detail) => p.getResult(row, detail, filter));
583
+ setData(res);
584
+ updateReport(res);
585
+ filteredDataRef.current = res;
586
+ }, 50);
587
+ };
588
+ const updateReport = (data) => {
589
+ if (!p.getReport) {
590
+ return;
591
+ }
592
+ const res = p.entity.getReport(data, (row, detail) => p.getReport(row, detail, filter));
593
+ setReport(res);
594
+ };
595
+ useEffect(() => { updateFilteredRows(p.entity.data); }, [filter, p.entity.data]);
596
+ return { filter, changeFilter, data, report };
597
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-entity",
3
- "version": "1.0.5",
3
+ "version": "2.0.1",
4
4
  "description": "kit",
5
5
  "main": "index.js",
6
6
  "scripts": {