aio-entity 1.0.2 → 1.0.4
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 +47 -63
- package/index.js +230 -254
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -9,91 +9,75 @@ type I_sort<T> = {
|
|
|
9
9
|
columnId?: string;
|
|
10
10
|
order?: number;
|
|
11
11
|
};
|
|
12
|
-
type
|
|
13
|
-
|
|
12
|
+
type I_detail = {
|
|
13
|
+
_parentIds?: (string | number)[];
|
|
14
14
|
_childsLength?: number;
|
|
15
|
+
_isLeaf: boolean;
|
|
15
16
|
};
|
|
16
|
-
export type I_useEntity<T
|
|
17
|
-
sync: I_useSync<T>;
|
|
17
|
+
export type I_useEntity<T> = {
|
|
18
18
|
getId: (row: T) => string | number;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
rows: T[];
|
|
26
|
-
data: any;
|
|
27
|
-
};
|
|
28
|
-
getDefaultFilter: () => F;
|
|
19
|
+
fetchData: () => Promise<T[] | false>;
|
|
20
|
+
saveData: (data: T[]) => Promise<boolean>;
|
|
21
|
+
initData?: T[];
|
|
22
|
+
childs?: {
|
|
23
|
+
get: (row: T) => (T[] | void);
|
|
24
|
+
set: (row: T, childs: T[] | undefined) => T;
|
|
29
25
|
};
|
|
30
|
-
|
|
26
|
+
setId: (row: T) => T;
|
|
31
27
|
};
|
|
32
|
-
export declare const useEntity: <T
|
|
28
|
+
export declare const useEntity: <T>(p: I_useEntity<T>) => {
|
|
33
29
|
data: T[];
|
|
34
|
-
changeData: (data: T[]) => Promise<
|
|
30
|
+
changeData: (data: T[]) => Promise<boolean>;
|
|
35
31
|
getId: (row: T) => string | number;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
data: any;
|
|
48
|
-
};
|
|
49
|
-
hasFilter: boolean;
|
|
32
|
+
add: (row: T, parentId?: string | number) => Promise<number | string | false>;
|
|
33
|
+
edit: (row: T) => Promise<boolean>;
|
|
34
|
+
remove: (rowId: string | number) => Promise<boolean>;
|
|
35
|
+
removeByIds: (rowIds: (string | number)[]) => Promise<boolean>;
|
|
36
|
+
mergeToExistRow: (rowIds: (string | number)[], parentId: string | number) => Promise<boolean>;
|
|
37
|
+
mergeToNewRow: (rowIds: (string | number)[], newRow: T, newRowParentId?: string | number) => Promise<boolean>;
|
|
38
|
+
moveTo: (fromIds: (string | number)[], toId: string | number) => Promise<boolean>;
|
|
39
|
+
editChilds: (rows: T[], callback: (row: T) => T) => Promise<boolean>;
|
|
40
|
+
duplicate: (row: T) => Promise<string | number | false>;
|
|
41
|
+
getLeafs: (rows?: T[]) => T[];
|
|
42
|
+
sort: (sorts: I_sort<T>[], parentRow?: T) => Promise<boolean>;
|
|
50
43
|
getNodes: <R>(p: {
|
|
51
44
|
row?: T;
|
|
52
|
-
|
|
53
|
-
map?: (row: I_flatRow<T>) => false | R;
|
|
45
|
+
getValue: (row: T, detail: I_detail) => R | false | undefined;
|
|
54
46
|
}) => R[];
|
|
55
47
|
getRowAndParentsById: (p: {
|
|
56
48
|
rowId: string | number;
|
|
57
49
|
}) => {
|
|
58
|
-
row?: T;
|
|
50
|
+
row?: T | undefined;
|
|
59
51
|
parents: T[];
|
|
60
52
|
};
|
|
61
|
-
importData: (callback: (data: T[]) => void) => Promise<void>;
|
|
62
|
-
exportData: (data: T[]) => void;
|
|
63
|
-
getLeafs: (p: {
|
|
64
|
-
row?: T;
|
|
65
|
-
rows: T[];
|
|
66
|
-
}) => T[];
|
|
67
53
|
getChilds: (row: T) => void | T[];
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
54
|
+
getReport: (p: {
|
|
55
|
+
rows?: T[];
|
|
56
|
+
getValue: (row: T, detail: I_detail) => {
|
|
57
|
+
[key: string]: any[] | number;
|
|
58
|
+
} | false | undefined;
|
|
59
|
+
getGroup?: (row: T) => string[];
|
|
60
|
+
}) => any;
|
|
74
61
|
getParentByRowId: (p: {
|
|
75
62
|
rowId: any;
|
|
76
63
|
rows: T[];
|
|
77
64
|
}) => T;
|
|
78
65
|
};
|
|
79
|
-
type
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
saveData?: (data: T[]) => Promise<boolean>;
|
|
84
|
-
apis?: {
|
|
85
|
-
save: (data: T[]) => Promise<boolean>;
|
|
86
|
-
load: (def: T[]) => Promise<false | T[]>;
|
|
66
|
+
type I_useFilter<T, F> = {
|
|
67
|
+
getFilteredRows: (rows: T[], filter: F) => {
|
|
68
|
+
rows: T[];
|
|
69
|
+
data: any;
|
|
87
70
|
};
|
|
88
|
-
|
|
71
|
+
getDefaultFilter: () => F;
|
|
72
|
+
getRows: () => T[];
|
|
89
73
|
};
|
|
90
|
-
export declare const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
74
|
+
export declare const useFilter: <T, F>(p: I_useFilter<T, F>) => {
|
|
75
|
+
filter: F;
|
|
76
|
+
changeFilter: (p: Partial<F>) => void;
|
|
77
|
+
filteredData: {
|
|
78
|
+
rows: T[];
|
|
79
|
+
data: any;
|
|
80
|
+
};
|
|
81
|
+
updateFilteredRows: (rows: T[], pFilter?: F) => void;
|
|
98
82
|
};
|
|
99
83
|
export {};
|
package/index.js
CHANGED
|
@@ -7,75 +7,68 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
11
10
|
import { useEffect, useRef, useState } from "react";
|
|
12
|
-
import
|
|
13
|
-
import { Cards } from "aio-input";
|
|
14
|
-
import { Debounce, SortArray } from 'aio-utils';
|
|
11
|
+
import { Debounce, SortArray } from '../../aio-utils';
|
|
15
12
|
export const useEntity = (p) => {
|
|
16
|
-
const { getFilteredRows = () => ({ rows: [], data: {} }), getDefaultFilter = () => { return {}; } } = p.filter || {};
|
|
17
13
|
const entity = new EntityClass({
|
|
18
|
-
getId: p.getId, setId: p.setId,
|
|
14
|
+
getId: p.getId, setId: p.setId, childs: p.childs
|
|
19
15
|
});
|
|
20
|
-
const
|
|
21
|
-
const [data, setData] = useState(p.sync.initData || []), dataRef = useRef(data);
|
|
16
|
+
const [data, setData] = useState(p.initData || []), dataRef = useRef(data);
|
|
22
17
|
dataRef.current = data;
|
|
23
|
-
const filterHook = useFilter({ getFilteredRows, getDefaultFilter, getRows: () => data });
|
|
24
18
|
const fetchData = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
-
const res = yield
|
|
19
|
+
const res = yield p.fetchData();
|
|
26
20
|
if (res !== false) {
|
|
27
21
|
setData(res);
|
|
28
22
|
}
|
|
29
23
|
});
|
|
30
24
|
useEffect(() => { fetchData(); }, []);
|
|
31
25
|
const changeData = (data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
|
-
const res = yield
|
|
26
|
+
const res = yield p.saveData(data);
|
|
33
27
|
if (res !== false) {
|
|
34
28
|
dataRef.current = data;
|
|
35
29
|
setData(data);
|
|
36
30
|
}
|
|
31
|
+
return res;
|
|
37
32
|
});
|
|
38
|
-
useEffect(() => { filterHook.updateFilteredRows(data); }, [data]);
|
|
39
|
-
const render = () => (_jsx(_Fragment, { children: sync.popup.render() }));
|
|
40
33
|
return {
|
|
41
|
-
data, changeData, getId: p.getId,
|
|
42
|
-
add: (row, parentId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
data, changeData, getId: p.getId,
|
|
35
|
+
add: (row, parentId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
+
const { rows, addedId } = entity.add({ row, parentId, rows: dataRef.current });
|
|
37
|
+
if (addedId === undefined) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
const res = yield changeData(rows);
|
|
41
|
+
if (!res) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return addedId;
|
|
45
|
+
}),
|
|
43
46
|
edit: (row) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.edit({ row, rows: dataRef.current })); }),
|
|
44
47
|
remove: (rowId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.remove({ rowId, rows: dataRef.current })); }),
|
|
45
|
-
|
|
48
|
+
removeByIds: (rowIds) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.removeByIds({ rowIds, rows: dataRef.current })); }),
|
|
49
|
+
mergeToExistRow: (rowIds, parentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToExistRow({ rowIds, rows: dataRef.current, parentId })); }),
|
|
50
|
+
mergeToNewRow: (rowIds, newRow, newRowParentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToNewRow({ rowIds, rows: dataRef.current, newRow, newRowParentId })); }),
|
|
51
|
+
moveTo: (fromIds, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo({ fromIds, toId, rows: dataRef.current })); }),
|
|
46
52
|
editChilds: (rows, callback) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.editChilds({ rows, callback })); }),
|
|
47
|
-
duplicate: (row) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
duplicate: (row) => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
+
const { rows, addedId } = entity.duplicate({ rows: dataRef.current, row });
|
|
55
|
+
if (addedId === undefined) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const res = yield changeData(rows);
|
|
59
|
+
if (!res) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return addedId;
|
|
63
|
+
}),
|
|
64
|
+
getLeafs: (rows) => entity.getLeafs(dataRef.current, rows),
|
|
48
65
|
sort: (sorts, parentRow) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.sort({ rows: dataRef.current, parentRow, sorts })); }),
|
|
49
|
-
|
|
50
|
-
getNodes: (p) => entity.getNodes(Object.assign(Object.assign({}, p), { rows: dataRef.current })),
|
|
66
|
+
getNodes: (p) => entity.getNodes(Object.assign(Object.assign({}, p), { data: dataRef.current })),
|
|
51
67
|
getRowAndParentsById: (p) => entity.getRowAndParentsById(Object.assign(Object.assign({}, p), { rows: dataRef.current })),
|
|
52
|
-
importData: sync.importData,
|
|
53
|
-
exportData: sync.exportData,
|
|
54
|
-
getLeafs: entity.getLeafs,
|
|
55
68
|
getChilds: entity.getChilds,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
getReports: entity.getReports,
|
|
59
|
-
getParentByRowId: entity.getParentByRowId,
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
const useFilter = (p) => {
|
|
63
|
-
const [filter, setFilter] = useState(p.getDefaultFilter);
|
|
64
|
-
const filterRef = useRef(filter);
|
|
65
|
-
filterRef.current = filter;
|
|
66
|
-
const [filteredData, setFilteredData] = useState({ rows: [], data: {} });
|
|
67
|
-
const filteredDataRef = useRef(filteredData);
|
|
68
|
-
filteredDataRef.current = filteredData;
|
|
69
|
-
const changeFilter = (p) => setFilter(Object.assign(Object.assign({}, filter), p));
|
|
70
|
-
const updateFilteredRows = (rows, pFilter) => {
|
|
71
|
-
Debounce(() => {
|
|
72
|
-
const filter = pFilter || filterRef.current;
|
|
73
|
-
const filteredData = p.getFilteredRows(rows, filter);
|
|
74
|
-
setFilteredData(filteredData);
|
|
75
|
-
}, 50);
|
|
69
|
+
getReport: (p) => entity.getReport({ data: dataRef.current, rows: p.rows, getValue: p.getValue, getGroup: p.getGroup }),
|
|
70
|
+
getParentByRowId: entity.getParentByRowId
|
|
76
71
|
};
|
|
77
|
-
useEffect(() => { updateFilteredRows(p.getRows()); }, [filter]);
|
|
78
|
-
return { filter, changeFilter, filteredData, updateFilteredRows };
|
|
79
72
|
};
|
|
80
73
|
class EntityClass {
|
|
81
74
|
constructor(p) {
|
|
@@ -90,106 +83,95 @@ class EntityClass {
|
|
|
90
83
|
result.parents = parents;
|
|
91
84
|
return;
|
|
92
85
|
}
|
|
93
|
-
const childs = !!this.params.
|
|
86
|
+
const childs = !!this.params.childs ? (this.params.childs.get(row) || []) : undefined;
|
|
94
87
|
if (childs && childs.length) {
|
|
95
88
|
this.getRowAndParentsById_req(childs, id, [...parents, row], result);
|
|
96
89
|
}
|
|
97
90
|
}
|
|
98
91
|
};
|
|
99
92
|
this.addToParent = (p) => {
|
|
100
|
-
const {
|
|
93
|
+
const { childs: c } = this.params;
|
|
101
94
|
const { parentRow, row } = p;
|
|
102
|
-
if (!
|
|
95
|
+
if (!c) {
|
|
103
96
|
return row;
|
|
104
97
|
}
|
|
105
|
-
const childs =
|
|
98
|
+
const childs = c.get(parentRow) || [];
|
|
106
99
|
if (!childs) {
|
|
107
100
|
return row;
|
|
108
101
|
}
|
|
109
|
-
return
|
|
102
|
+
return c.set(parentRow, [...childs, row]) || row;
|
|
110
103
|
};
|
|
111
104
|
this.editByParent = (p) => {
|
|
112
105
|
const { parentRow, row } = p;
|
|
113
|
-
const {
|
|
114
|
-
if (!
|
|
106
|
+
const { childs: c, getId } = this.params;
|
|
107
|
+
if (!c) {
|
|
115
108
|
return parentRow;
|
|
116
109
|
}
|
|
117
110
|
const rowId = getId(row);
|
|
118
|
-
const childs =
|
|
111
|
+
const childs = c.get(parentRow);
|
|
119
112
|
if (!childs) {
|
|
120
113
|
return parentRow;
|
|
121
114
|
}
|
|
122
115
|
const newChilds = childs.map((o) => getId(o) === rowId ? row : o);
|
|
123
|
-
return
|
|
116
|
+
return c.set(parentRow, newChilds) || parentRow;
|
|
124
117
|
};
|
|
125
118
|
this.removeChild = (p) => {
|
|
126
119
|
const { parentRow, rowId, rows } = p;
|
|
127
|
-
const {
|
|
128
|
-
if (!
|
|
120
|
+
const { childs: c, getId } = this.params;
|
|
121
|
+
if (!c) {
|
|
129
122
|
return rows;
|
|
130
123
|
}
|
|
131
|
-
let parentChilds =
|
|
124
|
+
let parentChilds = c.get(parentRow);
|
|
132
125
|
if (!parentChilds || !parentChilds.length) {
|
|
133
126
|
return rows;
|
|
134
127
|
}
|
|
135
128
|
parentChilds = parentChilds.filter((o) => getId(o) !== rowId);
|
|
136
|
-
const res =
|
|
129
|
+
const res = c.set(parentRow, parentChilds);
|
|
137
130
|
if (!res) {
|
|
138
131
|
return rows;
|
|
139
132
|
}
|
|
140
133
|
return this.edit({ row: res, rows });
|
|
141
134
|
};
|
|
142
|
-
this.
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
this.getLeafs_req(childs, result);
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
result.push(row);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
135
|
+
this.getLeafs = (data, rows) => {
|
|
136
|
+
const res = this.getReport({
|
|
137
|
+
rows, data,
|
|
138
|
+
getValue: (o, detail) => detail._isLeaf ? { rows: [o] } : undefined,
|
|
139
|
+
});
|
|
140
|
+
return res.rows;
|
|
154
141
|
};
|
|
155
|
-
this.
|
|
156
|
-
const {
|
|
157
|
-
if (!
|
|
158
|
-
return
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (res) {
|
|
164
|
-
_childsLength = res.length;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return Object.assign(Object.assign({}, setChilds(Object.assign({}, row), undefined)), { _parentIds, _childsLength });
|
|
142
|
+
this.getDetail = (row) => {
|
|
143
|
+
const { childs: c } = this.params;
|
|
144
|
+
if (!c) {
|
|
145
|
+
return { _parentIds: [], _childsLength: undefined, _isLeaf: false };
|
|
146
|
+
}
|
|
147
|
+
const childs = c.get(row);
|
|
148
|
+
const childsLength = childs ? childs.length : undefined;
|
|
149
|
+
return { _parentIds: [], _childsLength: childsLength, _isLeaf: childsLength === undefined };
|
|
168
150
|
};
|
|
169
151
|
this.walk = (p) => {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (!p.childs) {
|
|
152
|
+
const { childs: c } = this.params;
|
|
153
|
+
if (!p.childs || !c) {
|
|
173
154
|
return;
|
|
174
155
|
}
|
|
175
156
|
for (let i = 0; i < p.childs.length; i++) {
|
|
176
157
|
const row = p.childs[i];
|
|
177
158
|
const rowId = this.params.getId(row);
|
|
178
|
-
const
|
|
179
|
-
|
|
159
|
+
const detail = this.getDetail(row);
|
|
160
|
+
const mapped = p.getValue(row, detail);
|
|
161
|
+
if (mapped === false) {
|
|
180
162
|
continue;
|
|
181
|
-
|
|
182
|
-
if (mapped !==
|
|
163
|
+
}
|
|
164
|
+
if (mapped !== undefined) {
|
|
183
165
|
p.result.push(mapped);
|
|
184
166
|
}
|
|
185
|
-
const subchilds =
|
|
167
|
+
const subchilds = c.get(row);
|
|
186
168
|
if (subchilds === null || subchilds === void 0 ? void 0 : subchilds.length) {
|
|
187
|
-
this.walk({ childs: subchilds,
|
|
169
|
+
this.walk({ childs: subchilds, getValue: p.getValue, result: p.result, parentIds: [...p.parentIds, rowId] });
|
|
188
170
|
}
|
|
189
171
|
}
|
|
190
172
|
};
|
|
191
173
|
this.sort_req = (p) => {
|
|
192
|
-
const {
|
|
174
|
+
const { childs: c } = this.params;
|
|
193
175
|
const { rows, sorts, result, parentRow } = p;
|
|
194
176
|
let sortedChilds = [];
|
|
195
177
|
if (!parentRow) {
|
|
@@ -197,13 +179,13 @@ class EntityClass {
|
|
|
197
179
|
result.rows = sortedChilds;
|
|
198
180
|
}
|
|
199
181
|
else {
|
|
200
|
-
if (!
|
|
182
|
+
if (!c) {
|
|
201
183
|
return;
|
|
202
184
|
}
|
|
203
185
|
const childs = this.getChilds(parentRow);
|
|
204
186
|
if (childs && childs.length) {
|
|
205
187
|
sortedChilds = SortArray(childs, p.sorts);
|
|
206
|
-
const fixedParent =
|
|
188
|
+
const fixedParent = c.set(parentRow, sortedChilds);
|
|
207
189
|
p.result.rows = this.edit({ row: fixedParent, rows: p.result.rows });
|
|
208
190
|
}
|
|
209
191
|
}
|
|
@@ -212,89 +194,93 @@ class EntityClass {
|
|
|
212
194
|
}
|
|
213
195
|
};
|
|
214
196
|
this.editChilds_req = (rows, callback, result) => {
|
|
215
|
-
const {
|
|
216
|
-
if (!
|
|
197
|
+
const { childs: c } = this.params;
|
|
198
|
+
if (!c) {
|
|
217
199
|
return [];
|
|
218
200
|
}
|
|
219
201
|
for (let i = 0; i < rows.length; i++) {
|
|
220
202
|
const row = rows[i];
|
|
221
203
|
result.rows = this.edit({ row: callback(row), rows: result.rows });
|
|
222
|
-
const childs =
|
|
204
|
+
const childs = c.get(row);
|
|
223
205
|
if (!childs || !childs.length) {
|
|
224
206
|
continue;
|
|
225
207
|
}
|
|
226
208
|
this.editChilds_req(childs, callback, result);
|
|
227
209
|
}
|
|
228
210
|
};
|
|
229
|
-
this.
|
|
230
|
-
const
|
|
231
|
-
const res =
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
res[groupKey] = res[groupKey] || {};
|
|
238
|
-
for (const field in values) {
|
|
239
|
-
const value = values[field];
|
|
240
|
-
if (Array.isArray(value)) {
|
|
241
|
-
res[groupKey][field] = res[groupKey][field] || [];
|
|
242
|
-
res[groupKey][field] = res[groupKey][field].concat(value);
|
|
243
|
-
total[groupKey] = total[groupKey] || [];
|
|
244
|
-
total[groupKey] = total[groupKey].concat(value);
|
|
245
|
-
}
|
|
246
|
-
else if (typeof value === 'number') {
|
|
247
|
-
res[groupKey][field] = (res[groupKey][field] || 0) + value;
|
|
248
|
-
total[groupKey] = total[groupKey] || 0;
|
|
249
|
-
total[groupKey] += value;
|
|
250
|
-
}
|
|
211
|
+
this.filterRows = (rows, getResult) => {
|
|
212
|
+
const { childs: c } = this.params;
|
|
213
|
+
const res = rows.filter((o) => getResult(o));
|
|
214
|
+
return res.map((row) => {
|
|
215
|
+
const childs = c ? c.get(row) : undefined;
|
|
216
|
+
if (childs && c) {
|
|
217
|
+
const newChilds = this.filterRows(childs, getResult);
|
|
218
|
+
return c.set(row, newChilds);
|
|
251
219
|
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
};
|
|
255
|
-
this.getReports = (nodes, getValue, getGroup) => {
|
|
256
|
-
const res = {};
|
|
257
|
-
for (const node of nodes) {
|
|
258
|
-
const groupKeys = getGroup ? getGroup(node) : ['_all'];
|
|
259
|
-
const values = getValue(node);
|
|
260
|
-
if (!values)
|
|
261
|
-
continue;
|
|
262
|
-
let temp = res[groupKeys[0]];
|
|
263
|
-
if (!temp) {
|
|
264
|
-
res[groupKeys[0]] = res[groupKeys[0]] || {};
|
|
265
|
-
temp = res[groupKeys[0]];
|
|
220
|
+
else {
|
|
221
|
+
return row;
|
|
266
222
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
this.updateReport = (values, groups, dic) => {
|
|
226
|
+
let tempGroups = dic;
|
|
227
|
+
if (groups.length) {
|
|
228
|
+
for (let i = 0; i < groups.length; i++) {
|
|
229
|
+
const group = groups[i];
|
|
230
|
+
tempGroups[group] = tempGroups[group] || {};
|
|
231
|
+
tempGroups = tempGroups[group];
|
|
271
232
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
233
|
+
}
|
|
234
|
+
for (const field in values) {
|
|
235
|
+
const value = values[field];
|
|
236
|
+
if (Array.isArray(value)) {
|
|
237
|
+
if (!Array.isArray(tempGroups[field])) {
|
|
238
|
+
tempGroups[field] = [];
|
|
277
239
|
}
|
|
278
|
-
|
|
279
|
-
|
|
240
|
+
tempGroups[field] = tempGroups[field].concat(value);
|
|
241
|
+
}
|
|
242
|
+
else if (typeof value === 'number') {
|
|
243
|
+
if (typeof tempGroups[field] !== 'number') {
|
|
244
|
+
tempGroups[field] = 0;
|
|
280
245
|
}
|
|
246
|
+
tempGroups[field] = (tempGroups[field] || 0) + value;
|
|
281
247
|
}
|
|
282
248
|
}
|
|
283
|
-
|
|
249
|
+
};
|
|
250
|
+
this.getReport = (p) => {
|
|
251
|
+
const dic = {};
|
|
252
|
+
const rows = p.rows || p.data;
|
|
253
|
+
for (let i = 0; i < rows.length; i++) {
|
|
254
|
+
this.getNodes({
|
|
255
|
+
data: p.data, row: rows[i], getValue: (o) => {
|
|
256
|
+
const detail = this.getDetail(o);
|
|
257
|
+
const values = p.getValue(o, detail);
|
|
258
|
+
if (values === false || values === undefined) {
|
|
259
|
+
return values;
|
|
260
|
+
}
|
|
261
|
+
const groups = p.getGroup ? p.getGroup(o) : [];
|
|
262
|
+
this.updateReport(values, groups, dic);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
return dic;
|
|
284
267
|
};
|
|
285
268
|
this.add = (p) => {
|
|
286
269
|
const { rows, row, parentId } = p;
|
|
287
270
|
const newRow = this.params.setId(row);
|
|
271
|
+
const addedId = this.params.getId(newRow);
|
|
288
272
|
if (parentId !== undefined) {
|
|
289
273
|
const res = this.getRowAndParentsById({ rowId: parentId, rows });
|
|
290
274
|
if (!res.row) {
|
|
291
|
-
return rows;
|
|
275
|
+
return { rows };
|
|
292
276
|
}
|
|
293
277
|
const res1 = this.addToParent({ parentRow: res.row, row: newRow });
|
|
294
|
-
|
|
278
|
+
const newRows = this.edit({ row: res1, rows });
|
|
279
|
+
return { rows: newRows, addedId };
|
|
295
280
|
}
|
|
296
281
|
else {
|
|
297
|
-
|
|
282
|
+
const newRows = [...rows, newRow];
|
|
283
|
+
return { rows: newRows, addedId };
|
|
298
284
|
}
|
|
299
285
|
};
|
|
300
286
|
this.editRoot = (rows, row) => {
|
|
@@ -302,10 +288,10 @@ class EntityClass {
|
|
|
302
288
|
return rows.map((o) => getId(o) === getId(row) ? row : o);
|
|
303
289
|
};
|
|
304
290
|
this.edit = (p) => {
|
|
305
|
-
const {
|
|
291
|
+
const { childs: c } = this.params;
|
|
306
292
|
const { row, rows } = p;
|
|
307
293
|
const rowId = this.params.getId(row);
|
|
308
|
-
if (!!
|
|
294
|
+
if (!!c) {
|
|
309
295
|
let parentRow = this.getParentByRowId({ rowId, rows });
|
|
310
296
|
if (parentRow) {
|
|
311
297
|
parentRow = this.editByParent({ parentRow, row });
|
|
@@ -332,18 +318,70 @@ class EntityClass {
|
|
|
332
318
|
return rows.filter((o) => getId(o) !== rowId);
|
|
333
319
|
}
|
|
334
320
|
};
|
|
335
|
-
this.
|
|
321
|
+
this.removeByIds = (p) => {
|
|
322
|
+
const { rowIds, rows } = p;
|
|
323
|
+
let tempRows = rows;
|
|
324
|
+
for (let i = 0; i < rowIds.length; i++) {
|
|
325
|
+
const rowId = rowIds[i];
|
|
326
|
+
tempRows = this.remove({ rowId, rows: tempRows });
|
|
327
|
+
}
|
|
328
|
+
return tempRows;
|
|
329
|
+
};
|
|
330
|
+
this.mergeToExistRow = (p) => {
|
|
331
|
+
let rows = p.rows;
|
|
332
|
+
if (!this.params.childs) {
|
|
333
|
+
return rows;
|
|
334
|
+
}
|
|
335
|
+
const { row: parentRow } = this.getRowAndParentsById({ rows: p.rows, rowId: p.parentId });
|
|
336
|
+
if (!parentRow) {
|
|
337
|
+
return rows;
|
|
338
|
+
}
|
|
339
|
+
const newChilds = this.getRowsByIds({ rows: p.rows, rowIds: p.rowIds });
|
|
340
|
+
if (!newChilds.length) {
|
|
341
|
+
return rows;
|
|
342
|
+
}
|
|
343
|
+
const parentChilds = this.params.childs.get(parentRow);
|
|
344
|
+
if (!parentChilds) {
|
|
345
|
+
return rows;
|
|
346
|
+
}
|
|
347
|
+
rows = this.removeByIds({ rows, rowIds: p.rowIds });
|
|
348
|
+
const newParentChilds = [...parentChilds, ...newChilds];
|
|
349
|
+
const newParentRow = this.params.childs.set(parentRow, newParentChilds);
|
|
350
|
+
return this.edit({ rows, row: newParentRow });
|
|
351
|
+
};
|
|
352
|
+
this.mergeToNewRow = (p) => {
|
|
353
|
+
const { rows, addedId } = this.add({ rows: p.rows, row: p.newRow, parentId: p.newRowParentId });
|
|
354
|
+
if (addedId === undefined) {
|
|
355
|
+
return rows;
|
|
356
|
+
}
|
|
357
|
+
return this.mergeToExistRow({ rows, rowIds: p.rowIds, parentId: addedId });
|
|
358
|
+
};
|
|
336
359
|
this.getNodes = (params) => {
|
|
337
360
|
const result = [];
|
|
338
|
-
this.walk({ childs: params.row ? [params.row] : params.
|
|
361
|
+
this.walk({ childs: params.row ? [params.row] : params.data, getValue: params.getValue, result, parentIds: [] });
|
|
339
362
|
return result;
|
|
340
363
|
};
|
|
341
|
-
this.getChilds = (row) => !this.params.
|
|
364
|
+
this.getChilds = (row) => !this.params.childs ? undefined : this.params.childs.get(row);
|
|
342
365
|
this.getRowAndParentsById = (p) => {
|
|
343
366
|
let result = { parents: [] };
|
|
344
367
|
this.getRowAndParentsById_req(p.rows, p.rowId, [], result);
|
|
345
368
|
return result;
|
|
346
369
|
};
|
|
370
|
+
this.getRowsByIds = (p) => {
|
|
371
|
+
const res = [];
|
|
372
|
+
const usedIds = [];
|
|
373
|
+
for (let i = 0; i < p.rowIds.length; i++) {
|
|
374
|
+
const id = p.rowIds[i];
|
|
375
|
+
if (usedIds.indexOf(id) !== -1) {
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
const { row } = this.getRowAndParentsById({ rowId: id, rows: p.rows });
|
|
379
|
+
if (row) {
|
|
380
|
+
res.push(row);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return res;
|
|
384
|
+
};
|
|
347
385
|
this.getParentByRowId = (p) => {
|
|
348
386
|
const res = this.getRowAndParentsById(p);
|
|
349
387
|
if (!res.row) {
|
|
@@ -355,18 +393,34 @@ class EntityClass {
|
|
|
355
393
|
}
|
|
356
394
|
return parents[parents.length - 1];
|
|
357
395
|
};
|
|
358
|
-
this.
|
|
396
|
+
this.moveToUnit = (p) => {
|
|
359
397
|
const { fromId, toId, rows } = p;
|
|
360
|
-
|
|
361
|
-
if (!
|
|
398
|
+
let { row: toRow } = this.getRowAndParentsById({ rowId: toId, rows });
|
|
399
|
+
if (!toRow) {
|
|
400
|
+
return p.rows;
|
|
401
|
+
}
|
|
402
|
+
let { row: fromRow } = this.getRowAndParentsById({ rowId: fromId, rows });
|
|
403
|
+
if (!fromRow) {
|
|
362
404
|
return p.rows;
|
|
363
405
|
}
|
|
364
|
-
const
|
|
365
|
-
|
|
406
|
+
const rowsAfterRemove = this.remove({ rowId: fromId, rows });
|
|
407
|
+
//اگر بروز نشه مشکل بوجود میاد
|
|
408
|
+
toRow = this.getRowAndParentsById({ rowId: toId, rows: rowsAfterRemove }).row;
|
|
409
|
+
if (!toRow) {
|
|
366
410
|
return p.rows;
|
|
367
411
|
}
|
|
368
|
-
const
|
|
369
|
-
|
|
412
|
+
const newToRow = this.addToParent({ parentRow: toRow, row: fromRow });
|
|
413
|
+
const res = this.edit({ row: newToRow, rows: rowsAfterRemove });
|
|
414
|
+
return res;
|
|
415
|
+
};
|
|
416
|
+
this.moveTo = (p) => {
|
|
417
|
+
const { fromIds, toId } = p;
|
|
418
|
+
let rows = p.rows;
|
|
419
|
+
for (let i = 0; i < p.fromIds.length; i++) {
|
|
420
|
+
const fromId = fromIds[i];
|
|
421
|
+
rows = this.moveToUnit({ fromId, toId, rows });
|
|
422
|
+
}
|
|
423
|
+
return rows;
|
|
370
424
|
};
|
|
371
425
|
this.editChilds = (p) => {
|
|
372
426
|
const { rows, callback } = p;
|
|
@@ -393,99 +447,21 @@ class EntityClass {
|
|
|
393
447
|
this.params = p;
|
|
394
448
|
}
|
|
395
449
|
}
|
|
396
|
-
export const
|
|
397
|
-
const [
|
|
398
|
-
const
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
const
|
|
402
|
-
|
|
403
|
-
const
|
|
404
|
-
const
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
const res = yield postData(data);
|
|
413
|
-
if (res) {
|
|
414
|
-
yield setUnSync(!res);
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
const loadData = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
418
|
-
if (p.cordova) {
|
|
419
|
-
const data = yield p.cordova.storageLoad(p.key, p.initData || []);
|
|
420
|
-
if (p.apis) {
|
|
421
|
-
yield syncCordova(data);
|
|
422
|
-
}
|
|
423
|
-
return data;
|
|
424
|
-
}
|
|
425
|
-
else if (p.apis) {
|
|
426
|
-
return yield fetchData();
|
|
427
|
-
}
|
|
428
|
-
else if (p.fetchData) {
|
|
429
|
-
return yield p.fetchData();
|
|
430
|
-
}
|
|
431
|
-
else {
|
|
432
|
-
addLog('dont fetch and not cordova available.return init data');
|
|
433
|
-
return p.initData;
|
|
434
|
-
}
|
|
435
|
-
});
|
|
436
|
-
const fetchData = () => __awaiter(void 0, void 0, void 0, function* () { return p.apis ? yield p.apis.load(p.initData) : p.initData; });
|
|
437
|
-
const postData = (data) => __awaiter(void 0, void 0, void 0, function* () { return p.apis ? yield p.apis.save(data) : false; });
|
|
438
|
-
const saveCordova = (data) => __awaiter(void 0, void 0, void 0, function* () { return yield p.cordova.storageSave(p.key, data); });
|
|
439
|
-
const saveData = (data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
440
|
-
if (p.apis) {
|
|
441
|
-
setUnSync(true);
|
|
442
|
-
const res = yield postData(data);
|
|
443
|
-
setUnSync(res);
|
|
444
|
-
if (!res && !p.cordova) {
|
|
445
|
-
return false;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
else if (p.saveData) {
|
|
449
|
-
setUnSync(true);
|
|
450
|
-
const res = yield p.saveData(data);
|
|
451
|
-
if (res) {
|
|
452
|
-
setUnSync(res);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
if (p.cordova) {
|
|
456
|
-
saveCordova(data);
|
|
457
|
-
}
|
|
458
|
-
return true;
|
|
459
|
-
});
|
|
460
|
-
const forceFetchData = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
461
|
-
const res = yield loadData();
|
|
462
|
-
if (res) {
|
|
463
|
-
saveData(res);
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
alert('failed to force fetch data');
|
|
467
|
-
return false;
|
|
468
|
-
}
|
|
469
|
-
return res;
|
|
470
|
-
});
|
|
471
|
-
const importData = (callback) => __awaiter(void 0, void 0, void 0, function* () {
|
|
472
|
-
popup.addModal({ header: { title: 'ورود دیتا' }, body: _jsx(ImportDb, { onSubmit: (data) => { callback(data); popup.removeModal(); } }) });
|
|
473
|
-
});
|
|
474
|
-
const exportData = (data) => {
|
|
475
|
-
const isCordova = typeof window !== "undefined" && !!window.cordova;
|
|
476
|
-
if (!isCordova) {
|
|
477
|
-
popup.addModal({
|
|
478
|
-
header: { title: 'فایل دیتابیس' },
|
|
479
|
-
body: (_jsx("div", { className: "h-full", children: _jsx("textarea", { className: 'h-full w-full !text-white', value: JSON.stringify(data) }) }))
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
else if (p.cordova) {
|
|
483
|
-
p.cordova.share(data);
|
|
484
|
-
}
|
|
450
|
+
export const useFilter = (p) => {
|
|
451
|
+
const [filter, setFilter] = useState(p.getDefaultFilter);
|
|
452
|
+
const filterRef = useRef(filter);
|
|
453
|
+
filterRef.current = filter;
|
|
454
|
+
const [filteredData, setFilteredData] = useState({ rows: [], data: {} });
|
|
455
|
+
const filteredDataRef = useRef(filteredData);
|
|
456
|
+
filteredDataRef.current = filteredData;
|
|
457
|
+
const changeFilter = (p) => setFilter(Object.assign(Object.assign({}, filter), p));
|
|
458
|
+
const updateFilteredRows = (rows, pFilter) => {
|
|
459
|
+
Debounce(() => {
|
|
460
|
+
const filter = pFilter || filterRef.current;
|
|
461
|
+
const filteredData = p.getFilteredRows(rows, filter);
|
|
462
|
+
setFilteredData(filteredData);
|
|
463
|
+
}, 50);
|
|
485
464
|
};
|
|
486
|
-
|
|
487
|
-
};
|
|
488
|
-
const ImportDb = ({ onSubmit }) => {
|
|
489
|
-
const [str, setStr] = useState('');
|
|
490
|
-
return (_jsxs("div", { className: "flex flex-col gap-2 h-full p-2", style: { direction: 'ltr', color: '#333' }, children: [_jsx("textarea", { value: str, onChange: (e) => setStr(e.target.value), className: 'flex-1 w-full text-white p-1' }), _jsx("button", { className: "d1-active-button", onClick: () => onSubmit(JSON.parse(str)), children: "\u062A\u0627\u06CC\u06CC\u062F" })] }));
|
|
465
|
+
useEffect(() => { updateFilteredRows(p.getRows()); }, [filter]);
|
|
466
|
+
return { filter, changeFilter, filteredData, updateFilteredRows };
|
|
491
467
|
};
|