aio-entity 1.0.0
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 +99 -0
- package/index.js +491 -0
- package/package.json +33 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
type I_sort<T> = {
|
|
3
|
+
active?: boolean;
|
|
4
|
+
dir?: 'dec' | 'inc';
|
|
5
|
+
title?: ReactNode;
|
|
6
|
+
sortId?: string;
|
|
7
|
+
getValue?: (row: T, dir: 'inc' | 'dec') => any;
|
|
8
|
+
isDate?: boolean;
|
|
9
|
+
columnId?: string;
|
|
10
|
+
order?: number;
|
|
11
|
+
};
|
|
12
|
+
type I_flatRow<T> = T & {
|
|
13
|
+
_parent?: T;
|
|
14
|
+
_childsLength?: number;
|
|
15
|
+
};
|
|
16
|
+
export type I_useEntity<T, F> = {
|
|
17
|
+
sync: I_useSync<T>;
|
|
18
|
+
getId: (row: T) => string | number;
|
|
19
|
+
popup?: any;
|
|
20
|
+
getChilds?: (row: T) => (T[] | void);
|
|
21
|
+
setChilds?: (row: T, childs: T[] | undefined) => T;
|
|
22
|
+
setId: (row: T) => T;
|
|
23
|
+
filter?: {
|
|
24
|
+
getFilteredRows: (rows: T[], filter: F) => {
|
|
25
|
+
rows: T[];
|
|
26
|
+
data: any;
|
|
27
|
+
};
|
|
28
|
+
getDefaultFilter: () => F;
|
|
29
|
+
};
|
|
30
|
+
fa?: boolean;
|
|
31
|
+
};
|
|
32
|
+
export declare const useEntity: <T, F>(p: I_useEntity<T, F>) => {
|
|
33
|
+
data: T[];
|
|
34
|
+
changeData: (data: T[]) => Promise<void>;
|
|
35
|
+
getId: (row: T) => string | number;
|
|
36
|
+
render: () => import("react/jsx-runtime").JSX.Element;
|
|
37
|
+
add: (row: T, parentId?: string | number) => Promise<void>;
|
|
38
|
+
edit: (row: T) => Promise<void>;
|
|
39
|
+
remove: (rowId: string | number) => Promise<void>;
|
|
40
|
+
moveTo: (fromId: string | number, toId: string | number) => Promise<void>;
|
|
41
|
+
editChilds: (rows: T[], callback: (row: T) => T) => Promise<void>;
|
|
42
|
+
duplicate: (row: T) => Promise<void>;
|
|
43
|
+
sort: (sorts: I_sort<T>[], parentRow?: T) => Promise<void>;
|
|
44
|
+
filter: F;
|
|
45
|
+
filteredData: {
|
|
46
|
+
rows: T[];
|
|
47
|
+
data: any;
|
|
48
|
+
};
|
|
49
|
+
hasFilter: boolean;
|
|
50
|
+
getNodes: <R>(p: {
|
|
51
|
+
row?: T;
|
|
52
|
+
filter?: (row: T) => boolean;
|
|
53
|
+
map?: (row: I_flatRow<T>) => false | R;
|
|
54
|
+
}) => R[];
|
|
55
|
+
getRowAndParentsById: (p: {
|
|
56
|
+
rowId: string | number;
|
|
57
|
+
}) => {
|
|
58
|
+
row?: T;
|
|
59
|
+
parents: T[];
|
|
60
|
+
};
|
|
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
|
+
getChilds: (row: T) => void | T[];
|
|
68
|
+
changeFilter: (p: Partial<F>) => void;
|
|
69
|
+
getReport: <NODE, RES>(nodes: NODE[], getValue: (node: NODE) => RES, getGroup?: (node: NODE) => string) => {
|
|
70
|
+
dic: RES;
|
|
71
|
+
total: RES;
|
|
72
|
+
};
|
|
73
|
+
getReports: (nodes: any[], getValue: (node: any) => any, getGroup?: (node: any) => string[]) => any;
|
|
74
|
+
getParentByRowId: (p: {
|
|
75
|
+
rowId: any;
|
|
76
|
+
rows: T[];
|
|
77
|
+
}) => T;
|
|
78
|
+
};
|
|
79
|
+
type I_useSync<T> = {
|
|
80
|
+
cordova?: any;
|
|
81
|
+
key: string;
|
|
82
|
+
fetchData?: () => Promise<T[] | false>;
|
|
83
|
+
saveData?: (data: T[]) => Promise<boolean>;
|
|
84
|
+
apis?: {
|
|
85
|
+
save: (data: T[]) => Promise<boolean>;
|
|
86
|
+
load: (def: T[]) => Promise<false | T[]>;
|
|
87
|
+
};
|
|
88
|
+
initData: T[];
|
|
89
|
+
};
|
|
90
|
+
export declare const useSync: <T>(p: I_useSync<T>) => {
|
|
91
|
+
saveData: (data: T[]) => Promise<boolean>;
|
|
92
|
+
loadData: () => Promise<false | T[]>;
|
|
93
|
+
forceFetchData: () => Promise<false | T[]>;
|
|
94
|
+
openLogs: () => any;
|
|
95
|
+
importData: (callback: (data: T[]) => void) => Promise<void>;
|
|
96
|
+
exportData: (data: T[]) => void;
|
|
97
|
+
popup: any;
|
|
98
|
+
};
|
|
99
|
+
export {};
|
package/index.js
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
11
|
+
import { useEffect, useRef, useState } from "react";
|
|
12
|
+
import usePopup from "aio-popup";
|
|
13
|
+
import { Cards } from "aio-input";
|
|
14
|
+
import { Debounce, SortArray } from 'aio-utils';
|
|
15
|
+
export const useEntity = (p) => {
|
|
16
|
+
const { getFilteredRows = () => ({ rows: [], data: {} }), getDefaultFilter = () => { return {}; } } = p.filter || {};
|
|
17
|
+
const entity = new EntityClass({
|
|
18
|
+
getId: p.getId, setId: p.setId, getChilds: p.getChilds, setChilds: p.setChilds
|
|
19
|
+
});
|
|
20
|
+
const sync = useSync(p.sync);
|
|
21
|
+
const [data, setData] = useState(p.sync.initData || []), dataRef = useRef(data);
|
|
22
|
+
dataRef.current = data;
|
|
23
|
+
const filterHook = useFilter({ getFilteredRows, getDefaultFilter, getRows: () => data });
|
|
24
|
+
const fetchData = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
const res = yield sync.loadData();
|
|
26
|
+
if (res !== false) {
|
|
27
|
+
setData(res);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
useEffect(() => { fetchData(); }, []);
|
|
31
|
+
const changeData = (data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
|
+
const res = yield sync.saveData(data);
|
|
33
|
+
if (res !== false) {
|
|
34
|
+
dataRef.current = data;
|
|
35
|
+
setData(data);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
useEffect(() => { filterHook.updateFilteredRows(data); }, [data]);
|
|
39
|
+
const render = () => (_jsx(_Fragment, { children: sync.popup.render() }));
|
|
40
|
+
return {
|
|
41
|
+
data, changeData, getId: p.getId, render,
|
|
42
|
+
add: (row, parentId) => __awaiter(void 0, void 0, void 0, function* () { yield changeData(entity.add({ row, parentId, rows: dataRef.current })); }),
|
|
43
|
+
edit: (row) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.edit({ row, rows: dataRef.current })); }),
|
|
44
|
+
remove: (rowId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.remove({ rowId, rows: dataRef.current })); }),
|
|
45
|
+
moveTo: (fromId, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo({ fromId, toId, rows: dataRef.current })); }),
|
|
46
|
+
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* () { return yield changeData(entity.duplicate({ rows: dataRef.current, row })); }),
|
|
48
|
+
sort: (sorts, parentRow) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.sort({ rows: dataRef.current, parentRow, sorts })); }),
|
|
49
|
+
filter: filterHook.filter, filteredData: filterHook.filteredData, hasFilter: !!p.filter,
|
|
50
|
+
getNodes: (p) => entity.getNodes(Object.assign(Object.assign({}, p), { rows: dataRef.current })),
|
|
51
|
+
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
|
+
getChilds: entity.getChilds,
|
|
56
|
+
changeFilter: filterHook.changeFilter,
|
|
57
|
+
getReport: entity.getReport,
|
|
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);
|
|
76
|
+
};
|
|
77
|
+
useEffect(() => { updateFilteredRows(p.getRows()); }, [filter]);
|
|
78
|
+
return { filter, changeFilter, filteredData, updateFilteredRows };
|
|
79
|
+
};
|
|
80
|
+
class EntityClass {
|
|
81
|
+
constructor(p) {
|
|
82
|
+
this.getRowAndParentsById_req = (rows, id, parents, result) => {
|
|
83
|
+
if (result.row) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
for (let i = 0; i < rows.length; i++) {
|
|
87
|
+
const row = rows[i];
|
|
88
|
+
if (this.params.getId(row) === id) {
|
|
89
|
+
result.row = row;
|
|
90
|
+
result.parents = parents;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const childs = !!this.params.getChilds ? (this.params.getChilds(row) || []) : undefined;
|
|
94
|
+
if (childs && childs.length) {
|
|
95
|
+
this.getRowAndParentsById_req(childs, id, [...parents, row], result);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
this.addToParent = (p) => {
|
|
100
|
+
const { getChilds, setChilds } = this.params;
|
|
101
|
+
const { parentRow, row } = p;
|
|
102
|
+
if (!getChilds || !setChilds) {
|
|
103
|
+
return row;
|
|
104
|
+
}
|
|
105
|
+
const childs = getChilds ? (getChilds(parentRow) || []) : undefined;
|
|
106
|
+
if (!childs) {
|
|
107
|
+
return row;
|
|
108
|
+
}
|
|
109
|
+
return setChilds(parentRow, [...childs, row]) || row;
|
|
110
|
+
};
|
|
111
|
+
this.editByParent = (p) => {
|
|
112
|
+
const { parentRow, row } = p;
|
|
113
|
+
const { getChilds, setChilds, getId } = this.params;
|
|
114
|
+
if (!getChilds || !setChilds) {
|
|
115
|
+
return parentRow;
|
|
116
|
+
}
|
|
117
|
+
const rowId = getId(row);
|
|
118
|
+
const childs = getChilds(parentRow);
|
|
119
|
+
if (!childs) {
|
|
120
|
+
return parentRow;
|
|
121
|
+
}
|
|
122
|
+
const newChilds = childs.map((o) => getId(o) === rowId ? row : o);
|
|
123
|
+
return setChilds(parentRow, newChilds) || parentRow;
|
|
124
|
+
};
|
|
125
|
+
this.removeChild = (p) => {
|
|
126
|
+
const { parentRow, rowId, rows } = p;
|
|
127
|
+
const { getChilds, setChilds, getId } = this.params;
|
|
128
|
+
if (!getChilds || !setChilds) {
|
|
129
|
+
return rows;
|
|
130
|
+
}
|
|
131
|
+
let parentChilds = getChilds(parentRow);
|
|
132
|
+
if (!parentChilds || !parentChilds.length) {
|
|
133
|
+
return rows;
|
|
134
|
+
}
|
|
135
|
+
parentChilds = parentChilds.filter((o) => getId(o) !== rowId);
|
|
136
|
+
const res = setChilds(parentRow, parentChilds);
|
|
137
|
+
if (!res) {
|
|
138
|
+
return rows;
|
|
139
|
+
}
|
|
140
|
+
return this.edit({ row: res, rows });
|
|
141
|
+
};
|
|
142
|
+
this.getLeafs_req = (rows, result) => {
|
|
143
|
+
const { getChilds } = this.params;
|
|
144
|
+
for (let i = 0; i < rows.length; i++) {
|
|
145
|
+
const row = rows[i];
|
|
146
|
+
const childs = !!getChilds ? getChilds(row) : undefined;
|
|
147
|
+
if (childs) {
|
|
148
|
+
this.getLeafs_req(childs, result);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
result.push(row);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
this.getFlatRow = (row, _parentIds, childsLength) => {
|
|
156
|
+
const { getChilds, setChilds } = this.params;
|
|
157
|
+
if (!setChilds || !getChilds) {
|
|
158
|
+
return Object.assign(Object.assign({}, row), { _childsLength: undefined, _parentIds: [] });
|
|
159
|
+
}
|
|
160
|
+
let _childsLength = childsLength;
|
|
161
|
+
if (childsLength === undefined) {
|
|
162
|
+
const res = getChilds(row);
|
|
163
|
+
if (res) {
|
|
164
|
+
_childsLength = res.length;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return Object.assign(Object.assign({}, setChilds(Object.assign({}, row), undefined)), { _parentIds, _childsLength });
|
|
168
|
+
};
|
|
169
|
+
this.walk = (p) => {
|
|
170
|
+
var _a;
|
|
171
|
+
const { getChilds } = this.params;
|
|
172
|
+
if (!p.childs) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
for (let i = 0; i < p.childs.length; i++) {
|
|
176
|
+
const row = p.childs[i];
|
|
177
|
+
const rowId = this.params.getId(row);
|
|
178
|
+
const flat = this.getFlatRow(row, p.parentIds);
|
|
179
|
+
if ((_a = p.filter) === null || _a === void 0 ? void 0 : _a.call(p, flat))
|
|
180
|
+
continue;
|
|
181
|
+
const mapped = p.map ? p.map(flat) : flat;
|
|
182
|
+
if (mapped !== false) {
|
|
183
|
+
p.result.push(mapped);
|
|
184
|
+
}
|
|
185
|
+
const subchilds = getChilds === null || getChilds === void 0 ? void 0 : getChilds(row);
|
|
186
|
+
if (subchilds === null || subchilds === void 0 ? void 0 : subchilds.length) {
|
|
187
|
+
this.walk({ childs: subchilds, filter: p.filter, map: p.map, result: p.result, parentIds: [...p.parentIds, rowId] });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
this.sort_req = (p) => {
|
|
192
|
+
const { getChilds, setChilds } = this.params;
|
|
193
|
+
const { rows, sorts, result, parentRow } = p;
|
|
194
|
+
let sortedChilds = [];
|
|
195
|
+
if (!parentRow) {
|
|
196
|
+
sortedChilds = SortArray(rows, sorts);
|
|
197
|
+
result.rows = sortedChilds;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
if (!getChilds || !setChilds) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const childs = this.getChilds(parentRow);
|
|
204
|
+
if (childs && childs.length) {
|
|
205
|
+
sortedChilds = SortArray(childs, p.sorts);
|
|
206
|
+
const fixedParent = setChilds(parentRow, sortedChilds);
|
|
207
|
+
p.result.rows = this.edit({ row: fixedParent, rows: p.result.rows });
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
for (let i = 0; i < sortedChilds.length; i++) {
|
|
211
|
+
this.sort_req({ parentRow: sortedChilds[i], sorts, result, rows });
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
this.editChilds_req = (rows, callback, result) => {
|
|
215
|
+
const { getChilds } = this.params;
|
|
216
|
+
if (!getChilds) {
|
|
217
|
+
return [];
|
|
218
|
+
}
|
|
219
|
+
for (let i = 0; i < rows.length; i++) {
|
|
220
|
+
const row = rows[i];
|
|
221
|
+
result.rows = this.edit({ row: callback(row), rows: result.rows });
|
|
222
|
+
const childs = getChilds(row);
|
|
223
|
+
if (!childs || !childs.length) {
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
this.editChilds_req(childs, callback, result);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
this.getReport = (nodes, getValue, getGroup) => {
|
|
230
|
+
const total = {};
|
|
231
|
+
const res = {};
|
|
232
|
+
for (const node of nodes) {
|
|
233
|
+
const groupKey = getGroup ? getGroup(node) : '_all';
|
|
234
|
+
const values = getValue(node);
|
|
235
|
+
if (!values)
|
|
236
|
+
continue;
|
|
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
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return getGroup ? { dic: res, total } : { dic: res['_all'], total };
|
|
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]];
|
|
266
|
+
}
|
|
267
|
+
for (let i = 1; i < groupKeys.length; i++) {
|
|
268
|
+
const groupKey = groupKeys[i];
|
|
269
|
+
temp[groupKey] = temp[groupKey] || {};
|
|
270
|
+
temp = temp[groupKey];
|
|
271
|
+
}
|
|
272
|
+
for (const field in values) {
|
|
273
|
+
const value = values[field];
|
|
274
|
+
if (Array.isArray(value)) {
|
|
275
|
+
temp[field] = temp[field] || [];
|
|
276
|
+
temp[field] = temp[field].concat(value);
|
|
277
|
+
}
|
|
278
|
+
else if (typeof value === 'number') {
|
|
279
|
+
temp[field] = (temp[field] || 0) + value;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return getGroup ? res : res['_all'];
|
|
284
|
+
};
|
|
285
|
+
this.add = (p) => {
|
|
286
|
+
const { rows, row, parentId } = p;
|
|
287
|
+
const newRow = this.params.setId(row);
|
|
288
|
+
if (parentId !== undefined) {
|
|
289
|
+
const res = this.getRowAndParentsById({ rowId: parentId, rows });
|
|
290
|
+
if (!res.row) {
|
|
291
|
+
return rows;
|
|
292
|
+
}
|
|
293
|
+
const res1 = this.addToParent({ parentRow: res.row, row: newRow });
|
|
294
|
+
return this.edit({ row: res1, rows });
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
return [...rows, newRow];
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
this.editRoot = (rows, row) => {
|
|
301
|
+
const { getId } = this.params;
|
|
302
|
+
return rows.map((o) => getId(o) === getId(row) ? row : o);
|
|
303
|
+
};
|
|
304
|
+
this.edit = (p) => {
|
|
305
|
+
const { getChilds, setChilds } = this.params;
|
|
306
|
+
const { row, rows } = p;
|
|
307
|
+
const rowId = this.params.getId(row);
|
|
308
|
+
if (!!getChilds && !!setChilds) {
|
|
309
|
+
let parentRow = this.getParentByRowId({ rowId, rows });
|
|
310
|
+
if (parentRow) {
|
|
311
|
+
parentRow = this.editByParent({ parentRow, row });
|
|
312
|
+
return this.edit({ row: parentRow, rows: p.rows });
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
const res = this.editRoot(rows, row);
|
|
316
|
+
console.log(res);
|
|
317
|
+
return res;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
return this.editRoot(rows, row);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
this.remove = (p) => {
|
|
325
|
+
const { getId } = this.params;
|
|
326
|
+
const { rowId, rows } = p;
|
|
327
|
+
const parentResult = this.getParentByRowId({ rowId, rows });
|
|
328
|
+
if (parentResult) {
|
|
329
|
+
return this.removeChild({ parentRow: parentResult, rowId, rows });
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
return rows.filter((o) => getId(o) !== rowId);
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
this.getLeafs = (p) => { const result = []; this.getLeafs_req(p.row ? [p.row] : p.rows, result); return result; };
|
|
336
|
+
this.getNodes = (params) => {
|
|
337
|
+
const result = [];
|
|
338
|
+
this.walk({ childs: params.row ? [params.row] : params.rows, filter: params.filter, map: params.map, result, parentIds: [] });
|
|
339
|
+
return result;
|
|
340
|
+
};
|
|
341
|
+
this.getChilds = (row) => !this.params.getChilds ? undefined : this.params.getChilds(row);
|
|
342
|
+
this.getRowAndParentsById = (p) => {
|
|
343
|
+
let result = { parents: [] };
|
|
344
|
+
this.getRowAndParentsById_req(p.rows, p.rowId, [], result);
|
|
345
|
+
return result;
|
|
346
|
+
};
|
|
347
|
+
this.getParentByRowId = (p) => {
|
|
348
|
+
const res = this.getRowAndParentsById(p);
|
|
349
|
+
if (!res.row) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const { parents } = res;
|
|
353
|
+
if (!parents.length) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
return parents[parents.length - 1];
|
|
357
|
+
};
|
|
358
|
+
this.moveTo = (p) => {
|
|
359
|
+
const { fromId, toId, rows } = p;
|
|
360
|
+
const parentResult = this.getRowAndParentsById({ rowId: toId, rows });
|
|
361
|
+
if (!parentResult.row) {
|
|
362
|
+
return p.rows;
|
|
363
|
+
}
|
|
364
|
+
const childResult = this.getRowAndParentsById({ rowId: fromId, rows });
|
|
365
|
+
if (!childResult.row) {
|
|
366
|
+
return p.rows;
|
|
367
|
+
}
|
|
368
|
+
const editedRows = this.remove({ rowId: fromId, rows });
|
|
369
|
+
return this.edit({ row: this.addToParent({ parentRow: parentResult.row, row: childResult.row }), rows: editedRows });
|
|
370
|
+
};
|
|
371
|
+
this.editChilds = (p) => {
|
|
372
|
+
const { rows, callback } = p;
|
|
373
|
+
const result = { rows };
|
|
374
|
+
this.editChilds_req(rows ? [...rows] : rows, callback, result);
|
|
375
|
+
return result.rows;
|
|
376
|
+
};
|
|
377
|
+
this.duplicate = (p) => {
|
|
378
|
+
const { getId, setId } = this.params;
|
|
379
|
+
const { rows, row } = p;
|
|
380
|
+
const rowId = getId(row);
|
|
381
|
+
const parent = this.getParentByRowId({ rowId, rows });
|
|
382
|
+
const parentId = parent ? getId(parent) : undefined;
|
|
383
|
+
const newRow = setId(row);
|
|
384
|
+
return this.add({ row: newRow, parentId, rows });
|
|
385
|
+
};
|
|
386
|
+
this.sort = (p) => {
|
|
387
|
+
const fixedSorts = p.sorts.map((sort) => ({ getValue: (row) => row[sort.columnId], dir: sort.dir }));
|
|
388
|
+
const result = { rows: p.rows };
|
|
389
|
+
const { rows, parentRow } = p;
|
|
390
|
+
this.sort_req({ sorts: fixedSorts, parentRow, result, rows });
|
|
391
|
+
return result.rows;
|
|
392
|
+
};
|
|
393
|
+
this.params = p;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
export const useSync = (p) => {
|
|
397
|
+
const [logs, setLogs] = useState([]);
|
|
398
|
+
const popup = usePopup();
|
|
399
|
+
const logsRef = useRef(logs);
|
|
400
|
+
logsRef.current = logs;
|
|
401
|
+
const addLog = (log) => setLogs((prev) => [...prev, log]);
|
|
402
|
+
const openLogs = () => popup.addModal({ header: { title: 'Logs' }, body: (_jsx(Cards, { list: { items: logsRef.current, option: { text: (o) => o } } })) });
|
|
403
|
+
const hasUnSync = () => __awaiter(void 0, void 0, void 0, function* () { return !p.cordova ? false : yield p.cordova.storageLoad(p.key + '_unSync', false); });
|
|
404
|
+
const setUnSync = (v) => __awaiter(void 0, void 0, void 0, function* () { if (p.cordova) {
|
|
405
|
+
yield p.cordova.storageSave(p.key + '_unSync', !p.apis ? false : v);
|
|
406
|
+
} });
|
|
407
|
+
const syncCordova = (data) => __awaiter(void 0, void 0, void 0, function* () {
|
|
408
|
+
const unsync = yield hasUnSync();
|
|
409
|
+
if (!unsync) {
|
|
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
|
+
}
|
|
485
|
+
};
|
|
486
|
+
return { saveData, loadData, forceFetchData, openLogs, importData, exportData, popup };
|
|
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" })] }));
|
|
491
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "aio-entity",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "kit",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"types": "index.d.ts",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/mohammadFeiz/aio-entity.git"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"aio-table": "latest",
|
|
16
|
+
"aio-cordova": "latest",
|
|
17
|
+
"aio-entity": "latest",
|
|
18
|
+
"aio-input": "latest",
|
|
19
|
+
"aio-utils": "latest",
|
|
20
|
+
"aio-date": "latest"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"login",
|
|
24
|
+
"otp",
|
|
25
|
+
"jwt"
|
|
26
|
+
],
|
|
27
|
+
"author": "mohammad sharif feiz feiz.ms@gmail.com",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/mohammadFeiz/aio-entity/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/mohammadFeiz/aio-entity#readme"
|
|
33
|
+
}
|