@azrico/webobject 1.0.32 → 1.0.35

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.
@@ -1,28 +1,439 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=exports.SimpleObject=void 0;var _global=require("@azrico/global"),_object=require("@azrico/object"),_MapableObject2=require("./MapableObject"),_ObjectManager=_interopRequireDefault(require("./ObjectManager")),_string=require("@azrico/string");function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),Object.defineProperty(a,"prototype",{writable:!1}),a}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),Object.defineProperty(a,"prototype",{writable:!1}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _createSuper(a){var b=_isNativeReflectConstruct();return function(){var c,d=_getPrototypeOf(a);if(b){var e=_getPrototypeOf(this).constructor;c=Reflect.construct(d,arguments,e)}else c=d.apply(this,arguments);return _possibleConstructorReturn(this,c)}}function _possibleConstructorReturn(a,b){if(b&&("object"===_typeof(b)||"function"==typeof b))return b;if(void 0!==b)throw new TypeError("Derived constructors may only return object or undefined");return _assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(a){return!1}}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}var SimpleObject=/*#__PURE__*/function(a){//changes
2
- //errors
3
- //save results
4
- //sequence key
5
- //sequence value , used to index the object with increasing numbers
6
- function b(){var a,d=!(0<arguments.length&&void 0!==arguments[0])||arguments[0],e=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1];return _classCallCheck(this,b),a=c.call(this),_defineProperty(_assertThisInitialized(a),"__originalobject",void 0),_defineProperty(_assertThisInitialized(a),"__delta",void 0),_defineProperty(_assertThisInitialized(a),"__changed",!1),_defineProperty(_assertThisInitialized(a),"__lastChanged",""),_defineProperty(_assertThisInitialized(a),"__is_readonly",void 0),_defineProperty(_assertThisInitialized(a),"__is_edit_mode",void 0),_defineProperty(_assertThisInitialized(a),"__errors",void 0),_defineProperty(_assertThisInitialized(a),"__showErrors",void 0),_defineProperty(_assertThisInitialized(a),"__saveResultObject",void 0),_defineProperty(_assertThisInitialized(a),"__saveResultIsSuccess",void 0),_defineProperty(_assertThisInitialized(a),"__saveResultType",void 0),_defineProperty(_assertThisInitialized(a),"_index_key",void 0),_defineProperty(_assertThisInitialized(a),"_index_value",void 0),_defineProperty(_assertThisInitialized(a),"get_managerClass",function(){return null}),a.__delta={},a.__is_edit_mode=d,a.__is_readonly=e||!a.__is_edit_mode,a.clear_changes(),a}/* -------------------------------------------------------------------------- */ /* Load */ /* -------------------------------------------------------------------------- */_inherits(b,a);var c=_createSuper(b);return _createClass(b,[{key:"loadValues",value:function loadValues(a){var c=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1];return b.loadValues(this,a,c),!0}},{key:"getValue",value:function getValue(a){var b=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1];return!b&&this.__delta[a]?this.__delta[a]:this[a]}},{key:"get",value:function get(a){var b=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1];return this.getValue(a,b)}/**
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
- */},{key:"setValue",value:function setValue(a,b){if(!this.canModify)throw Error("Cant use setValue when on readonly mode");var c=!1;try{var d=this.getValue(a);(0,_object.object_equals)(d,b,{ignoredStartingWords:"_"})?c=!1:(this.__delta[a]=b,c=!0)}finally{c&&this.notifyChanged("",a,b)}return c}/**
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
- */},{key:"setSubValue",value:function setSubValue(a,b,c){if(!this.canModify)throw Error("Cant use setValue when on readonly mode");var d=!1;try{(null==this.__delta[a]||"object"!=_typeof(this.__delta[a]))&&(this.__delta[a]={});var e=this.__delta[a][b];(0,_object.object_equals)(e,c,{ignoredStartingWords:"_"})?d=!1:(this.__delta[a][b]=c,d=!0)}finally{d&&this.notifyChanged("",a,b,c)}return d}//Push value on array
18
- },{key:"pushValue",value:function pushValue(a,b){if(null==b)return null;if(!this.canModify)throw Error("Cant use pushValue when on readonly mode");if(null==this.__delta[a]&&(this.__delta[a]=this.getValue(a)),!Array.isArray(this.__delta[a]))throw Error("Cant use pushValue on non Array keys");var c=this.__delta[a];return c.push(b),this.__delta[a]=c,this.__changed=!0,this.__lastChanged="".concat(a," : +").concat(b),c}},{key:"notifyChanged",value:function notifyChanged(a,b,c,d){void 0===c&&(c=this.getValue(b)),this.__changed=!0;var e="";a&&(e+="[".concat(a,"] ")),e+=d?"".concat(b,"->").concat(c," = ").concat(d):"".concat(b," = ").concat(c),this.__lastChanged=e;/* --------------------------- notify the manager --------------------------- */var f=this.get_managerClass();f instanceof _ObjectManager["default"]&&f.notifyChanged(a,b,c,d)}/**
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
+ this.__changed = true;
153
+ this.__lastChanged = `${key} : +${value}`;
154
+ return tempArray;
155
+ }
156
+ 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
+ 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
- */},{key:"rn",value:function rn(a,b,c){return"object"==_typeof(a)&&"string"==typeof b&&"string"==typeof c?(0,_object.prop_rn)(a,b,c):"string"==typeof a&&"string"==typeof b&&(0,_object.prop_rn)(this,a,b)}},{key:"apply_changes",value:/* -------------------------------------------------------------------------- */ /* Changes */ /* -------------------------------------------------------------------------- */ /**
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(key) {
195
+ return ((this.__changeables == null || this.__changeables.includes(key)) &&
196
+ (this.__unchangeables == null ||
197
+ !this.__unchangeables.includes(key)));
198
+ }
199
+ /* -------------------------------------------------------------------------- */
200
+ /* Changes */
201
+ /* -------------------------------------------------------------------------- */
202
+ /**
25
203
  * Apply the changes and empty the delta object
26
- */function apply_changes(){for(var a in this.isChanged||console.warn("No changes to apply"),this.__delta){var b=this.__delta[a];this[a]=b}this.__delta={},this.__changed=!1}},{key:"clear_changes",value:function clear_changes(){this.__delta={},this.__changed=!1}},{key:"isChanged",get:/* -------------------------------------------------------------------------- */ /* Get/Check */ /* -------------------------------------------------------------------------- */function get(){return!(0,_object.object_isEmpty)(this.__delta)}},{key:"isDeleted",get:function get(){return!1}},{key:"d",get:function get(){return this.__delta}},{key:"canModify",get:function get(){return!this.__is_readonly&&this.isEditMode}},{key:"isEditMode",get:function get(){return!1!==this.__is_edit_mode}},{key:"__cache_name",get:function get(){return this.constructor.__cache_name}},{key:"isSaved",get:function get(){var a;return null!==(a=this.__saveResultIsSuccess)&&void 0!==a&&a}},{key:"isNew",get:function get(){var a=this.getID();return!(a&&"-1"!==a)}},{key:"manager",get:function get(){return this.get_managerClass()}},{key:"get_lastChanged",value:function get_lastChanged(){return!this.__lastChanged}},{key:"getID",value:function getID(){//@ts-ignore
27
- return this.__data_id||this._id||this.id||this.ID||"-1"}},{key:"getOriginal",value:/* -------------------------------------------------------------------------- */ /* Original */ /* -------------------------------------------------------------------------- */function getOriginal(){return this.__originalobject||void 0}},{key:"setOriginal",value:function setOriginal(){var a=0<arguments.length&&void 0!==arguments[0]?arguments[0]:0;if(null===a)return void delete this.__originalobject;if(!this.isEditMode)return void console.warn("Cant use setOriginal on non original objects ! this will create an infinite loop");if(this.__is_readonly)return void delete this.__originalobject;0===a&&(a=this);var b=(0,_object.object_clone)(a);//this.__originalobject = cloneObject, ["__"];
28
- return this.__originalobject=(0,_object.object_removeKeys)(b,["__"]),this}},{key:"setSaveResult",value:function setSaveResult(a,b){this.__saveResultType=b,!0===a||!1===a||(a?this.setSaveResultObject(a):this.setSaveResultObject(null)),this.__saveResultIsSuccess=("normal"===b||"nothing"===b)&&null!=a}},{key:"setSaveResultObject",value:function setSaveResultObject(a){this.__saveResultObject=a}},{key:"getSaveResult",value:function getSaveResult(){return!!this.isSaved&&this.__saveResultObject}/* -------------------------------------------------------------------------- */ /* statics */ /* -------------------------------------------------------------------------- */}],[{key:"loadValues",value:function loadValues(a,c){var d=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2];if(b.factoryClear(a),null==c)for(var e in a)e.startsWith("__")||delete a[e];else if("object"==_typeof(c))for(var f in c)f.startsWith("__")||(d||Object.prototype.hasOwnProperty.call(a,f))&&(a[f]=c[f]);return!0}/* -------------------------------------------------------------------------- */},{key:"getValue",value:function getValue(a,c){var d=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2];return a instanceof b&&"function"==typeof a.getValue?a.getValue(c,d):!d&&a.__delta[c]?a.__delta[c]:a[c]}},{key:"canChangeKey",value:function canChangeKey(a){return(null==this.__changeables||this.__changeables.includes(a))&&(null==this.__unchangeables||!this.__unchangeables.includes(a))}},{key:"setReadonly",value:function setReadonly(a){var b=!(1<arguments.length&&void 0!==arguments[1])||arguments[1],c=[];a||(a=[]),c=Array.isArray(a)?a:[a];for(var d,e=0;e<c.length;e++)d=c[e],d.__is_readonly=b}},{key:"isDeleted",value:function isDeleted(a){return!!(a instanceof b)&&!0===a.isDeleted}},{key:"getID",value:function getID(a){var c;if(null==a||null==a)return"-1";if(a instanceof b)return null!==(c=a.getID())&&void 0!==c?c:"-1";switch(_typeof(a)){case"object":return a.__data_id||a._id||a.id||a.ID||"-1";case"number":return a+"";}return"-1"}},{key:"setOriginal",value:function setOriginal(a){var c=1<arguments.length&&void 0!==arguments[1]?arguments[1]:0;return a instanceof b?a.setOriginal(c):void 0}},{key:"getOriginal",value:function getOriginal(a){return a instanceof b?a.getOriginal():null===a||void 0===a?void 0:a.__originalobject}/* -------------------------------------------------------------------------- */ /* Errors */ /* -------------------------------------------------------------------------- */},{key:"addError",value:function addError(a,b,c){var d=3<arguments.length&&void 0!==arguments[3]?arguments[3]:"auto",e=(0,_string.combine_if_not_blank)(b," : ",c);if(!a)"auto"===d&&(d=!0);else if(Array.isArray(a.__errors)||(a.__errors=[]),a.__errors.push(e),!1===a.__showErrors)return;return!0===d?(console.error("[REGISTER-ERROR]",e),_global.gfunc.run_action("add_error",{title:b,message:c,type:"error"})):console.error("[ERROR]",e),(null===a||void 0===a?void 0:a.__errors)||[]}},{key:"getErrors",value:function getErrors(a){return a.__errors||[]}},{key:"clearErrors",value:function clearErrors(a){return a.__errors=[],!0}/* -------------------------------------------------------------------------- */ /* Save Results */ /* -------------------------------------------------------------------------- */},{key:"isResultValid",value:function isResultValid(a){if((0,_object.object_isEmpty)(a))return!1;switch(_typeof(a)){case"string":try{return a=JSON.parse(a),this.isResultValid(a)}catch(a){return!1}case"object":return!!(0,_object.object_isTrue)(a.acknowledged)||(a.expected&&a.result?(0,_object.object_equals)(a.expected,a.result):0<=+this.getID(a));case"number":return 0<a;case"boolean":return!0===a;}return!1}},{key:"factoryClear",value:function factoryClear(a){a.clear_changes(),b.clearErrors(a)}},{key:"delete_cache",value:function delete_cache(){this.__cache_name&&_global.gcache.delete_cache(this.__cache_name)}},{key:"get_canModify",value:function get_canModify(a){return!!(a instanceof b)&&a.canModify}/* -------------------------------------------------------------------------- */},{key:"equals",value:function equals(c,a){return console.warn("Dont use the default equals method !"),null!=c&&null!=a&&c===a}}]),b}(_MapableObject2.MapableObject);exports.SimpleObject=SimpleObject,_defineProperty(SimpleObject,"__cache_name",void 0),_defineProperty(SimpleObject,"__unchangeables",void 0),_defineProperty(SimpleObject,"__changeables",void 0);var _default=SimpleObject;exports["default"]=_default;
204
+ */
205
+ apply_changes() {
206
+ if (!this.isChanged) {
207
+ console.warn("No changes to apply");
208
+ }
209
+ for (const key in this.__delta) {
210
+ const nv = this.__delta[key];
211
+ this[key] = nv;
212
+ }
213
+ this.__delta = {};
214
+ this.__changed = false;
215
+ }
216
+ clear_changes() {
217
+ this.__delta = {};
218
+ this.__changed = false;
219
+ }
220
+ static setReadonly(objs, readonlyval = true) {
221
+ let checkObjs = [];
222
+ if (!objs)
223
+ objs = [];
224
+ if (!Array.isArray(objs))
225
+ checkObjs = [objs];
226
+ else
227
+ checkObjs = objs;
228
+ for (let index = 0; index < checkObjs.length; index++) {
229
+ const element = checkObjs[index];
230
+ element.__is_readonly = readonlyval;
231
+ }
232
+ }
233
+ /* -------------------------------------------------------------------------- */
234
+ /* Get/Check */
235
+ /* -------------------------------------------------------------------------- */
236
+ get isChanged() {
237
+ return !object_isEmpty(this.__delta);
238
+ }
239
+ get isDeleted() {
240
+ return false;
241
+ }
242
+ get d() {
243
+ return this.__delta;
244
+ }
245
+ get canModify() {
246
+ return !this.__is_readonly && this.isEditMode;
247
+ }
248
+ get isEditMode() {
249
+ return this.__is_edit_mode !== false;
250
+ }
251
+ get __cache_name() {
252
+ return this.constructor["__cache_name"];
253
+ }
254
+ get isSaved() {
255
+ return Boolean(this.__saveResultIsSuccess ?? false);
256
+ }
257
+ get isNew() {
258
+ const mid = this.getID();
259
+ if (!mid || mid === "-1")
260
+ return true;
261
+ return false;
262
+ }
263
+ get manager() {
264
+ return this.get_managerClass();
265
+ }
266
+ get_lastChanged() {
267
+ return !this.__lastChanged;
268
+ }
269
+ get_managerClass = () => null;
270
+ static isDeleted(obj) {
271
+ if (obj instanceof SimpleObject)
272
+ return obj.isDeleted === true;
273
+ return false;
274
+ }
275
+ getIndexOrID() {
276
+ let res = this.getIndex();
277
+ if (!res || res == -1)
278
+ res = this.getID();
279
+ return res || -1;
280
+ }
281
+ getIndex() {
282
+ return this._index_value || -1;
283
+ }
284
+ getID() {
285
+ //@ts-ignore
286
+ return this.__data_id || this._id || this.id || this.ID || "-1";
287
+ }
288
+ /* -------------------------------------------------------------------------- */
289
+ /* Original */
290
+ /* -------------------------------------------------------------------------- */
291
+ getOriginal() {
292
+ return this.__originalobject || undefined;
293
+ }
294
+ setOriginal(toObj = 0) {
295
+ if (toObj === null) {
296
+ delete this.__originalobject;
297
+ return;
298
+ }
299
+ if (!this.isEditMode) {
300
+ console.warn("Cant use setOriginal on non original objects ! this will create an infinite loop");
301
+ return;
302
+ }
303
+ if (this.__is_readonly) {
304
+ delete this.__originalobject;
305
+ return;
306
+ }
307
+ if (toObj === 0) {
308
+ toObj = this;
309
+ }
310
+ const cloneObject = object_clone(toObj);
311
+ //this.__originalobject = cloneObject, ["__"];
312
+ this.__originalobject = object_removeKeys(cloneObject, ["__"]);
313
+ return this;
314
+ }
315
+ static setOriginal(obj, toObj = 0) {
316
+ if (!(obj instanceof SimpleObject))
317
+ return undefined;
318
+ return obj.setOriginal(toObj);
319
+ }
320
+ static getOriginal(obj) {
321
+ if (obj instanceof SimpleObject)
322
+ return obj.getOriginal();
323
+ return obj?.__originalobject;
324
+ }
325
+ /* -------------------------------------------------------------------------- */
326
+ /* Errors */
327
+ /* -------------------------------------------------------------------------- */
328
+ static addError(o, title, message, sendToRegister = "auto") {
329
+ const errStr = combine_if_not_blank(title, " : ", message);
330
+ if (o) {
331
+ if (!Array.isArray(o.__errors))
332
+ o.__errors = [];
333
+ o.__errors.push(errStr);
334
+ if (o.__showErrors === false)
335
+ return;
336
+ }
337
+ else if (sendToRegister === "auto")
338
+ sendToRegister = true;
339
+ if (sendToRegister === true) {
340
+ console.error("[REGISTER-ERROR]", errStr);
341
+ gfunc.run_action("add_error", {
342
+ title: title,
343
+ message: message,
344
+ type: "error",
345
+ });
346
+ }
347
+ else {
348
+ console.error("[ERROR]", errStr);
349
+ }
350
+ return o?.__errors || [];
351
+ }
352
+ static getErrors(o) {
353
+ return o.__errors || [];
354
+ }
355
+ static clearErrors(o) {
356
+ o.__errors = [];
357
+ return true;
358
+ }
359
+ /* -------------------------------------------------------------------------- */
360
+ /* Save Results */
361
+ /* -------------------------------------------------------------------------- */
362
+ static isResultValid(result_object) {
363
+ if (object_isEmpty(result_object))
364
+ return false;
365
+ switch (typeof result_object) {
366
+ case "string":
367
+ try {
368
+ result_object = JSON.parse(result_object);
369
+ return this.isResultValid(result_object);
370
+ }
371
+ catch {
372
+ return false;
373
+ }
374
+ case "object":
375
+ if (object_isTrue(result_object.acknowledged)) {
376
+ return true;
377
+ }
378
+ if (result_object.expected && result_object.result) {
379
+ return object_equals(result_object.expected, result_object.result);
380
+ }
381
+ return Number(this.getID(result_object)) >= 0;
382
+ case "number":
383
+ return result_object > 0;
384
+ case "boolean":
385
+ return result_object === true;
386
+ }
387
+ return false;
388
+ }
389
+ setSaveResult(toObj, type) {
390
+ this.__saveResultType = type;
391
+ if (toObj === true) {
392
+ }
393
+ else if (toObj === false) {
394
+ }
395
+ else {
396
+ if (toObj) {
397
+ this.setSaveResultObject(toObj);
398
+ }
399
+ else {
400
+ this.setSaveResultObject(null);
401
+ }
402
+ }
403
+ this.__saveResultIsSuccess =
404
+ (type === "normal" || type === "nothing") && toObj != null;
405
+ }
406
+ setSaveResultObject(toObj) {
407
+ this.__saveResultObject = toObj;
408
+ }
409
+ getSaveResult() {
410
+ if (this.isSaved)
411
+ return this.__saveResultObject;
412
+ return false;
413
+ }
414
+ /* -------------------------------------------------------------------------- */
415
+ /* statics */
416
+ /* -------------------------------------------------------------------------- */
417
+ static factoryClear(obj) {
418
+ obj.clear_changes();
419
+ SimpleObject.clearErrors(obj);
420
+ }
421
+ static delete_cache() {
422
+ if (!this.__cache_name)
423
+ return;
424
+ gcache.delete_cache(this.__cache_name);
425
+ }
426
+ static get_canModify(o) {
427
+ if (o instanceof SimpleObject)
428
+ return o.canModify;
429
+ return false;
430
+ }
431
+ /* -------------------------------------------------------------------------- */
432
+ static equals(a, b) {
433
+ console.warn("Dont use the default equals method !");
434
+ if (a == undefined || b == undefined)
435
+ return false;
436
+ return a === b;
437
+ }
438
+ }
439
+ export default SimpleObject;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azrico/webobject",
3
- "version": "1.0.32",
3
+ "version": "1.0.35",
4
4
  "description": "Stateful object",
5
5
  "author": "Azrideus",
6
6
  "license": "MIT",