@azrico/webobject 1.1.17 → 1.1.18
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/dist/index.js +1 -1
- package/dist/src/WebObjects/ParentWebObject.js +15 -1
- package/dist/src/WebObjects/WebObject.d.ts +1 -0
- package/dist/src/WebObjects/WebObject.js +138 -3
- package/dist/src/WebObjects/WebObjectManager.js +71 -6
- package/dist/src/index.js +8 -1
- package/dist/src/structs/DummyObject.js +8 -1
- package/dist/src/structs/MapableObject.js +72 -2
- package/dist/src/structs/MetaObject.js +35 -1
- package/dist/src/structs/ObjectArray.js +43 -1
- package/dist/src/structs/ObjectManager.js +198 -7
- package/dist/src/structs/SimpleObject.js +489 -13
- package/package.json +1 -1
|
@@ -1,27 +1,218 @@
|
|
|
1
|
-
|
|
1
|
+
import { gfunc } from "@azrico/global";
|
|
2
|
+
import { object_equals_loose, } from "@azrico/object";
|
|
3
|
+
import { EventManager } from "@azrico/event";
|
|
4
|
+
import SimpleObject from "./SimpleObject";
|
|
5
|
+
import { combine_if_not_blank } from "@azrico/string";
|
|
6
|
+
/**
|
|
2
7
|
* This class manages the events of an Simple Object
|
|
3
|
-
*/
|
|
8
|
+
*/
|
|
9
|
+
export class ObjectManager {
|
|
10
|
+
__eventList;
|
|
11
|
+
__eventPrefix;
|
|
12
|
+
__sendEvents = true;
|
|
13
|
+
__currentObject;
|
|
14
|
+
__options;
|
|
15
|
+
async notifyChanged(type, key, value, subvalue) {
|
|
16
|
+
const obj = this.getObject();
|
|
17
|
+
if (!obj)
|
|
18
|
+
return false;
|
|
19
|
+
if (value === undefined)
|
|
20
|
+
value = obj.getValue(key);
|
|
21
|
+
const typestr = (type ? `$${type}$` : "") + key;
|
|
22
|
+
return await this.event_run("on_change", typestr, value);
|
|
23
|
+
}
|
|
24
|
+
/* -------------------------------------------------------------------------- */
|
|
25
|
+
get obj() {
|
|
26
|
+
return this.getObject();
|
|
27
|
+
}
|
|
28
|
+
getObject() {
|
|
29
|
+
return this.__currentObject ?? undefined;
|
|
30
|
+
}
|
|
31
|
+
setObject(toObj) {
|
|
32
|
+
this.__currentObject = toObj;
|
|
33
|
+
if (this.__currentObject) {
|
|
34
|
+
this.__currentObject.get_managerClass = () => {
|
|
35
|
+
return this;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
/* -------------------------------------------------------------------------- */
|
|
41
|
+
/* Child functions */
|
|
42
|
+
/* -------------------------------------------------------------------------- */
|
|
43
|
+
get isChanged() {
|
|
44
|
+
return this.obj != null && this.obj.isChanged;
|
|
45
|
+
}
|
|
46
|
+
get isDeleted() {
|
|
47
|
+
return this.obj == null || this.obj.isDeleted;
|
|
48
|
+
}
|
|
49
|
+
get d() {
|
|
50
|
+
return this.obj?.d;
|
|
51
|
+
}
|
|
52
|
+
get canModify() {
|
|
53
|
+
return !this.obj?.canModify;
|
|
54
|
+
}
|
|
55
|
+
get isEditMode() {
|
|
56
|
+
return this.obj?.isEditMode;
|
|
57
|
+
}
|
|
58
|
+
get isNew() {
|
|
59
|
+
return this.obj == null || this.obj.isNew;
|
|
60
|
+
}
|
|
61
|
+
get isSaved() {
|
|
62
|
+
return this.obj?.isSaved || false;
|
|
63
|
+
}
|
|
64
|
+
get __cache_name() {
|
|
65
|
+
return this.constructor["__cache_name"];
|
|
66
|
+
}
|
|
67
|
+
apply_changes() {
|
|
68
|
+
return this.obj?.apply_changes();
|
|
69
|
+
}
|
|
70
|
+
getValue(key, original = false) {
|
|
71
|
+
return this.obj?.getValue(key, original);
|
|
72
|
+
}
|
|
73
|
+
get(key, original = false) {
|
|
74
|
+
return this.obj?.get(key, original);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
4
77
|
* set value
|
|
5
78
|
* @param key
|
|
6
79
|
* @param value
|
|
7
80
|
* @return true if changed , false otherwise
|
|
8
|
-
*/
|
|
81
|
+
*/
|
|
82
|
+
setValue(key, value) {
|
|
83
|
+
return this.obj?.setValue(key, value) || false;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
9
86
|
* set key of subObject
|
|
10
87
|
* @param key
|
|
11
88
|
* @param value
|
|
12
89
|
* @param subvalue
|
|
13
90
|
* @return true if changed , false otherwise
|
|
14
|
-
*/
|
|
15
|
-
|
|
91
|
+
*/
|
|
92
|
+
setSubValue(key, objKey, subvalue) {
|
|
93
|
+
return this.obj?.setSubValue(key, objKey, subvalue) || false;
|
|
94
|
+
}
|
|
95
|
+
//Push value on array
|
|
96
|
+
pushValue(key, value) {
|
|
97
|
+
return this.obj?.pushValue(key, value) || false;
|
|
98
|
+
}
|
|
99
|
+
/* -------------------------------------------------------------------------- */
|
|
100
|
+
getIndexOrID() {
|
|
101
|
+
return this.obj?.getIndexOrID() || -1;
|
|
102
|
+
}
|
|
103
|
+
getIndex() {
|
|
104
|
+
return this.obj?.getIndex() || -1;
|
|
105
|
+
}
|
|
106
|
+
getID() {
|
|
107
|
+
return this.obj?.getID() || "";
|
|
108
|
+
}
|
|
109
|
+
/* -------------------------------------------------------------------------- */
|
|
110
|
+
/* Getters */
|
|
111
|
+
/* -------------------------------------------------------------------------- */
|
|
112
|
+
/* -------------------------------------------------------------------------- */
|
|
113
|
+
/* Events */
|
|
114
|
+
/* -------------------------------------------------------------------------- */
|
|
115
|
+
async event_run(eventName, ...params) {
|
|
116
|
+
if (!this.__sendEvents)
|
|
117
|
+
return;
|
|
118
|
+
await EventManager.run(this, eventName, ...params);
|
|
119
|
+
}
|
|
120
|
+
event_use(name, runEvent, onlyOn = null, runOnce = false) {
|
|
121
|
+
return EventManager.use(this, name, runEvent, onlyOn, runOnce);
|
|
122
|
+
}
|
|
123
|
+
async event_add(name, uname, runEvent, onlyOn = null, runOnce = false) {
|
|
124
|
+
await EventManager.add(this, name, uname, runEvent, onlyOn, runOnce);
|
|
125
|
+
}
|
|
126
|
+
events_stop() {
|
|
127
|
+
this.__sendEvents = false;
|
|
128
|
+
}
|
|
129
|
+
events_clear() {
|
|
130
|
+
ObjectManager.events_clear(this);
|
|
131
|
+
}
|
|
132
|
+
static events_clear(obj) {
|
|
133
|
+
EventManager.removeAll(obj);
|
|
134
|
+
}
|
|
135
|
+
/* -------------------------------------------------------------------------- */
|
|
136
|
+
/* Options */
|
|
137
|
+
/* -------------------------------------------------------------------------- */
|
|
138
|
+
/**
|
|
16
139
|
* Set an option to some value, if its already equal to the value it will be deleted instead
|
|
17
140
|
* @param key
|
|
18
141
|
* @param value
|
|
19
142
|
* @returns
|
|
20
|
-
*/
|
|
143
|
+
*/
|
|
144
|
+
async toggleOption(key, value) {
|
|
145
|
+
var oldval = this.getOption(key);
|
|
146
|
+
if (object_equals_loose(oldval, value)) {
|
|
147
|
+
await this.setOption(key, null);
|
|
148
|
+
}
|
|
149
|
+
else
|
|
150
|
+
await this.setOption(key, value);
|
|
151
|
+
}
|
|
152
|
+
async setOption(key, value) {
|
|
153
|
+
if (this.__options == null)
|
|
154
|
+
this.__options = {};
|
|
155
|
+
if (value == null) {
|
|
156
|
+
delete this.__options[key];
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.__options[key] = value;
|
|
160
|
+
}
|
|
161
|
+
return await this.event_run("on_option_change", key, value);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
21
164
|
* Sets an option then does an action,
|
|
22
165
|
* after the action is run, updates the option again
|
|
23
166
|
* @param key
|
|
24
167
|
* @param value
|
|
25
168
|
* @param nextvalue
|
|
26
169
|
* @param action
|
|
27
|
-
*/
|
|
170
|
+
*/
|
|
171
|
+
async holdOption(key, value, nextvalue, action) {
|
|
172
|
+
try {
|
|
173
|
+
this.setOption(key, value);
|
|
174
|
+
if (typeof action === "function")
|
|
175
|
+
await action();
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
this.setOption(key, nextvalue);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
getOption(key) {
|
|
182
|
+
if (this.__options == null)
|
|
183
|
+
return undefined;
|
|
184
|
+
return this.__options[key];
|
|
185
|
+
}
|
|
186
|
+
/* -------------------------------------------------------------------------- */
|
|
187
|
+
/* Errors */
|
|
188
|
+
/* -------------------------------------------------------------------------- */
|
|
189
|
+
addError(title, message, sendToRegister = "auto") {
|
|
190
|
+
return ObjectManager.addError(this, title, message, sendToRegister);
|
|
191
|
+
}
|
|
192
|
+
getErrors() {
|
|
193
|
+
return ObjectManager.getErrors(this);
|
|
194
|
+
}
|
|
195
|
+
clearErrors() {
|
|
196
|
+
return ObjectManager.clearErrors(this);
|
|
197
|
+
}
|
|
198
|
+
static addError(o, title, message, sendToRegister = "auto") {
|
|
199
|
+
if (o && o.obj) {
|
|
200
|
+
return SimpleObject.addError(o.obj, title, message, sendToRegister);
|
|
201
|
+
}
|
|
202
|
+
const errStr = combine_if_not_blank(title, " : ", message);
|
|
203
|
+
console.error("[REGISTER-ERROR]", errStr);
|
|
204
|
+
gfunc.run_action("add_error", {
|
|
205
|
+
title: title,
|
|
206
|
+
message: message,
|
|
207
|
+
type: "error",
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
static getErrors(o) {
|
|
211
|
+
const obj = o.obj;
|
|
212
|
+
return obj != null ? SimpleObject.getErrors(o.obj) : [];
|
|
213
|
+
}
|
|
214
|
+
static clearErrors(o) {
|
|
215
|
+
return SimpleObject.clearErrors(o.obj);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
export default ObjectManager;
|