@ctzy-web-client/data-model 1.0.0 → 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 (51) hide show
  1. package/es/SimpleOrm.mjs +85 -0
  2. package/es/_virtual/_rollupPluginBabelHelpers.mjs +2830 -0
  3. package/es/abstract/DataColumn.mjs +65 -0
  4. package/es/abstract/DataForm.mjs +178 -0
  5. package/es/abstract/DataModel.mjs +223 -0
  6. package/es/abstract/DataModelAdapter.mjs +86 -0
  7. package/es/abstract/DataTable.mjs +339 -0
  8. package/es/abstract/FilterColumn.mjs +44 -0
  9. package/es/abstract/FilterPanel.mjs +392 -0
  10. package/es/abstract/FormColumn.mjs +27 -0
  11. package/es/abstract/TableColumn.mjs +34 -0
  12. package/es/abstract/dataForm2.mjs +178 -0
  13. package/es/abstract/where/CombineCondition.mjs +63 -0
  14. package/es/abstract/where/Condition.mjs +4 -0
  15. package/es/abstract/where/SingleCondition.mjs +22 -0
  16. package/es/abstract/where/Where.mjs +21 -0
  17. package/es/decorator/constant.mjs +11 -0
  18. package/es/decorator/dataForm.mjs +18 -0
  19. package/es/decorator/dataTable.mjs +18 -0
  20. package/es/decorator/filterColumn.mjs +21 -0
  21. package/es/decorator/formColumn.mjs +19 -0
  22. package/es/decorator/tableColumn.mjs +20 -0
  23. package/es/factory.mjs +14 -0
  24. package/es/index.mjs +14 -0
  25. package/es/utils/index.mjs +15 -0
  26. package/lib/SimpleOrm.js +89 -0
  27. package/lib/_virtual/_rollupPluginBabelHelpers.js +2948 -0
  28. package/lib/abstract/DataColumn.js +69 -0
  29. package/lib/abstract/DataForm.js +186 -0
  30. package/lib/abstract/DataModel.js +231 -0
  31. package/lib/abstract/DataModelAdapter.js +90 -0
  32. package/lib/abstract/DataTable.js +343 -0
  33. package/lib/abstract/FilterColumn.js +48 -0
  34. package/lib/abstract/FilterPanel.js +396 -0
  35. package/lib/abstract/FormColumn.js +31 -0
  36. package/lib/abstract/TableColumn.js +38 -0
  37. package/lib/abstract/dataForm2.js +186 -0
  38. package/lib/abstract/where/CombineCondition.js +67 -0
  39. package/lib/abstract/where/Condition.js +8 -0
  40. package/lib/abstract/where/SingleCondition.js +26 -0
  41. package/lib/abstract/where/Where.js +25 -0
  42. package/lib/decorator/constant.js +20 -0
  43. package/lib/decorator/dataForm.js +22 -0
  44. package/lib/decorator/dataTable.js +22 -0
  45. package/lib/decorator/filterColumn.js +25 -0
  46. package/lib/decorator/formColumn.js +23 -0
  47. package/lib/decorator/tableColumn.js +24 -0
  48. package/lib/factory.js +19 -0
  49. package/lib/index.js +36 -0
  50. package/lib/utils/index.js +19 -0
  51. package/package.json +33 -33
@@ -0,0 +1,339 @@
1
+ import { cloneDeep } from 'lodash';
2
+ import { DATA_TABLE, DATA_TABLE_COLUMNS, DATA_TABLE_FILTER_COLUMN } from '../decorator/constant.mjs';
3
+ import { sortColumn } from '../utils/index.mjs';
4
+ import DataModel from './DataModel.mjs';
5
+ import './FilterColumn.mjs';
6
+ import TableColumn from './TableColumn.mjs';
7
+ import { FilterPanel } from './FilterPanel.mjs';
8
+
9
+ class DataTable extends DataModel {
10
+ get filterColumns() {
11
+ return this.filterPanel.getColumns();
12
+ }
13
+ get locking() {
14
+ return this._loadingCount > 0;
15
+ }
16
+ get searchParams() {
17
+ return this.filterPanel._filterParams;
18
+ }
19
+ set whereForFilter(v) {
20
+ this._options.whereForFilter = v;
21
+ if (this.filterPanel) {
22
+ this.filterPanel.whereForFilter = v;
23
+ }
24
+ }
25
+ get whereForFilter() {
26
+ return this._options.whereForFilter;
27
+ }
28
+ set whereParamName(v) {
29
+ this.filterPanel.whereParamName = v;
30
+ }
31
+ get whereParamName() {
32
+ return this.filterPanel.whereParamName;
33
+ }
34
+ set encryptCondition(v) {
35
+ this.filterPanel.encryptCondition = v;
36
+ }
37
+ get encryptCondition() {
38
+ return this.filterPanel.encryptCondition;
39
+ }
40
+ constructor(option) {
41
+ super(option);
42
+ this._userService = null;
43
+ this._loadingCount = 0;
44
+ this.totalRecCount = 0;
45
+ this.pageNum = 1;
46
+ this.pageSize = 20;
47
+ this.selection = [];
48
+ this.data = [];
49
+ this._defaultColumnSort = [];
50
+ this.inited = false;
51
+ this.filterPanel = new FilterPanel(this);
52
+ this.whereForFilter = this.whereForFilter;
53
+ this._loadCallbacks = [];
54
+ this.filterPanel.on("ready", () => {
55
+ for (let callback of this._loadCallbacks) {
56
+ callback();
57
+ }
58
+ });
59
+ }
60
+ getTotalPage() {
61
+ const totalPage = Math.ceil(this.totalRecCount / this.pageSize);
62
+ return Number.isNaN(totalPage) ? 0 : totalPage;
63
+ }
64
+ setOptions(options) {
65
+ var _a;
66
+ if (!options.model) {
67
+ return;
68
+ }
69
+ super.setOptions({
70
+ ...options.model[DATA_TABLE],
71
+ model: options.model
72
+ });
73
+ this.whereForFilter = (_a = options.model[DATA_TABLE].whereForFilter) != null ? _a : false;
74
+ }
75
+ setSelection(selection) {
76
+ const oldSelection = this.selection;
77
+ this.selection = selection;
78
+ this.emit("select-change", selection, oldSelection);
79
+ }
80
+ getSelection() {
81
+ return this.selection;
82
+ }
83
+ clearSelection() {
84
+ this.setSelection([]);
85
+ }
86
+ setWhereParamName(whereParamName) {
87
+ this.whereParamName = whereParamName;
88
+ }
89
+ init() {
90
+ if (this.inited) {
91
+ return;
92
+ }
93
+ super.init();
94
+ const model = this.model;
95
+ if (!model) {
96
+ return;
97
+ }
98
+ if (this.extendField) {
99
+ this._loadingCount++;
100
+ return;
101
+ } else {
102
+ let tableColumnConfigs = model.tableColumns || [];
103
+ const tableColumns = (model[DATA_TABLE_COLUMNS] || []).map((columnConfig) => new TableColumn({
104
+ type: columnConfig.formComponent === "BwaRichText" ? "html" : "",
105
+ ...columnConfig
106
+ }));
107
+ tableColumnConfigs = tableColumnConfigs.concat(tableColumns.filter((column) => !tableColumnConfigs.includes(column.name)).map((column) => column.attrName));
108
+ sortColumn(tableColumns, tableColumnConfigs).forEach((tableColumn) => {
109
+ this.addColumn(tableColumn);
110
+ });
111
+ }
112
+ this._defaultColumnSort = this.getColumns().map((column) => ({
113
+ name: column.attrName,
114
+ visible: column.visible
115
+ }));
116
+ this.filterPanel.init([], model[DATA_TABLE_FILTER_COLUMN] || [], model.filterColumns || []);
117
+ this.inited = true;
118
+ this.emit("init-completed");
119
+ }
120
+ setData(data, append = false) {
121
+ if (!append) {
122
+ this.data.length = 0;
123
+ }
124
+ this.data.push(...data);
125
+ this.ready = true;
126
+ }
127
+ async setDataBeforeMount(data = []) {
128
+ if (this.extendFieldInfos.every((a) => a.type !== 0)) {
129
+ return data;
130
+ }
131
+ const colMap = {};
132
+ let userListResult = {};
133
+ if (this.extendFieldInfos.some((a) => ["BwaUserSelect", "BwaUserMultiSelect"].includes(a.component))) {
134
+ if (this._userService) {
135
+ userListResult = await this._userService({
136
+ name: ""
137
+ });
138
+ }
139
+ }
140
+ this.extendFieldInfos.forEach((e) => {
141
+ var _a;
142
+ if (e.type === 0) {
143
+ if (["BwaSelect", "BwaMultiSelect"].includes(e.component)) {
144
+ colMap[e.name] = (_a = e.extendInfo) == null ? void 0 : _a.options;
145
+ }
146
+ if (["BwaUserSelect", "BwaUserMultiSelect"].includes(e.component)) {
147
+ colMap[e.name] = (userListResult == null ? void 0 : userListResult.data) || [];
148
+ }
149
+ }
150
+ });
151
+ const getValueByString = (key, value) => {
152
+ let arr = colMap[key] || [];
153
+ let find = arr.find((item) => item.value == value);
154
+ return find ? find.label : value || "-";
155
+ };
156
+ const getValueByArray = (key, valueList) => {
157
+ let list = [];
158
+ valueList.forEach((v) => {
159
+ list.push(getValueByString(key, v));
160
+ });
161
+ return list;
162
+ };
163
+ data.forEach((e) => {
164
+ let obj = e.columnJson || e.json || {};
165
+ Object.keys(obj).forEach((key) => {
166
+ if (["string", "number"].includes(typeof obj[key]))
167
+ obj[key] = getValueByString(key, obj[key]);
168
+ if (Array.isArray(obj[key]))
169
+ obj[key] = getValueByArray(key, obj[key]);
170
+ });
171
+ });
172
+ return data;
173
+ }
174
+ async load(params) {
175
+ if (!this.filterPanel.ready) {
176
+ return new Promise((resolve, reject) => {
177
+ this._loadCallbacks.push(() => {
178
+ this.load(params).then(resolve, reject);
179
+ });
180
+ });
181
+ }
182
+ this.ready = false;
183
+ this._loadingCount++;
184
+ const loadParams = {
185
+ [this.adapter.pagerNumParamName]: this.pageNum,
186
+ [this.adapter.recCountParamName]: this.pageSize,
187
+ ...this.defaultParams,
188
+ ...this.filterPanel.getAjaxParams(),
189
+ ...params
190
+ };
191
+ this.emit("before-load", loadParams);
192
+ return this.adapter.loadDataListHandle(this, loadParams).then((res) => {
193
+ this.totalRecCount = res[this.adapter.totalRecCountNodeName] ? res[this.adapter.totalRecCountNodeName] * 1 : 0;
194
+ const data = (res[this.adapter.dataNodeName] || []).map(this.formatData.bind(this));
195
+ this.setDataBeforeMount(data).then((arr) => {
196
+ this.setData(arr);
197
+ });
198
+ this.emit("load-successfully", res);
199
+ return res;
200
+ }).catch((e) => {
201
+ this.emit("load-failed", e);
202
+ return Promise.reject(e);
203
+ }).finally(() => {
204
+ this.emit("load-complete");
205
+ this._loadingCount--;
206
+ });
207
+ }
208
+ refresh() {
209
+ return this.load();
210
+ }
211
+ pageTo(pageNum) {
212
+ this.pageNum = pageNum || 1;
213
+ this.emit("current-page-change", this.pageNum);
214
+ return this.refresh();
215
+ }
216
+ setPageSize(pageSize) {
217
+ this.pageSize = pageSize;
218
+ this.emit("page-size-change", this.pageSize);
219
+ return this.refresh();
220
+ }
221
+ async delete(params) {
222
+ if (this.locking) {
223
+ return;
224
+ }
225
+ this._loadingCount++;
226
+ if (typeof params !== "object") {
227
+ params = {
228
+ [this.primaryKey]: params
229
+ };
230
+ }
231
+ return this.adapter.deleteHandle(this, params).then((res) => {
232
+ this.emit("delete-successfully", res);
233
+ return res;
234
+ }).catch((e) => {
235
+ this.emit("delete-failed", e);
236
+ throw e;
237
+ }).finally(() => {
238
+ this._loadingCount--;
239
+ this.emit("delete-complete");
240
+ });
241
+ }
242
+ formatExtendFieldInfo(extendFieldInfo) {
243
+ return {
244
+ ...super.formatExtendFieldInfo(extendFieldInfo),
245
+ component: "",
246
+ formComponent: extendFieldInfo.component,
247
+ visible: !!extendFieldInfo.enablePageShow
248
+ };
249
+ }
250
+ mergeTableColumn(formatedExtendFieldInfo, modelFieldInfo) {
251
+ var _a;
252
+ return {
253
+ ...super.mergeTableColumn(formatedExtendFieldInfo, modelFieldInfo),
254
+ visible: (_a = modelFieldInfo == null ? void 0 : modelFieldInfo.visible) != null ? _a : true
255
+ };
256
+ }
257
+ setExtendFieldInfos(extendFieldInfos) {
258
+ this.extendFieldInfos = extendFieldInfos;
259
+ const mergedColumnInfos = this.mergeTableColumns(extendFieldInfos, this.model[DATA_TABLE_COLUMNS]);
260
+ const tableColumns = mergedColumnInfos.map((columnInfo) => new TableColumn({
261
+ type: columnInfo.formComponent === "BwaRichText" ? "html" : "",
262
+ ...columnInfo
263
+ }));
264
+ this.setColumns(tableColumns);
265
+ this._defaultColumnSort = this.getColumns().map((column) => ({
266
+ name: column.attrName,
267
+ visible: column.visible
268
+ }));
269
+ this.filterPanel.init(extendFieldInfos, this.model[DATA_TABLE_FILTER_COLUMN] || [], this.model.filterColumns || []);
270
+ this._loadingCount--;
271
+ this.inited = true;
272
+ this.emit("init-completed");
273
+ }
274
+ async refreshAfterDelete(params) {
275
+ const deleteResult = await this.delete(params);
276
+ this.refresh();
277
+ return deleteResult;
278
+ }
279
+ sortColumns(columns) {
280
+ return sortColumn(columns, this.model.tableColumns || []);
281
+ }
282
+ resetColumnSort() {
283
+ if (this.isDefaultColumnSort()) {
284
+ return;
285
+ }
286
+ this._defaultColumnSort.forEach((item) => {
287
+ const column = this.getColumn(item.name);
288
+ if (column) {
289
+ column.visible = item.visible;
290
+ }
291
+ });
292
+ const displayColumns = this._defaultColumnSort.filter((item) => item.visible).map((item) => {
293
+ return this.getColumn(item.name);
294
+ });
295
+ this.setDisplayColumns(displayColumns);
296
+ }
297
+ isDefaultColumnSort() {
298
+ const displayColumns = this.getDisplayColumns();
299
+ const defaultVisibleColumnInfos = this._defaultColumnSort.filter((item) => item.visible);
300
+ if (displayColumns.length !== defaultVisibleColumnInfos.length) {
301
+ return false;
302
+ }
303
+ for (let i = 0; i < displayColumns.length; i++) {
304
+ if (displayColumns[i].attrName !== defaultVisibleColumnInfos[i].name || displayColumns[i].visible !== defaultVisibleColumnInfos[i].visible) {
305
+ return false;
306
+ }
307
+ }
308
+ return true;
309
+ }
310
+ addFilterColumn(column) {
311
+ this.filterPanel.addColumn(column);
312
+ }
313
+ removeFilterColumn(column) {
314
+ this.filterPanel.removeColumn(column);
315
+ }
316
+ setFilterColumns(columns) {
317
+ this.filterPanel.setColumns(columns);
318
+ }
319
+ getFilterColumns() {
320
+ return this.filterPanel.getColumns();
321
+ }
322
+ getFilterColumn(columnName) {
323
+ return this.filterPanel.getColumn(columnName);
324
+ }
325
+ resetSearchParams() {
326
+ this.filterPanel.resetFilterParams();
327
+ }
328
+ clone() {
329
+ const cloned = super.clone();
330
+ cloned.data = cloneDeep(this.data);
331
+ cloned.filterPanel = cloneDeep(this.filterPanel);
332
+ cloned.selection = this.selection.map((select) => {
333
+ return cloned.data.find((item) => item[this.primaryKey] === select[this.primaryKey]);
334
+ }).filter(Boolean);
335
+ return cloned;
336
+ }
337
+ }
338
+
339
+ export { DataTable as default };
@@ -0,0 +1,44 @@
1
+ import DataColumn from './DataColumn.mjs';
2
+
3
+ class FilterColumn extends DataColumn {
4
+ constructor(filterPanel, options) {
5
+ var _a, _b, _c, _d;
6
+ super(options);
7
+ this.closable = true;
8
+ this.attrMapping = {
9
+ start: {
10
+ name: "${name}",
11
+ op: ">="
12
+ },
13
+ end: {
14
+ name: "${name}",
15
+ op: "<="
16
+ }
17
+ };
18
+ this.conditionOper = "AND";
19
+ this.formComponent = "";
20
+ this.twoFieldsDatePicker = false;
21
+ this.closable = (_a = options.closable) != null ? _a : this.closable;
22
+ this.attrMapping = (_c = (_b = options.attrMapping) != null ? _b : options.whereAttrMapping) != null ? _c : this.attrMapping;
23
+ this.filterPanel = filterPanel;
24
+ this.conditionOper = options.conditionOper;
25
+ this.formComponent = (_d = options.formComponent) != null ? _d : this.formComponent;
26
+ this.twoFieldsDatePicker = options.twoFieldsDatePicker;
27
+ }
28
+ show() {
29
+ const visible = this.visible;
30
+ super.show();
31
+ if (visible !== this.visible) {
32
+ this.filterPanel.appliedColumn(this);
33
+ }
34
+ }
35
+ hide() {
36
+ const visible = this.visible;
37
+ super.hide();
38
+ if (visible !== this.visible) {
39
+ this.filterPanel.cancelAppliedColumn(this);
40
+ }
41
+ }
42
+ }
43
+
44
+ export { FilterColumn as default };