@sankhyalabs/core 0.0.0-bugfix-dev-KB-6165.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/.docs/.nojekyll +1 -0
- package/.docs/README.md +63 -0
- package/.docs/classes/ApplicationContext.md +90 -0
- package/.docs/classes/ArrayUtils.md +99 -0
- package/.docs/classes/AuthorizedServiceCaller.md +76 -0
- package/.docs/classes/Change.md +190 -0
- package/.docs/classes/DataUnit.md +2453 -0
- package/.docs/classes/DataUnitAction.md +96 -0
- package/.docs/classes/DataUnitStorage.md +116 -0
- package/.docs/classes/DateUtils.md +327 -0
- package/.docs/classes/ElementIDUtils.md +308 -0
- package/.docs/classes/ErrorException.md +214 -0
- package/.docs/classes/ErrorTracking.md +62 -0
- package/.docs/classes/FloatingManager.md +530 -0
- package/.docs/classes/HTMLBuilder.md +45 -0
- package/.docs/classes/HttpProvider.md +96 -0
- package/.docs/classes/JSUtils.md +115 -0
- package/.docs/classes/MaskFormatter-1.md +347 -0
- package/.docs/classes/NumberUtils.md +335 -0
- package/.docs/classes/ObjectUtils.md +160 -0
- package/.docs/classes/OnboardingUtils.md +126 -0
- package/.docs/classes/PromiseSync.md +91 -0
- package/.docs/classes/ReadyUtil.md +115 -0
- package/.docs/classes/RequestMetadata.md +84 -0
- package/.docs/classes/SelectionInfo.md +168 -0
- package/.docs/classes/SkwHttpProvider.md +109 -0
- package/.docs/classes/StringUtils.md +562 -0
- package/.docs/classes/TimeFormatter.md +98 -0
- package/.docs/classes/UserAgentUtils.md +58 -0
- package/.docs/classes/VersionUtils.md +42 -0
- package/.docs/classes/WaitingChangeException.md +200 -0
- package/.docs/classes/WarningException.md +214 -0
- package/.docs/enums/Action.md +294 -0
- package/.docs/enums/ChangeOperation.md +52 -0
- package/.docs/enums/DataType.md +63 -0
- package/.docs/enums/DependencyType.md +41 -0
- package/.docs/enums/SelectionMode.md +30 -0
- package/.docs/enums/SortMode.md +30 -0
- package/.docs/enums/UserInterface.md +195 -0
- package/.docs/interfaces/ChildDescriptor.md +41 -0
- package/.docs/interfaces/ChildLink.md +30 -0
- package/.docs/interfaces/DUActionInterceptor.md +29 -0
- package/.docs/interfaces/ExecutionContext.md +58 -0
- package/.docs/interfaces/FieldDescriptor.md +140 -0
- package/.docs/interfaces/Filter.md +41 -0
- package/.docs/interfaces/IElementIDInfo.md +30 -0
- package/.docs/interfaces/LoadDataRequest.md +101 -0
- package/.docs/interfaces/LoadDataResponse.md +36 -0
- package/.docs/interfaces/PageRequest.md +41 -0
- package/.docs/interfaces/PaginationInfo.md +75 -0
- package/.docs/interfaces/PromiseSyncCallback.md +39 -0
- package/.docs/interfaces/QuickFilter.md +41 -0
- package/.docs/interfaces/Record.md +62 -0
- package/.docs/interfaces/SavedRecord.md +85 -0
- package/.docs/interfaces/Sort.md +41 -0
- package/.docs/interfaces/SortingProvider.md +29 -0
- package/.docs/interfaces/UnitMetadata.md +52 -0
- package/.docs/interfaces/WaitingChange.md +41 -0
- package/.docs/modules/MaskFormatter.md +37 -0
- package/.docs/modules.md +74 -0
- package/.docs/package.json +15 -0
- package/.eslintignore +2 -0
- package/.eslintrc.cjs +35 -0
- package/.husky/commit-msg +4 -0
- package/.releaserc +58 -0
- package/README.md +62 -0
- package/commitlint.config.cjs +14 -0
- package/dist/async/PromiseSync.d.ts +29 -0
- package/dist/async/PromiseSync.js +31 -0
- package/dist/async/PromiseSync.js.map +1 -0
- package/dist/dataunit/DataUnit.d.ts +924 -0
- package/dist/dataunit/DataUnit.js +1621 -0
- package/dist/dataunit/DataUnit.js.map +1 -0
- package/dist/dataunit/DataUnitStorage.d.ts +24 -0
- package/dist/dataunit/DataUnitStorage.js +39 -0
- package/dist/dataunit/DataUnitStorage.js.map +1 -0
- package/dist/dataunit/formatting/PrettyFormatter.d.ts +2 -0
- package/dist/dataunit/formatting/PrettyFormatter.js +94 -0
- package/dist/dataunit/formatting/PrettyFormatter.js.map +1 -0
- package/dist/dataunit/loading/LoadDataRequest.d.ts +19 -0
- package/dist/dataunit/loading/LoadDataRequest.js +2 -0
- package/dist/dataunit/loading/LoadDataRequest.js.map +1 -0
- package/dist/dataunit/loading/LoadDataResponse.d.ts +9 -0
- package/dist/dataunit/loading/LoadDataResponse.js +2 -0
- package/dist/dataunit/loading/LoadDataResponse.js.map +1 -0
- package/dist/dataunit/loading/PaginationInfo.d.ts +13 -0
- package/dist/dataunit/loading/PaginationInfo.js +2 -0
- package/dist/dataunit/loading/PaginationInfo.js.map +1 -0
- package/dist/dataunit/metadata/DataType.d.ts +54 -0
- package/dist/dataunit/metadata/DataType.js +124 -0
- package/dist/dataunit/metadata/DataType.js.map +1 -0
- package/dist/dataunit/metadata/UnitMetadata.d.ts +88 -0
- package/dist/dataunit/metadata/UnitMetadata.js +36 -0
- package/dist/dataunit/metadata/UnitMetadata.js.map +1 -0
- package/dist/dataunit/state/HistReducer.d.ts +10 -0
- package/dist/dataunit/state/HistReducer.js +28 -0
- package/dist/dataunit/state/HistReducer.js.map +1 -0
- package/dist/dataunit/state/StateManager.d.ts +57 -0
- package/dist/dataunit/state/StateManager.js +97 -0
- package/dist/dataunit/state/StateManager.js.map +1 -0
- package/dist/dataunit/state/action/DataUnitAction.d.ts +40 -0
- package/dist/dataunit/state/action/DataUnitAction.js +42 -0
- package/dist/dataunit/state/action/DataUnitAction.js.map +1 -0
- package/dist/dataunit/state/slice/AddedRecordsSlice.d.ts +12 -0
- package/dist/dataunit/state/slice/AddedRecordsSlice.js +30 -0
- package/dist/dataunit/state/slice/AddedRecordsSlice.js.map +1 -0
- package/dist/dataunit/state/slice/ChangesSlice.d.ts +13 -0
- package/dist/dataunit/state/slice/ChangesSlice.js +105 -0
- package/dist/dataunit/state/slice/ChangesSlice.js.map +1 -0
- package/dist/dataunit/state/slice/InvalidFieldsSlice.d.ts +10 -0
- package/dist/dataunit/state/slice/InvalidFieldsSlice.js +66 -0
- package/dist/dataunit/state/slice/InvalidFieldsSlice.js.map +1 -0
- package/dist/dataunit/state/slice/LoadingControlSlice.d.ts +19 -0
- package/dist/dataunit/state/slice/LoadingControlSlice.js +45 -0
- package/dist/dataunit/state/slice/LoadingControlSlice.js.map +1 -0
- package/dist/dataunit/state/slice/RecordsSlice.d.ts +11 -0
- package/dist/dataunit/state/slice/RecordsSlice.js +62 -0
- package/dist/dataunit/state/slice/RecordsSlice.js.map +1 -0
- package/dist/dataunit/state/slice/RemovedRecordsSlice.d.ts +9 -0
- package/dist/dataunit/state/slice/RemovedRecordsSlice.js +25 -0
- package/dist/dataunit/state/slice/RemovedRecordsSlice.js.map +1 -0
- package/dist/dataunit/state/slice/SelectionSlice.d.ts +19 -0
- package/dist/dataunit/state/slice/SelectionSlice.js +155 -0
- package/dist/dataunit/state/slice/SelectionSlice.js.map +1 -0
- package/dist/dataunit/state/slice/SnapshotSlice.d.ts +19 -0
- package/dist/dataunit/state/slice/SnapshotSlice.js +106 -0
- package/dist/dataunit/state/slice/SnapshotSlice.js.map +1 -0
- package/dist/dataunit/state/slice/UnitMetadataSlice.d.ts +11 -0
- package/dist/dataunit/state/slice/UnitMetadataSlice.js +21 -0
- package/dist/dataunit/state/slice/UnitMetadataSlice.js.map +1 -0
- package/dist/dataunit/state/slice/WaitingChangesSlice.d.ts +12 -0
- package/dist/dataunit/state/slice/WaitingChangesSlice.js +56 -0
- package/dist/dataunit/state/slice/WaitingChangesSlice.js.map +1 -0
- package/dist/exceptions/ErrorException.d.ts +14 -0
- package/dist/exceptions/ErrorException.js +13 -0
- package/dist/exceptions/ErrorException.js.map +1 -0
- package/dist/exceptions/WaitingChangeException.d.ts +12 -0
- package/dist/exceptions/WaitingChangeException.js +12 -0
- package/dist/exceptions/WaitingChangeException.js.map +1 -0
- package/dist/exceptions/WarningException.d.ts +14 -0
- package/dist/exceptions/WarningException.js +13 -0
- package/dist/exceptions/WarningException.js.map +1 -0
- package/dist/html/HTMLBuilder.d.ts +3 -0
- package/dist/html/HTMLBuilder.js +9 -0
- package/dist/html/HTMLBuilder.js.map +1 -0
- package/dist/http/AuthorizedServiceCaller.d.ts +11 -0
- package/dist/http/AuthorizedServiceCaller.js +54 -0
- package/dist/http/AuthorizedServiceCaller.js.map +1 -0
- package/dist/http/HttpProvider.d.ts +25 -0
- package/dist/http/HttpProvider.js +74 -0
- package/dist/http/HttpProvider.js.map +1 -0
- package/dist/http/RequestMetadata.d.ts +30 -0
- package/dist/http/RequestMetadata.js +25 -0
- package/dist/http/RequestMetadata.js.map +1 -0
- package/dist/http/SkwHttpProvider.d.ts +9 -0
- package/dist/http/SkwHttpProvider.js +67 -0
- package/dist/http/SkwHttpProvider.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/traking/ErrorTraking.d.ts +19 -0
- package/dist/traking/ErrorTraking.js +34 -0
- package/dist/traking/ErrorTraking.js.map +1 -0
- package/dist/ui/FloatingManager.d.ts +164 -0
- package/dist/ui/FloatingManager.js +358 -0
- package/dist/ui/FloatingManager.js.map +1 -0
- package/dist/utils/ApplicationContext.d.ts +26 -0
- package/dist/utils/ApplicationContext.js +38 -0
- package/dist/utils/ApplicationContext.js.map +1 -0
- package/dist/utils/ArrayUtils.d.ts +29 -0
- package/dist/utils/ArrayUtils.js +57 -0
- package/dist/utils/ArrayUtils.js.map +1 -0
- package/dist/utils/CriteriaModel.d.ts +107 -0
- package/dist/utils/CriteriaModel.js +172 -0
- package/dist/utils/CriteriaModel.js.map +1 -0
- package/dist/utils/CriteriaParameter.d.ts +69 -0
- package/dist/utils/CriteriaParameter.js +83 -0
- package/dist/utils/CriteriaParameter.js.map +1 -0
- package/dist/utils/DateUtils.d.ts +97 -0
- package/dist/utils/DateUtils.js +171 -0
- package/dist/utils/DateUtils.js.map +1 -0
- package/dist/utils/ElementIDUtils.d.ts +72 -0
- package/dist/utils/ElementIDUtils.js +161 -0
- package/dist/utils/ElementIDUtils.js.map +1 -0
- package/dist/utils/JSUtils.d.ts +36 -0
- package/dist/utils/JSUtils.js +66 -0
- package/dist/utils/JSUtils.js.map +1 -0
- package/dist/utils/MaskFormatter.d.ts +148 -0
- package/dist/utils/MaskFormatter.js +356 -0
- package/dist/utils/MaskFormatter.js.map +1 -0
- package/dist/utils/NumberUtils.d.ts +127 -0
- package/dist/utils/NumberUtils.js +245 -0
- package/dist/utils/NumberUtils.js.map +1 -0
- package/dist/utils/ObjectUtils.d.ts +47 -0
- package/dist/utils/ObjectUtils.js +68 -0
- package/dist/utils/ObjectUtils.js.map +1 -0
- package/dist/utils/OnboardingUtils.d.ts +17 -0
- package/dist/utils/OnboardingUtils.js +41 -0
- package/dist/utils/OnboardingUtils.js.map +1 -0
- package/dist/utils/ReadyUtil.d.ts +24 -0
- package/dist/utils/ReadyUtil.js +41 -0
- package/dist/utils/ReadyUtil.js.map +1 -0
- package/dist/utils/StringUtils.d.ts +167 -0
- package/dist/utils/StringUtils.js +328 -0
- package/dist/utils/StringUtils.js.map +1 -0
- package/dist/utils/TimeFormatter.d.ts +33 -0
- package/dist/utils/TimeFormatter.js +98 -0
- package/dist/utils/TimeFormatter.js.map +1 -0
- package/dist/utils/UserAgentUtils/index.d.ts +15 -0
- package/dist/utils/UserAgentUtils/index.js +48 -0
- package/dist/utils/UserAgentUtils/index.js.map +1 -0
- package/dist/utils/UserAgentUtils/navigatorAgentList.d.ts +17 -0
- package/dist/utils/UserAgentUtils/navigatorAgentList.js +40 -0
- package/dist/utils/UserAgentUtils/navigatorAgentList.js.map +1 -0
- package/dist/utils/VersionUtils.d.ts +7 -0
- package/dist/utils/VersionUtils.js +31 -0
- package/dist/utils/VersionUtils.js.map +1 -0
- package/jest.config.ts +17 -0
- package/mock/http/XMLHttpRequest-mock.js +26 -0
- package/package.json +52 -0
- package/scripts/runlink.bat +1 -0
- package/scripts/runlink.sh +1 -0
- package/src/async/PromiseSync.ts +49 -0
- package/src/dataunit/DataUnit.ts +1835 -0
- package/src/dataunit/DataUnitStorage.ts +43 -0
- package/src/dataunit/formatting/PrettyFormatter.ts +113 -0
- package/src/dataunit/loading/LoadDataRequest.ts +26 -0
- package/src/dataunit/loading/LoadDataResponse.ts +11 -0
- package/src/dataunit/loading/PaginationInfo.ts +18 -0
- package/src/dataunit/metadata/DataType.ts +127 -0
- package/src/dataunit/metadata/UnitMetadata.ts +102 -0
- package/src/dataunit/state/HistReducer.ts +34 -0
- package/src/dataunit/state/StateManager.ts +142 -0
- package/src/dataunit/state/action/DataUnitAction.ts +67 -0
- package/src/dataunit/state/slice/AddedRecordsSlice.ts +48 -0
- package/src/dataunit/state/slice/ChangesSlice.ts +125 -0
- package/src/dataunit/state/slice/InvalidFieldsSlice.ts +85 -0
- package/src/dataunit/state/slice/LoadingControlSlice.ts +60 -0
- package/src/dataunit/state/slice/RecordsSlice.ts +74 -0
- package/src/dataunit/state/slice/RemovedRecordsSlice.ts +30 -0
- package/src/dataunit/state/slice/SelectionSlice.ts +193 -0
- package/src/dataunit/state/slice/SnapshotSlice.ts +139 -0
- package/src/dataunit/state/slice/UnitMetadataSlice.ts +30 -0
- package/src/dataunit/state/slice/WaitingChangesSlice.ts +74 -0
- package/src/exceptions/ErrorException.ts +25 -0
- package/src/exceptions/WaitingChangeException.ts +21 -0
- package/src/exceptions/WarningException.ts +26 -0
- package/src/html/HTMLBuilder.ts +8 -0
- package/src/http/AuthorizedServiceCaller.ts +58 -0
- package/src/http/HttpProvider.ts +94 -0
- package/src/http/RequestMetadata.ts +42 -0
- package/src/http/SkwHttpProvider.ts +78 -0
- package/src/index.ts +93 -0
- package/src/traking/ErrorTraking.ts +37 -0
- package/src/ui/FloatingManager.ts +417 -0
- package/src/utils/ApplicationContext.ts +42 -0
- package/src/utils/ArrayUtils.ts +61 -0
- package/src/utils/CriteriaModel.ts +205 -0
- package/src/utils/CriteriaParameter.ts +108 -0
- package/src/utils/DateUtils.ts +195 -0
- package/src/utils/ElementIDUtils.ts +184 -0
- package/src/utils/JSUtils.ts +69 -0
- package/src/utils/MaskFormatter.ts +407 -0
- package/src/utils/NumberUtils.ts +283 -0
- package/src/utils/ObjectUtils.ts +76 -0
- package/src/utils/OnboardingUtils.ts +46 -0
- package/src/utils/ReadyUtil.ts +45 -0
- package/src/utils/StringUtils.ts +359 -0
- package/src/utils/TimeFormatter.ts +98 -0
- package/src/utils/UserAgentUtils/index.ts +62 -0
- package/src/utils/UserAgentUtils/navigatorAgentList.ts +39 -0
- package/src/utils/VersionUtils.ts +37 -0
- package/test/http/HttpProvider.spec.ts +35 -0
- package/test/http/SkwHttpProvider.ts.spec.ts +34 -0
- package/test/util/CriteriaModel.spec.ts +232 -0
- package/test/util/CriteriaParameter.spec.ts +52 -0
- package/test/util/DataUnitStorage.spec.ts +63 -0
- package/test/util/ElementIDUtils.spec.ts +272 -0
- package/test/util/MaskFormatter.spec.ts +138 -0
- package/test/util/NumberUtils.spec.ts +182 -0
- package/test/util/StringUtils.spec.ts +51 -0
- package/test/util/TimeFormatter.spec.ts +26 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,1835 @@
|
|
|
1
|
+
import { convertType, DataType, toString } from "./metadata/DataType.js";
|
|
2
|
+
import { ChildDescriptor, FieldDescriptor, Filter, FilterProvider, Sort, SortingProvider, UnitMetadata } from "./metadata/UnitMetadata.js";
|
|
3
|
+
|
|
4
|
+
import { Action, DataUnitAction, ExecutionContext } from "./state/action/DataUnitAction.js";
|
|
5
|
+
import StateManager from "./state/StateManager.js";
|
|
6
|
+
|
|
7
|
+
import ErrorException from "../exceptions/ErrorException.js";
|
|
8
|
+
import WaitingChangeException from "../exceptions/WaitingChangeException.js";
|
|
9
|
+
import { StringUtils } from "../utils/StringUtils.js";
|
|
10
|
+
import ObjectUtils from "../utils/ObjectUtils.js";
|
|
11
|
+
import { LoadDataRequest } from "./loading/LoadDataRequest.js";
|
|
12
|
+
import { LoadDataResponse } from "./loading/LoadDataResponse.js";
|
|
13
|
+
import { PaginationInfo } from "./loading/PaginationInfo.js";
|
|
14
|
+
import { AddedRecordsReducer, getAddedRecords, prepareAddedRecordId, prepareCopiedRecord } from "./state/slice/AddedRecordsSlice.js";
|
|
15
|
+
import { ChangesReducer, getChangesToSave, isDirty, hasDirtyRecords } from "./state/slice/ChangesSlice.js";
|
|
16
|
+
import {
|
|
17
|
+
SnapshotReducer,
|
|
18
|
+
getCurrentRecords,
|
|
19
|
+
getFieldValue,
|
|
20
|
+
getModifiedRecords,
|
|
21
|
+
getSelectionRecords,
|
|
22
|
+
} from './state/slice/SnapshotSlice.js';
|
|
23
|
+
import { getCurrentPage, getLastPage, getPaginationInfo, getCurrentRequest, LoadingControlReducer, hasMorePages, hasPreviousPages } from "./state/slice/LoadingControlSlice.js";
|
|
24
|
+
import { getRecords, RecordsReducer } from "./state/slice/RecordsSlice.js";
|
|
25
|
+
import { RemovedRecordsReducer } from "./state/slice/RemovedRecordsSlice.js";
|
|
26
|
+
import { getSelection, getSelectionInfo, hasNext, hasPrevious, SelectionReducer } from "./state/slice/SelectionSlice.js";
|
|
27
|
+
import { getField, getMetadata, UnitMetadataReducer } from "./state/slice/UnitMetadataSlice.js";
|
|
28
|
+
import { getBlockingWaitingChanges, getWaitingChangePromisses, getWaitingChanges, isWaiting, WaitingChangesReducer } from "./state/slice/WaitingChangesSlice.js";
|
|
29
|
+
import { canRedo, canUndo, HistReducer } from "./state/HistReducer.js";
|
|
30
|
+
import { getFormattedValue } from "./formatting/PrettyFormatter.js";
|
|
31
|
+
import { v4 as uuid } from "uuid";
|
|
32
|
+
import { DataUnitStorage } from "./DataUnitStorage.js";
|
|
33
|
+
import { getInvalidFieldMessage, InvalidFieldsReducer } from "./state/slice/InvalidFieldsSlice.js";
|
|
34
|
+
|
|
35
|
+
const DEFAULT_DATAUNIT_NAME = "dataunit";
|
|
36
|
+
|
|
37
|
+
/***
|
|
38
|
+
* `DataUnit`: Atua como uma camada de abstração entre o back-end e a interface do usuário.
|
|
39
|
+
*/
|
|
40
|
+
export default class DataUnit {
|
|
41
|
+
private _uuid: string;
|
|
42
|
+
private _name: string;
|
|
43
|
+
private _observers: Array<(action: DataUnitAction) => void>;
|
|
44
|
+
private _sortingProvider?: SortingProvider;
|
|
45
|
+
private _filterProviders: Map<string, FilterProvider>;
|
|
46
|
+
private _stateManager: StateManager;
|
|
47
|
+
private _interceptors: Array<DUActionInterceptor>;
|
|
48
|
+
private _pageSize: number;
|
|
49
|
+
private _childByName = new Map<string, DataUnit>();
|
|
50
|
+
private _parentDataUnit: DataUnit | undefined;
|
|
51
|
+
private _loadingLockers: Promise<void>[];
|
|
52
|
+
private _savingLockers: Promise<any>[] = [];
|
|
53
|
+
|
|
54
|
+
public metadataLoader?: (dataUnit: DataUnit) => Promise<UnitMetadata>;
|
|
55
|
+
public dataLoader?: (dataUnit: DataUnit, request: LoadDataRequest) => Promise<LoadDataResponse>;
|
|
56
|
+
public saveLoader?: (dataUnit: DataUnit, changes: Array<Change>) => Promise<Array<SavedRecord>>;
|
|
57
|
+
public removeLoader?: (dataUnit: DataUnit, recordIds: Array<string>) => Promise<Array<string>>;
|
|
58
|
+
public recordLoader?: (dataUnit: DataUnit, recordIds: Array<string>) => Promise<Array<Record>>;
|
|
59
|
+
|
|
60
|
+
constructor(name: string = DEFAULT_DATAUNIT_NAME, parentDataUnit?: DataUnit) {
|
|
61
|
+
this._uuid = uuid()
|
|
62
|
+
this._name = `${name}?uuid=${this._uuid}`;
|
|
63
|
+
this._pageSize = 0;
|
|
64
|
+
this._stateManager = new StateManager(
|
|
65
|
+
[
|
|
66
|
+
HistReducer,
|
|
67
|
+
UnitMetadataReducer,
|
|
68
|
+
LoadingControlReducer,
|
|
69
|
+
RecordsReducer,
|
|
70
|
+
RemovedRecordsReducer,
|
|
71
|
+
AddedRecordsReducer,
|
|
72
|
+
SelectionReducer,
|
|
73
|
+
ChangesReducer,
|
|
74
|
+
WaitingChangesReducer,
|
|
75
|
+
InvalidFieldsReducer,
|
|
76
|
+
SnapshotReducer
|
|
77
|
+
]
|
|
78
|
+
);
|
|
79
|
+
this._observers = [];
|
|
80
|
+
this._filterProviders = new Map<string, FilterProvider>();
|
|
81
|
+
this._sortingProvider = undefined;
|
|
82
|
+
this._interceptors = [];
|
|
83
|
+
this._parentDataUnit = parentDataUnit;
|
|
84
|
+
this._parentDataUnit?.subscribe(this.onDataUnitParentEvent);
|
|
85
|
+
this._loadingLockers = [];
|
|
86
|
+
DataUnitStorage.put(this);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Desfaz vinculos do DataUnit. Chamado quando o DU não é mais necessário.
|
|
91
|
+
*/
|
|
92
|
+
public release(){
|
|
93
|
+
DataUnitStorage.remove(this._name);
|
|
94
|
+
if(this._parentDataUnit){
|
|
95
|
+
this._parentDataUnit.unsubscribe(this.onDataUnitParentEvent);
|
|
96
|
+
this._parentDataUnit.removeChildDataunit(this._name);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public get dataUnitId(): string{
|
|
101
|
+
return this._uuid;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
*
|
|
106
|
+
* Obtém o nome de identificação do DataUnit (geralmente em formato de URI - Uniform Resource Identifier).
|
|
107
|
+
*
|
|
108
|
+
* @returns - Nome de identificação do DataUnit.
|
|
109
|
+
*
|
|
110
|
+
*/
|
|
111
|
+
public get name(): string {
|
|
112
|
+
return this._name;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
*
|
|
117
|
+
* Obtém o valor convertido de acordo com o tipo do campo.
|
|
118
|
+
*
|
|
119
|
+
* @param fieldName - Identificador do campo.
|
|
120
|
+
* @param newValue - Novo valor que será atribuído ao campo pós validação.
|
|
121
|
+
*
|
|
122
|
+
* @returns - Novo valor convertido em um tipo valido.
|
|
123
|
+
*
|
|
124
|
+
*/
|
|
125
|
+
private validateAndTypeValue(fieldName: string, newValue: any): any {
|
|
126
|
+
const descriptor: FieldDescriptor | undefined = this.getField(fieldName);
|
|
127
|
+
return descriptor ? convertType(descriptor.dataType, newValue) : newValue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
*
|
|
132
|
+
* Trata as Actions do DataUnit Parent
|
|
133
|
+
*
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
136
|
+
private onDataUnitParentEvent = (action: DataUnitAction) => {
|
|
137
|
+
switch(action.type){
|
|
138
|
+
case Action.SELECTION_CHANGED:
|
|
139
|
+
case Action.NEXT_SELECTED:
|
|
140
|
+
case Action.PREVIOUS_SELECTED:
|
|
141
|
+
case Action.DATA_LOADED:
|
|
142
|
+
this.clearDataUnit();
|
|
143
|
+
const selectedRecord = this._parentDataUnit?.getSelectedRecord();
|
|
144
|
+
if(selectedRecord != undefined && !this.isNewRecord(selectedRecord.__record__id__)){
|
|
145
|
+
this.loadData();
|
|
146
|
+
}
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
*
|
|
153
|
+
* Obtém chave única para identificação do FilterProvider.
|
|
154
|
+
*
|
|
155
|
+
* @param provider - Interface FilterProvider na qual será retornada uma chave correspondente.
|
|
156
|
+
*
|
|
157
|
+
* @returns - A chave do provider.
|
|
158
|
+
*
|
|
159
|
+
*/
|
|
160
|
+
private getFielterProviderKey(provider: FilterProvider): string {
|
|
161
|
+
if (provider.getKey) {
|
|
162
|
+
return provider.getKey();
|
|
163
|
+
}
|
|
164
|
+
return StringUtils.hashCode(provider.getFilter.toString());
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
*
|
|
169
|
+
* Executa o carregamento dos registros.
|
|
170
|
+
*
|
|
171
|
+
* @param request - Dados da requisição para carregamento dos registros.
|
|
172
|
+
* @param executionCtx - Contexto de execução do carregamento dos registros do DataUnit.
|
|
173
|
+
* @param checkLastFilter - Habilita a verificação da última requisição, evitando carga desnecessária.
|
|
174
|
+
*
|
|
175
|
+
* @returns - Registros do DataUnit.
|
|
176
|
+
*
|
|
177
|
+
*/
|
|
178
|
+
private executeLoadData(request: LoadDataRequest, executionCtx?: ExecutionContext, checkLastFilter?: boolean): Promise<LoadDataResponse> {
|
|
179
|
+
|
|
180
|
+
if(checkLastFilter && this.isSameRequest(request)){
|
|
181
|
+
const paginationInfo = getPaginationInfo(this._stateManager);
|
|
182
|
+
if(paginationInfo){
|
|
183
|
+
const response: LoadDataResponse = {
|
|
184
|
+
records: getRecords(this._stateManager),
|
|
185
|
+
paginationInfo
|
|
186
|
+
};
|
|
187
|
+
return Promise.resolve(response);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return new Promise(async (resolve, fail) => {
|
|
192
|
+
if (await this.dispatchAction(Action.LOADING_DATA, request, executionCtx)) {
|
|
193
|
+
if (this.dataLoader) {
|
|
194
|
+
this.dataLoader(this, request).then(
|
|
195
|
+
response => {
|
|
196
|
+
this.dispatchAction(Action.DATA_LOADED, {...response, keepSelection: request.keepSelection}, executionCtx);
|
|
197
|
+
resolve(response);
|
|
198
|
+
}
|
|
199
|
+
).catch(error => {
|
|
200
|
+
const {errorCode} = error;
|
|
201
|
+
fail(new ErrorException("Erro ao carregar registros", error, errorCode))
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private isSameRequest(request: LoadDataRequest): boolean{
|
|
209
|
+
const lastRequest = getCurrentRequest(this._stateManager);
|
|
210
|
+
return ObjectUtils.objectToString(request) === ObjectUtils.objectToString(lastRequest || {});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Loaders
|
|
214
|
+
/**
|
|
215
|
+
*
|
|
216
|
+
* Carrega os metadados do DataUnit.
|
|
217
|
+
*
|
|
218
|
+
* @param executionCtx - Contexto de execução do carregamento dos metadados do DataUnit.
|
|
219
|
+
*
|
|
220
|
+
* @returns - Metadados carregados.
|
|
221
|
+
*
|
|
222
|
+
*/
|
|
223
|
+
public async loadMetadata(executionCtx?: ExecutionContext): Promise<UnitMetadata | void> {
|
|
224
|
+
if (await this.dispatchAction(Action.LOADING_METADATA, undefined, executionCtx)) {
|
|
225
|
+
return new Promise((resolve, fail) => {
|
|
226
|
+
if (this.metadataLoader) {
|
|
227
|
+
this.metadataLoader(this).then(
|
|
228
|
+
metadata => {
|
|
229
|
+
this.metadata = metadata
|
|
230
|
+
resolve(this.metadata);
|
|
231
|
+
}
|
|
232
|
+
).catch(error => {
|
|
233
|
+
const {errorCode} = error;
|
|
234
|
+
fail(new ErrorException("Erro ao carregar metadados", error, errorCode))
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
*
|
|
243
|
+
* Carrega os registros do DataUnit.
|
|
244
|
+
*
|
|
245
|
+
* @param quickFilter - Filtros a serem aplicados.
|
|
246
|
+
* @param executionCtx - Contexto de execução do carregamento dos registros.
|
|
247
|
+
* @param checkLastFilter - Habilita a verificação da última requisição, evitando carga desnecessária.
|
|
248
|
+
*
|
|
249
|
+
* @returns - Registros requisitados.
|
|
250
|
+
*
|
|
251
|
+
*/
|
|
252
|
+
public async loadData(quickFilter?: QuickFilter, executionCtx?: ExecutionContext, checkLastFilter?: boolean): Promise<LoadDataResponse> {
|
|
253
|
+
|
|
254
|
+
await this.processLoadingLockers();
|
|
255
|
+
|
|
256
|
+
if (this._parentDataUnit && !this._parentDataUnit.getSelectedRecord()) {
|
|
257
|
+
if(this.records){
|
|
258
|
+
this.clearDataUnit();
|
|
259
|
+
}
|
|
260
|
+
return Promise.resolve({
|
|
261
|
+
records: []
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const loadDataRequest = this.getLoadDataRequest(quickFilter);
|
|
266
|
+
return this.executeLoadData(loadDataRequest, executionCtx, checkLastFilter);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
*
|
|
271
|
+
* Alterna entre os registros por número de página.
|
|
272
|
+
*
|
|
273
|
+
* @param page - Número da página desejada.
|
|
274
|
+
* @param executionCtx - Contexto de execução do carregamento dos registros do DataUnit.
|
|
275
|
+
*
|
|
276
|
+
* @returns - Registros da página desejada.
|
|
277
|
+
*
|
|
278
|
+
*/
|
|
279
|
+
public async gotoPage(page: number, executionCtx?: ExecutionContext): Promise<LoadDataResponse | void> {
|
|
280
|
+
|
|
281
|
+
let request = getCurrentRequest(this._stateManager);
|
|
282
|
+
|
|
283
|
+
if (!request) {
|
|
284
|
+
request = {
|
|
285
|
+
filters: this.getFilters(),
|
|
286
|
+
sort: this.getSort()
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (page >= 0 && page <= getLastPage(this._stateManager, this._pageSize)) {
|
|
291
|
+
return this.executeLoadData({
|
|
292
|
+
...request,
|
|
293
|
+
limit: this._pageSize,
|
|
294
|
+
offset: page * this._pageSize,
|
|
295
|
+
keepSelection: true
|
|
296
|
+
}, executionCtx);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
*
|
|
302
|
+
* Vai para os registros da página seguinte.
|
|
303
|
+
*
|
|
304
|
+
* @param executionCtx - Contexto de execução do carregamento dos registros do DataUnit.
|
|
305
|
+
*
|
|
306
|
+
* @returns - Registros da página seguinte.
|
|
307
|
+
*
|
|
308
|
+
*/
|
|
309
|
+
public async nextPage(executionCtx?: ExecutionContext): Promise<LoadDataResponse | void> {
|
|
310
|
+
return this.gotoPage(getCurrentPage(this._stateManager) + 1, executionCtx);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
*
|
|
315
|
+
* Vai para os registros da página anterior.
|
|
316
|
+
*
|
|
317
|
+
* @param executionCtx - Contexto de execução do carregamento dos registros do DataUnit.
|
|
318
|
+
*
|
|
319
|
+
* @returns - Registros da página anterior.
|
|
320
|
+
*
|
|
321
|
+
*/
|
|
322
|
+
public async previousPage(executionCtx?: ExecutionContext): Promise<LoadDataResponse | void> {
|
|
323
|
+
return this.gotoPage(getCurrentPage(this._stateManager) - 1, executionCtx);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private getLoadDataRequest(quickFilter?: QuickFilter): LoadDataRequest {
|
|
327
|
+
const request: LoadDataRequest = {
|
|
328
|
+
filters: quickFilter?.filter ? [quickFilter.filter] : this.getFilters(),
|
|
329
|
+
sort: this.getSort(),
|
|
330
|
+
parentRecordId: this.getParentRecordId()
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
if (quickFilter) {
|
|
334
|
+
request.quickFilter = quickFilter;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (this._pageSize > 0) {
|
|
338
|
+
request.limit = this._pageSize
|
|
339
|
+
request.offset = 0;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return request;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private async notifySavingData(executionCtx?: ExecutionContext): Promise<boolean>{
|
|
346
|
+
const notifyPromises: Array<Promise<boolean>> = [];
|
|
347
|
+
Array.from(this._childByName.values()).forEach(
|
|
348
|
+
child => notifyPromises.push(child.notifySavingData(executionCtx))
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
if(isDirty(this._stateManager)){
|
|
352
|
+
notifyPromises.push(this.dispatchAction(Action.SAVING_DATA, undefined, executionCtx));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return new Promise(accept => Promise.all(notifyPromises).then(responses => {
|
|
356
|
+
accept(responses.reduce((previous, current) => previous && current, true));
|
|
357
|
+
}));
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
private getBeforeSavePromisses(): Array<Promise<any>|undefined>{
|
|
361
|
+
let promises = getWaitingChangePromisses(this._stateManager)||[];
|
|
362
|
+
promises = promises.concat(this._savingLockers);
|
|
363
|
+
return promises;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
*
|
|
368
|
+
* Salva o estado do registro do DataUnit.
|
|
369
|
+
*
|
|
370
|
+
* @param executionCtx - Contexto de execução da persistencia do registro do DataUnit.
|
|
371
|
+
*
|
|
372
|
+
* @returns - Resposta da solicitação.
|
|
373
|
+
*
|
|
374
|
+
*/
|
|
375
|
+
public async saveData(executionCtx?: ExecutionContext): Promise<void> {
|
|
376
|
+
const blockingWaitingChanges = getBlockingWaitingChanges(this._stateManager);
|
|
377
|
+
|
|
378
|
+
if (blockingWaitingChanges && blockingWaitingChanges.size > 0) {
|
|
379
|
+
const [_, waitingChange] = blockingWaitingChanges.entries().next().value;
|
|
380
|
+
return Promise.reject(new WaitingChangeException("Aguardando alteração de campo", (waitingChange as WaitingChange).waitmessage));
|
|
381
|
+
} else {
|
|
382
|
+
if (this.isDirty()) {
|
|
383
|
+
|
|
384
|
+
if (await this.notifySavingData(executionCtx)) {
|
|
385
|
+
const promisses = this.getBeforeSavePromisses();
|
|
386
|
+
return new Promise((resolve, fail) => {
|
|
387
|
+
Promise.all(promisses || []).then(() => {
|
|
388
|
+
this._savingLockers = [];
|
|
389
|
+
if (this.saveLoader) {
|
|
390
|
+
const changes: Array<Change> = this.getAllChangesToSave();
|
|
391
|
+
|
|
392
|
+
this.saveLoader(this, changes).then((records) => {
|
|
393
|
+
const recordsByDataUnit = this.getRecordsByDataUnit(records);
|
|
394
|
+
|
|
395
|
+
const dispatchPromisses = [];
|
|
396
|
+
for (const [ownerDataUnitName, splittedRecords] of recordsByDataUnit) {
|
|
397
|
+
const ownerDataUnit = DataUnitStorage.get(ownerDataUnitName);
|
|
398
|
+
const changesByDataUnit = changes.filter(change => change.dataUnit === ownerDataUnitName);
|
|
399
|
+
|
|
400
|
+
if(ownerDataUnit){
|
|
401
|
+
dispatchPromisses.push(ownerDataUnit.dispatchAction(Action.DATA_SAVED, { changes: changesByDataUnit, records: splittedRecords }, executionCtx));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
Promise.all(dispatchPromisses).then(() => resolve());
|
|
405
|
+
}).catch(cause => {
|
|
406
|
+
const {errorCode} = cause;
|
|
407
|
+
fail(new ErrorException("Erro ao salvar alterações", cause, errorCode));
|
|
408
|
+
});
|
|
409
|
+
} else {
|
|
410
|
+
resolve();
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return Promise.resolve();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
*
|
|
423
|
+
* Retorna as alterações a serem salvas no DataUnit atual.
|
|
424
|
+
*
|
|
425
|
+
*
|
|
426
|
+
*
|
|
427
|
+
* @returns - Mudanças realizadas no DataUnit atual
|
|
428
|
+
*
|
|
429
|
+
*/
|
|
430
|
+
public buildChangesToSave(): Array<Change>{
|
|
431
|
+
return getChangesToSave(this._name, this._stateManager);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
public buildChangesToSaveFromChild(allChanges: Array<Change>, dataUnit: DataUnit): void {
|
|
435
|
+
dataUnit._childByName?.forEach((dataUnitChild: DataUnit) => {
|
|
436
|
+
if (dataUnitChild.isDirty()) {
|
|
437
|
+
allChanges.push(...getChangesToSave(dataUnitChild.name, dataUnitChild._stateManager));
|
|
438
|
+
this.buildChangesToSaveFromChild(allChanges, dataUnitChild);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
*
|
|
445
|
+
* Retorna todas as alterações do DataUnit a serem salvas, incluindo no nível Master e Detail.
|
|
446
|
+
*
|
|
447
|
+
*
|
|
448
|
+
*
|
|
449
|
+
* @returns - Todas as mudanças realizadas no DataUnit, tanto Master quanto Detail;
|
|
450
|
+
*
|
|
451
|
+
*/
|
|
452
|
+
public getAllChangesToSave(): Array<Change> {
|
|
453
|
+
const allChanges: Array<Change> = [];
|
|
454
|
+
allChanges.push(...this.buildChangesToSave());
|
|
455
|
+
this.buildChangesToSaveFromChild(allChanges, this);
|
|
456
|
+
return allChanges;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
*
|
|
461
|
+
* Remove o registro selecionado.
|
|
462
|
+
*
|
|
463
|
+
* @param buffered - Se será utilizado buffer na solicitação.
|
|
464
|
+
* @param silent - Define se haverá mensagem de confirmação da remoção
|
|
465
|
+
*
|
|
466
|
+
* @returns - ID's dos registros removidos.
|
|
467
|
+
*
|
|
468
|
+
*/
|
|
469
|
+
public async removeSelectedRecords(buffered: boolean = false, silent: boolean = false): Promise<Array<string>> {
|
|
470
|
+
const selection = this.getSelectionInfo();
|
|
471
|
+
if (selection) {
|
|
472
|
+
if(selection.isAllRecords()){
|
|
473
|
+
throw new Error("Exclusão remota não implementada.");
|
|
474
|
+
}
|
|
475
|
+
const records = selection?.records || [];
|
|
476
|
+
const recordIds = selection?.recordIds || [];
|
|
477
|
+
return this.removeRecords(recordIds, records, buffered, undefined, silent);
|
|
478
|
+
}
|
|
479
|
+
return Promise.resolve([]);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
*
|
|
484
|
+
* Efetua requisição para remoção dos registros.
|
|
485
|
+
*
|
|
486
|
+
* @param recordIds - Lista de IDs dos registros que serão removidos.
|
|
487
|
+
* @param cachedRecords - Dados dos registros que serão removidos.
|
|
488
|
+
* @param buffered - Se será utilizado buffer na solicitação.
|
|
489
|
+
* @param executionCtx - Contexto de execução da remoção do registro do DataUnit.
|
|
490
|
+
* @param silent - Define se haverá mensagem de confirmação da remoção
|
|
491
|
+
*
|
|
492
|
+
* @returns - ID's dos registros removidos.
|
|
493
|
+
*
|
|
494
|
+
*/
|
|
495
|
+
public async removeRecords(recordIds: Array<string>, cachedRecords: Array<Record>, buffered: boolean = false, executionCtx?: ExecutionContext, silent: boolean = false): Promise<Array<string>> {
|
|
496
|
+
if (recordIds) {
|
|
497
|
+
if (buffered || !this.removeLoader) {
|
|
498
|
+
this.dispatchAction(Action.RECORDS_REMOVED, { records: recordIds, cachedRecords, buffered: true }, executionCtx);
|
|
499
|
+
} else {
|
|
500
|
+
if (await this.dispatchAction(Action.REMOVING_RECORDS, {silent}, executionCtx)) {
|
|
501
|
+
|
|
502
|
+
return new Promise((resolve, fail) => {
|
|
503
|
+
if (this.removeLoader) {
|
|
504
|
+
this.removeLoader(this, recordIds).then(
|
|
505
|
+
removedIds => {
|
|
506
|
+
let currentIndex = 0;
|
|
507
|
+
const removedIndex: Array<number> = [];
|
|
508
|
+
const currentRecords = getCurrentRecords(this._stateManager);
|
|
509
|
+
|
|
510
|
+
currentRecords.forEach((value, key) => {
|
|
511
|
+
if (removedIds.includes(key)) {
|
|
512
|
+
removedIndex.push(currentIndex);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
currentIndex++
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
this.dispatchAction(Action.RECORDS_REMOVED, { records: removedIds, cachedRecords, removedIndex, buffered: false }, executionCtx);
|
|
519
|
+
resolve(removedIds);
|
|
520
|
+
}
|
|
521
|
+
).catch(error => {
|
|
522
|
+
const {errorCode} = error;
|
|
523
|
+
fail(new ErrorException("Erro ao remover registros", error, errorCode))
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
return Promise.resolve(recordIds);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// API
|
|
534
|
+
/**
|
|
535
|
+
*
|
|
536
|
+
* Obtém o valor do campo em seu formato/tipo correto a partir de uma string.
|
|
537
|
+
*
|
|
538
|
+
* @param fieldName - Nome do campo que terá o tipo identificado para conversão.
|
|
539
|
+
* @param value - Texto que será convertido de acordo com o tipo identificado no campo.
|
|
540
|
+
*
|
|
541
|
+
* @returns - Valor convertido ou ele mesmo.
|
|
542
|
+
*
|
|
543
|
+
*/
|
|
544
|
+
public valueFromString(fieldName: string, value: string): any {
|
|
545
|
+
const descriptor = this.getField(fieldName);
|
|
546
|
+
return descriptor ? convertType(descriptor.dataType, value) : value;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
*
|
|
551
|
+
* Converte o valor informado para texto de acordo com o tipo do campo informado.
|
|
552
|
+
*
|
|
553
|
+
* @param fieldName - Nome do campo utilizado para buscar o tipo de dado com o padrão de conversão para string.
|
|
554
|
+
* @param value - Valor a ser convertido.
|
|
555
|
+
*
|
|
556
|
+
* @returns - Valor informado convertido.
|
|
557
|
+
*
|
|
558
|
+
*/
|
|
559
|
+
public valueToString(fieldName: string, value: any): string {
|
|
560
|
+
const descriptor = this.getField(fieldName);
|
|
561
|
+
return toString(descriptor?.dataType, value);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
*
|
|
566
|
+
* Formata o valor do campo considerando as informações do descriptor.
|
|
567
|
+
* Diferente do método "valueToString" que retorna o dado em valor textual,
|
|
568
|
+
* getFormattedValue retorna uma informação amigável ao usuário, geralmente
|
|
569
|
+
* usada na interface.
|
|
570
|
+
*
|
|
571
|
+
* @param fieldName - Nome do campo utilizado do qual se quer obter valor.
|
|
572
|
+
* @param value (opcional) - O valor a ser convertido. Caso omitido pega do registro selecionado.
|
|
573
|
+
*
|
|
574
|
+
* @returns - Valor formatado.
|
|
575
|
+
*
|
|
576
|
+
*/
|
|
577
|
+
public getFormattedValue(fieldName: string, value?: any): string {
|
|
578
|
+
|
|
579
|
+
const descriptor = this.getField(fieldName);
|
|
580
|
+
if (value == undefined) {
|
|
581
|
+
value = this.getFieldValue(fieldName);
|
|
582
|
+
} else if (typeof value === "string" && descriptor?.dataType != DataType.TEXT){
|
|
583
|
+
value = this.valueFromString(fieldName, value);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if(value == undefined){
|
|
587
|
+
return "";
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
return getFormattedValue(value, descriptor);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
*
|
|
595
|
+
* Adiciona um interceptor correspondente a uma ação do DataUnit para fazer um processamento customizado.
|
|
596
|
+
*
|
|
597
|
+
* @param interceptor - Interceptor a ser adicionado.
|
|
598
|
+
*
|
|
599
|
+
*/
|
|
600
|
+
public addInterceptor(interceptor: DUActionInterceptor): void {
|
|
601
|
+
this._interceptors.push(interceptor);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
*
|
|
606
|
+
* Remove um interceptor da lista de interceptors.
|
|
607
|
+
*
|
|
608
|
+
* @param interceptor - Interceptor a ser removido.
|
|
609
|
+
*
|
|
610
|
+
*/
|
|
611
|
+
public removeInterceptor(interceptor: DUActionInterceptor) {
|
|
612
|
+
this._interceptors = this._interceptors.filter(i => i !== interceptor);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
*
|
|
617
|
+
* Adiciona um FilterProvider.
|
|
618
|
+
*
|
|
619
|
+
* @param provider - FilterProvider que será adicionado.
|
|
620
|
+
*
|
|
621
|
+
*/
|
|
622
|
+
public addFilterProvider(provider: FilterProvider): void {
|
|
623
|
+
this._filterProviders.set(this.getFielterProviderKey(provider), provider);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
*
|
|
628
|
+
* Obtém informações de paginação dos registros.
|
|
629
|
+
*
|
|
630
|
+
* @returns - Informações da paginação de registros.
|
|
631
|
+
*/
|
|
632
|
+
public getPaginationInfo(): PaginationInfo | void {
|
|
633
|
+
return getPaginationInfo(this._stateManager);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
*
|
|
638
|
+
* Define a lógica de ordenação dos registros.
|
|
639
|
+
*
|
|
640
|
+
* @param provider - Objeto usado para definir a propriedade sortingProvider da instância da classe.
|
|
641
|
+
*
|
|
642
|
+
*/
|
|
643
|
+
public set sortingProvider(provider: SortingProvider) {
|
|
644
|
+
this._sortingProvider = provider;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
*
|
|
649
|
+
* Define a propriedade metadata da instância da classe com um novo valor e chama o método dispatchAction para notificar os observers da aplicação sobre a mudança.
|
|
650
|
+
*
|
|
651
|
+
*/
|
|
652
|
+
public set metadata(md: UnitMetadata) {
|
|
653
|
+
this.dispatchAction(Action.METADATA_LOADED, md, undefined);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
*
|
|
658
|
+
* Obtém os metadados do DataUnit.
|
|
659
|
+
*
|
|
660
|
+
*/
|
|
661
|
+
public get metadata(): UnitMetadata {
|
|
662
|
+
return getMetadata(this._stateManager);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
*
|
|
667
|
+
* Obtém informações da ligação para um DataUnit filho.
|
|
668
|
+
*
|
|
669
|
+
* @param name - Nome do DataUnit que se deseja.
|
|
670
|
+
*
|
|
671
|
+
* @returns - As informações sobre a ligação solicitada. Pode retornar undefined.
|
|
672
|
+
*
|
|
673
|
+
*/
|
|
674
|
+
public getChildInfo(name: string): ChildDescriptor|undefined {
|
|
675
|
+
const children = this.metadata?.children;
|
|
676
|
+
if(children == undefined){
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
return children.find(child => child.name === name);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
*
|
|
685
|
+
* Define a propriedade records da instância da classe com um novo valor e chama o método dispatchAction para notificar os observers da aplicação sobre a mudança.
|
|
686
|
+
*
|
|
687
|
+
*/
|
|
688
|
+
public set records(records: Array<Record>) {
|
|
689
|
+
this.dispatchAction(Action.DATA_LOADED, { records }, undefined);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
*
|
|
694
|
+
* Obtém todos os registros atuais.
|
|
695
|
+
*
|
|
696
|
+
* @returns - Todos os registros atuais.
|
|
697
|
+
*
|
|
698
|
+
*/
|
|
699
|
+
public get records(): Array<Record> {
|
|
700
|
+
const records = getCurrentRecords(this._stateManager);
|
|
701
|
+
return records ? Array.from(records.values()) : [];
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
*
|
|
706
|
+
* Define a quantidade de registros que será exibido por página.
|
|
707
|
+
*
|
|
708
|
+
* @param size - Quantidade de registros que será exibido por página.
|
|
709
|
+
*
|
|
710
|
+
*/
|
|
711
|
+
public set pageSize(size: number) {
|
|
712
|
+
this._pageSize = size;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
*
|
|
717
|
+
* Obtém a quantidade de registros que está sendo exibido por página.
|
|
718
|
+
*
|
|
719
|
+
* @returns - Quantidade de registros exibidos por página.
|
|
720
|
+
*
|
|
721
|
+
*/
|
|
722
|
+
public get pageSize(): number {
|
|
723
|
+
return this._pageSize;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
*
|
|
728
|
+
* Obtém os registros modificados e ainda não salvos no DataUnit.
|
|
729
|
+
*
|
|
730
|
+
* @returns - Lista dos registros em edição.
|
|
731
|
+
*
|
|
732
|
+
*/
|
|
733
|
+
public getModifiedRecords(): Array<Record> {
|
|
734
|
+
const modified = getModifiedRecords(this._stateManager);
|
|
735
|
+
return modified || [];
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
*
|
|
740
|
+
* Obtém os registros adicionados no DataUnit.
|
|
741
|
+
*
|
|
742
|
+
* @returns - Lista dos registros adicionados.
|
|
743
|
+
*
|
|
744
|
+
*/
|
|
745
|
+
public getAddedRecords(): Array<Record> {
|
|
746
|
+
return getAddedRecords(this._stateManager);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
*
|
|
751
|
+
* Obtém metadados de um campo específico.
|
|
752
|
+
*
|
|
753
|
+
* @param fieldName - Identificador do campo.
|
|
754
|
+
*
|
|
755
|
+
* @returns - Metadados do campo informado.
|
|
756
|
+
*/
|
|
757
|
+
public getField(fieldName: string): FieldDescriptor | undefined {
|
|
758
|
+
return getField(this._stateManager, fieldName);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
*
|
|
763
|
+
* Adiciona um novo registro.
|
|
764
|
+
*
|
|
765
|
+
* @param executionCtx - Contexto de execução da inserção do dado no DataUnit.
|
|
766
|
+
*
|
|
767
|
+
*/
|
|
768
|
+
public async addRecord(executionCtx?: ExecutionContext): Promise<boolean> {
|
|
769
|
+
return this.dispatchAction(Action.RECORDS_ADDED, prepareAddedRecordId(this._stateManager, [{}], this.getParentRecordId()), executionCtx);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
*
|
|
774
|
+
* Efetua a cópia do registro selecionado.
|
|
775
|
+
*
|
|
776
|
+
* @param executionCtx - Contexto de execução da cópia do dado do DataUnit.
|
|
777
|
+
*
|
|
778
|
+
*/
|
|
779
|
+
public copySelected(executionCtx?: ExecutionContext): void {
|
|
780
|
+
const selectionInfo = this.getSelectionInfo();
|
|
781
|
+
if (selectionInfo) {
|
|
782
|
+
const selectedRecords = selectionInfo.records;
|
|
783
|
+
if(selectedRecords){
|
|
784
|
+
this.dispatchAction(Action.RECORDS_COPIED, prepareCopiedRecord(this._stateManager, selectedRecords, this.getParentRecordId()), executionCtx);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
private getRecordsByDataUnit(records: Array<Record>): Map<string, Array<Record>> {
|
|
790
|
+
const recordsMap = new Map<string, Array<Record>>();
|
|
791
|
+
|
|
792
|
+
records.forEach(record => {
|
|
793
|
+
let { __owner__dataunit__name__: ownerDataUnitName } = record;
|
|
794
|
+
|
|
795
|
+
if(!ownerDataUnitName){
|
|
796
|
+
ownerDataUnitName = this.name;
|
|
797
|
+
}
|
|
798
|
+
let changes = recordsMap.get(ownerDataUnitName);
|
|
799
|
+
|
|
800
|
+
if(!changes){
|
|
801
|
+
changes = [];
|
|
802
|
+
recordsMap.set(ownerDataUnitName, changes);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
changes.push(record);
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
return recordsMap;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
private getParentRecordId(){
|
|
812
|
+
const parentRecord = this._parentDataUnit?.getSelectedRecord();
|
|
813
|
+
return parentRecord?.__record__id__;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
*
|
|
818
|
+
* Retorna se a alteração no campo já foi concluída ou se ainda está incompleta.
|
|
819
|
+
*
|
|
820
|
+
* @param fieldName - Identificador do campo a ser verificado.
|
|
821
|
+
*
|
|
822
|
+
* @returns - Verdadeiro se ainda está pendente.
|
|
823
|
+
*
|
|
824
|
+
*/
|
|
825
|
+
public waitingForChange(fieldName: string): boolean {
|
|
826
|
+
return isWaiting(this._stateManager, fieldName);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/**
|
|
830
|
+
*
|
|
831
|
+
* Obtém valor do campo desejado.
|
|
832
|
+
*
|
|
833
|
+
* @param fieldName - Identificador do campo a ser buscado.
|
|
834
|
+
*
|
|
835
|
+
* @returns - Valor do campo.
|
|
836
|
+
*
|
|
837
|
+
*/
|
|
838
|
+
public getFieldValue(fieldName: string): any {
|
|
839
|
+
return getFieldValue(this._stateManager, fieldName);
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
*
|
|
844
|
+
* Insere valor no campo desejado.
|
|
845
|
+
*
|
|
846
|
+
* @param fieldName -Identificador do campo a ser modificado.
|
|
847
|
+
* @param newValue - Valor a ser inserido no campo.
|
|
848
|
+
* @param records - Indica quais registros foram afetados pela alteração no valor do campo.
|
|
849
|
+
* @returns - Promise que será resolvida quando o novo valor for persistido no state.
|
|
850
|
+
*
|
|
851
|
+
*/
|
|
852
|
+
public async setFieldValue(fieldName: string, newValue: any, records?: Array<string>): Promise<boolean> {
|
|
853
|
+
|
|
854
|
+
if(!this.hasNewRecord() && !this.getSelectedRecord()) await this.addRecord();
|
|
855
|
+
|
|
856
|
+
const typedValue = this.validateAndTypeValue(fieldName, newValue);
|
|
857
|
+
const currentValue = this.getFieldValue(fieldName);
|
|
858
|
+
|
|
859
|
+
if(newValue instanceof Promise){
|
|
860
|
+
const promise:Promise<boolean> = new Promise(accept => {
|
|
861
|
+
newValue.then(resolvedValue => {
|
|
862
|
+
this.dispatchAction(Action.DATA_RESOLVED, { [fieldName]: resolvedValue, records }, undefined);
|
|
863
|
+
accept(this.setFieldValue(fieldName, resolvedValue, records));
|
|
864
|
+
});
|
|
865
|
+
});
|
|
866
|
+
this._savingLockers.push(promise);
|
|
867
|
+
return promise;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (currentValue !== typedValue) {
|
|
871
|
+
const promise = this.dispatchAction(Action.DATA_CHANGED, { [fieldName]: typedValue, records }, undefined);
|
|
872
|
+
this._savingLockers.push(promise);
|
|
873
|
+
return promise;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
return Promise.resolve(false);
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
*
|
|
881
|
+
* Marca campos como inválidos.
|
|
882
|
+
*
|
|
883
|
+
* @param fieldName - Nome do campo inválido.
|
|
884
|
+
* @param message - Mensagem descrevendo o motivo da invalidade.
|
|
885
|
+
* @param recordId - Indica qual registro está com o campo inválido.
|
|
886
|
+
*
|
|
887
|
+
*/
|
|
888
|
+
public setInvalidField(fieldName: string, message: string, recordId: string): void {
|
|
889
|
+
this.dispatchAction(Action.FIELD_INVALIDATED, { fieldName, message, recordId }, undefined);
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
*
|
|
894
|
+
* Limpa campos inválidos.
|
|
895
|
+
*
|
|
896
|
+
* @param recordId - Indica em qual registro o campo não está mais inválido.
|
|
897
|
+
* @param fieldName - Nome do campo. Caso omitido, todos os campos serão limpos.
|
|
898
|
+
*
|
|
899
|
+
*/
|
|
900
|
+
public clearInvalid(recordId: string, fieldName?: string): void {
|
|
901
|
+
this.dispatchAction(Action.INVALIDATE_CLEAN, { fieldName, recordId }, undefined);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
*
|
|
906
|
+
* Obtém a mensagem de campo inválido para determinado registro.
|
|
907
|
+
*
|
|
908
|
+
* @param recordId - Identificador do registro.
|
|
909
|
+
* @param fieldName - Nome do campo.
|
|
910
|
+
*
|
|
911
|
+
*/
|
|
912
|
+
public getInvalidMessage(recordId: string, fieldName: string): string|undefined {
|
|
913
|
+
return getInvalidFieldMessage(this._stateManager, fieldName, recordId);
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
*
|
|
918
|
+
* Inicia alteração no campo.
|
|
919
|
+
*
|
|
920
|
+
* @param fieldName - Identificador do campo a ser modificado.
|
|
921
|
+
* @param waitingChange - Informa que uma mudança irá iniciar.
|
|
922
|
+
*
|
|
923
|
+
*/
|
|
924
|
+
public startChange(fieldName: string, waitingChange: WaitingChange): void {
|
|
925
|
+
this.dispatchAction(Action.CHANGING_DATA, { [fieldName]: waitingChange }, undefined);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
/**
|
|
929
|
+
*
|
|
930
|
+
* Cancela o início de uma alteração no campo.
|
|
931
|
+
*
|
|
932
|
+
* @param fieldName - Identificador do campo.
|
|
933
|
+
*
|
|
934
|
+
*/
|
|
935
|
+
public cancelWaitingChange(fieldName: string): void {
|
|
936
|
+
this.dispatchAction(Action.WAITING_CHANGE_CANCELED, { fieldName }, undefined);
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* Retorna se existe alterações pendentes.
|
|
941
|
+
* @returns Verdadeiro se existir pendências de modificações.
|
|
942
|
+
*/
|
|
943
|
+
public hasWaitingChanges(): boolean {
|
|
944
|
+
const waitingChanges = getWaitingChanges(this._stateManager);
|
|
945
|
+
return waitingChanges ? !!waitingChanges.size : false;
|
|
946
|
+
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
*
|
|
951
|
+
* Seleciona o primeiro registro.
|
|
952
|
+
*
|
|
953
|
+
* @param executionCtx - Contexto de execução da seleção do registro do DataUnit.
|
|
954
|
+
*
|
|
955
|
+
*/
|
|
956
|
+
public selectFirst(executionCtx?: ExecutionContext): void {
|
|
957
|
+
if (this.records.length > 0) {
|
|
958
|
+
this.setSelectionByIndex([0], executionCtx);
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
*
|
|
964
|
+
* Seleciona o último registro.
|
|
965
|
+
*
|
|
966
|
+
* @param executionCtx - Contexto de execução da seleção do registro do DataUnit.
|
|
967
|
+
*
|
|
968
|
+
*/
|
|
969
|
+
public selectLast(executionCtx?: ExecutionContext): void {
|
|
970
|
+
if (this.records.length > 0) {
|
|
971
|
+
this.setSelectionByIndex([this.records.length - 1], executionCtx);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/**
|
|
976
|
+
*
|
|
977
|
+
* Seleciona múltiplos registros por índice.
|
|
978
|
+
*
|
|
979
|
+
* @param selection - Índices desejados para a seleção.
|
|
980
|
+
* @param executionCtx - Contexto de execução da seleção do registro do DataUnit.
|
|
981
|
+
*
|
|
982
|
+
*/
|
|
983
|
+
public setSelectionByIndex(selection: Array<number>, executionCtx?: ExecutionContext): void {
|
|
984
|
+
this.dispatchAction(Action.SELECTION_CHANGED, { type: "index", selection }, executionCtx);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
*
|
|
989
|
+
* Seleciona múltiplos registros por ID ou todos os registros (multipágina) .
|
|
990
|
+
*
|
|
991
|
+
* @param selection - IDs para selecionar ou o modo de seleção completo.
|
|
992
|
+
* @param executionCtx - Contexto de execução da seleção dos registros do DataUnit.
|
|
993
|
+
*
|
|
994
|
+
*
|
|
995
|
+
*/
|
|
996
|
+
public setSelection(selection: Array<string> | SelectionMode.ALL_RECORDS, executionCtx?: ExecutionContext): Promise<SelectionInfo> {
|
|
997
|
+
return new Promise(resolve => {
|
|
998
|
+
this.dispatchAction(Action.SELECTION_CHANGED, { type: "id", selection }, executionCtx)
|
|
999
|
+
.then(()=>resolve(getSelectionInfo(this._stateManager)));
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
/**
|
|
1004
|
+
*
|
|
1005
|
+
* Limpa a seleção.
|
|
1006
|
+
*
|
|
1007
|
+
* @param executionCtx - Contexto de execução da seleção dos registros do DataUnit.
|
|
1008
|
+
*
|
|
1009
|
+
*/
|
|
1010
|
+
public clearSelection(executionCtx?: ExecutionContext){
|
|
1011
|
+
this.setSelection([], executionCtx);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
/**
|
|
1015
|
+
*
|
|
1016
|
+
* Atualiza a seleção dos registros atuais.
|
|
1017
|
+
*
|
|
1018
|
+
* @param selection - IDs dos registros selecionados no snapshot atual
|
|
1019
|
+
* @param executionCtx - Contexto de execução da seleção dos registros do DataUnit.
|
|
1020
|
+
*
|
|
1021
|
+
* @returns - Informações sobre a seleção.
|
|
1022
|
+
*/
|
|
1023
|
+
public updatePageSelection(selection: Array<string>, executionCtx?: ExecutionContext): Promise<SelectionInfo|undefined>{
|
|
1024
|
+
|
|
1025
|
+
if(!selection){
|
|
1026
|
+
return Promise.resolve(this.getSelectionInfo());
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
return new Promise(resolve => {
|
|
1030
|
+
const newSelection: Set<string> = new Set(getSelection(this._stateManager));
|
|
1031
|
+
const currentRecords = getCurrentRecords(this._stateManager) || new Map();
|
|
1032
|
+
Array.from(currentRecords.keys()).forEach(
|
|
1033
|
+
recordId => {
|
|
1034
|
+
if(selection.includes(recordId)){
|
|
1035
|
+
newSelection.add(recordId);
|
|
1036
|
+
} else {
|
|
1037
|
+
newSelection.delete(recordId);
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
);
|
|
1041
|
+
|
|
1042
|
+
this.dispatchAction(Action.SELECTION_CHANGED, { type: "id", selection: Array.from(newSelection) }, executionCtx)
|
|
1043
|
+
.then(()=>{
|
|
1044
|
+
resolve(this.getSelectionInfo())
|
|
1045
|
+
});
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
*
|
|
1051
|
+
* Obtém informações sobre a seleção atual.
|
|
1052
|
+
*
|
|
1053
|
+
* @returns - Objeto com informações como registros selecionados e seleção por critério.
|
|
1054
|
+
*
|
|
1055
|
+
**/
|
|
1056
|
+
public getSelectionInfo(): SelectionInfo | undefined {
|
|
1057
|
+
return getSelectionInfo(this._stateManager)
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/**
|
|
1061
|
+
*
|
|
1062
|
+
* Retorna apenas um registro selecionado no Dataunit
|
|
1063
|
+
*
|
|
1064
|
+
* @returns - Registro selecionado.
|
|
1065
|
+
*
|
|
1066
|
+
*/
|
|
1067
|
+
public getSelectedRecord(): Record | undefined {
|
|
1068
|
+
const selection = getSelectionRecords(this._stateManager);
|
|
1069
|
+
if (!selection || selection.length == 0) return;
|
|
1070
|
+
return selection[0];
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
*
|
|
1075
|
+
* Limpa todos os registros do DataUnit
|
|
1076
|
+
*
|
|
1077
|
+
*
|
|
1078
|
+
*/
|
|
1079
|
+
public clearDataUnit(): void {
|
|
1080
|
+
this.records = [];
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
/**
|
|
1084
|
+
*
|
|
1085
|
+
* Seleciona o próximo registro.
|
|
1086
|
+
*
|
|
1087
|
+
* @param executionCtx - Contexto de execução da seleção do registro do DataUnit.
|
|
1088
|
+
*
|
|
1089
|
+
*
|
|
1090
|
+
*/
|
|
1091
|
+
public nextRecord(executionCtx?: ExecutionContext): void {
|
|
1092
|
+
if(!hasNext(this._stateManager)){
|
|
1093
|
+
if(hasMorePages(this._stateManager)){
|
|
1094
|
+
this.nextPage({
|
|
1095
|
+
before: act =>{
|
|
1096
|
+
if(executionCtx && executionCtx.before){
|
|
1097
|
+
act = executionCtx.before(act);
|
|
1098
|
+
}
|
|
1099
|
+
return act;
|
|
1100
|
+
},
|
|
1101
|
+
after: act => {
|
|
1102
|
+
this.selectFirst(executionCtx);
|
|
1103
|
+
}
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
} else {
|
|
1107
|
+
this.dispatchAction(Action.NEXT_SELECTED, undefined, executionCtx);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
*
|
|
1113
|
+
* Seleciona o registro anterior.
|
|
1114
|
+
*
|
|
1115
|
+
* @param executionCtx - Contexto de execução da seleção do registro do DataUnit.
|
|
1116
|
+
*
|
|
1117
|
+
*/
|
|
1118
|
+
public previousRecord(executionCtx?: ExecutionContext): void {
|
|
1119
|
+
if(!hasPrevious(this._stateManager)){
|
|
1120
|
+
if(hasPreviousPages(this._stateManager)){
|
|
1121
|
+
this.previousPage({
|
|
1122
|
+
before: act =>{
|
|
1123
|
+
if(executionCtx && executionCtx.before){
|
|
1124
|
+
act = executionCtx.before(act);
|
|
1125
|
+
}
|
|
1126
|
+
return act;
|
|
1127
|
+
},
|
|
1128
|
+
after: act => {
|
|
1129
|
+
this.selectLast(executionCtx);
|
|
1130
|
+
}
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
} else {
|
|
1134
|
+
this.dispatchAction(Action.PREVIOUS_SELECTED, undefined, executionCtx);
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
*
|
|
1140
|
+
* Cancela edição do registro atual.
|
|
1141
|
+
*
|
|
1142
|
+
* @param executionCtx - Contexto de execução do cancelamento da seleção dos registros.
|
|
1143
|
+
* @param silent: Define se haverá mensagem de confirmação do cancelamento
|
|
1144
|
+
*
|
|
1145
|
+
*/
|
|
1146
|
+
public async cancelEdition(executionCtx?: ExecutionContext, fromParent?: boolean, silent: boolean = false): Promise<boolean> {
|
|
1147
|
+
const cancelledAction = await this.dispatchAction(Action.EDITION_CANCELED, {fromParent, silent}, executionCtx);
|
|
1148
|
+
for (const [, dataUnit] of this._childByName) {
|
|
1149
|
+
if(dataUnit.isDirty()){
|
|
1150
|
+
dataUnit.cancelEdition(undefined, true, silent);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
return Promise.resolve(cancelledAction);
|
|
1154
|
+
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/**
|
|
1158
|
+
*
|
|
1159
|
+
* Retorna se existe algum tipo de alteração pendente.
|
|
1160
|
+
*
|
|
1161
|
+
* @returns Verdadeiro se existir alterações pendentes.
|
|
1162
|
+
*
|
|
1163
|
+
*/
|
|
1164
|
+
public isDirty(): boolean {
|
|
1165
|
+
return isDirty(this._stateManager) || this.childrenIsDirty();
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
/**
|
|
1169
|
+
*
|
|
1170
|
+
* Retorna se existe algum DataUnit detail com alterações pendentes.
|
|
1171
|
+
*
|
|
1172
|
+
* @returns Verdadeiro se existir alterações pendentes em algum DataUnit detail.
|
|
1173
|
+
*
|
|
1174
|
+
*/
|
|
1175
|
+
private childrenIsDirty():boolean {
|
|
1176
|
+
for (let [key, dataUnitChild] of this._childByName) {
|
|
1177
|
+
if(dataUnitChild.isDirty()) return true;
|
|
1178
|
+
}
|
|
1179
|
+
return false;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
/**
|
|
1183
|
+
*
|
|
1184
|
+
* Retorna se existe algum registro em modo de edição.
|
|
1185
|
+
*
|
|
1186
|
+
* @returns Verdadeiro se existir alterações de registros pendentes.
|
|
1187
|
+
*
|
|
1188
|
+
*/
|
|
1189
|
+
public hasDirtyRecords(): boolean {
|
|
1190
|
+
return hasDirtyRecords(this._stateManager);
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
*
|
|
1195
|
+
* Retorna se existir uma pagina seguinte a atual na paginação.
|
|
1196
|
+
*
|
|
1197
|
+
* @returns Verdadeiro se existir uma próxima página.
|
|
1198
|
+
*
|
|
1199
|
+
*/
|
|
1200
|
+
public hasNext(): boolean {
|
|
1201
|
+
let result = hasNext(this._stateManager);
|
|
1202
|
+
if(!result){
|
|
1203
|
+
result = hasMorePages(this._stateManager);
|
|
1204
|
+
}
|
|
1205
|
+
return result;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
*
|
|
1210
|
+
* Retorna se existe uma página anterior a atual na paginação.
|
|
1211
|
+
*
|
|
1212
|
+
* @returns Verdadeiro se existir uma página anterior.
|
|
1213
|
+
*
|
|
1214
|
+
*/
|
|
1215
|
+
public hasPrevious(): boolean {
|
|
1216
|
+
let result = hasPrevious(this._stateManager);
|
|
1217
|
+
if(!result){
|
|
1218
|
+
result = hasPreviousPages(this._stateManager);
|
|
1219
|
+
}
|
|
1220
|
+
return result;
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
*
|
|
1225
|
+
* Verifica se um registro é proveniente de inclusão.
|
|
1226
|
+
*
|
|
1227
|
+
* @param recordId - O id do registro a ser verificado.
|
|
1228
|
+
* @returns Verdadeiro se o id solicitado é de um registro novo.
|
|
1229
|
+
*
|
|
1230
|
+
*/
|
|
1231
|
+
public isNewRecord(recordId: string): boolean {
|
|
1232
|
+
const newRecords = getAddedRecords(this._stateManager);
|
|
1233
|
+
if(newRecords && newRecords.length > 0){
|
|
1234
|
+
const record = newRecords.find(newRecord => newRecord.__record__id__ === recordId);
|
|
1235
|
+
return record != undefined;
|
|
1236
|
+
}
|
|
1237
|
+
return false;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
*
|
|
1242
|
+
* Retorna se existe pelo menos um registro novo.
|
|
1243
|
+
*
|
|
1244
|
+
* @returns Verdadeiro se algum registro foi adicionado.
|
|
1245
|
+
*
|
|
1246
|
+
*/
|
|
1247
|
+
public hasNewRecord(): boolean {
|
|
1248
|
+
const newRecords = getAddedRecords(this._stateManager);
|
|
1249
|
+
return newRecords && newRecords.length > 0;
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
/**
|
|
1253
|
+
*
|
|
1254
|
+
* Retorna se existe pelo menos um registro novo.
|
|
1255
|
+
*
|
|
1256
|
+
* @returns Verdadeiro se algum registro foi adicionado.
|
|
1257
|
+
*
|
|
1258
|
+
*/
|
|
1259
|
+
public hasCopiedRecord(): boolean {
|
|
1260
|
+
const newRecords = getAddedRecords(this._stateManager);
|
|
1261
|
+
if(newRecords == undefined){
|
|
1262
|
+
return false;
|
|
1263
|
+
}
|
|
1264
|
+
for(let record of newRecords){
|
|
1265
|
+
if (record['__record__source__id__'] != undefined) {
|
|
1266
|
+
return true
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
return false;
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
*
|
|
1274
|
+
* Retorna se a informação do estado anterior está salva, permitindo desfazer a ação.
|
|
1275
|
+
*
|
|
1276
|
+
* @returns Verdadeiro se for possível desfazer a ação.
|
|
1277
|
+
*
|
|
1278
|
+
*/
|
|
1279
|
+
public canUndo(): boolean {
|
|
1280
|
+
return canUndo(this._stateManager);
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
/**
|
|
1284
|
+
*
|
|
1285
|
+
* Retorna se a informação do estado futuro está salva, permitindo refazer a ação.
|
|
1286
|
+
*
|
|
1287
|
+
* @returns Verdadeiro se for possível refazer a ação.
|
|
1288
|
+
*
|
|
1289
|
+
*/
|
|
1290
|
+
public canRedo(): boolean {
|
|
1291
|
+
return canRedo(this._stateManager);
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
*
|
|
1296
|
+
* Desfaz a última ação.
|
|
1297
|
+
*
|
|
1298
|
+
* @param executionCtx - Contexto de execução de desfazer a última ação.
|
|
1299
|
+
*
|
|
1300
|
+
*
|
|
1301
|
+
*/
|
|
1302
|
+
public undo(executionCtx?: ExecutionContext) {
|
|
1303
|
+
this.dispatchAction(Action.CHANGE_UNDONE, undefined, executionCtx);
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
/**
|
|
1307
|
+
*
|
|
1308
|
+
* Refaz a última ação.
|
|
1309
|
+
*
|
|
1310
|
+
* @param executionCtx - Contexto de execução de refazer a última ação.
|
|
1311
|
+
*
|
|
1312
|
+
*/
|
|
1313
|
+
public redo(executionCtx?: ExecutionContext) {
|
|
1314
|
+
this.dispatchAction(Action.CHANGE_REDONE, undefined, executionCtx);
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
/**
|
|
1318
|
+
*
|
|
1319
|
+
* Obtém a representação textual do DataUnit, nesse caso, o nome do DataUnit.
|
|
1320
|
+
*
|
|
1321
|
+
* @returns - Valor contido na propriedade name.
|
|
1322
|
+
*
|
|
1323
|
+
*/
|
|
1324
|
+
public toString() {
|
|
1325
|
+
return this.name;
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// Actions / State manager
|
|
1329
|
+
/**
|
|
1330
|
+
*
|
|
1331
|
+
* Lança ação do DataUnit para que sejam processadas.
|
|
1332
|
+
*
|
|
1333
|
+
* @param actionType - Tipo de ação que será executada.
|
|
1334
|
+
* @param payload - Dados que serão processados na ação.
|
|
1335
|
+
* @param executionCtx - Contexto de execução de lançar a ação que será executada.
|
|
1336
|
+
*
|
|
1337
|
+
* @returns - Verdadeiro se ação iniciada.
|
|
1338
|
+
*
|
|
1339
|
+
*/
|
|
1340
|
+
private async dispatchAction(actionType: Action, payload?: any, executionCtx?: ExecutionContext): Promise<boolean> {
|
|
1341
|
+
return new Promise(async resolve => {
|
|
1342
|
+
let action = new DataUnitAction(actionType, payload);
|
|
1343
|
+
if (executionCtx && executionCtx.before) {
|
|
1344
|
+
action = executionCtx.before(action);
|
|
1345
|
+
}
|
|
1346
|
+
if (action && this._interceptors && this._interceptors.length > 0) {
|
|
1347
|
+
action = await this.intercept(action, this._interceptors.values());
|
|
1348
|
+
}
|
|
1349
|
+
if (action) {
|
|
1350
|
+
this.doDispatchAction(action);
|
|
1351
|
+
if (executionCtx && executionCtx.after) {
|
|
1352
|
+
executionCtx.after(action)
|
|
1353
|
+
}
|
|
1354
|
+
resolve(true);
|
|
1355
|
+
} else {
|
|
1356
|
+
resolve(false);
|
|
1357
|
+
}
|
|
1358
|
+
});
|
|
1359
|
+
}
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
*
|
|
1363
|
+
* Notifica os interceptors de que uma ação foi executada, para que cada interceptor possa fazer sua respectiva tratativa dos dados.
|
|
1364
|
+
*
|
|
1365
|
+
* @param action - Ação que foi executada.
|
|
1366
|
+
* @param interceptors - Interceptors que serão notificados.
|
|
1367
|
+
*
|
|
1368
|
+
* @returns - Ação executada no DataUnit.
|
|
1369
|
+
*
|
|
1370
|
+
*/
|
|
1371
|
+
private async intercept(action: DataUnitAction, interceptors: IterableIterator<DUActionInterceptor>): Promise<DataUnitAction> {
|
|
1372
|
+
return new Promise(async resolve => {
|
|
1373
|
+
let ite;
|
|
1374
|
+
while (action && !(ite = interceptors.next()).done) {
|
|
1375
|
+
action = await ite.value.interceptAction(action);
|
|
1376
|
+
}
|
|
1377
|
+
resolve(action);
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
*
|
|
1383
|
+
* Processa as ações no DataUnit e notifica os observers.
|
|
1384
|
+
*
|
|
1385
|
+
* @param action - Ações em execução no DataUnit.
|
|
1386
|
+
*
|
|
1387
|
+
*/
|
|
1388
|
+
private doDispatchAction(action: DataUnitAction): void {
|
|
1389
|
+
this._stateManager.process(action);
|
|
1390
|
+
this?._parentDataUnit?.dispatchAction(Action.CHILD_CHANGED, { srcAction: action, srcDataUnit: this });
|
|
1391
|
+
this._observers.forEach(f => {
|
|
1392
|
+
/*
|
|
1393
|
+
if some observer throws exceptions,
|
|
1394
|
+
should be continued
|
|
1395
|
+
*/
|
|
1396
|
+
try {
|
|
1397
|
+
f(action);
|
|
1398
|
+
} catch (e) {
|
|
1399
|
+
console.warn("[DataUnit] error while call observer", e);
|
|
1400
|
+
}
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
/**
|
|
1405
|
+
*
|
|
1406
|
+
* Cria um dataunit filho.
|
|
1407
|
+
*
|
|
1408
|
+
* @param name - Nome do dataunit filho.
|
|
1409
|
+
*
|
|
1410
|
+
*/
|
|
1411
|
+
public getChildDataunit(name:string){
|
|
1412
|
+
const detail: DataUnit = new DataUnit(name, this);
|
|
1413
|
+
this._childByName.set(name, detail);
|
|
1414
|
+
return detail;
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
/**
|
|
1418
|
+
*
|
|
1419
|
+
* Remove um dataunit filho.
|
|
1420
|
+
*
|
|
1421
|
+
* @param name - Nome do dataunit filho.
|
|
1422
|
+
*
|
|
1423
|
+
*/
|
|
1424
|
+
public removeChildDataunit(name:string){
|
|
1425
|
+
this._childByName.delete(name);
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
*
|
|
1430
|
+
* Adiciona um novo observer no DataUnit.
|
|
1431
|
+
* Ela vai ser chamada sempre que uma ação for despachada (dispatchAction()).
|
|
1432
|
+
*
|
|
1433
|
+
* @param observer - Função que recebe como parâmetro as ações que serão monitoradas.
|
|
1434
|
+
*
|
|
1435
|
+
*/
|
|
1436
|
+
public subscribe(observer: (action: DataUnitAction) => void) {
|
|
1437
|
+
this._observers.push(observer);
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/**
|
|
1441
|
+
*
|
|
1442
|
+
* Remove um observer existente.
|
|
1443
|
+
*
|
|
1444
|
+
* @param observer - Observer que se deseja remover.
|
|
1445
|
+
*
|
|
1446
|
+
*/
|
|
1447
|
+
public unsubscribe(observer: Function) {
|
|
1448
|
+
this._observers = this._observers.filter(f => f !== observer);
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
/**
|
|
1452
|
+
*
|
|
1453
|
+
* Recarrega registro selecionado com dados atualizados do servidor.
|
|
1454
|
+
*
|
|
1455
|
+
* @returns - Dados atualizados do registro selecionado.
|
|
1456
|
+
*
|
|
1457
|
+
*/
|
|
1458
|
+
public reloadCurrentRecord(): Promise<Array<Record>>{
|
|
1459
|
+
return new Promise(async (resolve, fail) => {
|
|
1460
|
+
const selection = getSelection(this._stateManager);
|
|
1461
|
+
this.dispatchAction(Action.LOADING_RECORD, selection);
|
|
1462
|
+
|
|
1463
|
+
if(!this.dataLoader) return;
|
|
1464
|
+
if(!this.recordLoader) return;
|
|
1465
|
+
|
|
1466
|
+
this.recordLoader(this, selection).then(response => {
|
|
1467
|
+
this.dispatchAction(Action.RECORD_LOADED, response)
|
|
1468
|
+
}).catch(cause => {
|
|
1469
|
+
const {errorCode} = cause;
|
|
1470
|
+
fail(new ErrorException("Erro ao recarregar registro", cause, errorCode));
|
|
1471
|
+
});
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
/**
|
|
1476
|
+
*
|
|
1477
|
+
* Obtém a estrutura de ordenação das colunas dos dados.
|
|
1478
|
+
*
|
|
1479
|
+
* @returns - Lista dos ordenáveis por prioridade.
|
|
1480
|
+
*
|
|
1481
|
+
*/
|
|
1482
|
+
public getSort(): Array<Sort> | undefined {
|
|
1483
|
+
return this._sortingProvider ? this._sortingProvider.getSort(this._name) : undefined;
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
/**
|
|
1487
|
+
*
|
|
1488
|
+
* Obtém todos os filtros de dados.
|
|
1489
|
+
*
|
|
1490
|
+
* @returns - Lista de filtros.
|
|
1491
|
+
*
|
|
1492
|
+
*/
|
|
1493
|
+
public getFilters(): Array<Filter> | undefined {
|
|
1494
|
+
let filters: Array<Filter> | undefined = undefined;
|
|
1495
|
+
this._filterProviders.forEach(p => {
|
|
1496
|
+
const f = p.getFilter(this.name);
|
|
1497
|
+
if (f) {
|
|
1498
|
+
filters = (filters || []).concat(f);
|
|
1499
|
+
}
|
|
1500
|
+
});
|
|
1501
|
+
return filters;
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
/**
|
|
1505
|
+
*
|
|
1506
|
+
* Obtém as informações da última carga de dados.
|
|
1507
|
+
*
|
|
1508
|
+
* @returns - As informações de filtro e paginação.
|
|
1509
|
+
*
|
|
1510
|
+
*/
|
|
1511
|
+
public getLastLoadRequest(): LoadDataRequest | undefined {
|
|
1512
|
+
return getCurrentRequest(this._stateManager);
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/**
|
|
1516
|
+
*
|
|
1517
|
+
* Obtém os filtros aplicados.
|
|
1518
|
+
*
|
|
1519
|
+
* @returns - Lista de filtros.
|
|
1520
|
+
*
|
|
1521
|
+
*/
|
|
1522
|
+
public getAppliedFilters(): Array<Filter> | undefined {
|
|
1523
|
+
const { filters }: LoadDataRequest = getCurrentRequest(this._stateManager) || {};
|
|
1524
|
+
return filters
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
/**
|
|
1528
|
+
*
|
|
1529
|
+
* Habilita um campo do DataUnit
|
|
1530
|
+
* @param fieldName - nome do campo para ser habilitado.
|
|
1531
|
+
*
|
|
1532
|
+
*/
|
|
1533
|
+
public enableField(fieldName:string) {
|
|
1534
|
+
const fieldDescriptor = this.getField(fieldName);
|
|
1535
|
+
|
|
1536
|
+
if(fieldDescriptor?.readOnly === true){
|
|
1537
|
+
fieldDescriptor.readOnly = false;
|
|
1538
|
+
this.metadata = {...this.metadata};
|
|
1539
|
+
}
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
/**
|
|
1543
|
+
*
|
|
1544
|
+
* Desabilita um campo do DataUnit
|
|
1545
|
+
* @param fieldName - nome do campo para ficar desabilitado.
|
|
1546
|
+
*
|
|
1547
|
+
*/
|
|
1548
|
+
public disableField(fieldName:string) {
|
|
1549
|
+
const fieldDescriptor = this.getField(fieldName);
|
|
1550
|
+
|
|
1551
|
+
if(fieldDescriptor?.readOnly === false){
|
|
1552
|
+
fieldDescriptor.readOnly = true;
|
|
1553
|
+
this.metadata = {...this.metadata};
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
/**
|
|
1558
|
+
*
|
|
1559
|
+
* Deixa um campo do DataUnit invisível
|
|
1560
|
+
* @param fieldName - nome do campo para ficar invisível.
|
|
1561
|
+
*
|
|
1562
|
+
*/
|
|
1563
|
+
public hideField(fieldName:string) {
|
|
1564
|
+
const fieldDescriptor = this.getField(fieldName);
|
|
1565
|
+
|
|
1566
|
+
if(fieldDescriptor?.visible === true){
|
|
1567
|
+
fieldDescriptor.visible = false;
|
|
1568
|
+
this.metadata = {...this.metadata};
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
/**
|
|
1573
|
+
*
|
|
1574
|
+
* Deixa um campo do DataUnit visível
|
|
1575
|
+
* @param fieldName - nome do campo para ficar visível.
|
|
1576
|
+
*
|
|
1577
|
+
*/
|
|
1578
|
+
public showField(fieldName:string) {
|
|
1579
|
+
const fieldDescriptor = this.getField(fieldName);
|
|
1580
|
+
|
|
1581
|
+
if(fieldDescriptor?.visible === false){
|
|
1582
|
+
fieldDescriptor.visible = true;
|
|
1583
|
+
this.metadata = {...this.metadata};
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
/**
|
|
1588
|
+
*
|
|
1589
|
+
* Obtém todos os registros selecionados.
|
|
1590
|
+
*
|
|
1591
|
+
* @returns - Lista de registros selecionados.
|
|
1592
|
+
*
|
|
1593
|
+
* @deprecated - Utilize o método `getSelectionInfo()` para obter os registros selecionados.
|
|
1594
|
+
* Devido a seleção virtual baseada em critérios e ordenação (ALL_RECORDS), esse
|
|
1595
|
+
* método foi descontinuado e pode retornar erros no caso da seleção virtual.
|
|
1596
|
+
*
|
|
1597
|
+
*/
|
|
1598
|
+
public getSelectedRecords(): Array<Record> | undefined {
|
|
1599
|
+
console.warn("DataUnit: O método `getSelectedRecords` foi descontinuado. Use o método `getSelectionInfo`.");
|
|
1600
|
+
const selection = this.getSelectionInfo();
|
|
1601
|
+
|
|
1602
|
+
if(selection != undefined && selection.isAllRecords()){
|
|
1603
|
+
throw new Error("Erro interno: Impossível retornar os registros selecionados. A seleção atual é virtual. Use o método `getSelectionInfo`.");
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
if (selection) {
|
|
1607
|
+
return selection.records;
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
/**
|
|
1612
|
+
*
|
|
1613
|
+
* Obtém ids dos registros selecionados.
|
|
1614
|
+
*
|
|
1615
|
+
* @returns - Lista com id de todos os registros selecionados.
|
|
1616
|
+
*
|
|
1617
|
+
* @deprecated - Utilize o método `getSelectionInfo()` para obter os registros selecionados.
|
|
1618
|
+
* Devido a seleção virtual baseada em critérios e ordenação (ALL_RECORDS), esse
|
|
1619
|
+
* método foi descontinuado e pode retornar erros no caso da seleção virtual.
|
|
1620
|
+
*
|
|
1621
|
+
*/
|
|
1622
|
+
public getSelection(): Array<string> {
|
|
1623
|
+
console.warn("DataUnit: O método `getSelection` foi descontinuado. Use o método `getSelectionInfo`.");
|
|
1624
|
+
const selection = this.getSelectionInfo();
|
|
1625
|
+
|
|
1626
|
+
if(selection != undefined && selection.isAllRecords()){
|
|
1627
|
+
throw new Error("Erro interno: Impossível retornar os registros selecionados. A seleção atual é virtual. Use o método `getSelectionInfo`.");
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
return selection?.recordIds || [];
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
/**
|
|
1634
|
+
* Adiciona um locker para impedir o carregamento dos registros do dataUnit.
|
|
1635
|
+
* @returns Retorna uma função responsável por liberar o lock adicionado.
|
|
1636
|
+
*/
|
|
1637
|
+
public addLoadingLocker(): Function {
|
|
1638
|
+
let loadingLockerResolver;
|
|
1639
|
+
|
|
1640
|
+
this._loadingLockers.push(new Promise((resolve) => {
|
|
1641
|
+
loadingLockerResolver = resolve;
|
|
1642
|
+
}));
|
|
1643
|
+
|
|
1644
|
+
return loadingLockerResolver || Promise.resolve;
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
private async processLoadingLockers(){
|
|
1648
|
+
if(this._loadingLockers.length) {
|
|
1649
|
+
await Promise.all(this._loadingLockers);
|
|
1650
|
+
this._loadingLockers = [];
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
export interface DUActionInterceptor {
|
|
1657
|
+
interceptAction(action: DataUnitAction): DataUnitAction | Promise<DataUnitAction>;
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
export interface Record {
|
|
1661
|
+
__record__id__: string;
|
|
1662
|
+
__record__label__?: string;
|
|
1663
|
+
__parent__record__id__?: string;
|
|
1664
|
+
__owner__dataunit__name__?: string;
|
|
1665
|
+
[key: string]: any;
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
export interface SavedRecord extends Record {
|
|
1669
|
+
__old__id__?: string;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
export enum ChangeOperation {
|
|
1673
|
+
INSERT = "INSERT",
|
|
1674
|
+
COPY = "COPY",
|
|
1675
|
+
UPDATE = "UPDATE",
|
|
1676
|
+
DELETE = "DELETE",
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
/***
|
|
1680
|
+
* `Change`: Dados que representam uma alteração.
|
|
1681
|
+
*/
|
|
1682
|
+
export class Change {
|
|
1683
|
+
|
|
1684
|
+
public dataUnit: string;
|
|
1685
|
+
public record: Record;
|
|
1686
|
+
public sourceId: string | undefined;
|
|
1687
|
+
public updatingFields: any;
|
|
1688
|
+
|
|
1689
|
+
private _operation: ChangeOperation;
|
|
1690
|
+
|
|
1691
|
+
constructor(dataUnit: string, record: Record, updates: any, operation: ChangeOperation, sourceId?: string) {
|
|
1692
|
+
this.dataUnit = dataUnit;
|
|
1693
|
+
this.record = record;
|
|
1694
|
+
this.sourceId = sourceId;
|
|
1695
|
+
this.updatingFields = updates;
|
|
1696
|
+
this._operation = operation;
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
/**
|
|
1700
|
+
*
|
|
1701
|
+
* Obtém o tipo de operação que está sendo realizada.
|
|
1702
|
+
*
|
|
1703
|
+
* @returns - Ação que está sendo executada.
|
|
1704
|
+
*
|
|
1705
|
+
*/
|
|
1706
|
+
public get operation(): string {
|
|
1707
|
+
return this._operation.toString();
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
*
|
|
1712
|
+
* Retorna se o DataUnit está em uma operação de inserção.
|
|
1713
|
+
*
|
|
1714
|
+
* @returns - Verdadeiro se a operação for de inserção.
|
|
1715
|
+
*
|
|
1716
|
+
*/
|
|
1717
|
+
public isInsert(): boolean {
|
|
1718
|
+
return this._operation === ChangeOperation.INSERT;
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
/**
|
|
1722
|
+
*
|
|
1723
|
+
* Retorna se o DataUnit está em uma operação de cópia.
|
|
1724
|
+
*
|
|
1725
|
+
* @returns - Verdadeiro se a operação for de cópia.
|
|
1726
|
+
*
|
|
1727
|
+
*/
|
|
1728
|
+
public isCopy(): boolean {
|
|
1729
|
+
return this._operation === ChangeOperation.COPY;
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
/**
|
|
1733
|
+
*
|
|
1734
|
+
* Retorna se o DataUnit está em uma operação de deleção.
|
|
1735
|
+
*
|
|
1736
|
+
* @returns - Verdadeiro se a operação for de deleção.
|
|
1737
|
+
*
|
|
1738
|
+
*/
|
|
1739
|
+
public isDelete(): boolean {
|
|
1740
|
+
return this._operation === ChangeOperation.DELETE;
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
/**
|
|
1744
|
+
*
|
|
1745
|
+
* Retorna se o DataUnit está em uma operação de atualização.
|
|
1746
|
+
*
|
|
1747
|
+
* @returns - Verdadeiro se a operação for de atualização.
|
|
1748
|
+
*
|
|
1749
|
+
*/
|
|
1750
|
+
public isUpdate(): boolean {
|
|
1751
|
+
return this._operation === ChangeOperation.UPDATE;
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
export interface WaitingChange {
|
|
1758
|
+
waitmessage: string;
|
|
1759
|
+
blocking: boolean;
|
|
1760
|
+
promise?: Promise<any>;
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
export interface PageRequest {
|
|
1764
|
+
limit: number;
|
|
1765
|
+
offset: number;
|
|
1766
|
+
quickFilter?: QuickFilter;
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
export interface QuickFilter {
|
|
1770
|
+
term: string;
|
|
1771
|
+
fields?: Array<string>;
|
|
1772
|
+
filter?: Filter;
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
export interface PageResponse {
|
|
1776
|
+
limit: number;
|
|
1777
|
+
offset: number;
|
|
1778
|
+
total: number;
|
|
1779
|
+
hasMore: boolean;
|
|
1780
|
+
records: Array<Record>;
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
export enum SelectionMode{
|
|
1784
|
+
ALL_RECORDS = "ALL_RECORDS",
|
|
1785
|
+
SOME_RECORDS = "SOME_RECORDS"
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
export class SelectionInfo{
|
|
1789
|
+
|
|
1790
|
+
private _records: Array<Record>;
|
|
1791
|
+
public mode: SelectionMode;
|
|
1792
|
+
private _total?: number;
|
|
1793
|
+
public filters?: Array<Filter>;
|
|
1794
|
+
public sort?: Array<Sort>;
|
|
1795
|
+
|
|
1796
|
+
constructor(records: Array<Record>, mode: SelectionMode = SelectionMode.SOME_RECORDS, total?:number, filters?: Array<Filter>, sort?: Array<Sort>){
|
|
1797
|
+
this._records = records;
|
|
1798
|
+
this._total = total;
|
|
1799
|
+
this.mode = mode;
|
|
1800
|
+
this.filters = filters;
|
|
1801
|
+
this.sort = sort;
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
public get records(): Array<Record> | undefined{
|
|
1805
|
+
if(this.isAllRecords()){
|
|
1806
|
+
throw new Error("Erro interno: [ALL_RECORDS] - Impossível retornar os registros selecionados numa seleção virtual.")
|
|
1807
|
+
}
|
|
1808
|
+
return this._records;
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
public get recordIds(): Array<string> | undefined{
|
|
1812
|
+
|
|
1813
|
+
const records = this.records;
|
|
1814
|
+
|
|
1815
|
+
if(records == undefined){
|
|
1816
|
+
return undefined;
|
|
1817
|
+
}
|
|
1818
|
+
return records.map(record => record.__record__id__);
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
public get length(): number{
|
|
1822
|
+
if(this.isAllRecords()){
|
|
1823
|
+
return this._total || 0;
|
|
1824
|
+
}
|
|
1825
|
+
return this.records == undefined ? 0: this.records.length;
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
public isAllRecords(): boolean{
|
|
1829
|
+
return this.mode === SelectionMode.ALL_RECORDS;
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
public isEmpty(): boolean{
|
|
1833
|
+
return this.length === 0;
|
|
1834
|
+
}
|
|
1835
|
+
}
|