@azrico/webobject 1.1.7 → 1.1.10
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 +3 -2
- package/dist/src/WebObjects/WebObject.js +136 -4
- 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.d.ts +2 -1
- package/dist/src/structs/SimpleObject.js +472 -12
- package/package.json +1 -1
|
@@ -1,27 +1,487 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { gcache, gfunc } from "@azrico/global";
|
|
2
|
+
import { object_clone, object_equals, object_isEmpty, object_isTrue, object_removeKeys, prop_rn, } from "@azrico/object";
|
|
3
|
+
import { MapableObject } from "./MapableObject";
|
|
4
|
+
import ObjectManager from "./ObjectManager";
|
|
5
|
+
import { combine_if_not_blank } from "@azrico/string";
|
|
6
|
+
export class SimpleObject extends MapableObject {
|
|
7
|
+
static __cache_name;
|
|
8
|
+
static __unchangeables;
|
|
9
|
+
static __changeables;
|
|
10
|
+
__originalobject;
|
|
11
|
+
//changes
|
|
12
|
+
__delta;
|
|
13
|
+
__changed = false;
|
|
14
|
+
__lastChanged = "";
|
|
15
|
+
__is_readonly;
|
|
16
|
+
__is_edit_mode;
|
|
17
|
+
//errors
|
|
18
|
+
__errors;
|
|
19
|
+
__showErrors;
|
|
20
|
+
//save results
|
|
21
|
+
__saveResultObject;
|
|
22
|
+
__saveResultIsSuccess;
|
|
23
|
+
__saveResultType;
|
|
24
|
+
//sequence key
|
|
25
|
+
_index_key;
|
|
26
|
+
//sequence value , used to index the object with increasing numbers
|
|
27
|
+
_index_value;
|
|
28
|
+
constructor(isEditMode = true, is_readonly = false) {
|
|
29
|
+
super();
|
|
30
|
+
this.__delta = {};
|
|
31
|
+
this.__is_edit_mode = isEditMode;
|
|
32
|
+
this.__is_readonly = is_readonly || !this.__is_edit_mode;
|
|
33
|
+
this.clear_changes();
|
|
34
|
+
}
|
|
35
|
+
/* -------------------------------------------------------------------------- */
|
|
36
|
+
/* Load */
|
|
37
|
+
/* -------------------------------------------------------------------------- */
|
|
38
|
+
loadValues(initObject, fullUpdate = false) {
|
|
39
|
+
SimpleObject.loadValues(this, initObject, fullUpdate);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
static loadValues(obj, initObject, fullUpdate = false) {
|
|
43
|
+
SimpleObject.factoryClear(obj);
|
|
44
|
+
if (initObject == null) {
|
|
45
|
+
for (const key in obj) {
|
|
46
|
+
if (key.startsWith("__"))
|
|
47
|
+
continue;
|
|
48
|
+
delete obj[key];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else if (typeof initObject == "object") {
|
|
52
|
+
for (const key in initObject) {
|
|
53
|
+
if (key.startsWith("__"))
|
|
54
|
+
continue;
|
|
55
|
+
if (fullUpdate ||
|
|
56
|
+
Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
57
|
+
obj[key] = initObject[key];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
/* -------------------------------------------------------------------------- */
|
|
64
|
+
static getValue(o, key, original = false) {
|
|
65
|
+
if (o instanceof SimpleObject && typeof o.getValue === "function")
|
|
66
|
+
return o.getValue(key, original);
|
|
67
|
+
if (!original && o.__delta[key])
|
|
68
|
+
return o.__delta[key];
|
|
69
|
+
return o[key];
|
|
70
|
+
}
|
|
71
|
+
getValue(key, original = false) {
|
|
72
|
+
if (!original && this.__delta[key])
|
|
73
|
+
return this.__delta[key];
|
|
74
|
+
return this[key];
|
|
75
|
+
}
|
|
76
|
+
get(key, original = false) {
|
|
77
|
+
return this.getValue(key, original);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
7
80
|
* set value
|
|
8
81
|
* @param key
|
|
9
82
|
* @param value
|
|
10
83
|
* @return true if changed , false otherwise
|
|
11
|
-
*/
|
|
84
|
+
*/
|
|
85
|
+
setValue(key, value) {
|
|
86
|
+
if (!this.canModify)
|
|
87
|
+
throw Error("Cant use setValue when on readonly mode");
|
|
88
|
+
let isValueChanged = false;
|
|
89
|
+
try {
|
|
90
|
+
const oldval = this.getValue(key);
|
|
91
|
+
if (!object_equals(oldval, value, { ignoredStartingWords: "_" })) {
|
|
92
|
+
this.__delta[key] = value;
|
|
93
|
+
isValueChanged = true;
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
isValueChanged = false;
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
if (isValueChanged) {
|
|
100
|
+
this.notifyChanged("", key, value);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return isValueChanged;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
12
106
|
* set key of subObject
|
|
13
107
|
* @param key
|
|
14
108
|
* @param value
|
|
15
109
|
* @param subvalue
|
|
16
110
|
* @return true if changed , false otherwise
|
|
17
|
-
*/
|
|
18
|
-
|
|
111
|
+
*/
|
|
112
|
+
setSubValue(key, objKey, subvalue) {
|
|
113
|
+
if (!this.canModify)
|
|
114
|
+
throw Error("Cant use setValue when on readonly mode");
|
|
115
|
+
let isValueChanged = false;
|
|
116
|
+
try {
|
|
117
|
+
//if accessing subval of an object, make sure the object is not null
|
|
118
|
+
if (this.__delta[key] == null ||
|
|
119
|
+
typeof this.__delta[key] != "object")
|
|
120
|
+
this.__delta[key] = {};
|
|
121
|
+
const subval = this.__delta[key][objKey];
|
|
122
|
+
if (!object_equals(subval, subvalue, {
|
|
123
|
+
ignoredStartingWords: "_",
|
|
124
|
+
})) {
|
|
125
|
+
this.__delta[key][objKey] = subvalue;
|
|
126
|
+
isValueChanged = true;
|
|
127
|
+
}
|
|
128
|
+
else
|
|
129
|
+
isValueChanged = false;
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
if (isValueChanged) {
|
|
133
|
+
this.notifyChanged("", key, objKey, subvalue);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return isValueChanged;
|
|
137
|
+
}
|
|
138
|
+
//Push value on array
|
|
139
|
+
pushValue(key, value) {
|
|
140
|
+
if (value == null)
|
|
141
|
+
return null;
|
|
142
|
+
if (!this.canModify)
|
|
143
|
+
throw Error("Cant use pushValue when on readonly mode");
|
|
144
|
+
if (this.__delta[key] == null) {
|
|
145
|
+
this.__delta[key] = this.getValue(key);
|
|
146
|
+
}
|
|
147
|
+
if (!Array.isArray(this.__delta[key]))
|
|
148
|
+
throw Error("Cant use pushValue on non Array keys");
|
|
149
|
+
let tempArray = this.__delta[key];
|
|
150
|
+
tempArray.push(value);
|
|
151
|
+
this.__delta[key] = tempArray;
|
|
152
|
+
//
|
|
153
|
+
this.notifyChanged("", key, value);
|
|
154
|
+
return tempArray;
|
|
155
|
+
}
|
|
156
|
+
async notifyChanged(type, key, value, subvalue) {
|
|
157
|
+
if (value === undefined)
|
|
158
|
+
value = this.getValue(key);
|
|
159
|
+
this.__changed = true;
|
|
160
|
+
let lc = "";
|
|
161
|
+
if (type)
|
|
162
|
+
lc += `[${type}] `;
|
|
163
|
+
if (subvalue) {
|
|
164
|
+
lc += `${key}->${value} = ${subvalue}`;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
lc += `${key} = ${value}`;
|
|
168
|
+
}
|
|
169
|
+
this.__lastChanged = lc;
|
|
170
|
+
/* --------------------------- notify the manager --------------------------- */
|
|
171
|
+
const manager_class = this.get_managerClass();
|
|
172
|
+
if (manager_class instanceof ObjectManager) {
|
|
173
|
+
await manager_class.notifyChanged(type, key, value, subvalue);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
19
177
|
* Rename object property, mostly used to fix init_object to match legacy versions
|
|
20
178
|
* @param o
|
|
21
179
|
* @param f
|
|
22
180
|
* @param t
|
|
23
181
|
* @returns
|
|
24
|
-
*/
|
|
182
|
+
*/
|
|
183
|
+
rn(o, f, t) {
|
|
184
|
+
if (typeof o == "object") {
|
|
185
|
+
if (typeof f === "string" && typeof t === "string") {
|
|
186
|
+
return prop_rn(o, f, t);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (typeof o === "string" && typeof f === "string") {
|
|
190
|
+
return prop_rn(this, o, f);
|
|
191
|
+
}
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
static canChangeKey(object, key) {
|
|
195
|
+
if (!object || !key)
|
|
196
|
+
return false;
|
|
197
|
+
const __changeables = object.constructor["__changeables"];
|
|
198
|
+
const __unchangeables = object.constructor["__unchangeables"];
|
|
199
|
+
if (__changeables != null && !__changeables.includes(key))
|
|
200
|
+
return false;
|
|
201
|
+
if (__unchangeables != null && __unchangeables.includes(key))
|
|
202
|
+
return false;
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
/* -------------------------------------------------------------------------- */
|
|
206
|
+
/* Changes */
|
|
207
|
+
/* -------------------------------------------------------------------------- */
|
|
208
|
+
/**
|
|
25
209
|
* Apply the changes and empty the delta object
|
|
26
|
-
*/
|
|
27
|
-
|
|
210
|
+
*/
|
|
211
|
+
apply_changes() {
|
|
212
|
+
if (!this.isChanged) {
|
|
213
|
+
console.warn("No changes to apply");
|
|
214
|
+
}
|
|
215
|
+
for (const key in this.__delta) {
|
|
216
|
+
const nv = this.__delta[key];
|
|
217
|
+
this[key] = nv;
|
|
218
|
+
}
|
|
219
|
+
this.clear_changes();
|
|
220
|
+
}
|
|
221
|
+
clear_changes() {
|
|
222
|
+
this.__delta = {};
|
|
223
|
+
this.__changed = false;
|
|
224
|
+
}
|
|
225
|
+
static setReadonly(objs, readonlyval = true) {
|
|
226
|
+
let checkObjs = [];
|
|
227
|
+
if (!objs)
|
|
228
|
+
objs = [];
|
|
229
|
+
if (!Array.isArray(objs))
|
|
230
|
+
checkObjs = [objs];
|
|
231
|
+
else
|
|
232
|
+
checkObjs = objs;
|
|
233
|
+
for (let index = 0; index < checkObjs.length; index++) {
|
|
234
|
+
const element = checkObjs[index];
|
|
235
|
+
element.__is_readonly = readonlyval;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/* -------------------------------------------------------------------------- */
|
|
239
|
+
/* Get/Check */
|
|
240
|
+
/* -------------------------------------------------------------------------- */
|
|
241
|
+
get isChanged() {
|
|
242
|
+
return !object_isEmpty(this.__delta);
|
|
243
|
+
}
|
|
244
|
+
get isDeleted() {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
get d() {
|
|
248
|
+
return this.__delta;
|
|
249
|
+
}
|
|
250
|
+
/* all updated properties */
|
|
251
|
+
get allproperties() {
|
|
252
|
+
const result = { ...this, ...this.__delta };
|
|
253
|
+
result.__delta = {};
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
get canModify() {
|
|
257
|
+
return !this.__is_readonly && this.isEditMode;
|
|
258
|
+
}
|
|
259
|
+
get isEditMode() {
|
|
260
|
+
return this.__is_edit_mode !== false;
|
|
261
|
+
}
|
|
262
|
+
get __cache_name() {
|
|
263
|
+
return this.constructor["__cache_name"];
|
|
264
|
+
}
|
|
265
|
+
get __changeables() {
|
|
266
|
+
return this.constructor["__changeables"];
|
|
267
|
+
}
|
|
268
|
+
get __unchangeables() {
|
|
269
|
+
return this.constructor["__unchangeables"];
|
|
270
|
+
}
|
|
271
|
+
get isSaved() {
|
|
272
|
+
return Boolean(this.__saveResultIsSuccess ?? false);
|
|
273
|
+
}
|
|
274
|
+
get isNew() {
|
|
275
|
+
const mid = this.getID();
|
|
276
|
+
if (!mid)
|
|
277
|
+
return true;
|
|
278
|
+
if (typeof mid === "string")
|
|
279
|
+
return mid === "" || mid === "-1";
|
|
280
|
+
if (typeof mid === "number")
|
|
281
|
+
return mid === -1;
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
get manager() {
|
|
285
|
+
return this.get_managerClass();
|
|
286
|
+
}
|
|
287
|
+
get_lastChanged() {
|
|
288
|
+
return !this.__lastChanged;
|
|
289
|
+
}
|
|
290
|
+
get_managerClass = () => null;
|
|
291
|
+
static isDeleted(obj) {
|
|
292
|
+
if (obj instanceof SimpleObject)
|
|
293
|
+
return obj.isDeleted === true;
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
getIndexOrID() {
|
|
297
|
+
return SimpleObject.getIndexOrID(this);
|
|
298
|
+
}
|
|
299
|
+
getIndex() {
|
|
300
|
+
return SimpleObject.getIndex(this);
|
|
301
|
+
}
|
|
302
|
+
getID() {
|
|
303
|
+
return SimpleObject.getID(this);
|
|
304
|
+
}
|
|
305
|
+
static getIndexOrID(o) {
|
|
306
|
+
let res = SimpleObject.getIndex(o);
|
|
307
|
+
if (!res || res == -1)
|
|
308
|
+
res = SimpleObject.getID(o);
|
|
309
|
+
return res || -1;
|
|
310
|
+
}
|
|
311
|
+
static getIndex(o) {
|
|
312
|
+
if (o == null || o == undefined)
|
|
313
|
+
return -1;
|
|
314
|
+
switch (typeof o) {
|
|
315
|
+
case "object":
|
|
316
|
+
return o._index_value || -1;
|
|
317
|
+
case "number":
|
|
318
|
+
case "string":
|
|
319
|
+
return Number(o);
|
|
320
|
+
}
|
|
321
|
+
return -1;
|
|
322
|
+
}
|
|
323
|
+
static getID(o) {
|
|
324
|
+
if (o == null || o == undefined)
|
|
325
|
+
return "";
|
|
326
|
+
switch (typeof o) {
|
|
327
|
+
case "object":
|
|
328
|
+
return o.__data_id || o._id || o.id || o.ID || "";
|
|
329
|
+
case "string":
|
|
330
|
+
case "number":
|
|
331
|
+
return String(o);
|
|
332
|
+
}
|
|
333
|
+
return "";
|
|
334
|
+
}
|
|
335
|
+
/* -------------------------------------------------------------------------- */
|
|
336
|
+
/* Original */
|
|
337
|
+
/* -------------------------------------------------------------------------- */
|
|
338
|
+
getOriginal() {
|
|
339
|
+
return this.__originalobject || undefined;
|
|
340
|
+
}
|
|
341
|
+
setOriginal(toObj = 0) {
|
|
342
|
+
if (toObj === null) {
|
|
343
|
+
delete this.__originalobject;
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if (!this.isEditMode) {
|
|
347
|
+
console.warn("Cant use setOriginal on non original objects ! this will create an infinite loop");
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (this.__is_readonly) {
|
|
351
|
+
delete this.__originalobject;
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (toObj === 0) {
|
|
355
|
+
toObj = this;
|
|
356
|
+
}
|
|
357
|
+
const cloneObject = object_clone(toObj);
|
|
358
|
+
//this.__originalobject = cloneObject, ["__"];
|
|
359
|
+
this.__originalobject = object_removeKeys(cloneObject, ["__"]);
|
|
360
|
+
return this;
|
|
361
|
+
}
|
|
362
|
+
static setOriginal(obj, toObj = 0) {
|
|
363
|
+
if (!(obj instanceof SimpleObject))
|
|
364
|
+
return undefined;
|
|
365
|
+
return obj.setOriginal(toObj);
|
|
366
|
+
}
|
|
367
|
+
static getOriginal(obj) {
|
|
368
|
+
if (obj instanceof SimpleObject)
|
|
369
|
+
return obj.getOriginal();
|
|
370
|
+
return obj?.__originalobject;
|
|
371
|
+
}
|
|
372
|
+
/* -------------------------------------------------------------------------- */
|
|
373
|
+
/* Errors */
|
|
374
|
+
/* -------------------------------------------------------------------------- */
|
|
375
|
+
static addError(o, title, message, sendToRegister = "auto") {
|
|
376
|
+
const errStr = combine_if_not_blank(title, " : ", message);
|
|
377
|
+
if (o) {
|
|
378
|
+
if (!Array.isArray(o.__errors))
|
|
379
|
+
o.__errors = [];
|
|
380
|
+
o.__errors.push(errStr);
|
|
381
|
+
if (o.__showErrors === false)
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
else if (sendToRegister === "auto")
|
|
385
|
+
sendToRegister = true;
|
|
386
|
+
if (sendToRegister === true) {
|
|
387
|
+
console.error("[REGISTER-ERROR]", errStr);
|
|
388
|
+
gfunc.run_action("add_error", {
|
|
389
|
+
title: title,
|
|
390
|
+
message: message,
|
|
391
|
+
type: "error",
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
console.error("[ERROR]", errStr);
|
|
396
|
+
}
|
|
397
|
+
return o?.__errors || [];
|
|
398
|
+
}
|
|
399
|
+
static getErrors(o) {
|
|
400
|
+
return o?.__errors || [];
|
|
401
|
+
}
|
|
402
|
+
static clearErrors(o) {
|
|
403
|
+
if (o)
|
|
404
|
+
o.__errors = [];
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
/* -------------------------------------------------------------------------- */
|
|
408
|
+
/* Save Results */
|
|
409
|
+
/* -------------------------------------------------------------------------- */
|
|
410
|
+
static isResultValid(result_object) {
|
|
411
|
+
if (object_isEmpty(result_object))
|
|
412
|
+
return false;
|
|
413
|
+
switch (typeof result_object) {
|
|
414
|
+
case "string":
|
|
415
|
+
try {
|
|
416
|
+
result_object = JSON.parse(result_object);
|
|
417
|
+
return this.isResultValid(result_object);
|
|
418
|
+
}
|
|
419
|
+
catch {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
case "object":
|
|
423
|
+
if (object_isTrue(result_object.acknowledged)) {
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
if (result_object.expected && result_object.result) {
|
|
427
|
+
return object_equals(result_object.expected, result_object.result);
|
|
428
|
+
}
|
|
429
|
+
return Number(this.getID(result_object)) >= 0;
|
|
430
|
+
case "number":
|
|
431
|
+
return result_object > 0;
|
|
432
|
+
case "boolean":
|
|
433
|
+
return result_object === true;
|
|
434
|
+
}
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
setSaveResult(toObj, type) {
|
|
438
|
+
this.__saveResultType = type;
|
|
439
|
+
if (toObj === true) {
|
|
440
|
+
}
|
|
441
|
+
else if (toObj === false) {
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
if (toObj) {
|
|
445
|
+
this.setSaveResultObject(toObj);
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
this.setSaveResultObject(null);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
this.__saveResultIsSuccess =
|
|
452
|
+
(type === "normal" || type === "nothing") && toObj != null;
|
|
453
|
+
}
|
|
454
|
+
setSaveResultObject(toObj) {
|
|
455
|
+
this.__saveResultObject = toObj;
|
|
456
|
+
}
|
|
457
|
+
getSaveResult() {
|
|
458
|
+
if (this.isSaved)
|
|
459
|
+
return this.__saveResultObject;
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
/* -------------------------------------------------------------------------- */
|
|
463
|
+
/* statics */
|
|
464
|
+
/* -------------------------------------------------------------------------- */
|
|
465
|
+
static factoryClear(obj) {
|
|
466
|
+
obj.clear_changes();
|
|
467
|
+
SimpleObject.clearErrors(obj);
|
|
468
|
+
}
|
|
469
|
+
static delete_cache() {
|
|
470
|
+
if (!this.__cache_name)
|
|
471
|
+
return;
|
|
472
|
+
gcache.delete_cache(this.__cache_name);
|
|
473
|
+
}
|
|
474
|
+
static get_canModify(o) {
|
|
475
|
+
if (o instanceof SimpleObject)
|
|
476
|
+
return o.canModify;
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
479
|
+
/* -------------------------------------------------------------------------- */
|
|
480
|
+
static equals(a, b) {
|
|
481
|
+
console.warn("Dont use the default equals method !");
|
|
482
|
+
if (a == undefined || b == undefined)
|
|
483
|
+
return false;
|
|
484
|
+
return a === b;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
export default SimpleObject;
|