@azrico/webobject 1.5.58 → 1.5.61
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/README.md +9 -9
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/src/WebObjects/ParentWebObject.d.ts +24 -0
- package/dist/src/WebObjects/ParentWebObject.js +77 -0
- package/dist/src/WebObjects/WebObject.d.ts +153 -0
- package/dist/src/WebObjects/WebObject.js +654 -0
- package/dist/src/WebObjects/WebObjectHelper.d.ts +9 -0
- package/dist/src/WebObjects/WebObjectHelper.js +18 -0
- package/dist/src/WebObjects/WebObjectHelper.test.d.ts +1 -0
- package/dist/src/WebObjects/WebObjectHelper.test.js +45 -0
- package/dist/src/WebObjects/WebObjectManager.d.ts +68 -0
- package/dist/src/WebObjects/WebObjectManager.js +290 -0
- package/dist/src/WebObjects/WebObjectPathHelper.d.ts +36 -0
- package/dist/src/WebObjects/WebObjectPathHelper.js +84 -0
- package/dist/src/WebObjects/WebObjectPathHelper.test.d.ts +1 -0
- package/dist/src/WebObjects/WebObjectPathHelper.test.js +31 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/index.js +10 -0
- package/dist/src/structs/DummyObject.d.ts +5 -0
- package/dist/src/structs/DummyObject.js +8 -0
- package/dist/src/structs/MapableObject.d.ts +43 -0
- package/dist/src/structs/MapableObject.js +77 -0
- package/dist/src/structs/MetaObject.d.ts +15 -0
- package/dist/src/structs/MetaObject.js +35 -0
- package/dist/src/structs/ObjectArray.d.ts +10 -0
- package/dist/src/structs/ObjectArray.js +43 -0
- package/dist/src/structs/ObjectManager.d.ts +136 -0
- package/dist/src/structs/ObjectManager.js +332 -0
- package/dist/src/structs/ObjectWithTemp.d.ts +16 -0
- package/dist/src/structs/ObjectWithTemp.js +35 -0
- package/dist/src/structs/SimpleObject.js +1138 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1138 @@
|
|
|
1
|
+
import { array_intersection, array_isEmpty, array_merge, fn_eval, fn_eval_this, object_clone, object_equals, object_get, object_getStatic, object_isClass, object_isEmpty, object_isTrue, object_removeKeys, object_set, wrap_array, } from "@azrico/object";
|
|
2
|
+
import { combine_if_not_blank, string_isEmpty, string_to_color, } from "@azrico/string";
|
|
3
|
+
import { gcache, gfunc } from "@azrico/global";
|
|
4
|
+
import { MapableObject } from "./MapableObject";
|
|
5
|
+
import ObjectManager from "./ObjectManager";
|
|
6
|
+
import EventManager from "@azrico/event";
|
|
7
|
+
export class SimpleObject extends MapableObject {
|
|
8
|
+
static __db_name = "";
|
|
9
|
+
static __object_name = "";
|
|
10
|
+
//these properties should not be changed and are usually set by server
|
|
11
|
+
static __unchangeables = [];
|
|
12
|
+
//these properties can be directly changed and are included in deltaObject
|
|
13
|
+
static __changeables = [];
|
|
14
|
+
//these properties usually are not directly changed and sometimes are included in deltaObject
|
|
15
|
+
static __extraProperties = [];
|
|
16
|
+
//these properties are never sent to database but may be sent to client
|
|
17
|
+
static __tempProperties = [];
|
|
18
|
+
//these properties can only be changed by the _creator
|
|
19
|
+
static __protectedProperties = [];
|
|
20
|
+
//these properties will be evaluated when needed
|
|
21
|
+
static __functionProperties = [];
|
|
22
|
+
//apply these string formats
|
|
23
|
+
static __formatList;
|
|
24
|
+
//types for children can be used when importing children
|
|
25
|
+
static __childTypes;
|
|
26
|
+
//these fields are used when importing
|
|
27
|
+
static __importColumns;
|
|
28
|
+
//these fields can be used when importing and checking if an item exists
|
|
29
|
+
static __checkProperties = [];
|
|
30
|
+
static __use_delta = true;
|
|
31
|
+
_static() {
|
|
32
|
+
return this.constructor;
|
|
33
|
+
}
|
|
34
|
+
//an exact copy of this object is stored here
|
|
35
|
+
__originalobject;
|
|
36
|
+
//changes are saved here
|
|
37
|
+
__delta = {};
|
|
38
|
+
//// protected __has_delta: boolean = false;
|
|
39
|
+
//the last thing that was changed on this object
|
|
40
|
+
__lastChanged;
|
|
41
|
+
//is the object read only
|
|
42
|
+
__is_readonly;
|
|
43
|
+
//should we throw errors when encountering an error?
|
|
44
|
+
//is TRUE by default
|
|
45
|
+
__throw_errors;
|
|
46
|
+
//is this object marked as deleted?
|
|
47
|
+
_deleted;
|
|
48
|
+
_archived;
|
|
49
|
+
_deletedBy;
|
|
50
|
+
//is this object locked?
|
|
51
|
+
_locked;
|
|
52
|
+
//errors
|
|
53
|
+
__errors;
|
|
54
|
+
__showErrors;
|
|
55
|
+
//refresh func
|
|
56
|
+
__refresh_functions;
|
|
57
|
+
//save results
|
|
58
|
+
__saveResultObject;
|
|
59
|
+
__saveResultIsSuccess = true;
|
|
60
|
+
__saveResultType;
|
|
61
|
+
//init function
|
|
62
|
+
__initialized;
|
|
63
|
+
#objectColor;
|
|
64
|
+
//id in the database
|
|
65
|
+
_id = "";
|
|
66
|
+
//sequence key (usually managed by the database)
|
|
67
|
+
_index_key = "";
|
|
68
|
+
//sequence value , used to index the object with increasing numbers
|
|
69
|
+
_index_value = -1;
|
|
70
|
+
constructor(is_readonly = false) {
|
|
71
|
+
super();
|
|
72
|
+
this.__is_readonly = is_readonly;
|
|
73
|
+
if (this._id)
|
|
74
|
+
this.__table_id = this._id;
|
|
75
|
+
this.clear_changes();
|
|
76
|
+
}
|
|
77
|
+
init(...p) {
|
|
78
|
+
this.__initialized = true;
|
|
79
|
+
}
|
|
80
|
+
get(key, original = false) {
|
|
81
|
+
//check if we should treat the prop as a function
|
|
82
|
+
const is_fnc = this.constructor["__functionProperties"] != null &&
|
|
83
|
+
this.constructor["__functionProperties"].includes(key);
|
|
84
|
+
//check if we should use delta version or normal version for this property
|
|
85
|
+
let used_value;
|
|
86
|
+
if (typeof key === "string" && key.includes(".")) {
|
|
87
|
+
/* ----------------------------- using sub value ---------------------------- */
|
|
88
|
+
const keysp = key.split(".");
|
|
89
|
+
const key_part = keysp.shift();
|
|
90
|
+
const subkey_part = keysp.shift();
|
|
91
|
+
used_value = this.#getSubProperty(key_part, subkey_part);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
/* --------------------------- using this or delta -------------------------- */
|
|
95
|
+
const self_key = key;
|
|
96
|
+
const use_orig = original || !this.__use_delta || !(self_key in this.__delta);
|
|
97
|
+
used_value = (use_orig ? this[self_key] : this.__delta[self_key]);
|
|
98
|
+
}
|
|
99
|
+
return is_fnc ? fn_eval_this(this, used_value) : used_value;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* static get method for SimpleObject
|
|
103
|
+
* @param obj
|
|
104
|
+
* @param key
|
|
105
|
+
* @param original
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
108
|
+
static get(obj, key, original = false) {
|
|
109
|
+
if (obj == null)
|
|
110
|
+
return undefined;
|
|
111
|
+
if (obj instanceof SimpleObject && typeof obj.get === "function")
|
|
112
|
+
return obj.get(key, original);
|
|
113
|
+
/* ------------------------------- manual get ------------------------------- */
|
|
114
|
+
if (!original &&
|
|
115
|
+
obj.__use_delta != false &&
|
|
116
|
+
typeof obj.__delta === "object" &&
|
|
117
|
+
key in obj.__delta) {
|
|
118
|
+
//@ts-ignore
|
|
119
|
+
return obj.__delta[key];
|
|
120
|
+
}
|
|
121
|
+
return obj[key];
|
|
122
|
+
}
|
|
123
|
+
useProperty(key) {
|
|
124
|
+
if (this.manager == null) {
|
|
125
|
+
console.warn("Objects with no manager should not use useProperty");
|
|
126
|
+
return this.get(key);
|
|
127
|
+
}
|
|
128
|
+
return EventManager.useProperty(this.manager, key);
|
|
129
|
+
}
|
|
130
|
+
#getSubProperty(key, subkey, original = false) {
|
|
131
|
+
let subobj = this.get(key, original);
|
|
132
|
+
if (subobj == null || typeof subobj !== "object")
|
|
133
|
+
return undefined;
|
|
134
|
+
return subobj[subkey];
|
|
135
|
+
}
|
|
136
|
+
getSubValue = this.#getSubProperty;
|
|
137
|
+
/* -------------------------------------------------------------------------- */
|
|
138
|
+
/* set */
|
|
139
|
+
/* -------------------------------------------------------------------------- */
|
|
140
|
+
/**
|
|
141
|
+
* set value of a key only if the value is empty (null,undef,"")
|
|
142
|
+
* @param key
|
|
143
|
+
* @param value
|
|
144
|
+
* @returns
|
|
145
|
+
*/
|
|
146
|
+
setDefaultValue(key, value) {
|
|
147
|
+
//@ts-ignore
|
|
148
|
+
if (this[key] == undefined || this[key] === "" || this[key] === 0) {
|
|
149
|
+
this[key] = value;
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
setDefault = this.setDefaultValue;
|
|
155
|
+
setDefaultPropertyNow(key) {
|
|
156
|
+
//@ts-ignore
|
|
157
|
+
if (this[key] == undefined || this[key] === "" || this[key] === 0) {
|
|
158
|
+
this[key] = new Date();
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
#setSubProperty(key, subkey, value) {
|
|
164
|
+
let subobj = this.get(key);
|
|
165
|
+
//@ts-ignore
|
|
166
|
+
if (subobj == null || typeof subobj !== "object")
|
|
167
|
+
subobj = {};
|
|
168
|
+
else
|
|
169
|
+
subobj = { ...subobj }; //make a clone so we dont update the original
|
|
170
|
+
subobj[subkey] = value;
|
|
171
|
+
this.#setProperty(key, subobj);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* update a property on this object by updating the `__delta`
|
|
175
|
+
* or update the object property directly depending on `__use_delta` setting
|
|
176
|
+
*
|
|
177
|
+
* @param key
|
|
178
|
+
* @param value
|
|
179
|
+
*/
|
|
180
|
+
#setProperty(key, value) {
|
|
181
|
+
let checkIsChanged = false;
|
|
182
|
+
if (this.__use_delta) {
|
|
183
|
+
if (this[key] === value) {
|
|
184
|
+
/**
|
|
185
|
+
* we set something back to its original value
|
|
186
|
+
* no need to save it as delta
|
|
187
|
+
*/
|
|
188
|
+
delete this.__delta[key];
|
|
189
|
+
////checkIsChanged = true; //check if we should set __has_delta to false
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
this.__delta[key] = value; //always update delta to keep track of changes
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
this[key] = value; //on noDelta mode update the object itself
|
|
197
|
+
this.__delta[key] = value; //always update delta to keep track of changes
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* set value and run the event handlers if needed
|
|
203
|
+
* @param key
|
|
204
|
+
* @param value
|
|
205
|
+
* @param shouldNotify
|
|
206
|
+
* @param autoApply if the field is not in delta, instantly calls the `apply` function to remove from delta
|
|
207
|
+
* @returns
|
|
208
|
+
*/
|
|
209
|
+
async set(key, value, shouldNotify = "auto", autoApply = false) {
|
|
210
|
+
/* -------------------- check if modification is allowed -------------------- */
|
|
211
|
+
if (!this.canModify) {
|
|
212
|
+
if (this.__throw_errors !== false)
|
|
213
|
+
throw Error(`Cant use set(${String(key)}) when on readonly mode`);
|
|
214
|
+
else
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
let isValueChanged, mkey, keyExists = false;
|
|
218
|
+
/* -------------------------------------------------------------------------- */
|
|
219
|
+
/* set all values from object */
|
|
220
|
+
/* -------------------------------------------------------------------------- */
|
|
221
|
+
if (typeof key === "object") {
|
|
222
|
+
let finalResult = true;
|
|
223
|
+
let changeablesOnly = Boolean(value);
|
|
224
|
+
value = key;
|
|
225
|
+
const checkKeys = changeablesOnly
|
|
226
|
+
? array_intersection(this.__changeables, Object.keys(value))
|
|
227
|
+
: Object.keys(value);
|
|
228
|
+
for (let index = 0; index < checkKeys.length; index++) {
|
|
229
|
+
const key = checkKeys[index];
|
|
230
|
+
if (false ==
|
|
231
|
+
(await this.set(key, value[key], shouldNotify, autoApply)))
|
|
232
|
+
finalResult = false;
|
|
233
|
+
}
|
|
234
|
+
return finalResult;
|
|
235
|
+
}
|
|
236
|
+
if (typeof key === "string" && key.includes(".")) {
|
|
237
|
+
/* -------------------------------------------------------------------------- */
|
|
238
|
+
/* set value of object */
|
|
239
|
+
/* -------------------------------------------------------------------------- */
|
|
240
|
+
const keysp = key.split(".");
|
|
241
|
+
mkey = keysp.shift();
|
|
242
|
+
keyExists = mkey in this.__delta;
|
|
243
|
+
const subkey_part = keysp.shift();
|
|
244
|
+
//if accessing subval of an object, make sure the object is not null
|
|
245
|
+
const subval = this.#getSubProperty(mkey, subkey_part);
|
|
246
|
+
isValueChanged = !object_equals(subval, value, {
|
|
247
|
+
ignoredStartingWords: "_",
|
|
248
|
+
strictArrayCheck: true,
|
|
249
|
+
});
|
|
250
|
+
if (isValueChanged)
|
|
251
|
+
this.#setSubProperty(mkey, subkey_part, value);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
/* -------------------------------------------------------------------------- */
|
|
255
|
+
/* set basic value */
|
|
256
|
+
/* -------------------------------------------------------------------------- */
|
|
257
|
+
mkey = key;
|
|
258
|
+
keyExists = mkey in this.__delta;
|
|
259
|
+
const old_value = this.get(mkey);
|
|
260
|
+
isValueChanged = !object_equals(old_value, value, {
|
|
261
|
+
strictArrayCheck: true,
|
|
262
|
+
ignoredStartingWords: "_",
|
|
263
|
+
});
|
|
264
|
+
if (isValueChanged)
|
|
265
|
+
this.#setProperty(mkey, value);
|
|
266
|
+
}
|
|
267
|
+
if (autoApply && keyExists === false) {
|
|
268
|
+
/**
|
|
269
|
+
* if the field is not in delta
|
|
270
|
+
* instantly calls the `apply` function to remove from delta
|
|
271
|
+
*/
|
|
272
|
+
this.apply(mkey);
|
|
273
|
+
if (shouldNotify === "auto")
|
|
274
|
+
shouldNotify = false;
|
|
275
|
+
}
|
|
276
|
+
if (isValueChanged) {
|
|
277
|
+
/**
|
|
278
|
+
* notify the manager when the state of `__has_delta` is changed
|
|
279
|
+
* managers can use this to detect if they need to save
|
|
280
|
+
*/
|
|
281
|
+
this.manager?.checkIsChanged();
|
|
282
|
+
}
|
|
283
|
+
/* --------------------------------- notify --------------------------------- */
|
|
284
|
+
if (shouldNotify === true ||
|
|
285
|
+
(shouldNotify === "auto" && isValueChanged)) {
|
|
286
|
+
await this.notifyChanged("", key, value);
|
|
287
|
+
}
|
|
288
|
+
return isValueChanged;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* @deprecated use `set`
|
|
292
|
+
* set value and run the event handlers if needed
|
|
293
|
+
* @return true if changed , false otherwise
|
|
294
|
+
*/
|
|
295
|
+
setValue = this.set;
|
|
296
|
+
/**
|
|
297
|
+
* @deprecated use set function
|
|
298
|
+
*/
|
|
299
|
+
setAll = this.set;
|
|
300
|
+
/**
|
|
301
|
+
* set value and run the event handlers if needed
|
|
302
|
+
*
|
|
303
|
+
* if the field is not in delta instantly calls the `apply` function to remove from delta
|
|
304
|
+
* @param key
|
|
305
|
+
* @param value
|
|
306
|
+
*/
|
|
307
|
+
async setPreferApply(key, value, shouldNotify = "auto") {
|
|
308
|
+
return await this.set(key, value, shouldNotify, true);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* set key of subObject
|
|
312
|
+
* @param key
|
|
313
|
+
* @param value
|
|
314
|
+
* @param subvalue
|
|
315
|
+
* @param sendChanges
|
|
316
|
+
* @return true if changed , false otherwise
|
|
317
|
+
*/
|
|
318
|
+
async setSubValue(key, objKey, subvalue, sendChanges = "auto") {
|
|
319
|
+
return await this.set(`${String(key)}.${objKey}`, subvalue, sendChanges);
|
|
320
|
+
}
|
|
321
|
+
async setTemp(key, value, sendChanges = false) {
|
|
322
|
+
let isValueChanged = await super.setTemp(key, value);
|
|
323
|
+
if (sendChanges === true ||
|
|
324
|
+
(sendChanges === "auto" && isValueChanged)) {
|
|
325
|
+
await this.notifyChanged("temp", key, value);
|
|
326
|
+
}
|
|
327
|
+
return isValueChanged;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Push a single value to array property
|
|
331
|
+
* @param key
|
|
332
|
+
* @param value
|
|
333
|
+
* @param sendChanges
|
|
334
|
+
* @returns
|
|
335
|
+
*/
|
|
336
|
+
async pushValue(key, value, sendChanges = "auto") {
|
|
337
|
+
return await this.pushValues(key, [value], sendChanges);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Push a list of values to array property
|
|
341
|
+
* @param key
|
|
342
|
+
* @param values
|
|
343
|
+
* @param sendChanges
|
|
344
|
+
* @returns
|
|
345
|
+
*/
|
|
346
|
+
async pushValues(key, values, sendChanges = "auto") {
|
|
347
|
+
if (array_isEmpty(values))
|
|
348
|
+
return false;
|
|
349
|
+
if (!this.canModify) {
|
|
350
|
+
if (this.__throw_errors !== false)
|
|
351
|
+
throw Error(`Cant use pushValues (${String(key)}) when on readonly mode`);
|
|
352
|
+
else
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
if (this.get(key) == null)
|
|
356
|
+
this.#setProperty(key, []);
|
|
357
|
+
let tempArray = this.get(key);
|
|
358
|
+
if (!Array.isArray(tempArray)) {
|
|
359
|
+
if (this.__throw_errors !== false)
|
|
360
|
+
throw Error(`Cant use pushValues (${String(key)}) on non Array keys`);
|
|
361
|
+
else
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
this.#setProperty(key, array_merge(tempArray, values));
|
|
365
|
+
if (sendChanges === true || sendChanges === "auto")
|
|
366
|
+
await this.notifyChanged("", key, values);
|
|
367
|
+
return tempArray;
|
|
368
|
+
}
|
|
369
|
+
/* -------------------------------------------------------------------------- */
|
|
370
|
+
/* Load */
|
|
371
|
+
/* -------------------------------------------------------------------------- */
|
|
372
|
+
/**
|
|
373
|
+
* load all properties in `initObject` into the current object
|
|
374
|
+
* @param initObject
|
|
375
|
+
* @param fullUpdate
|
|
376
|
+
* @returns
|
|
377
|
+
*/
|
|
378
|
+
loadValues(initObject, fullUpdate = false) {
|
|
379
|
+
return SimpleObject.loadValues(this, initObject, fullUpdate);
|
|
380
|
+
}
|
|
381
|
+
static loadValues(obj, initObject, fullUpdate = false) {
|
|
382
|
+
if (typeof initObject === "number" || typeof initObject === "string") {
|
|
383
|
+
initObject = { _id: initObject };
|
|
384
|
+
}
|
|
385
|
+
SimpleObject.factoryClear(obj);
|
|
386
|
+
if (initObject == null) {
|
|
387
|
+
for (const key in obj) {
|
|
388
|
+
if (typeof obj[key] === "function")
|
|
389
|
+
continue;
|
|
390
|
+
if (key.startsWith("__"))
|
|
391
|
+
continue;
|
|
392
|
+
delete obj[key];
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
else if (typeof initObject == "object") {
|
|
396
|
+
for (const key in initObject) {
|
|
397
|
+
if (key.startsWith("__"))
|
|
398
|
+
continue;
|
|
399
|
+
if (fullUpdate ||
|
|
400
|
+
Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
401
|
+
obj[key] = initObject[key];
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/* --------------------- special case 1, temp properties -------------------- */
|
|
405
|
+
if ("__temp" in initObject) {
|
|
406
|
+
obj["__temp"] = initObject["__temp"];
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
/* -------------------------------------------------------------------------- */
|
|
412
|
+
/* Refresh Functions */
|
|
413
|
+
/* -------------------------------------------------------------------------- */
|
|
414
|
+
async clearRefreshFunction() {
|
|
415
|
+
this.__refresh_functions = undefined;
|
|
416
|
+
}
|
|
417
|
+
async setRefreshFunction(new_fnc) {
|
|
418
|
+
return await this.addRefreshFunction(new_fnc);
|
|
419
|
+
}
|
|
420
|
+
async addRefreshFunction(key, new_fnc) {
|
|
421
|
+
if (typeof key === "function") {
|
|
422
|
+
new_fnc = key;
|
|
423
|
+
key = "main";
|
|
424
|
+
}
|
|
425
|
+
key = key || "main";
|
|
426
|
+
let oldFunctions = this.__refresh_functions;
|
|
427
|
+
if (oldFunctions == null || typeof oldFunctions !== "object")
|
|
428
|
+
oldFunctions = {};
|
|
429
|
+
oldFunctions[key] = new_fnc;
|
|
430
|
+
this.__refresh_functions = oldFunctions;
|
|
431
|
+
}
|
|
432
|
+
async runRefreshFunction(...params) {
|
|
433
|
+
let oldFunctions = this.__refresh_functions;
|
|
434
|
+
if (oldFunctions == null || typeof oldFunctions !== "object")
|
|
435
|
+
return;
|
|
436
|
+
const fncArray = Object.values(oldFunctions);
|
|
437
|
+
for (let index = 0; index < fncArray.length; index++) {
|
|
438
|
+
const fnc = fncArray[index];
|
|
439
|
+
await fn_eval(fnc, ...params);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
*
|
|
444
|
+
* @param type
|
|
445
|
+
* @param key
|
|
446
|
+
* @param value
|
|
447
|
+
* @param subvalue
|
|
448
|
+
* @returns true if manager was notified. false otherwise
|
|
449
|
+
*/
|
|
450
|
+
async notifyChanged(type, key, value, subvalue) {
|
|
451
|
+
if (value === undefined && type == "")
|
|
452
|
+
value = this.get(key);
|
|
453
|
+
let lc = "";
|
|
454
|
+
if (type)
|
|
455
|
+
lc += `$${String(type)}$`;
|
|
456
|
+
if (subvalue) {
|
|
457
|
+
lc += `${String(key)}.${value}`;
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
lc += `${String(key)}`;
|
|
461
|
+
}
|
|
462
|
+
this.__lastChanged = lc;
|
|
463
|
+
/* --------------------------- notify the manager --------------------------- */
|
|
464
|
+
const manager_class = this.manager;
|
|
465
|
+
if (typeof manager_class?.notifyChanged === "function") {
|
|
466
|
+
return await manager_class.notifyChanged(type, key, value, subvalue);
|
|
467
|
+
}
|
|
468
|
+
return false;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* get a list of all keys that can be changed in given objcet
|
|
472
|
+
* @param object
|
|
473
|
+
* @param options
|
|
474
|
+
* @returns
|
|
475
|
+
*/
|
|
476
|
+
static getChangeableKeys(object, options) {
|
|
477
|
+
if (!object)
|
|
478
|
+
return [];
|
|
479
|
+
let key_list = [];
|
|
480
|
+
key_list.push(...object.constructor["__changeables"]);
|
|
481
|
+
const __unchangeables = object.constructor["__unchangeables"];
|
|
482
|
+
if (!array_isEmpty(__unchangeables)) {
|
|
483
|
+
key_list = key_list.filter((s) => !__unchangeables.includes(s));
|
|
484
|
+
}
|
|
485
|
+
/* -------------------------------------------------------------------------- */
|
|
486
|
+
if (options.extraProperties) {
|
|
487
|
+
key_list.push(...object.constructor["__extraProperties"]);
|
|
488
|
+
}
|
|
489
|
+
if (options.tempProperties) {
|
|
490
|
+
key_list.push("__temp");
|
|
491
|
+
key_list.push(...object.constructor["__tempProperties"]);
|
|
492
|
+
}
|
|
493
|
+
if (options.bypassProtected !== true && !options.allProperties) {
|
|
494
|
+
const __protectedProperties = object.constructor["__protectedProperties"];
|
|
495
|
+
//protected keys can only be changed by the creator of this object
|
|
496
|
+
//make sure you set the is_current_user function correctly
|
|
497
|
+
if (!object.isNew &&
|
|
498
|
+
!array_isEmpty(__protectedProperties) &&
|
|
499
|
+
!Boolean(gfunc.run_action_sync("is_current_user", object.get("_creator")))) {
|
|
500
|
+
key_list = key_list.filter((s) => !__protectedProperties.includes(s));
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return key_list;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* check if a key can be changed on an object
|
|
507
|
+
* @param object the object we are checking
|
|
508
|
+
* @param key key of the object
|
|
509
|
+
* @param useExtraProps should we check extra props?
|
|
510
|
+
* @returns
|
|
511
|
+
*/
|
|
512
|
+
static canChangeKey(object, key, options) {
|
|
513
|
+
if (!object || !key)
|
|
514
|
+
return false;
|
|
515
|
+
const keys = this.getChangeableKeys(object, options);
|
|
516
|
+
return keys.includes(key);
|
|
517
|
+
}
|
|
518
|
+
static hasIllegalChanges(referenceObject, changesObject, updaterUser) {
|
|
519
|
+
const __protectedProperties = object_getStatic(referenceObject, "__protectedProperties");
|
|
520
|
+
if (object_isEmpty(__protectedProperties))
|
|
521
|
+
return false;
|
|
522
|
+
const referenceUser = object_isClass(referenceObject)
|
|
523
|
+
? object_get(changesObject, "__user", "_creator")
|
|
524
|
+
: object_get(referenceObject, "__user", "_creator");
|
|
525
|
+
if (Boolean(SimpleObject.getID(referenceUser) ==
|
|
526
|
+
SimpleObject.getID(updaterUser))) {
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
for (const key in changesObject) {
|
|
530
|
+
if (__protectedProperties.includes(key)) {
|
|
531
|
+
return true;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
/* -------------------------------------------------------------------------- */
|
|
537
|
+
/* Changes */
|
|
538
|
+
/* -------------------------------------------------------------------------- */
|
|
539
|
+
/**
|
|
540
|
+
* Apply all the changes and empty the delta object
|
|
541
|
+
*/
|
|
542
|
+
apply_changes() {
|
|
543
|
+
try {
|
|
544
|
+
if (!this.__use_delta) {
|
|
545
|
+
/**
|
|
546
|
+
* in this mode , we track changes by updating the original product
|
|
547
|
+
*/
|
|
548
|
+
this.setOriginal();
|
|
549
|
+
return true;
|
|
550
|
+
}
|
|
551
|
+
if (!this.isChanged) {
|
|
552
|
+
console.warn("No changes to apply");
|
|
553
|
+
return false;
|
|
554
|
+
}
|
|
555
|
+
for (const key in this.__delta) {
|
|
556
|
+
const nv = this.__delta[key];
|
|
557
|
+
this[key] = nv;
|
|
558
|
+
}
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
finally {
|
|
562
|
+
this.clear_changes();
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* apply changes for the given fields
|
|
567
|
+
* @param key
|
|
568
|
+
* @returns
|
|
569
|
+
*/
|
|
570
|
+
apply(keylist) {
|
|
571
|
+
if (!this.__use_delta) {
|
|
572
|
+
/**
|
|
573
|
+
* in this mode , we track changes by updating the original
|
|
574
|
+
*/
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
if (this.__delta == null || Object.keys(this.__delta).length == 0) {
|
|
578
|
+
/**
|
|
579
|
+
* nothing to apply
|
|
580
|
+
*/
|
|
581
|
+
return true;
|
|
582
|
+
}
|
|
583
|
+
keylist = wrap_array(keylist);
|
|
584
|
+
if (keylist.length == 0) {
|
|
585
|
+
/**
|
|
586
|
+
* nothing to apply
|
|
587
|
+
*/
|
|
588
|
+
return true;
|
|
589
|
+
}
|
|
590
|
+
for (const key of keylist) {
|
|
591
|
+
if (key in this.__delta) {
|
|
592
|
+
this[key] = this.__delta[key];
|
|
593
|
+
delete this.__delta[key];
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return true;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* set the value to its original
|
|
600
|
+
* @param key
|
|
601
|
+
*/
|
|
602
|
+
async discard(key, shouldNotify = "auto") {
|
|
603
|
+
let originalValue;
|
|
604
|
+
if (this.__use_delta) {
|
|
605
|
+
originalValue = this[key];
|
|
606
|
+
}
|
|
607
|
+
else if (this.__originalobject)
|
|
608
|
+
originalValue = this.__originalobject[key];
|
|
609
|
+
else
|
|
610
|
+
originalValue = this[key];
|
|
611
|
+
return await this.set(key, originalValue, shouldNotify);
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* reset all values back to their original
|
|
615
|
+
*
|
|
616
|
+
* this function also calls `on_change` and then `clear_changes`
|
|
617
|
+
* @param key
|
|
618
|
+
*/
|
|
619
|
+
async discardAll(shouldNotify = "auto") {
|
|
620
|
+
let final_result = true;
|
|
621
|
+
for (const key in this.__delta) {
|
|
622
|
+
final_result =
|
|
623
|
+
final_result && (await this.discard(key, shouldNotify));
|
|
624
|
+
}
|
|
625
|
+
if (final_result)
|
|
626
|
+
this.clear_changes();
|
|
627
|
+
return final_result;
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* clear and discard all changes
|
|
631
|
+
*
|
|
632
|
+
* does not call `on_change` functions
|
|
633
|
+
* @returns
|
|
634
|
+
*/
|
|
635
|
+
clear_changes() {
|
|
636
|
+
this.__delta = {};
|
|
637
|
+
////this.__has_delta = false;
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Sets readonly property on given objects
|
|
642
|
+
* @param objs array of given objects or a single object
|
|
643
|
+
* @param readonlyval readonly
|
|
644
|
+
* @param clearChanges should we clean the changes after setting readonly
|
|
645
|
+
*/
|
|
646
|
+
static setReadonly(objs, readonlyval = true, clearChanges = false) {
|
|
647
|
+
let checkObjs = [];
|
|
648
|
+
if (!objs)
|
|
649
|
+
objs = [];
|
|
650
|
+
if (!Array.isArray(objs))
|
|
651
|
+
checkObjs = [objs];
|
|
652
|
+
else
|
|
653
|
+
checkObjs = objs;
|
|
654
|
+
for (let index = 0; index < checkObjs.length; index++) {
|
|
655
|
+
const obj = checkObjs[index];
|
|
656
|
+
obj.__is_readonly = Boolean(readonlyval);
|
|
657
|
+
if (obj instanceof SimpleObject) {
|
|
658
|
+
if (obj.canModify) {
|
|
659
|
+
if (!obj.getOriginal())
|
|
660
|
+
obj.setOriginal();
|
|
661
|
+
}
|
|
662
|
+
else {
|
|
663
|
+
if (clearChanges)
|
|
664
|
+
obj.clear_changes();
|
|
665
|
+
//we dont need the original object in readonly mode
|
|
666
|
+
obj.setOriginal(null);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
static setThrowErrors(objs, throwErr = true) {
|
|
672
|
+
let checkObjs = [];
|
|
673
|
+
if (!objs)
|
|
674
|
+
objs = [];
|
|
675
|
+
if (!Array.isArray(objs))
|
|
676
|
+
checkObjs = [objs];
|
|
677
|
+
else
|
|
678
|
+
checkObjs = objs;
|
|
679
|
+
for (let index = 0; index < checkObjs.length; index++) {
|
|
680
|
+
const element = checkObjs[index];
|
|
681
|
+
element.__throw_errors = throwErr;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/* -------------------------------------------------------------------------- */
|
|
685
|
+
/* Get/Check */
|
|
686
|
+
/* -------------------------------------------------------------------------- */
|
|
687
|
+
get __object_name() {
|
|
688
|
+
return this.constructor["__object_name"] || this.__db_name;
|
|
689
|
+
}
|
|
690
|
+
get __db_name() {
|
|
691
|
+
return this.constructor["__db_name"] || this.constructor["__web_path"];
|
|
692
|
+
}
|
|
693
|
+
get isChanged() {
|
|
694
|
+
////const deltaIsBlank = object_isEmpty(this.__delta);
|
|
695
|
+
////if (!deltaIsBlank) return true; //delta is not blank, definitely changed
|
|
696
|
+
////if (this.__use_delta) return !deltaIsBlank; //trust the delta.
|
|
697
|
+
if (this.__use_delta) {
|
|
698
|
+
////return this.__has_delta;
|
|
699
|
+
/**
|
|
700
|
+
* if there is any key in delta, then the object is changed
|
|
701
|
+
*/
|
|
702
|
+
for (const key in this.__delta)
|
|
703
|
+
return true;
|
|
704
|
+
return false;
|
|
705
|
+
}
|
|
706
|
+
/* --------------------- check the properties one by one -------------------- */
|
|
707
|
+
const orig = this.getOriginal();
|
|
708
|
+
if (!orig)
|
|
709
|
+
return false; //we dont know if anything has changed
|
|
710
|
+
/**
|
|
711
|
+
* if object is not using delta mode,
|
|
712
|
+
* check changes by comparing changeable fields with original object
|
|
713
|
+
*/
|
|
714
|
+
if (Array.isArray(this.__changeables)) {
|
|
715
|
+
return (this.__changeables.find((c) => {
|
|
716
|
+
return !object_equals(this[c], orig[c]);
|
|
717
|
+
}) != undefined);
|
|
718
|
+
}
|
|
719
|
+
else {
|
|
720
|
+
/* check all fields */
|
|
721
|
+
for (const key in this) {
|
|
722
|
+
if (key.startsWith("__"))
|
|
723
|
+
continue;
|
|
724
|
+
if (!object_equals(this[key], orig[key]))
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
return false;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
get isArchived() {
|
|
731
|
+
return this._archived || false;
|
|
732
|
+
}
|
|
733
|
+
set isArchived(v) {
|
|
734
|
+
this._archived = v;
|
|
735
|
+
}
|
|
736
|
+
get isDeleted() {
|
|
737
|
+
return this.get("_deleted") || false;
|
|
738
|
+
}
|
|
739
|
+
get isLocked() {
|
|
740
|
+
return this.get("_locked") || false;
|
|
741
|
+
}
|
|
742
|
+
get objectColor() {
|
|
743
|
+
if (!this.#objectColor) {
|
|
744
|
+
const lastid = this.getID();
|
|
745
|
+
if (lastid)
|
|
746
|
+
this.#objectColor = string_to_color(lastid);
|
|
747
|
+
}
|
|
748
|
+
return this.#objectColor || "#000000";
|
|
749
|
+
}
|
|
750
|
+
set isLocked(v) {
|
|
751
|
+
this._locked = v;
|
|
752
|
+
}
|
|
753
|
+
set isDeleted(v) {
|
|
754
|
+
this._deleted = v;
|
|
755
|
+
}
|
|
756
|
+
get d() {
|
|
757
|
+
return this.__use_delta ? this.__delta : this;
|
|
758
|
+
}
|
|
759
|
+
/* all updated properties */
|
|
760
|
+
get allproperties() {
|
|
761
|
+
const result = { ...this, ...this.__delta };
|
|
762
|
+
result.__delta = {};
|
|
763
|
+
return result;
|
|
764
|
+
}
|
|
765
|
+
get canModify() {
|
|
766
|
+
return !Boolean(this.__is_readonly);
|
|
767
|
+
}
|
|
768
|
+
get __changeables() {
|
|
769
|
+
return this.constructor["__changeables"];
|
|
770
|
+
}
|
|
771
|
+
get __unchangeables() {
|
|
772
|
+
return this.constructor["__unchangeables"];
|
|
773
|
+
}
|
|
774
|
+
get __use_delta() {
|
|
775
|
+
return this.constructor["__use_delta"];
|
|
776
|
+
}
|
|
777
|
+
get isSaved() {
|
|
778
|
+
return Boolean(this.__saveResultIsSuccess ?? false);
|
|
779
|
+
}
|
|
780
|
+
get isNew() {
|
|
781
|
+
const mid = this.getID();
|
|
782
|
+
if (!mid)
|
|
783
|
+
return true;
|
|
784
|
+
if (typeof mid === "string")
|
|
785
|
+
return mid === "" || mid === "-1";
|
|
786
|
+
if (typeof mid === "number")
|
|
787
|
+
return mid === -1;
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
get lastChanged() {
|
|
791
|
+
return this.__lastChanged || "";
|
|
792
|
+
}
|
|
793
|
+
get manager() {
|
|
794
|
+
return this.get_managerClass() || undefined;
|
|
795
|
+
}
|
|
796
|
+
get_managerClass = () => undefined;
|
|
797
|
+
/**
|
|
798
|
+
* @deprecated use setManager
|
|
799
|
+
* @param mng
|
|
800
|
+
*/
|
|
801
|
+
set_managerClass(mng) {
|
|
802
|
+
return this.setManager(mng);
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* update the manager class for this object
|
|
806
|
+
* @param mng
|
|
807
|
+
*/
|
|
808
|
+
setManager(mng) {
|
|
809
|
+
if (mng instanceof ObjectManager &&
|
|
810
|
+
typeof mng.setObject === "function") {
|
|
811
|
+
//make sure the manager knows what object it is managing
|
|
812
|
+
return mng.setObject(this);
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
console.error("set_managerClass was used with a bad manager. make sure the input to this function is a correct ObjectManager");
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
static isDeleted(obj) {
|
|
819
|
+
if (obj instanceof SimpleObject)
|
|
820
|
+
return obj.isDeleted === true;
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
getIndexOrID() {
|
|
824
|
+
return SimpleObject.getIndexOrID(this);
|
|
825
|
+
}
|
|
826
|
+
getIndex() {
|
|
827
|
+
return SimpleObject.getIndex(this);
|
|
828
|
+
}
|
|
829
|
+
getID() {
|
|
830
|
+
return SimpleObject.getID(this);
|
|
831
|
+
}
|
|
832
|
+
static getIndexOrID(o) {
|
|
833
|
+
let res = SimpleObject.getIndex(o);
|
|
834
|
+
if (!res || res == -1)
|
|
835
|
+
res = SimpleObject.getID(o);
|
|
836
|
+
return res || -1;
|
|
837
|
+
}
|
|
838
|
+
static getIndex(o) {
|
|
839
|
+
if (o == null || o == undefined)
|
|
840
|
+
return -1;
|
|
841
|
+
switch (typeof o) {
|
|
842
|
+
case "object":
|
|
843
|
+
return o._index_value || -1;
|
|
844
|
+
case "number":
|
|
845
|
+
case "string":
|
|
846
|
+
return Number(o);
|
|
847
|
+
}
|
|
848
|
+
return -1;
|
|
849
|
+
}
|
|
850
|
+
static getID(obj) {
|
|
851
|
+
if (obj == null || obj == undefined)
|
|
852
|
+
return "";
|
|
853
|
+
switch (typeof obj) {
|
|
854
|
+
case "object":
|
|
855
|
+
if (String(obj._bsontype).toLowerCase() == "objectid") {
|
|
856
|
+
return String(obj);
|
|
857
|
+
}
|
|
858
|
+
return String(obj._id ||
|
|
859
|
+
obj.insertedId ||
|
|
860
|
+
obj.ID ||
|
|
861
|
+
obj.id ||
|
|
862
|
+
obj.__data_id ||
|
|
863
|
+
"");
|
|
864
|
+
case "string":
|
|
865
|
+
case "number":
|
|
866
|
+
return String(obj);
|
|
867
|
+
}
|
|
868
|
+
return "";
|
|
869
|
+
}
|
|
870
|
+
/* -------------------------------------------------------------------------- */
|
|
871
|
+
/* Events */
|
|
872
|
+
/* -------------------------------------------------------------------------- */
|
|
873
|
+
async event_run(eventName, ...params) {
|
|
874
|
+
if (!this.manager) {
|
|
875
|
+
if (this.__showErrors === true)
|
|
876
|
+
console.error(`you cant use events (${eventName}) if your object has no manager`);
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
return await this.manager.event_run(eventName, ...params);
|
|
880
|
+
}
|
|
881
|
+
event_use(name, runEvent, onlyOn = null, runOnce = false) {
|
|
882
|
+
if (!this.manager) {
|
|
883
|
+
if (this.__showErrors === true)
|
|
884
|
+
console.error(`you cant use events (${name}) if your object has no manager`);
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
return this.manager.event_use(name, runEvent, onlyOn, runOnce);
|
|
888
|
+
}
|
|
889
|
+
event_remove(name, uname) {
|
|
890
|
+
if (!this.manager) {
|
|
891
|
+
if (this.__showErrors === true)
|
|
892
|
+
console.error(`you cant use events (${name}) if your object has no manager`);
|
|
893
|
+
return;
|
|
894
|
+
}
|
|
895
|
+
return this.manager.event_remove(name, uname);
|
|
896
|
+
}
|
|
897
|
+
async event_add(name, uname, runEvent, onlyOn = null, runOnce = false) {
|
|
898
|
+
if (!this.manager) {
|
|
899
|
+
if (this.__showErrors === true)
|
|
900
|
+
console.error(`you cant use events (${name}) if your object has no manager`);
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
await this.manager.event_add(name, uname, runEvent, onlyOn, runOnce);
|
|
904
|
+
}
|
|
905
|
+
/* -------------------------------------------------------------------------- */
|
|
906
|
+
/* Original */
|
|
907
|
+
/* -------------------------------------------------------------------------- */
|
|
908
|
+
getOriginal() {
|
|
909
|
+
return this.__originalobject || undefined;
|
|
910
|
+
}
|
|
911
|
+
setOriginal(toObj = 0) {
|
|
912
|
+
if (toObj === null) {
|
|
913
|
+
delete this.__originalobject;
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
if (!this.canModify) {
|
|
917
|
+
console.warn("Cant use setOriginal on non original OR readonly objects ! this will create an infinite loop");
|
|
918
|
+
delete this.__originalobject;
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
if (toObj === 0) {
|
|
922
|
+
toObj = this;
|
|
923
|
+
}
|
|
924
|
+
const cloneObject = object_clone(toObj);
|
|
925
|
+
SimpleObject.setReadonly(cloneObject, true);
|
|
926
|
+
this.__originalobject = object_removeKeys(cloneObject, ["__"]);
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
static setOriginal(obj, toObj = 0) {
|
|
930
|
+
if (!(obj instanceof SimpleObject))
|
|
931
|
+
return undefined;
|
|
932
|
+
return obj.setOriginal(toObj);
|
|
933
|
+
}
|
|
934
|
+
static getOriginal(obj) {
|
|
935
|
+
if (obj instanceof SimpleObject)
|
|
936
|
+
return obj.getOriginal();
|
|
937
|
+
return obj?.__originalobject;
|
|
938
|
+
}
|
|
939
|
+
/* -------------------------------------------------------------------------- */
|
|
940
|
+
/* Errors */
|
|
941
|
+
/* -------------------------------------------------------------------------- */
|
|
942
|
+
addError(title, message, sendToRegister = "auto") {
|
|
943
|
+
return SimpleObject.addError(this, title, message, sendToRegister);
|
|
944
|
+
}
|
|
945
|
+
clearErrors() {
|
|
946
|
+
return SimpleObject.clearErrors(this);
|
|
947
|
+
}
|
|
948
|
+
getErrors() {
|
|
949
|
+
return SimpleObject.getErrors(this);
|
|
950
|
+
}
|
|
951
|
+
static addError(o, title, message, sendToRegister = "auto") {
|
|
952
|
+
const errStr = combine_if_not_blank(title, " : ", message);
|
|
953
|
+
if (o) {
|
|
954
|
+
if (!Array.isArray(o.__errors))
|
|
955
|
+
o.__errors = [];
|
|
956
|
+
o.__errors.push(errStr);
|
|
957
|
+
if (o.__showErrors === false)
|
|
958
|
+
return;
|
|
959
|
+
}
|
|
960
|
+
else if (sendToRegister === "auto")
|
|
961
|
+
sendToRegister = true;
|
|
962
|
+
if (sendToRegister === true) {
|
|
963
|
+
console.error("[REGISTER-ERROR]", errStr);
|
|
964
|
+
gfunc.run_action("add_error", {
|
|
965
|
+
title: title,
|
|
966
|
+
message: message,
|
|
967
|
+
type: "error",
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
else {
|
|
971
|
+
console.error("[ERROR]", errStr);
|
|
972
|
+
}
|
|
973
|
+
return o?.__errors || [];
|
|
974
|
+
}
|
|
975
|
+
static getErrors(o) {
|
|
976
|
+
return o?.__errors || [];
|
|
977
|
+
}
|
|
978
|
+
static clearErrors(o) {
|
|
979
|
+
if (o)
|
|
980
|
+
o.__errors = [];
|
|
981
|
+
return true;
|
|
982
|
+
}
|
|
983
|
+
/* -------------------------------------------------------------------------- */
|
|
984
|
+
/* Save Results */
|
|
985
|
+
/* -------------------------------------------------------------------------- */
|
|
986
|
+
/**
|
|
987
|
+
* Checks if the given object is a valid and truthy save result
|
|
988
|
+
* @param result_object
|
|
989
|
+
* @returns
|
|
990
|
+
*/
|
|
991
|
+
static isResultValid(result_object, depth = 0) {
|
|
992
|
+
if (object_isEmpty(result_object))
|
|
993
|
+
return false;
|
|
994
|
+
switch (typeof result_object) {
|
|
995
|
+
case "string":
|
|
996
|
+
try {
|
|
997
|
+
result_object = JSON.parse(result_object);
|
|
998
|
+
return this.isResultValid(result_object);
|
|
999
|
+
}
|
|
1000
|
+
catch {
|
|
1001
|
+
return false;
|
|
1002
|
+
}
|
|
1003
|
+
case "number":
|
|
1004
|
+
return result_object > 0;
|
|
1005
|
+
case "boolean":
|
|
1006
|
+
return result_object === true;
|
|
1007
|
+
case "object":
|
|
1008
|
+
/* ----------------------------- result is error ---------------------------- */
|
|
1009
|
+
if (result_object instanceof Error) {
|
|
1010
|
+
return false;
|
|
1011
|
+
}
|
|
1012
|
+
/* ----------------- maybe website sends the result as array ---------------- */
|
|
1013
|
+
if (Array.isArray(result_object)) {
|
|
1014
|
+
const allResults = result_object.map((r) => {
|
|
1015
|
+
return Boolean(this.isResultValid(r, depth + 1));
|
|
1016
|
+
});
|
|
1017
|
+
return allResults.find((s) => s === false) == null;
|
|
1018
|
+
}
|
|
1019
|
+
/* ----------------------------- check products/variations ----------------------------- */
|
|
1020
|
+
const varKeys = ["products", "variations"];
|
|
1021
|
+
for (const vk of varKeys) {
|
|
1022
|
+
if (!(vk in result_object) ||
|
|
1023
|
+
array_isEmpty(result_object[vk]))
|
|
1024
|
+
continue;
|
|
1025
|
+
/**
|
|
1026
|
+
* if one product is not valid the result is not valid
|
|
1027
|
+
* but if all products are valid we still need to check the object itself
|
|
1028
|
+
*/
|
|
1029
|
+
if (!this.isResultValid(result_object[vk], depth + 1))
|
|
1030
|
+
return false;
|
|
1031
|
+
}
|
|
1032
|
+
if (result_object.order) {
|
|
1033
|
+
/**
|
|
1034
|
+
* if we get a order-products pair, checking the order is enough
|
|
1035
|
+
*/
|
|
1036
|
+
return this.isResultValid(result_object.order, depth + 1);
|
|
1037
|
+
}
|
|
1038
|
+
/* -------------------------- check if acknowledged ------------------------- */
|
|
1039
|
+
if (object_isTrue(result_object.acknowledged)) {
|
|
1040
|
+
return true;
|
|
1041
|
+
}
|
|
1042
|
+
if (result_object.expected && result_object.result) {
|
|
1043
|
+
return object_equals(result_object.expected, result_object.result);
|
|
1044
|
+
}
|
|
1045
|
+
return !string_isEmpty(this.getID(result_object));
|
|
1046
|
+
}
|
|
1047
|
+
return false;
|
|
1048
|
+
}
|
|
1049
|
+
/**
|
|
1050
|
+
* set the response of last save from site
|
|
1051
|
+
* and its type
|
|
1052
|
+
* @param toObj
|
|
1053
|
+
*/
|
|
1054
|
+
setSaveResult(toObj, type = "auto", registerErrors = false) {
|
|
1055
|
+
this.#setSaveResultObject(toObj);
|
|
1056
|
+
//check if the given object is a valid save result
|
|
1057
|
+
if (type === "error")
|
|
1058
|
+
this.__saveResultIsSuccess = false;
|
|
1059
|
+
else
|
|
1060
|
+
this.__saveResultIsSuccess = Boolean(type === "nothing" || this.constructor["isResultValid"](toObj));
|
|
1061
|
+
if (type === "auto") {
|
|
1062
|
+
type = this.__saveResultIsSuccess ? "normal" : "error";
|
|
1063
|
+
}
|
|
1064
|
+
if (registerErrors && !this.__saveResultIsSuccess) {
|
|
1065
|
+
const sr = this.__saveResultObject;
|
|
1066
|
+
const errorString = sr == null
|
|
1067
|
+
? ""
|
|
1068
|
+
: sr instanceof Error
|
|
1069
|
+
? sr.message
|
|
1070
|
+
: JSON.stringify(sr);
|
|
1071
|
+
SimpleObject.addError(this, "save result is not valid", errorString, true);
|
|
1072
|
+
}
|
|
1073
|
+
this.__saveResultType = type;
|
|
1074
|
+
return type;
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* set the response of last save from site
|
|
1078
|
+
* @param toObj
|
|
1079
|
+
*/
|
|
1080
|
+
#setSaveResultObject(toObj) {
|
|
1081
|
+
if (toObj == null)
|
|
1082
|
+
toObj = null; //null or undefined
|
|
1083
|
+
this.__saveResultObject = toObj;
|
|
1084
|
+
return true;
|
|
1085
|
+
}
|
|
1086
|
+
getSaveResult(original = false) {
|
|
1087
|
+
if (this.isSaved)
|
|
1088
|
+
return this.__saveResultObject;
|
|
1089
|
+
return false;
|
|
1090
|
+
}
|
|
1091
|
+
getSaveResultType() {
|
|
1092
|
+
return this.__saveResultType || "nothing";
|
|
1093
|
+
}
|
|
1094
|
+
/** did we create on the last save */
|
|
1095
|
+
getIsLastSaveCreate() {
|
|
1096
|
+
return this.isSaved && this.getSaveResult().insertedId;
|
|
1097
|
+
}
|
|
1098
|
+
/* -------------------------------------------------------------------------- */
|
|
1099
|
+
/* statics */
|
|
1100
|
+
/* -------------------------------------------------------------------------- */
|
|
1101
|
+
static async setTemp(obj, key, value, sendChanges = false) {
|
|
1102
|
+
if (typeof obj !== "object" || !obj || Array.isArray(obj))
|
|
1103
|
+
return;
|
|
1104
|
+
if (obj instanceof SimpleObject)
|
|
1105
|
+
return await obj.setTemp(key, value, sendChanges);
|
|
1106
|
+
//
|
|
1107
|
+
if (obj.__temp == null)
|
|
1108
|
+
obj.__temp = {};
|
|
1109
|
+
object_set(obj.__temp, key, value);
|
|
1110
|
+
}
|
|
1111
|
+
static factoryClear(obj) {
|
|
1112
|
+
obj.clear_changes();
|
|
1113
|
+
SimpleObject.clearErrors(obj);
|
|
1114
|
+
}
|
|
1115
|
+
static async delete_cache(context) {
|
|
1116
|
+
if (!this.__db_name)
|
|
1117
|
+
return;
|
|
1118
|
+
if (!context)
|
|
1119
|
+
await gcache.delete_session_cache(this.__db_name);
|
|
1120
|
+
else {
|
|
1121
|
+
const cacheKey = `${this.__db_name}-${context}`;
|
|
1122
|
+
await gcache.delete_session_cache(cacheKey);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
static get_canModify(o) {
|
|
1126
|
+
if (o instanceof SimpleObject)
|
|
1127
|
+
return o.canModify;
|
|
1128
|
+
return false;
|
|
1129
|
+
}
|
|
1130
|
+
/* -------------------------------------------------------------------------- */
|
|
1131
|
+
static equals(a, b) {
|
|
1132
|
+
console.warn("Dont use the default equals method !");
|
|
1133
|
+
if (a == undefined || b == undefined)
|
|
1134
|
+
return false;
|
|
1135
|
+
return a === b;
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
export default SimpleObject;
|