aio-entity 2.0.0 → 2.0.2

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 +58 -10
  2. package/index.js +108 -0
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -39,6 +39,7 @@ export declare const useEntity: <T>(p: I_useEntity<T>) => {
39
39
  mergeToNewRow: (rowIds: (string | number)[], newRow: T, newRowParentId?: string | number) => Promise<boolean>;
40
40
  moveTo: (fromIds: (string | number)[], toId: string | number) => Promise<boolean>;
41
41
  editChilds: (rows: T[], callback: (row: T) => T) => Promise<boolean>;
42
+ changeOrderByDir: (row: T, dir: 1 | -1) => Promise<boolean>;
42
43
  duplicate: (row: T) => Promise<string | number | false>;
43
44
  filterRows: (rows: T[], getResult: (row: T, detail: I_detail) => boolean, parentIds?: (string | number)[]) => T[];
44
45
  search: (rows: T[], getResult: (row: T, detail: I_detail) => boolean) => T[];
@@ -60,9 +61,9 @@ export declare const useEntity: <T>(p: I_useEntity<T>) => {
60
61
  get: () => any;
61
62
  set: () => any;
62
63
  };
63
- getReport: (rows: T[], getValue: (row: T, detail: I_detail) => {
64
- [key: string]: any[] | number;
65
- } | false | undefined, getGroup?: (row: T) => string[]) => any;
64
+ getReport: (rows: T[], getValue: (row: T, detail: I_detail) => false | {
65
+ [key: string]: number | any[];
66
+ }, getGroup?: (row: T) => string[]) => any;
66
67
  getParentByRowId: (data: T[], rowId: string | number) => T;
67
68
  };
68
69
  type I_EntityClass<T> = {
@@ -84,11 +85,12 @@ declare class EntityClass<T> {
84
85
  private removeChild;
85
86
  private walk;
86
87
  private editChilds_req;
88
+ private replace;
87
89
  filterRows: (rows: T[], getResult: (row: T, detail: I_detail) => boolean, parentIds?: (string | number)[]) => T[];
88
90
  updateReport: (values: any, groups: string[], dic: any) => void;
89
- getReport: (rows: T[], getValue: (row: T, detail: I_detail) => {
90
- [key: string]: any[] | number;
91
- } | false | undefined, getGroup?: (row: T) => string[]) => any;
91
+ getReport: (rows: T[], getValue: (row: T, detail: I_detail) => false | {
92
+ [key: string]: number | any[];
93
+ }, getGroup?: (row: T) => string[]) => any;
92
94
  add: (data: T[], row: T, parentId?: string | number) => {
93
95
  data: T[];
94
96
  addedId?: string | number;
@@ -99,7 +101,7 @@ declare class EntityClass<T> {
99
101
  removeByIds: (data: T[], rowIds: (string | number)[]) => T[];
100
102
  mergeToExistRow: (data: T[], rowIds: (string | number)[], parentId: string | number) => T[];
101
103
  mergeToNewRow: (data: T[], rowIds: (string | number)[], newRow: T, newRowParentId?: string | number) => T[];
102
- getNodes: <R = any>(data: T[], getValue: (row: T, detail: I_detail) => R | false | undefined) => R[];
104
+ getNodes: <R = any>(data: T[], getValue: (row: T, detail: I_detail) => false | R) => R[];
103
105
  getChilds: (row: T) => void | T[];
104
106
  getRowAndParentsById: (data: T[], rowId: string | number) => {
105
107
  row?: T;
@@ -111,6 +113,10 @@ declare class EntityClass<T> {
111
113
  moveTo: (data: T[], fromIds: (string | number)[], toId: string | number) => T[];
112
114
  editChilds: (rows: T[], callback: (row: T) => T) => T[];
113
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[];
114
120
  duplicate: (data: T[], row: T) => {
115
121
  data: T[];
116
122
  addedId?: string | number;
@@ -132,11 +138,53 @@ export declare const useFilter: <T, F>(p: {
132
138
  report: any;
133
139
  };
134
140
  export declare const useEntityFilter: <T, F>(p: {
135
- entity: ReturnType<typeof useEntity<T>>;
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
+ };
136
184
  defaultFilter: F;
137
185
  getResult: (row: T, detail: I_detail, filter: F) => boolean;
138
- getReport?: (row: T, detail: I_detail, filter: F) => false | undefined | {
139
- [key: string]: any[] | number;
186
+ getReport?: (row: T, detail: I_detail, filter: F) => false | void | {
187
+ [key: string]: number | any[];
140
188
  };
141
189
  defaultReport?: any;
142
190
  }) => {
package/index.js CHANGED
@@ -53,6 +53,7 @@ export const useEntity = (p) => {
53
53
  mergeToNewRow: (rowIds, newRow, newRowParentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToNewRow(dataRef.current, rowIds, newRow, newRowParentId)); }),
54
54
  moveTo: (fromIds, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo(dataRef.current, fromIds, toId)); }),
55
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)); }),
56
57
  duplicate: (row) => __awaiter(void 0, void 0, void 0, function* () {
57
58
  const { data, addedId } = entity.duplicate(dataRef.current, row);
58
59
  if (addedId === undefined) {
@@ -193,6 +194,59 @@ class EntityClass {
193
194
  this.editChilds_req(childs, callback, result);
194
195
  }
195
196
  };
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
+ };
196
250
  this.filterRows = (rows, getResult, parentIds) => {
197
251
  const { childs: c, id } = this.params;
198
252
  if (!c) {
@@ -406,6 +460,60 @@ class EntityClass {
406
460
  });
407
461
  return this.filterRows(rows, (row) => !!dic['a' + id.get(row)]);
408
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 });
516
+ };
409
517
  this.duplicate = (data, row) => {
410
518
  const { id } = this.params;
411
519
  const rowId = id.get(row);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-entity",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "kit",
5
5
  "main": "index.js",
6
6
  "scripts": {