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.
- package/index.d.ts +124 -26
- package/index.js +236 -138
- 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: (
|
|
43
|
-
|
|
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>(
|
|
57
|
-
|
|
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: (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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 {
|
|
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(
|
|
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(
|
|
49
|
-
remove: (rowId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.remove(
|
|
50
|
-
removeByIds: (rowIds) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.removeByIds(
|
|
51
|
-
mergeToExistRow: (rowIds, parentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToExistRow(
|
|
52
|
-
mergeToNewRow: (rowIds, newRow, newRowParentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToNewRow(
|
|
53
|
-
moveTo: (fromIds, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo(
|
|
54
|
-
editChilds: (rows, callback) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.editChilds(
|
|
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 {
|
|
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(
|
|
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(
|
|
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: (
|
|
94
|
+
getRowAndParentsById: (rowId) => entity.getRowAndParentsById(dataRef.current, rowId),
|
|
94
95
|
childs: p.childs || { get: () => undefined, set: () => { return undefined; } },
|
|
95
|
-
getReport: (
|
|
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 = (
|
|
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 = (
|
|
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 = (
|
|
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(
|
|
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(
|
|
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.
|
|
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
|
|
253
|
+
return rows.filter((o) => getResult(o, { _parentIds: [], _childsLength: undefined, _isLeaf: false }));
|
|
204
254
|
}
|
|
205
|
-
const res =
|
|
255
|
+
const res = rows.filter((o) => {
|
|
206
256
|
const childs = c.get(o);
|
|
207
257
|
const childsLength = childs ? childs.length : undefined;
|
|
208
|
-
return
|
|
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(
|
|
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 = (
|
|
297
|
+
this.getReport = (rows, getValue, getGroup) => {
|
|
248
298
|
const dic = {};
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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 = (
|
|
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(
|
|
313
|
+
const res = this.getRowAndParentsById(data, parentId);
|
|
269
314
|
if (!res.row) {
|
|
270
|
-
return {
|
|
315
|
+
return { data };
|
|
271
316
|
}
|
|
272
|
-
const res1 = this.addToParent(
|
|
273
|
-
const
|
|
274
|
-
return {
|
|
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
|
-
|
|
278
|
-
return { rows: newRows, addedId };
|
|
322
|
+
return { data: [...data, newRow], addedId };
|
|
279
323
|
}
|
|
280
324
|
};
|
|
281
|
-
this.editRoot = (rows, row) =>
|
|
282
|
-
|
|
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(
|
|
291
|
-
|
|
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(
|
|
334
|
+
return this.editRoot(data, row);
|
|
303
335
|
}
|
|
304
336
|
};
|
|
305
|
-
this.remove = (
|
|
337
|
+
this.remove = (data, rowId) => {
|
|
306
338
|
const { id } = this.params;
|
|
307
|
-
const
|
|
308
|
-
const parentResult = this.getParentByRowId({ rowId, rows: data });
|
|
339
|
+
const parentResult = this.getParentByRowId(data, rowId);
|
|
309
340
|
if (parentResult) {
|
|
310
|
-
return this.removeChild(
|
|
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 = (
|
|
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
|
-
|
|
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 = (
|
|
326
|
-
let rows =
|
|
354
|
+
this.mergeToExistRow = (data, rowIds, parentId) => {
|
|
355
|
+
let rows = data;
|
|
327
356
|
if (!this.params.childs) {
|
|
328
|
-
return
|
|
357
|
+
return data;
|
|
329
358
|
}
|
|
330
|
-
const { row: parentRow } = this.getRowAndParentsById(
|
|
359
|
+
const { row: parentRow } = this.getRowAndParentsById(data, parentId);
|
|
331
360
|
if (!parentRow) {
|
|
332
361
|
return rows;
|
|
333
362
|
}
|
|
334
|
-
const newChilds = this.getRowsByIds(
|
|
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(
|
|
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(
|
|
374
|
+
return this.edit(rows, newParentRow);
|
|
346
375
|
};
|
|
347
|
-
this.mergeToNewRow = (
|
|
348
|
-
const
|
|
349
|
-
|
|
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 = (
|
|
380
|
+
this.getNodes = (data, getValue) => {
|
|
355
381
|
const result = [];
|
|
356
|
-
this.walk({ childs:
|
|
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 = (
|
|
386
|
+
this.getRowAndParentsById = (data, rowId) => {
|
|
361
387
|
let result = { parents: [] };
|
|
362
|
-
this.getRowAndParentsById_req(
|
|
388
|
+
this.getRowAndParentsById_req(data, rowId, [], result);
|
|
363
389
|
return result;
|
|
364
390
|
};
|
|
365
|
-
this.getRowsByIds = (
|
|
391
|
+
this.getRowsByIds = (data, rowIds) => {
|
|
366
392
|
const res = [];
|
|
367
393
|
const usedIds = [];
|
|
368
|
-
for (let i = 0; i <
|
|
369
|
-
const id =
|
|
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(
|
|
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 = (
|
|
381
|
-
const res = this.getRowAndParentsById(
|
|
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 = (
|
|
392
|
-
|
|
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
|
|
420
|
+
return data;
|
|
396
421
|
}
|
|
397
|
-
let { row: fromRow } = this.getRowAndParentsById(
|
|
422
|
+
let { row: fromRow } = this.getRowAndParentsById(data, fromId);
|
|
398
423
|
if (!fromRow) {
|
|
399
|
-
return
|
|
424
|
+
return data;
|
|
400
425
|
}
|
|
401
|
-
const rowsAfterRemove = this.remove(
|
|
426
|
+
const rowsAfterRemove = this.remove(data, fromId);
|
|
402
427
|
//اگر بروز نشه مشکل بوجود میاد
|
|
403
|
-
toRow = this.getRowAndParentsById(
|
|
428
|
+
toRow = this.getRowAndParentsById(rowsAfterRemove, toId).row;
|
|
404
429
|
if (!toRow) {
|
|
405
|
-
return
|
|
430
|
+
return data;
|
|
406
431
|
}
|
|
407
|
-
const newToRow = this.addToParent(
|
|
408
|
-
const res = this.edit(
|
|
432
|
+
const newToRow = this.addToParent(toRow, fromRow);
|
|
433
|
+
const res = this.edit(rowsAfterRemove, newToRow);
|
|
409
434
|
return res;
|
|
410
435
|
};
|
|
411
|
-
this.moveTo = (
|
|
412
|
-
|
|
413
|
-
let
|
|
414
|
-
|
|
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 = (
|
|
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 = (
|
|
448
|
+
this.search = (rows, getResult) => {
|
|
427
449
|
const { id } = this.params;
|
|
428
450
|
const dic = {};
|
|
429
|
-
this.getNodes({
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
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(
|
|
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 = (
|
|
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(
|
|
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(
|
|
523
|
+
return this.add(data, newRow, parentId);
|
|
453
524
|
};
|
|
454
525
|
this.getLeafs = (rows) => {
|
|
455
|
-
const res = this.getReport(
|
|
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
|
+
};
|