@sankhyalabs/core 1.0.42 → 1.0.43

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 (135) hide show
  1. package/.eslintignore +1 -1
  2. package/.eslintrc.cjs +34 -34
  3. package/README.md +54 -54
  4. package/dist/dataunit/DataUnit.d.ts +82 -82
  5. package/dist/dataunit/DataUnit.js +278 -278
  6. package/dist/dataunit/metadata/DataType.d.ts +9 -9
  7. package/dist/dataunit/metadata/DataType.js +36 -36
  8. package/dist/dataunit/metadata/UnitMetadata.d.ts +71 -71
  9. package/dist/dataunit/metadata/UnitMetadata.js +31 -31
  10. package/dist/dataunit/state/HistReducer.d.ts +10 -10
  11. package/dist/dataunit/state/HistReducer.js +27 -27
  12. package/dist/dataunit/state/StateManager.d.ts +57 -57
  13. package/dist/dataunit/state/StateManager.js +96 -96
  14. package/dist/dataunit/state/action/DataUnitAction.d.ts +28 -28
  15. package/dist/dataunit/state/action/DataUnitAction.js +33 -33
  16. package/dist/dataunit/state/slice/AddedRecordsSlice.d.ts +11 -11
  17. package/dist/dataunit/state/slice/AddedRecordsSlice.js +25 -25
  18. package/dist/dataunit/state/slice/ChangesSlice.d.ts +12 -12
  19. package/dist/dataunit/state/slice/ChangesSlice.js +72 -72
  20. package/dist/dataunit/state/slice/CurrentRecordsSlice.d.ts +11 -11
  21. package/dist/dataunit/state/slice/CurrentRecordsSlice.js +45 -45
  22. package/dist/dataunit/state/slice/RecordsSlice.d.ts +10 -10
  23. package/dist/dataunit/state/slice/RecordsSlice.js +43 -43
  24. package/dist/dataunit/state/slice/RemovedRecordsSlice.d.ts +9 -9
  25. package/dist/dataunit/state/slice/RemovedRecordsSlice.js +24 -24
  26. package/dist/dataunit/state/slice/SelectionSlice.d.ts +11 -11
  27. package/dist/dataunit/state/slice/SelectionSlice.js +111 -111
  28. package/dist/dataunit/state/slice/UnitMetadataSlice.d.ts +11 -11
  29. package/dist/dataunit/state/slice/UnitMetadataSlice.js +20 -20
  30. package/dist/http/AuthorizedServiceCaller.d.ts +11 -11
  31. package/dist/http/AuthorizedServiceCaller.js +53 -53
  32. package/dist/http/HttpProvider.d.ts +25 -25
  33. package/dist/http/HttpProvider.js +73 -73
  34. package/dist/http/RequestMetadata.d.ts +30 -30
  35. package/dist/http/RequestMetadata.js +24 -24
  36. package/dist/http/SkwHttpProvider.d.ts +9 -9
  37. package/dist/http/SkwHttpProvider.js +66 -66
  38. package/dist/index.d.ts +16 -16
  39. package/dist/index.js +17 -17
  40. package/dist/ui/FloatingManager.d.ts +24 -24
  41. package/dist/ui/FloatingManager.js +122 -122
  42. package/dist/utils/ApplicationContext.d.ts +5 -5
  43. package/dist/utils/ApplicationContext.js +16 -16
  44. package/dist/utils/CriteriaModel.d.ts +109 -109
  45. package/dist/utils/CriteriaModel.js +173 -173
  46. package/dist/utils/CriteriaParameter.d.ts +69 -69
  47. package/dist/utils/CriteriaParameter.js +82 -82
  48. package/dist/utils/DateUtils.d.ts +5 -5
  49. package/dist/utils/DateUtils.js +42 -42
  50. package/dist/utils/MaskFormatter.d.ts +126 -126
  51. package/dist/utils/MaskFormatter.js +275 -275
  52. package/dist/utils/NumberUtils.d.ts +53 -53
  53. package/dist/utils/NumberUtils.js +141 -141
  54. package/dist/utils/StringUtils.d.ts +18 -18
  55. package/dist/utils/StringUtils.js +53 -53
  56. package/dist/utils/TimeFormatter.d.ts +33 -33
  57. package/dist/utils/TimeFormatter.js +97 -97
  58. package/jest.config.ts +16 -16
  59. package/mock/http/XMLHttpRequest-mock.js +25 -25
  60. package/package.json +37 -37
  61. package/src/dataunit/DataUnit.ts +356 -356
  62. package/src/dataunit/metadata/DataType.ts +37 -37
  63. package/src/dataunit/metadata/UnitMetadata.ts +82 -82
  64. package/src/dataunit/state/HistReducer.ts +33 -33
  65. package/src/dataunit/state/StateManager.ts +141 -141
  66. package/src/dataunit/state/action/DataUnitAction.ts +50 -50
  67. package/src/dataunit/state/slice/AddedRecordsSlice.ts +32 -32
  68. package/src/dataunit/state/slice/ChangesSlice.ts +90 -90
  69. package/src/dataunit/state/slice/CurrentRecordsSlice.ts +53 -53
  70. package/src/dataunit/state/slice/RecordsSlice.ts +56 -56
  71. package/src/dataunit/state/slice/RemovedRecordsSlice.ts +29 -29
  72. package/src/dataunit/state/slice/SelectionSlice.ts +130 -130
  73. package/src/dataunit/state/slice/UnitMetadataSlice.ts +29 -29
  74. package/src/http/AuthorizedServiceCaller.ts +58 -58
  75. package/src/http/HttpProvider.ts +93 -93
  76. package/src/http/RequestMetadata.ts +41 -41
  77. package/src/http/SkwHttpProvider.ts +77 -77
  78. package/src/index.ts +44 -44
  79. package/src/ui/FloatingManager.ts +165 -165
  80. package/src/utils/ApplicationContext.ts +18 -18
  81. package/src/utils/CriteriaModel.ts +207 -207
  82. package/src/utils/CriteriaParameter.ts +107 -107
  83. package/src/utils/DateUtils.ts +52 -52
  84. package/src/utils/MaskFormatter.ts +322 -322
  85. package/src/utils/NumberUtils.ts +174 -174
  86. package/src/utils/StringUtils.ts +60 -60
  87. package/src/utils/TimeFormatter.ts +97 -97
  88. package/test/http/HttpProvider.spec.ts +34 -34
  89. package/test/http/SkwHttpProvider.ts.spec.ts +33 -33
  90. package/test/util/CriteriaModel.spec.ts +231 -231
  91. package/test/util/CriteriaParameter.spec.ts +51 -51
  92. package/test/util/MaskFormatter.spec.ts +138 -138
  93. package/test/util/NumberUtils.spec.ts +156 -156
  94. package/test/util/StringUtils.spec.ts +43 -43
  95. package/test/util/TimeFormatter.spec.ts +25 -25
  96. package/tsconfig.json +15 -15
  97. package/dist/application/Application.d.ts +0 -8
  98. package/dist/application/Application.js +0 -19
  99. package/dist/application/Application.js.map +0 -1
  100. package/dist/http/data-fetcher/HttpFetcher.d.ts +0 -14
  101. package/dist/http/data-fetcher/HttpFetcher.js +0 -164
  102. package/dist/http/data-fetcher/HttpFetcher.js.map +0 -1
  103. package/dist/http/data-fetcher/default/DefaultGraphQL.d.ts +0 -3
  104. package/dist/http/data-fetcher/default/DefaultGraphQL.js +0 -18
  105. package/dist/http/data-fetcher/default/DefaultGraphQL.js.map +0 -1
  106. package/dist/http/data-fetcher/default/application-config-fetcher.d.ts +0 -6
  107. package/dist/http/data-fetcher/default/application-config-fetcher.js +0 -21
  108. package/dist/http/data-fetcher/default/application-config-fetcher.js.map +0 -1
  109. package/dist/http/data-fetcher/default/data-unit-graphql.d.ts +0 -3
  110. package/dist/http/data-fetcher/default/data-unit-graphql.js +0 -18
  111. package/dist/http/data-fetcher/default/data-unit-graphql.js.map +0 -1
  112. package/dist/http/data-fetcher/default/dataunit-executor.d.ts +0 -10
  113. package/dist/http/data-fetcher/default/dataunit-executor.js +0 -56
  114. package/dist/http/data-fetcher/default/dataunit-executor.js.map +0 -1
  115. package/dist/http/data-fetcher/default/dataunit-fetcher.d.ts +0 -9
  116. package/dist/http/data-fetcher/default/dataunit-fetcher.js +0 -69
  117. package/dist/http/data-fetcher/default/dataunit-fetcher.js.map +0 -1
  118. package/dist/http/data-fetcher/default/form-config-fetcher.d.ts +0 -2
  119. package/dist/http/data-fetcher/default/form-config-fetcher.js +0 -3
  120. package/dist/http/data-fetcher/default/form-config-fetcher.js.map +0 -1
  121. package/dist/http/data-fetcher/default/grid-config-fetcher.d.ts +0 -2
  122. package/dist/http/data-fetcher/default/grid-config-fetcher.js +0 -3
  123. package/dist/http/data-fetcher/default/grid-config-fetcher.js.map +0 -1
  124. package/dist/http/data-fetcher/default/index.d.ts +0 -0
  125. package/dist/http/data-fetcher/default/index.js +0 -2
  126. package/dist/http/data-fetcher/default/index.js.map +0 -1
  127. package/dist/http/data-fetcher/default/sankhya-graphql.d.ts +0 -3
  128. package/dist/http/data-fetcher/default/sankhya-graphql.js +0 -8
  129. package/dist/http/data-fetcher/default/sankhya-graphql.js.map +0 -1
  130. package/dist/http/data-fetcher/graphql/DefaultGraphQl.d.ts +0 -3
  131. package/dist/http/data-fetcher/graphql/DefaultGraphQl.js +0 -18
  132. package/dist/http/data-fetcher/graphql/DefaultGraphQl.js.map +0 -1
  133. package/dist/http/data-fetcher/state/LoadStateManager.d.ts +0 -23
  134. package/dist/http/data-fetcher/state/LoadStateManager.js +0 -78
  135. package/dist/http/data-fetcher/state/LoadStateManager.js.map +0 -1
@@ -1,279 +1,279 @@
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 { convertType } from "./metadata/DataType.js";
11
- import { DataUnitAction, Action } from "./state/action/DataUnitAction.js";
12
- import StateManager from "./state/StateManager.js";
13
- import { HistReducer, canRedo, canUndo } from "./state/HistReducer.js";
14
- import { UnitMetadataReducer, getField, getMetadata } from "./state/slice/UnitMetadataSlice.js";
15
- import { RecordsReducer } from "./state/slice/RecordsSlice.js";
16
- import { SelectionReducer, getSelection, hasNext, hasPrevious } from "./state/slice/SelectionSlice.js";
17
- import { ChangesReducer, isDirty, getChangesToSave } from "./state/slice/ChangesSlice.js";
18
- import { RemovedRecordsReducer } from "./state/slice/RemovedRecordsSlice.js";
19
- import { AddedRecordsReducer, prepareAddedRecordId } from "./state/slice/AddedRecordsSlice.js";
20
- import { CurrentRecordsReducer, getCurrentRecords, getFieldValue } from "./state/slice/CurrentRecordsSlice.js";
21
- export default class DataUnit {
22
- constructor(name) {
23
- this._name = name;
24
- this._stateManager = new StateManager([
25
- HistReducer,
26
- UnitMetadataReducer,
27
- RecordsReducer,
28
- RemovedRecordsReducer,
29
- AddedRecordsReducer,
30
- SelectionReducer,
31
- ChangesReducer,
32
- CurrentRecordsReducer
33
- ]);
34
- this._observers = [];
35
- this._filterProviders = [];
36
- this._sortingProvider = undefined;
37
- this._interceptors = [];
38
- }
39
- get name() {
40
- return this._name;
41
- }
42
- // Métodos privados
43
- validateAndTypeValue(fieldName, newValue) {
44
- //FIXME: Validações devem ser feitas aqui
45
- const descriptor = this.getField(fieldName);
46
- return descriptor ? convertType(descriptor.dataType, newValue) : newValue;
47
- }
48
- getFilters() {
49
- let filters = undefined;
50
- this._filterProviders.forEach(p => {
51
- const f = p.getFilter(this.name);
52
- if (f) {
53
- filters = (filters || []).concat(f);
54
- }
55
- });
56
- return filters;
57
- }
58
- getSort() {
59
- return this._sortingProvider ? this._sortingProvider.getSort(this._name) : undefined;
60
- }
61
- // Loaders
62
- loadMetadata() {
63
- return __awaiter(this, void 0, void 0, function* () {
64
- this.dispatchAction(Action.LOADING_METADATA);
65
- return new Promise((resolve, fail) => {
66
- if (this.metadataLoader) {
67
- this.metadataLoader(this).then(metadata => {
68
- this.metadata = metadata;
69
- resolve(this.metadata);
70
- }).catch(error => fail(error));
71
- }
72
- });
73
- });
74
- }
75
- loadData() {
76
- return __awaiter(this, void 0, void 0, function* () {
77
- this.dispatchAction(Action.LOADING_DATA);
78
- return new Promise((resolve, fail) => {
79
- if (this.dataLoader) {
80
- const sort = this.getSort();
81
- const filters = this.getFilters();
82
- this.dataLoader(this, sort, filters).then(records => {
83
- this.records = records;
84
- resolve(this.records);
85
- }).catch(error => fail(error));
86
- }
87
- });
88
- });
89
- }
90
- saveData() {
91
- return __awaiter(this, void 0, void 0, function* () {
92
- const changes = getChangesToSave(this._name, this._stateManager);
93
- if (changes.length > 0) {
94
- this.dispatchAction(Action.SAVING_DATA);
95
- return new Promise((resolve, fail) => {
96
- if (this.saveLoader) {
97
- this.saveLoader(this, changes).then(records => this.dispatchAction(Action.DATA_SAVED, { changes, records })).catch(error => fail(error));
98
- }
99
- });
100
- }
101
- return Promise.resolve();
102
- });
103
- }
104
- removeSelectedRecords(buffered = false) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- const selection = getSelection(this._stateManager);
107
- if (selection) {
108
- return this.removeRecords(selection, buffered);
109
- }
110
- return Promise.resolve(selection);
111
- });
112
- }
113
- removeRecords(records, buffered = false) {
114
- return __awaiter(this, void 0, void 0, function* () {
115
- if (records) {
116
- if (buffered || !this.removeLoader) {
117
- this.dispatchAction(Action.RECORDS_REMOVED, { records, buffered: true });
118
- }
119
- else {
120
- this.dispatchAction(Action.REMOVING_RECORDS);
121
- return new Promise((resolve, fail) => {
122
- if (this.removeLoader) {
123
- this.removeLoader(this, records).then(records => {
124
- this.dispatchAction(Action.RECORDS_REMOVED, { records, buffered: false });
125
- resolve(records);
126
- }).catch(error => fail(error));
127
- }
128
- });
129
- }
130
- }
131
- return Promise.resolve(records);
132
- });
133
- }
134
- // API
135
- valueFromString(fieldName, value) {
136
- const descriptor = this.getField(fieldName);
137
- return descriptor ? convertType(descriptor.dataType, value) : value;
138
- }
139
- addInterceptor(interceptor) {
140
- this._interceptors.push(interceptor);
141
- }
142
- addFilterProvider(provider) {
143
- this._filterProviders.push(provider);
144
- }
145
- set sortingProvider(provider) {
146
- this._sortingProvider = provider;
147
- }
148
- set metadata(md) {
149
- this.dispatchAction(Action.METADATA_LOADED, md);
150
- }
151
- get metadata() {
152
- return getMetadata(this._stateManager);
153
- }
154
- set records(r) {
155
- this.dispatchAction(Action.DATA_LOADED, r);
156
- }
157
- get records() {
158
- const records = getCurrentRecords(this._stateManager);
159
- return records ? Array.from(records.values()) : [];
160
- }
161
- getField(fieldName) {
162
- return getField(this._stateManager, fieldName);
163
- }
164
- addRecord() {
165
- this.dispatchAction(Action.RECORDS_ADDED, prepareAddedRecordId(this._stateManager, [{}]));
166
- }
167
- copySelected() {
168
- const selectedRecords = this.getSelectedRecords();
169
- if (selectedRecords) {
170
- this.dispatchAction(Action.RECORDS_COPIED, prepareAddedRecordId(this._stateManager, selectedRecords));
171
- }
172
- }
173
- getFieldValue(fieldName) {
174
- return getFieldValue(this._stateManager, fieldName);
175
- }
176
- setFieldValue(fieldName, newValue, records) {
177
- const typedValue = this.validateAndTypeValue(fieldName, newValue);
178
- const currentValue = this.getFieldValue(fieldName);
179
- if (currentValue !== typedValue) {
180
- this.dispatchAction(Action.DATA_CHANGED, { [fieldName]: typedValue, records });
181
- }
182
- }
183
- getSelection() {
184
- return getSelection(this._stateManager);
185
- }
186
- setSelection(selection) {
187
- this.dispatchAction(Action.SELECTION_CHANGED, { type: "id", selection });
188
- }
189
- setSelectionByIndex(selection) {
190
- this.dispatchAction(Action.SELECTION_CHANGED, { type: "index", selection });
191
- }
192
- getSelectedRecords() {
193
- const selection = this.getSelection();
194
- if (selection) {
195
- const currentRecords = this.records;
196
- return currentRecords === null || currentRecords === void 0 ? void 0 : currentRecords.filter(r => selection.includes(r.__record__id__));
197
- }
198
- }
199
- nextRecord() {
200
- this.dispatchAction(Action.NEXT_SELECTED);
201
- }
202
- previousRecord() {
203
- this.dispatchAction(Action.PREVIOUS_SELECTED);
204
- }
205
- cancelEdition() {
206
- this.dispatchAction(Action.EDITION_CANCELED);
207
- }
208
- isDirty() {
209
- return isDirty(this._stateManager);
210
- }
211
- hasNext() {
212
- return hasNext(this._stateManager);
213
- }
214
- hasPrevious() {
215
- return hasPrevious(this._stateManager);
216
- }
217
- canUndo() {
218
- return canUndo(this._stateManager);
219
- }
220
- canRedo() {
221
- return canRedo(this._stateManager);
222
- }
223
- undo() {
224
- this.dispatchAction(Action.CHANGE_UNDONE);
225
- }
226
- redo() {
227
- this.dispatchAction(Action.CHANGE_REDONE);
228
- }
229
- toString() {
230
- return this.name;
231
- }
232
- // Actions / State manager
233
- dispatchAction(actionType, payload) {
234
- var _a;
235
- let action = new DataUnitAction(actionType, payload);
236
- (_a = this._interceptors) === null || _a === void 0 ? void 0 : _a.forEach(interceptor => {
237
- if (action) {
238
- action = interceptor.interceptAction(action);
239
- }
240
- });
241
- if (action) {
242
- this._stateManager.process(action);
243
- this._observers.forEach(f => f(action));
244
- }
245
- }
246
- subscribe(observer) {
247
- this._observers.push(observer);
248
- }
249
- unsubscribe(observer) {
250
- this._observers = this._observers.filter(f => f !== observer);
251
- }
252
- }
253
- export var ChangeOperation;
254
- (function (ChangeOperation) {
255
- ChangeOperation["INSERT"] = "INSERT";
256
- ChangeOperation["UPDATE"] = "UPDATE";
257
- ChangeOperation["DELETE"] = "DELETE";
258
- })(ChangeOperation || (ChangeOperation = {}));
259
- export class Change {
260
- constructor(dataUnit, record, updates, operation) {
261
- this.dataUnit = dataUnit;
262
- this.record = record;
263
- this.updatingFields = updates;
264
- this._operation = operation;
265
- }
266
- get operation() {
267
- return this._operation.toString();
268
- }
269
- isInsert() {
270
- return this._operation === ChangeOperation.INSERT;
271
- }
272
- isDelete() {
273
- return this._operation === ChangeOperation.DELETE;
274
- }
275
- isUpdate() {
276
- return this._operation === ChangeOperation.UPDATE;
277
- }
278
- }
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 { convertType } from "./metadata/DataType.js";
11
+ import { DataUnitAction, Action } from "./state/action/DataUnitAction.js";
12
+ import StateManager from "./state/StateManager.js";
13
+ import { HistReducer, canRedo, canUndo } from "./state/HistReducer.js";
14
+ import { UnitMetadataReducer, getField, getMetadata } from "./state/slice/UnitMetadataSlice.js";
15
+ import { RecordsReducer } from "./state/slice/RecordsSlice.js";
16
+ import { SelectionReducer, getSelection, hasNext, hasPrevious } from "./state/slice/SelectionSlice.js";
17
+ import { ChangesReducer, isDirty, getChangesToSave } from "./state/slice/ChangesSlice.js";
18
+ import { RemovedRecordsReducer } from "./state/slice/RemovedRecordsSlice.js";
19
+ import { AddedRecordsReducer, prepareAddedRecordId } from "./state/slice/AddedRecordsSlice.js";
20
+ import { CurrentRecordsReducer, getCurrentRecords, getFieldValue } from "./state/slice/CurrentRecordsSlice.js";
21
+ export default class DataUnit {
22
+ constructor(name) {
23
+ this._name = name;
24
+ this._stateManager = new StateManager([
25
+ HistReducer,
26
+ UnitMetadataReducer,
27
+ RecordsReducer,
28
+ RemovedRecordsReducer,
29
+ AddedRecordsReducer,
30
+ SelectionReducer,
31
+ ChangesReducer,
32
+ CurrentRecordsReducer
33
+ ]);
34
+ this._observers = [];
35
+ this._filterProviders = [];
36
+ this._sortingProvider = undefined;
37
+ this._interceptors = [];
38
+ }
39
+ get name() {
40
+ return this._name;
41
+ }
42
+ // Métodos privados
43
+ validateAndTypeValue(fieldName, newValue) {
44
+ //FIXME: Validações devem ser feitas aqui
45
+ const descriptor = this.getField(fieldName);
46
+ return descriptor ? convertType(descriptor.dataType, newValue) : newValue;
47
+ }
48
+ getFilters() {
49
+ let filters = undefined;
50
+ this._filterProviders.forEach(p => {
51
+ const f = p.getFilter(this.name);
52
+ if (f) {
53
+ filters = (filters || []).concat(f);
54
+ }
55
+ });
56
+ return filters;
57
+ }
58
+ getSort() {
59
+ return this._sortingProvider ? this._sortingProvider.getSort(this._name) : undefined;
60
+ }
61
+ // Loaders
62
+ loadMetadata() {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ this.dispatchAction(Action.LOADING_METADATA);
65
+ return new Promise((resolve, fail) => {
66
+ if (this.metadataLoader) {
67
+ this.metadataLoader(this).then(metadata => {
68
+ this.metadata = metadata;
69
+ resolve(this.metadata);
70
+ }).catch(error => fail(error));
71
+ }
72
+ });
73
+ });
74
+ }
75
+ loadData() {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ this.dispatchAction(Action.LOADING_DATA);
78
+ return new Promise((resolve, fail) => {
79
+ if (this.dataLoader) {
80
+ const sort = this.getSort();
81
+ const filters = this.getFilters();
82
+ this.dataLoader(this, sort, filters).then(records => {
83
+ this.records = records;
84
+ resolve(this.records);
85
+ }).catch(error => fail(error));
86
+ }
87
+ });
88
+ });
89
+ }
90
+ saveData() {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ const changes = getChangesToSave(this._name, this._stateManager);
93
+ if (changes.length > 0) {
94
+ this.dispatchAction(Action.SAVING_DATA);
95
+ return new Promise((resolve, fail) => {
96
+ if (this.saveLoader) {
97
+ this.saveLoader(this, changes).then(records => this.dispatchAction(Action.DATA_SAVED, { changes, records })).catch(error => fail(error));
98
+ }
99
+ });
100
+ }
101
+ return Promise.resolve();
102
+ });
103
+ }
104
+ removeSelectedRecords(buffered = false) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ const selection = getSelection(this._stateManager);
107
+ if (selection) {
108
+ return this.removeRecords(selection, buffered);
109
+ }
110
+ return Promise.resolve(selection);
111
+ });
112
+ }
113
+ removeRecords(records, buffered = false) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ if (records) {
116
+ if (buffered || !this.removeLoader) {
117
+ this.dispatchAction(Action.RECORDS_REMOVED, { records, buffered: true });
118
+ }
119
+ else {
120
+ this.dispatchAction(Action.REMOVING_RECORDS);
121
+ return new Promise((resolve, fail) => {
122
+ if (this.removeLoader) {
123
+ this.removeLoader(this, records).then(records => {
124
+ this.dispatchAction(Action.RECORDS_REMOVED, { records, buffered: false });
125
+ resolve(records);
126
+ }).catch(error => fail(error));
127
+ }
128
+ });
129
+ }
130
+ }
131
+ return Promise.resolve(records);
132
+ });
133
+ }
134
+ // API
135
+ valueFromString(fieldName, value) {
136
+ const descriptor = this.getField(fieldName);
137
+ return descriptor ? convertType(descriptor.dataType, value) : value;
138
+ }
139
+ addInterceptor(interceptor) {
140
+ this._interceptors.push(interceptor);
141
+ }
142
+ addFilterProvider(provider) {
143
+ this._filterProviders.push(provider);
144
+ }
145
+ set sortingProvider(provider) {
146
+ this._sortingProvider = provider;
147
+ }
148
+ set metadata(md) {
149
+ this.dispatchAction(Action.METADATA_LOADED, md);
150
+ }
151
+ get metadata() {
152
+ return getMetadata(this._stateManager);
153
+ }
154
+ set records(r) {
155
+ this.dispatchAction(Action.DATA_LOADED, r);
156
+ }
157
+ get records() {
158
+ const records = getCurrentRecords(this._stateManager);
159
+ return records ? Array.from(records.values()) : [];
160
+ }
161
+ getField(fieldName) {
162
+ return getField(this._stateManager, fieldName);
163
+ }
164
+ addRecord() {
165
+ this.dispatchAction(Action.RECORDS_ADDED, prepareAddedRecordId(this._stateManager, [{}]));
166
+ }
167
+ copySelected() {
168
+ const selectedRecords = this.getSelectedRecords();
169
+ if (selectedRecords) {
170
+ this.dispatchAction(Action.RECORDS_COPIED, prepareAddedRecordId(this._stateManager, selectedRecords));
171
+ }
172
+ }
173
+ getFieldValue(fieldName) {
174
+ return getFieldValue(this._stateManager, fieldName);
175
+ }
176
+ setFieldValue(fieldName, newValue, records) {
177
+ const typedValue = this.validateAndTypeValue(fieldName, newValue);
178
+ const currentValue = this.getFieldValue(fieldName);
179
+ if (currentValue !== typedValue) {
180
+ this.dispatchAction(Action.DATA_CHANGED, { [fieldName]: typedValue, records });
181
+ }
182
+ }
183
+ getSelection() {
184
+ return getSelection(this._stateManager);
185
+ }
186
+ setSelection(selection) {
187
+ this.dispatchAction(Action.SELECTION_CHANGED, { type: "id", selection });
188
+ }
189
+ setSelectionByIndex(selection) {
190
+ this.dispatchAction(Action.SELECTION_CHANGED, { type: "index", selection });
191
+ }
192
+ getSelectedRecords() {
193
+ const selection = this.getSelection();
194
+ if (selection) {
195
+ const currentRecords = this.records;
196
+ return currentRecords === null || currentRecords === void 0 ? void 0 : currentRecords.filter(r => selection.includes(r.__record__id__));
197
+ }
198
+ }
199
+ nextRecord() {
200
+ this.dispatchAction(Action.NEXT_SELECTED);
201
+ }
202
+ previousRecord() {
203
+ this.dispatchAction(Action.PREVIOUS_SELECTED);
204
+ }
205
+ cancelEdition() {
206
+ this.dispatchAction(Action.EDITION_CANCELED);
207
+ }
208
+ isDirty() {
209
+ return isDirty(this._stateManager);
210
+ }
211
+ hasNext() {
212
+ return hasNext(this._stateManager);
213
+ }
214
+ hasPrevious() {
215
+ return hasPrevious(this._stateManager);
216
+ }
217
+ canUndo() {
218
+ return canUndo(this._stateManager);
219
+ }
220
+ canRedo() {
221
+ return canRedo(this._stateManager);
222
+ }
223
+ undo() {
224
+ this.dispatchAction(Action.CHANGE_UNDONE);
225
+ }
226
+ redo() {
227
+ this.dispatchAction(Action.CHANGE_REDONE);
228
+ }
229
+ toString() {
230
+ return this.name;
231
+ }
232
+ // Actions / State manager
233
+ dispatchAction(actionType, payload) {
234
+ var _a;
235
+ let action = new DataUnitAction(actionType, payload);
236
+ (_a = this._interceptors) === null || _a === void 0 ? void 0 : _a.forEach(interceptor => {
237
+ if (action) {
238
+ action = interceptor.interceptAction(action);
239
+ }
240
+ });
241
+ if (action) {
242
+ this._stateManager.process(action);
243
+ this._observers.forEach(f => f(action));
244
+ }
245
+ }
246
+ subscribe(observer) {
247
+ this._observers.push(observer);
248
+ }
249
+ unsubscribe(observer) {
250
+ this._observers = this._observers.filter(f => f !== observer);
251
+ }
252
+ }
253
+ export var ChangeOperation;
254
+ (function (ChangeOperation) {
255
+ ChangeOperation["INSERT"] = "INSERT";
256
+ ChangeOperation["UPDATE"] = "UPDATE";
257
+ ChangeOperation["DELETE"] = "DELETE";
258
+ })(ChangeOperation || (ChangeOperation = {}));
259
+ export class Change {
260
+ constructor(dataUnit, record, updates, operation) {
261
+ this.dataUnit = dataUnit;
262
+ this.record = record;
263
+ this.updatingFields = updates;
264
+ this._operation = operation;
265
+ }
266
+ get operation() {
267
+ return this._operation.toString();
268
+ }
269
+ isInsert() {
270
+ return this._operation === ChangeOperation.INSERT;
271
+ }
272
+ isDelete() {
273
+ return this._operation === ChangeOperation.DELETE;
274
+ }
275
+ isUpdate() {
276
+ return this._operation === ChangeOperation.UPDATE;
277
+ }
278
+ }
279
279
  //# sourceMappingURL=DataUnit.js.map
@@ -1,9 +1,9 @@
1
- export declare enum DataType {
2
- NUMBER = "NUMBER",
3
- DATE = "DATE",
4
- TEXT = "TEXT",
5
- BOOLEAN = "BOOLEAN",
6
- OBJECT = "OBJECT"
7
- }
8
- export declare const convertType: (dataType: DataType, value: any) => any;
9
- export declare const toString: (dataType: DataType, value: any) => string;
1
+ export declare enum DataType {
2
+ NUMBER = "NUMBER",
3
+ DATE = "DATE",
4
+ TEXT = "TEXT",
5
+ BOOLEAN = "BOOLEAN",
6
+ OBJECT = "OBJECT"
7
+ }
8
+ export declare const convertType: (dataType: DataType, value: any) => any;
9
+ export declare const toString: (dataType: DataType, value: any) => string;
@@ -1,37 +1,37 @@
1
- export var DataType;
2
- (function (DataType) {
3
- DataType["NUMBER"] = "NUMBER";
4
- DataType["DATE"] = "DATE";
5
- DataType["TEXT"] = "TEXT";
6
- DataType["BOOLEAN"] = "BOOLEAN";
7
- DataType["OBJECT"] = "OBJECT";
8
- })(DataType || (DataType = {}));
9
- export const convertType = (dataType, value) => {
10
- if (value === undefined || value === null) {
11
- return value;
12
- }
13
- switch (dataType) {
14
- case DataType.NUMBER:
15
- return value === "" || isNaN(value) ? null : Number(value);
16
- case DataType.OBJECT:
17
- return typeof value === "string" ? JSON.parse(value) : value;
18
- case DataType.BOOLEAN:
19
- return Boolean(value);
20
- case DataType.DATE:
21
- return new Date(value.toString());
22
- default:
23
- return value;
24
- }
25
- };
26
- export const toString = (dataType, value) => {
27
- if (value === undefined || value === null) {
28
- return value;
29
- }
30
- if (dataType === DataType.OBJECT) {
31
- return JSON.stringify(value);
32
- }
33
- else {
34
- return value.toString();
35
- }
36
- };
1
+ export var DataType;
2
+ (function (DataType) {
3
+ DataType["NUMBER"] = "NUMBER";
4
+ DataType["DATE"] = "DATE";
5
+ DataType["TEXT"] = "TEXT";
6
+ DataType["BOOLEAN"] = "BOOLEAN";
7
+ DataType["OBJECT"] = "OBJECT";
8
+ })(DataType || (DataType = {}));
9
+ export const convertType = (dataType, value) => {
10
+ if (value === undefined || value === null) {
11
+ return value;
12
+ }
13
+ switch (dataType) {
14
+ case DataType.NUMBER:
15
+ return value === "" || isNaN(value) ? null : Number(value);
16
+ case DataType.OBJECT:
17
+ return typeof value === "string" ? JSON.parse(value) : value;
18
+ case DataType.BOOLEAN:
19
+ return Boolean(value);
20
+ case DataType.DATE:
21
+ return new Date(value.toString());
22
+ default:
23
+ return value;
24
+ }
25
+ };
26
+ export const toString = (dataType, value) => {
27
+ if (value === undefined || value === null) {
28
+ return value;
29
+ }
30
+ if (dataType === DataType.OBJECT) {
31
+ return JSON.stringify(value);
32
+ }
33
+ else {
34
+ return value.toString();
35
+ }
36
+ };
37
37
  //# sourceMappingURL=DataType.js.map