@azrico/webobject 1.1.15 → 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/README.md +52 -52
- 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 +12 -12
|
@@ -1,23 +1,499 @@
|
|
|
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 ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function _regeneratorRuntime(){"use strict";/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */function a(a,b,c){return Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}),a[b]}function b(a,b,e,f){var g=b&&b.prototype instanceof d?b:d,h=Object.create(g.prototype),j=new l(f||[]);return h._invoke=function(a,b,d){var e="suspendedStart";return function(f,g){if("executing"===e)throw new Error("Generator is already running");if("completed"===e){if("throw"===f)throw g;return n()}for(d.method=f,d.arg=g;;){var h=d.delegate;if(h){var j=i(h,d);if(j){if(j===v)continue;return j}}if("next"===d.method)d.sent=d._sent=d.arg;else if("throw"===d.method){if("suspendedStart"===e)throw e="completed",d.arg;d.dispatchException(d.arg)}else"return"===d.method&&d.abrupt("return",d.arg);e="executing";var k=c(a,b,d);if("normal"===k.type){if(e=d.done?"completed":"suspendedYield",k.arg===v)continue;return{value:k.arg,done:d.done}}"throw"===k.type&&(e="completed",d.method="throw",d.arg=k.arg)}}}(a,e,j),h}function c(a,b,c){try{return{type:"normal",arg:a.call(b,c)}}catch(a){return{type:"throw",arg:a}}}function d(){}function e(){}function f(){}function g(b){["next","throw","return"].forEach(function(c){a(b,c,function(a){return this._invoke(c,a)})})}function h(a,b){function d(e,f,g,h){var i=c(a[e],a,f);if("throw"!==i.type){var j=i.arg,k=j.value;return k&&"object"==_typeof(k)&&q.call(k,"__await")?b.resolve(k.__await).then(function(a){d("next",a,g,h)},function(a){d("throw",a,g,h)}):b.resolve(k).then(function(a){j.value=a,g(j)},function(a){return d("throw",a,g,h)})}h(i.arg)}var e;this._invoke=function(a,c){function f(){return new b(function(b,e){d(a,c,b,e)})}return e=e?e.then(f,f):f()}}function i(a,b){var d=a.iterator[b.method];if(void 0===d){if(b.delegate=null,"throw"===b.method){if(a.iterator["return"]&&(b.method="return",b.arg=void 0,i(a,b),"throw"===b.method))return v;b.method="throw",b.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var e=c(d,a.iterator,b.arg);if("throw"===e.type)return b.method="throw",b.arg=e.arg,b.delegate=null,v;var f=e.arg;return f?f.done?(b[a.resultName]=f.value,b.next=a.nextLoc,"return"!==b.method&&(b.method="next",b.arg=void 0),b.delegate=null,v):f:(b.method="throw",b.arg=new TypeError("iterator result is not an object"),b.delegate=null,v)}function j(a){var b={tryLoc:a[0]};1 in a&&(b.catchLoc=a[1]),2 in a&&(b.finallyLoc=a[2],b.afterLoc=a[3]),this.tryEntries.push(b)}function k(a){var b=a.completion||{};b.type="normal",delete b.arg,a.completion=b}function l(a){this.tryEntries=[{tryLoc:"root"}],a.forEach(j,this),this.reset(!0)}function m(a){if(a){var b=a[s];if(b)return b.call(a);if("function"==typeof a.next)return a;if(!isNaN(a.length)){var c=-1,d=function b(){for(;++c<a.length;)if(q.call(a,c))return b.value=a[c],b.done=!1,b;return b.value=void 0,b.done=!0,b};return d.next=d}}return{next:n}}function n(){return{value:void 0,done:!0}}_regeneratorRuntime=function(){return o};var o={},p=Object.prototype,q=p.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},s=r.iterator||"@@iterator",t=r.asyncIterator||"@@asyncIterator",u=r.toStringTag||"@@toStringTag";try{a({},"")}catch(b){a=function(a,b,c){return a[b]=c}}o.wrap=b;var v={},w={};a(w,s,function(){return this});var x=Object.getPrototypeOf,y=x&&x(x(m([])));y&&y!==p&&q.call(y,s)&&(w=y);var z=f.prototype=d.prototype=Object.create(w);return e.prototype=f,a(z,"constructor",f),a(f,"constructor",e),e.displayName=a(f,u,"GeneratorFunction"),o.isGeneratorFunction=function(a){var b="function"==typeof a&&a.constructor;return!!b&&(b===e||"GeneratorFunction"===(b.displayName||b.name))},o.mark=function(b){return Object.setPrototypeOf?Object.setPrototypeOf(b,f):(b.__proto__=f,a(b,u,"GeneratorFunction")),b.prototype=Object.create(z),b},o.awrap=function(a){return{__await:a}},g(h.prototype),a(h.prototype,t,function(){return this}),o.AsyncIterator=h,o.async=function(a,c,d,e,f){void 0===f&&(f=Promise);var g=new h(b(a,c,d,e),f);return o.isGeneratorFunction(c)?g:g.next().then(function(a){return a.done?a.value:g.next()})},g(z),a(z,u,"Generator"),a(z,s,function(){return this}),a(z,"toString",function(){return"[object Generator]"}),o.keys=function(a){var b=[];for(var c in a)b.push(c);return b.reverse(),function c(){for(;b.length;){var d=b.pop();if(d in a)return c.value=d,c.done=!1,c}return c.done=!0,c}},o.values=m,l.prototype={constructor:l,reset:function reset(a){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!a)for(var b in this)"t"===b.charAt(0)&&q.call(this,b)&&!isNaN(+b.slice(1))&&(this[b]=void 0)},stop:function stop(){this.done=!0;var a=this.tryEntries[0].completion;if("throw"===a.type)throw a.arg;return this.rval},dispatchException:function dispatchException(a){function b(b,d){return f.type="throw",f.arg=a,c.next=b,d&&(c.method="next",c.arg=void 0),!!d}if(this.done)throw a;for(var c=this,d=this.tryEntries.length-1;0<=d;--d){var e=this.tryEntries[d],f=e.completion;if("root"===e.tryLoc)return b("end");if(e.tryLoc<=this.prev){var g=q.call(e,"catchLoc"),h=q.call(e,"finallyLoc");if(g&&h){if(this.prev<e.catchLoc)return b(e.catchLoc,!0);if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(!g){if(!h)throw new Error("try statement without catch or finally");if(this.prev<e.finallyLoc)return b(e.finallyLoc)}else if(this.prev<e.catchLoc)return b(e.catchLoc,!0)}}},abrupt:function abrupt(a,b){for(var c,d=this.tryEntries.length-1;0<=d;--d)if(c=this.tryEntries[d],c.tryLoc<=this.prev&&q.call(c,"finallyLoc")&&this.prev<c.finallyLoc){var e=c;break}e&&("break"===a||"continue"===a)&&e.tryLoc<=b&&b<=e.finallyLoc&&(e=null);var f=e?e.completion:{};return f.type=a,f.arg=b,e?(this.method="next",this.next=e.finallyLoc,v):this.complete(f)},complete:function complete(a,b){if("throw"===a.type)throw a.arg;return"break"===a.type||"continue"===a.type?this.next=a.arg:"return"===a.type?(this.rval=this.arg=a.arg,this.method="return",this.next="end"):"normal"===a.type&&b&&(this.next=b),v},finish:function finish(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.finallyLoc===a)return this.complete(b.completion,b.afterLoc),k(b),v},catch:function _catch(a){for(var b,c=this.tryEntries.length-1;0<=c;--c)if(b=this.tryEntries[c],b.tryLoc===a){var d=b.completion;if("throw"===d.type){var e=d.arg;k(b)}return e}throw new Error("illegal catch attempt")},delegateYield:function delegateYield(a,b,c){return this.delegate={iterator:m(a),resultName:b,nextLoc:c},"next"===this.method&&(this.arg=void 0),v}},o}function asyncGeneratorStep(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){return void c(a)}h.done?b(i):Promise.resolve(i).then(d,e)}function _asyncToGenerator(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){asyncGeneratorStep(h,d,e,f,g,"next",a)}function g(a){asyncGeneratorStep(h,d,e,f,g,"throw",a)}var h=a.apply(b,c);f(void 0)})}}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 _classPrivateMethodInitSpec(a,b){_checkPrivateRedeclaration(a,b),b.add(a)}function _checkPrivateRedeclaration(a,b){if(b.has(a))throw new TypeError("Cannot initialize the same private elements twice on an object")}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}function _classPrivateMethodGet(a,b,c){if(!b.has(a))throw new TypeError("attempted to get private field on non-instance");return c}var _setSubProperty=/*#__PURE__*/new WeakSet,_getSubProperty=/*#__PURE__*/new WeakSet,_setProperty=/*#__PURE__*/new WeakSet,_getProperty=/*#__PURE__*/new WeakSet,SimpleObject=/*#__PURE__*/function(a){//changes
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { gcache, gfunc } from "@azrico/global";
|
|
2
|
+
import { object_clone, object_equals, object_isEmpty, object_isTrue, object_removeKeys, } 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
|
+
static __use_delta = true;
|
|
11
|
+
__originalobject;
|
|
12
|
+
//changes
|
|
13
|
+
__delta;
|
|
14
|
+
__changed = false;
|
|
15
|
+
__lastChanged = "";
|
|
16
|
+
__is_readonly;
|
|
17
|
+
__is_edit_mode;
|
|
18
|
+
//errors
|
|
19
|
+
__errors;
|
|
20
|
+
__showErrors;
|
|
21
|
+
//save results
|
|
22
|
+
__saveResultObject;
|
|
23
|
+
__saveResultIsSuccess;
|
|
24
|
+
__saveResultType;
|
|
25
|
+
//sequence key
|
|
26
|
+
_index_key;
|
|
27
|
+
//sequence value , used to index the object with increasing numbers
|
|
28
|
+
_index_value;
|
|
29
|
+
constructor(isEditMode = true, is_readonly = false) {
|
|
30
|
+
super();
|
|
31
|
+
this.__delta = {};
|
|
32
|
+
this.__is_edit_mode = isEditMode;
|
|
33
|
+
this.__is_readonly = is_readonly || !this.__is_edit_mode;
|
|
34
|
+
this.clear_changes();
|
|
35
|
+
}
|
|
36
|
+
#setSubProperty(key, subkey, value) {
|
|
37
|
+
let subobj = this.#getProperty(key);
|
|
38
|
+
if (typeof subobj !== "object")
|
|
39
|
+
subobj = {};
|
|
40
|
+
subobj[subkey] = value;
|
|
41
|
+
this.#setProperty(key, subobj);
|
|
42
|
+
}
|
|
43
|
+
#getSubProperty(key, subkey, original = false) {
|
|
44
|
+
let subobj = this.#getProperty(key, original);
|
|
45
|
+
if (typeof subobj !== "object")
|
|
46
|
+
return undefined;
|
|
47
|
+
return subobj[subkey];
|
|
48
|
+
}
|
|
49
|
+
#setProperty(key, value) {
|
|
50
|
+
if (this.__use_delta)
|
|
51
|
+
this.__delta[key] = value;
|
|
52
|
+
else
|
|
53
|
+
this[key] = value;
|
|
54
|
+
}
|
|
55
|
+
#getProperty(key, original = false) {
|
|
56
|
+
//check if we should use delta version or normal version for this property
|
|
57
|
+
if (this.__use_delta && !original && key in this.__delta)
|
|
58
|
+
return this.__delta[key];
|
|
59
|
+
return this[key];
|
|
60
|
+
}
|
|
61
|
+
/* -------------------------------------------------------------------------- */
|
|
62
|
+
/* Load */
|
|
63
|
+
/* -------------------------------------------------------------------------- */
|
|
64
|
+
loadValues(initObject, fullUpdate = false) {
|
|
65
|
+
return SimpleObject.loadValues(this, initObject, fullUpdate);
|
|
66
|
+
}
|
|
67
|
+
static loadValues(obj, initObject, fullUpdate = false) {
|
|
68
|
+
SimpleObject.factoryClear(obj);
|
|
69
|
+
if (initObject == null) {
|
|
70
|
+
for (const key in obj) {
|
|
71
|
+
if (key.startsWith("__"))
|
|
72
|
+
continue;
|
|
73
|
+
delete obj[key];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (typeof initObject == "object") {
|
|
77
|
+
for (const key in initObject) {
|
|
78
|
+
if (key.startsWith("__"))
|
|
79
|
+
continue;
|
|
80
|
+
if (fullUpdate ||
|
|
81
|
+
Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
82
|
+
obj[key] = initObject[key];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
/* -------------------------------------------------------------------------- */
|
|
89
|
+
static getValue(o, key, original = false) {
|
|
90
|
+
if (o instanceof SimpleObject && typeof o.getValue === "function")
|
|
91
|
+
return o.getValue(key, original);
|
|
92
|
+
if (!original && o.__delta[key])
|
|
93
|
+
return o.__delta[key];
|
|
94
|
+
return o[key];
|
|
95
|
+
}
|
|
96
|
+
getValue(key, original = false) {
|
|
97
|
+
return this.#getProperty(key, original);
|
|
98
|
+
}
|
|
99
|
+
get(key, original = false) {
|
|
100
|
+
return this.#getProperty(key, original);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
7
103
|
* set value
|
|
8
104
|
* @param key
|
|
9
105
|
* @param value
|
|
10
106
|
* @return true if changed , false otherwise
|
|
11
|
-
*/
|
|
107
|
+
*/
|
|
108
|
+
setValue(key, value, sendChanges = "auto") {
|
|
109
|
+
if (!this.canModify)
|
|
110
|
+
throw Error("Cant use setValue when on readonly mode");
|
|
111
|
+
let isValueChanged = false;
|
|
112
|
+
try {
|
|
113
|
+
const oldval = this.getValue(key);
|
|
114
|
+
if (!object_equals(oldval, value, { ignoredStartingWords: "_" })) {
|
|
115
|
+
this.#setProperty(key, value);
|
|
116
|
+
isValueChanged = true;
|
|
117
|
+
}
|
|
118
|
+
else
|
|
119
|
+
isValueChanged = false;
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
if (sendChanges === true ||
|
|
123
|
+
(sendChanges === "auto" && isValueChanged)) {
|
|
124
|
+
this.notifyChanged("", key, value);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return isValueChanged;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
12
130
|
* set key of subObject
|
|
13
131
|
* @param key
|
|
14
132
|
* @param value
|
|
15
133
|
* @param subvalue
|
|
16
134
|
* @return true if changed , false otherwise
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
|
|
135
|
+
*/
|
|
136
|
+
setSubValue(key, objKey, subvalue, sendChanges = "auto") {
|
|
137
|
+
if (!this.canModify)
|
|
138
|
+
throw Error("Cant use setValue when on readonly mode");
|
|
139
|
+
let isValueChanged = false;
|
|
140
|
+
try {
|
|
141
|
+
//if accessing subval of an object, make sure the object is not null
|
|
142
|
+
const subval = this.#getSubProperty(key, objKey);
|
|
143
|
+
if (!object_equals(subval, subvalue, {
|
|
144
|
+
ignoredStartingWords: "_",
|
|
145
|
+
})) {
|
|
146
|
+
this.#setSubProperty(key, objKey, subvalue);
|
|
147
|
+
isValueChanged = true;
|
|
148
|
+
}
|
|
149
|
+
else
|
|
150
|
+
isValueChanged = false;
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
if (sendChanges === true ||
|
|
154
|
+
(sendChanges === "auto" && isValueChanged)) {
|
|
155
|
+
this.notifyChanged("", key, objKey, subvalue);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return isValueChanged;
|
|
159
|
+
}
|
|
160
|
+
//Push value on array
|
|
161
|
+
pushValue(key, value, sendChanges = "auto") {
|
|
162
|
+
if (value == null)
|
|
163
|
+
return null;
|
|
164
|
+
if (!this.canModify)
|
|
165
|
+
throw Error("Cant use pushValue when on readonly mode");
|
|
166
|
+
if (this.#getProperty(key) == null) {
|
|
167
|
+
this.#setProperty(key, this.getValue(key));
|
|
168
|
+
}
|
|
169
|
+
if (!Array.isArray(this.#getProperty(key)))
|
|
170
|
+
throw Error("Cant use pushValue on non Array keys");
|
|
171
|
+
let tempArray = this.#getProperty(key);
|
|
172
|
+
tempArray.push(value);
|
|
173
|
+
this.#setProperty(key, tempArray);
|
|
174
|
+
if (sendChanges === true || sendChanges === "auto") {
|
|
175
|
+
this.notifyChanged("", key, value);
|
|
176
|
+
}
|
|
177
|
+
return tempArray;
|
|
178
|
+
}
|
|
179
|
+
async notifyChanged(type, key, value, subvalue) {
|
|
180
|
+
if (value === undefined)
|
|
181
|
+
value = this.getValue(key);
|
|
182
|
+
this.__changed = true;
|
|
183
|
+
let lc = "";
|
|
184
|
+
if (type)
|
|
185
|
+
lc += `[${type}] `;
|
|
186
|
+
if (subvalue) {
|
|
187
|
+
lc += `${key}->${value} = ${subvalue}`;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
lc += `${key} = ${value}`;
|
|
191
|
+
}
|
|
192
|
+
this.__lastChanged = lc;
|
|
193
|
+
/* --------------------------- notify the manager --------------------------- */
|
|
194
|
+
const manager_class = this.get_managerClass();
|
|
195
|
+
if (manager_class instanceof ObjectManager) {
|
|
196
|
+
await manager_class.notifyChanged(type, key, value, subvalue);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
static canChangeKey(object, key) {
|
|
200
|
+
if (!object || !key)
|
|
201
|
+
return false;
|
|
202
|
+
const __changeables = object.constructor["__changeables"];
|
|
203
|
+
const __unchangeables = object.constructor["__unchangeables"];
|
|
204
|
+
if (__changeables != null && !__changeables.includes(key))
|
|
205
|
+
return false;
|
|
206
|
+
if (__unchangeables != null && __unchangeables.includes(key))
|
|
207
|
+
return false;
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
/* -------------------------------------------------------------------------- */
|
|
211
|
+
/* Changes */
|
|
212
|
+
/* -------------------------------------------------------------------------- */
|
|
213
|
+
/**
|
|
20
214
|
* Apply the changes and empty the delta object
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
|
|
215
|
+
*/
|
|
216
|
+
apply_changes() {
|
|
217
|
+
if (!this.__use_delta) {
|
|
218
|
+
console.warn("you cant use apply_changes if you are not using Delta mode");
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
if (!this.isChanged) {
|
|
222
|
+
console.warn("No changes to apply");
|
|
223
|
+
}
|
|
224
|
+
for (const key in this.__delta) {
|
|
225
|
+
const nv = this.__delta[key];
|
|
226
|
+
this[key] = nv;
|
|
227
|
+
}
|
|
228
|
+
this.clear_changes();
|
|
229
|
+
}
|
|
230
|
+
clear_changes() {
|
|
231
|
+
this.__delta = {};
|
|
232
|
+
this.__changed = false;
|
|
233
|
+
}
|
|
234
|
+
static setReadonly(objs, readonlyval = true) {
|
|
235
|
+
let checkObjs = [];
|
|
236
|
+
if (!objs)
|
|
237
|
+
objs = [];
|
|
238
|
+
if (!Array.isArray(objs))
|
|
239
|
+
checkObjs = [objs];
|
|
240
|
+
else
|
|
241
|
+
checkObjs = objs;
|
|
242
|
+
for (let index = 0; index < checkObjs.length; index++) {
|
|
243
|
+
const element = checkObjs[index];
|
|
244
|
+
element.__is_readonly = readonlyval;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/* -------------------------------------------------------------------------- */
|
|
248
|
+
/* Get/Check */
|
|
249
|
+
/* -------------------------------------------------------------------------- */
|
|
250
|
+
get isChanged() {
|
|
251
|
+
return this.__use_delta ? !object_isEmpty(this.__delta) : false;
|
|
252
|
+
}
|
|
253
|
+
get isDeleted() {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
get d() {
|
|
257
|
+
return this.__use_delta ? this.__delta : this;
|
|
258
|
+
}
|
|
259
|
+
/* all updated properties */
|
|
260
|
+
get allproperties() {
|
|
261
|
+
const result = { ...this, ...this.__delta };
|
|
262
|
+
result.__delta = {};
|
|
263
|
+
return result;
|
|
264
|
+
}
|
|
265
|
+
get canModify() {
|
|
266
|
+
return !this.__is_readonly && this.isEditMode;
|
|
267
|
+
}
|
|
268
|
+
get isEditMode() {
|
|
269
|
+
return this.__is_edit_mode !== false;
|
|
270
|
+
}
|
|
271
|
+
get __cache_name() {
|
|
272
|
+
return this.constructor["__cache_name"];
|
|
273
|
+
}
|
|
274
|
+
get __changeables() {
|
|
275
|
+
return this.constructor["__changeables"];
|
|
276
|
+
}
|
|
277
|
+
get __unchangeables() {
|
|
278
|
+
return this.constructor["__unchangeables"];
|
|
279
|
+
}
|
|
280
|
+
get __use_delta() {
|
|
281
|
+
return this.constructor["__use_delta"];
|
|
282
|
+
}
|
|
283
|
+
get isSaved() {
|
|
284
|
+
return Boolean(this.__saveResultIsSuccess ?? false);
|
|
285
|
+
}
|
|
286
|
+
get isNew() {
|
|
287
|
+
const mid = this.getID();
|
|
288
|
+
if (!mid)
|
|
289
|
+
return true;
|
|
290
|
+
if (typeof mid === "string")
|
|
291
|
+
return mid === "" || mid === "-1";
|
|
292
|
+
if (typeof mid === "number")
|
|
293
|
+
return mid === -1;
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
get manager() {
|
|
297
|
+
return this.get_managerClass();
|
|
298
|
+
}
|
|
299
|
+
get_lastChanged() {
|
|
300
|
+
return !this.__lastChanged;
|
|
301
|
+
}
|
|
302
|
+
get_managerClass = () => null;
|
|
303
|
+
static isDeleted(obj) {
|
|
304
|
+
if (obj instanceof SimpleObject)
|
|
305
|
+
return obj.isDeleted === true;
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
getIndexOrID() {
|
|
309
|
+
return SimpleObject.getIndexOrID(this);
|
|
310
|
+
}
|
|
311
|
+
getIndex() {
|
|
312
|
+
return SimpleObject.getIndex(this);
|
|
313
|
+
}
|
|
314
|
+
getID() {
|
|
315
|
+
return SimpleObject.getID(this);
|
|
316
|
+
}
|
|
317
|
+
static getIndexOrID(o) {
|
|
318
|
+
let res = SimpleObject.getIndex(o);
|
|
319
|
+
if (!res || res == -1)
|
|
320
|
+
res = SimpleObject.getID(o);
|
|
321
|
+
return res || -1;
|
|
322
|
+
}
|
|
323
|
+
static getIndex(o) {
|
|
324
|
+
if (o == null || o == undefined)
|
|
325
|
+
return -1;
|
|
326
|
+
switch (typeof o) {
|
|
327
|
+
case "object":
|
|
328
|
+
return o._index_value || -1;
|
|
329
|
+
case "number":
|
|
330
|
+
case "string":
|
|
331
|
+
return Number(o);
|
|
332
|
+
}
|
|
333
|
+
return -1;
|
|
334
|
+
}
|
|
335
|
+
static getID(o) {
|
|
336
|
+
if (o == null || o == undefined)
|
|
337
|
+
return "";
|
|
338
|
+
switch (typeof o) {
|
|
339
|
+
case "object":
|
|
340
|
+
return o.__data_id || o._id || o.id || o.ID || "";
|
|
341
|
+
case "string":
|
|
342
|
+
case "number":
|
|
343
|
+
return String(o);
|
|
344
|
+
}
|
|
345
|
+
return "";
|
|
346
|
+
}
|
|
347
|
+
/* -------------------------------------------------------------------------- */
|
|
348
|
+
/* Original */
|
|
349
|
+
/* -------------------------------------------------------------------------- */
|
|
350
|
+
getOriginal() {
|
|
351
|
+
return this.__originalobject || undefined;
|
|
352
|
+
}
|
|
353
|
+
setOriginal(toObj = 0) {
|
|
354
|
+
if (toObj === null) {
|
|
355
|
+
delete this.__originalobject;
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (!this.isEditMode) {
|
|
359
|
+
console.warn("Cant use setOriginal on non original objects ! this will create an infinite loop");
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (this.__is_readonly) {
|
|
363
|
+
delete this.__originalobject;
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (toObj === 0) {
|
|
367
|
+
toObj = this;
|
|
368
|
+
}
|
|
369
|
+
const cloneObject = object_clone(toObj);
|
|
370
|
+
//this.__originalobject = cloneObject, ["__"];
|
|
371
|
+
this.__originalobject = object_removeKeys(cloneObject, ["__"]);
|
|
372
|
+
return this;
|
|
373
|
+
}
|
|
374
|
+
static setOriginal(obj, toObj = 0) {
|
|
375
|
+
if (!(obj instanceof SimpleObject))
|
|
376
|
+
return undefined;
|
|
377
|
+
return obj.setOriginal(toObj);
|
|
378
|
+
}
|
|
379
|
+
static getOriginal(obj) {
|
|
380
|
+
if (obj instanceof SimpleObject)
|
|
381
|
+
return obj.getOriginal();
|
|
382
|
+
return obj?.__originalobject;
|
|
383
|
+
}
|
|
384
|
+
/* -------------------------------------------------------------------------- */
|
|
385
|
+
/* Errors */
|
|
386
|
+
/* -------------------------------------------------------------------------- */
|
|
387
|
+
static addError(o, title, message, sendToRegister = "auto") {
|
|
388
|
+
const errStr = combine_if_not_blank(title, " : ", message);
|
|
389
|
+
if (o) {
|
|
390
|
+
if (!Array.isArray(o.__errors))
|
|
391
|
+
o.__errors = [];
|
|
392
|
+
o.__errors.push(errStr);
|
|
393
|
+
if (o.__showErrors === false)
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
else if (sendToRegister === "auto")
|
|
397
|
+
sendToRegister = true;
|
|
398
|
+
if (sendToRegister === true) {
|
|
399
|
+
console.error("[REGISTER-ERROR]", errStr);
|
|
400
|
+
gfunc.run_action("add_error", {
|
|
401
|
+
title: title,
|
|
402
|
+
message: message,
|
|
403
|
+
type: "error",
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
console.error("[ERROR]", errStr);
|
|
408
|
+
}
|
|
409
|
+
return o?.__errors || [];
|
|
410
|
+
}
|
|
411
|
+
static getErrors(o) {
|
|
412
|
+
return o?.__errors || [];
|
|
413
|
+
}
|
|
414
|
+
static clearErrors(o) {
|
|
415
|
+
if (o)
|
|
416
|
+
o.__errors = [];
|
|
417
|
+
return true;
|
|
418
|
+
}
|
|
419
|
+
/* -------------------------------------------------------------------------- */
|
|
420
|
+
/* Save Results */
|
|
421
|
+
/* -------------------------------------------------------------------------- */
|
|
422
|
+
static isResultValid(result_object) {
|
|
423
|
+
if (object_isEmpty(result_object))
|
|
424
|
+
return false;
|
|
425
|
+
switch (typeof result_object) {
|
|
426
|
+
case "string":
|
|
427
|
+
try {
|
|
428
|
+
result_object = JSON.parse(result_object);
|
|
429
|
+
return this.isResultValid(result_object);
|
|
430
|
+
}
|
|
431
|
+
catch {
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
case "object":
|
|
435
|
+
if (object_isTrue(result_object.acknowledged)) {
|
|
436
|
+
return true;
|
|
437
|
+
}
|
|
438
|
+
if (result_object.expected && result_object.result) {
|
|
439
|
+
return object_equals(result_object.expected, result_object.result);
|
|
440
|
+
}
|
|
441
|
+
return Number(this.getID(result_object)) >= 0;
|
|
442
|
+
case "number":
|
|
443
|
+
return result_object > 0;
|
|
444
|
+
case "boolean":
|
|
445
|
+
return result_object === true;
|
|
446
|
+
}
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
setSaveResult(toObj, type) {
|
|
450
|
+
this.__saveResultType = type;
|
|
451
|
+
if (toObj === true) {
|
|
452
|
+
}
|
|
453
|
+
else if (toObj === false) {
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
if (toObj) {
|
|
457
|
+
this.setSaveResultObject(toObj);
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
this.setSaveResultObject(null);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
this.__saveResultIsSuccess =
|
|
464
|
+
(type === "normal" || type === "nothing") && toObj != null;
|
|
465
|
+
}
|
|
466
|
+
setSaveResultObject(toObj) {
|
|
467
|
+
this.__saveResultObject = toObj;
|
|
468
|
+
}
|
|
469
|
+
getSaveResult() {
|
|
470
|
+
if (this.isSaved)
|
|
471
|
+
return this.__saveResultObject;
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
/* -------------------------------------------------------------------------- */
|
|
475
|
+
/* statics */
|
|
476
|
+
/* -------------------------------------------------------------------------- */
|
|
477
|
+
static factoryClear(obj) {
|
|
478
|
+
obj.clear_changes();
|
|
479
|
+
SimpleObject.clearErrors(obj);
|
|
480
|
+
}
|
|
481
|
+
static delete_cache() {
|
|
482
|
+
if (!this.__cache_name)
|
|
483
|
+
return;
|
|
484
|
+
gcache.delete_cache(this.__cache_name);
|
|
485
|
+
}
|
|
486
|
+
static get_canModify(o) {
|
|
487
|
+
if (o instanceof SimpleObject)
|
|
488
|
+
return o.canModify;
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
/* -------------------------------------------------------------------------- */
|
|
492
|
+
static equals(a, b) {
|
|
493
|
+
console.warn("Dont use the default equals method !");
|
|
494
|
+
if (a == undefined || b == undefined)
|
|
495
|
+
return false;
|
|
496
|
+
return a === b;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
export default SimpleObject;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azrico/webobject",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.18",
|
|
4
4
|
"description": "Stateful object",
|
|
5
5
|
"author": "Azrideus",
|
|
6
6
|
"license": "MIT",
|
|
@@ -61,18 +61,18 @@
|
|
|
61
61
|
"dist"
|
|
62
62
|
],
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"
|
|
65
|
-
"@azrico/
|
|
66
|
-
"@azrico/
|
|
67
|
-
"@azrico/
|
|
68
|
-
"@azrico/
|
|
69
|
-
"
|
|
64
|
+
"@azrico/event": "*",
|
|
65
|
+
"@azrico/global": "*",
|
|
66
|
+
"@azrico/object": "*",
|
|
67
|
+
"@azrico/string": "*",
|
|
68
|
+
"@azrico/uid": "^*",
|
|
69
|
+
"lodash.clonedeep": "*"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"@azrico/event": "^1
|
|
73
|
-
"@azrico/global": "^1
|
|
74
|
-
"@azrico/object": "^1
|
|
75
|
-
"@azrico/string": "^1
|
|
76
|
-
"@azrico/uid": "^1
|
|
72
|
+
"@azrico/event": "^1",
|
|
73
|
+
"@azrico/global": "^1",
|
|
74
|
+
"@azrico/object": "^1",
|
|
75
|
+
"@azrico/string": "^1",
|
|
76
|
+
"@azrico/uid": "^1"
|
|
77
77
|
}
|
|
78
78
|
}
|