aio-entity 1.0.1 → 1.0.3

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