@azrico/webobject 1.4.179 → 1.4.182

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,421 +1,588 @@
1
- import { array_first, 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 } from "@azrico/string";
3
- import { gcache, gfunc } from "@azrico/global";
4
- import { MapableObject } from "./MapableObject";
5
- import ObjectManager from "./ObjectManager";
6
- export class SimpleObject extends MapableObject {
7
- static __db_name = "";
8
- static __object_name = "";
9
- //these properties should not be changed and are usually set by server
10
- static __unchangeables;
11
- //these properties can be directly changed and are included in deltaObject
12
- static __changeables;
13
- //these properties can only be changed by the _creator
14
- static __protectedProperties;
15
- //these properties will be evaluated when needed
16
- static __functionProperties;
17
- //apply these string formats
18
- static __formatList;
19
- //these properties usually are not directly changed and sometimes are included in deltaObject
20
- static __extraProperties;
21
- //types for children can be used when importing children
22
- static __childTypes;
23
- //these fields are used when importing
24
- static __importColumns;
25
- //these fields can be used when importing and checking if an item exists
26
- static __checkProperties;
27
- static __use_delta = true;
28
- _static() {
29
- return this.constructor;
30
- }
31
- //an exact copy of this object is stored here
32
- __originalobject;
33
- //changes are saved here
34
- __delta = {};
35
- __has_delta = false;
36
- //the last thing that was changed on this object
37
- __lastChanged;
38
- //is the object read only
39
- __is_readonly;
40
- //should we throw errors when encountering an error?
41
- //is TRUE by default
42
- __throw_errors;
43
- //is this object marked as deleted?
44
- _deleted;
45
- _archived;
46
- _deletedBy;
47
- //is this object locked?
48
- _locked;
49
- //errors
50
- __errors;
51
- __showErrors;
52
- //refresh func
53
- __refresh_functions;
54
- //save results
55
- __saveResultObject;
56
- __saveResultIsSuccess = true;
57
- __saveResultType;
58
- _id = "";
59
- //sequence key (usually managed by the database)
60
- _index_key = "";
61
- //sequence value , used to index the object with increasing numbers
62
- _index_value = -1;
63
- constructor(is_readonly = false) {
64
- super();
65
- this.__is_readonly = is_readonly;
66
- if (this._id)
67
- this.__table_id = this._id;
68
- this.clear_changes();
69
- }
70
- #setDefaultProperty(key, value) {
71
- //@ts-ignore
72
- if (this[key] == undefined || this[key] === "" || this[key] === 0) {
73
- this[key] = value;
74
- return true;
75
- }
76
- return false;
77
- }
78
- #setDefaultPropertyNow(key) {
79
- //@ts-ignore
80
- if (this[key] == undefined || this[key] === "" || this[key] === 0) {
81
- this[key] = new Date();
82
- return true;
83
- }
84
- return false;
85
- }
86
- /* -------------------------------------------------------------------------- */
87
- /* getProperty */
88
- /* -------------------------------------------------------------------------- */
89
- #getProperty(key, original = false) {
90
- //check if we should treat the prop as a function
91
- const is_fnc = this.constructor["__functionProperties"] != null &&
92
- this.constructor["__functionProperties"].includes(key);
93
- //check if we should use delta version or normal version for this property
94
- const use_orig = original || !this.__use_delta || !(key in this.__delta);
95
- if (is_fnc) {
96
- if (use_orig)
97
- return fn_eval_this(this, this[key]);
98
- return fn_eval_this(this, this.__delta[key]);
99
- }
100
- if (use_orig)
101
- return this[key];
102
- return this.__delta[key];
103
- }
104
- getValue = this.#getProperty;
105
- get = this.#getProperty;
106
- static getValue(o, key, original = false) {
107
- if (o == null)
108
- return undefined;
109
- if (o instanceof SimpleObject && typeof o.getValue === "function")
110
- return o.getValue(key, original);
111
- if (!original &&
112
- o.__use_delta != false &&
113
- typeof o.__delta === "object" &&
114
- key in o.__delta) {
115
- //@ts-ignore
116
- return o.__delta[key];
117
- }
118
- return o[key];
119
- }
120
- /* -------------------------------------------------------------------------- */
121
- /* setValue */
122
- /* -------------------------------------------------------------------------- */
123
- #setSubProperty(key, subkey, value) {
124
- let subobj = this.#getProperty(key);
125
- //@ts-ignore
126
- if (subobj == null || typeof subobj !== "object")
127
- subobj = {};
128
- else
129
- subobj = { ...subobj }; //make a clone so we dont update the original
130
- subobj[subkey] = value;
131
- this.#setProperty(key, subobj);
132
- }
133
- #getSubProperty(key, subkey, original = false) {
134
- let subobj = this.#getProperty(key, original);
135
- if (subobj == null || typeof subobj !== "object")
136
- return undefined;
137
- return subobj[subkey];
138
- }
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = exports.SimpleObject = void 0;
7
+ var _object = require("@azrico/object");
8
+ var _string = require("@azrico/string");
9
+ var _global = require("@azrico/global");
10
+ var _MapableObject2 = require("./MapableObject");
11
+ var _ObjectManager = _interopRequireDefault(require("./ObjectManager"));
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
14
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
15
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
16
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
17
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
18
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
19
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
20
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
21
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
22
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
23
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
24
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
25
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
26
+ function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
27
+ function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
28
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
29
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
30
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
31
+ function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
32
+ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
33
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
34
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
35
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
36
+ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
37
+ var _SimpleObject_brand = /*#__PURE__*/new WeakSet();
38
+ var SimpleObject = exports.SimpleObject = /*#__PURE__*/function (_MapableObject) {
39
+ function SimpleObject() {
40
+ var _this;
41
+ var is_readonly = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
42
+ _classCallCheck(this, SimpleObject);
43
+ _this = _callSuper(this, SimpleObject);
44
+ _classPrivateMethodInitSpec(_this, _SimpleObject_brand);
45
+ //an exact copy of this object is stored here
46
+ _defineProperty(_this, "__originalobject", void 0);
47
+ //changes are saved here
48
+ _defineProperty(_this, "__delta", {});
49
+ _defineProperty(_this, "__has_delta", false);
50
+ //the last thing that was changed on this object
51
+ _defineProperty(_this, "__lastChanged", void 0);
52
+ //is the object read only
53
+ _defineProperty(_this, "__is_readonly", void 0);
54
+ //should we throw errors when encountering an error?
55
+ //is TRUE by default
56
+ _defineProperty(_this, "__throw_errors", void 0);
57
+ //is this object marked as deleted?
58
+ _defineProperty(_this, "_deleted", void 0);
59
+ _defineProperty(_this, "_archived", void 0);
60
+ _defineProperty(_this, "_deletedBy", void 0);
61
+ //is this object locked?
62
+ _defineProperty(_this, "_locked", void 0);
63
+ //errors
64
+ _defineProperty(_this, "__errors", void 0);
65
+ _defineProperty(_this, "__showErrors", void 0);
66
+ //refresh func
67
+ _defineProperty(_this, "__refresh_functions", void 0);
68
+ //save results
69
+ _defineProperty(_this, "__saveResultObject", void 0);
70
+ _defineProperty(_this, "__saveResultIsSuccess", true);
71
+ _defineProperty(_this, "__saveResultType", void 0);
72
+ _defineProperty(_this, "_id", "");
73
+ //sequence key (usually managed by the database)
74
+ _defineProperty(_this, "_index_key", "");
75
+ //sequence value , used to index the object with increasing numbers
76
+ _defineProperty(_this, "_index_value", -1);
77
+ _defineProperty(_this, "getValue", _assertClassBrand(_SimpleObject_brand, _this, _getProperty));
78
+ _defineProperty(_this, "get", _assertClassBrand(_SimpleObject_brand, _this, _getProperty));
139
79
  /**
140
- * update a property on this object by updating the `__delta`
141
- * or updating the object property directly depending on `__use_delta` setting
142
- *
80
+ * set value and run the event handlers if needed
81
+ * @return true if changed , false otherwise
82
+ */
83
+ _defineProperty(_this, "set", _this.setValue);
84
+ /**
85
+ * set value of a key to now date time
143
86
  * @param key
144
87
  * @param value
145
- * @returns true if state of `__has_delta` is changed, false otherwise
146
- */
147
- #setProperty(key, value) {
148
- let checkIsChanged = false;
149
- if (this.__use_delta) {
150
- if (this[key] === value) {
151
- /**
152
- * we set something back to its original value
153
- * no need to save it as delta
154
- */
155
- delete this.__delta[key];
156
- checkIsChanged = true; //check if we should set __has_delta to false
157
- }
158
- else {
159
- this.__delta[key] = value; //always update delta to keep track of changes
160
- }
161
- }
162
- else {
163
- this[key] = value; //on noDelta mode update the object itself
164
- this.__delta[key] = value; //always update delta to keep track of changes
165
- }
166
- if (checkIsChanged || this.__has_delta === false) {
167
- /**
168
- * if any key exists in the `__delta` then the object is changed
169
- */
170
- let next__has_delta = false;
171
- for (const key in this.__delta) {
172
- next__has_delta = true;
173
- break;
174
- }
175
- let previous__has_delta = this.__has_delta;
176
- this.__has_delta = next__has_delta;
177
- /**
178
- * if the state of __has_delta is changed we return true
179
- * so we can run the needed events
180
- */
181
- return previous__has_delta != this.__has_delta;
182
- }
183
- return false;
184
- }
88
+ * @returns
89
+ */
90
+ _defineProperty(_this, "setDefaultValueNow", _assertClassBrand(_SimpleObject_brand, _this, _setDefaultPropertyNow));
185
91
  /**
186
- * set value and run the event handlers if needed
187
- * @return true if changed , false otherwise
188
- */
189
- async setValue(key, value, shouldNotify = "auto") {
190
- /* -------------------- check if modification is allowed -------------------- */
191
- if (!this.canModify) {
192
- if (this.__throw_errors !== false)
193
- throw Error(`Cant use setValue (${String(key)}) when on readonly mode`);
194
- else
195
- return false;
196
- }
197
- /* --------------------------- set if not changed --------------------------- */
198
- const old_value = this.getValue(key);
199
- const needsChange = !object_equals(old_value, value, {
200
- strictArrayCheck: true,
201
- ignoredStartingWords: "_",
202
- });
203
- if (needsChange) {
204
- const __has_delta_isChanged = this.#setProperty(key, value);
205
- /**
206
- * notify the manager when the state of `__has_delta` is changed
207
- * managers can use this to detect if they need to save
208
- */
209
- this.manager?.setOption("ischanged", this.isChanged);
210
- ////if (__has_delta_isChanged && this.manager) {
211
- //// this.manager.setOption("hasDelta", this.__has_delta);
212
- ////}
213
- }
214
- /* --------------------------------- notify --------------------------------- */
215
- if (shouldNotify === true || (shouldNotify === "auto" && needsChange)) {
216
- await this.notifyChanged("", key, value);
217
- }
218
- return needsChange;
219
- }
92
+ * set value of a key only if the value is empty (null,undef,"")
93
+ * @param key
94
+ * @param value
95
+ * @returns
96
+ */
97
+ _defineProperty(_this, "setDefault", _this.setDefaultValue);
98
+ _defineProperty(_this, "getSubValue", _assertClassBrand(_SimpleObject_brand, _this, _getSubProperty));
99
+ _defineProperty(_this, "get_managerClass", function () {
100
+ return undefined;
101
+ });
102
+ _this.__is_readonly = is_readonly;
103
+ if (_this._id) _this.__table_id = _this._id;
104
+ _this.clear_changes();
105
+ return _this;
106
+ }
107
+ _inherits(SimpleObject, _MapableObject);
108
+ return _createClass(SimpleObject, [{
109
+ key: "_static",
110
+ value: function _static() {
111
+ return this.constructor;
112
+ }
113
+ }, {
114
+ key: "setValue",
115
+ value: (
220
116
  /**
221
117
  * set value and run the event handlers if needed
222
118
  * @return true if changed , false otherwise
223
- */
224
- set = this.setValue;
225
- /* -------------------------------------------------------------------------- */
226
- /* Load */
227
- /* -------------------------------------------------------------------------- */
119
+ */
120
+ function () {
121
+ var _setValue = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(key, value) {
122
+ var shouldNotify,
123
+ old_value,
124
+ needsChange,
125
+ _this$manager,
126
+ __has_delta_isChanged,
127
+ _args = arguments;
128
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
129
+ while (1) switch (_context.prev = _context.next) {
130
+ case 0:
131
+ shouldNotify = _args.length > 2 && _args[2] !== undefined ? _args[2] : "auto";
132
+ if (this.canModify) {
133
+ _context.next = 7;
134
+ break;
135
+ }
136
+ if (!(this.__throw_errors !== false)) {
137
+ _context.next = 6;
138
+ break;
139
+ }
140
+ throw Error("Cant use setValue (".concat(String(key), ") when on readonly mode"));
141
+ case 6:
142
+ return _context.abrupt("return", false);
143
+ case 7:
144
+ /* --------------------------- set if not changed --------------------------- */
145
+ old_value = this.getValue(key);
146
+ needsChange = !(0, _object.object_equals)(old_value, value, {
147
+ strictArrayCheck: true,
148
+ ignoredStartingWords: "_"
149
+ });
150
+ if (needsChange) {
151
+ __has_delta_isChanged = _assertClassBrand(_SimpleObject_brand, this, _setProperty).call(this, key, value);
152
+ /**
153
+ * notify the manager when the state of `__has_delta` is changed
154
+ * managers can use this to detect if they need to save
155
+ */
156
+ (_this$manager = this.manager) === null || _this$manager === void 0 || _this$manager.setOption("ischanged", this.isChanged);
157
+ ////if (__has_delta_isChanged && this.manager) {
158
+ //// this.manager.setOption("hasDelta", this.__has_delta);
159
+ ////}
160
+ }
161
+ /* --------------------------------- notify --------------------------------- */
162
+ if (!(shouldNotify === true || shouldNotify === "auto" && needsChange)) {
163
+ _context.next = 13;
164
+ break;
165
+ }
166
+ _context.next = 13;
167
+ return this.notifyChanged("", key, value);
168
+ case 13:
169
+ return _context.abrupt("return", needsChange);
170
+ case 14:
171
+ case "end":
172
+ return _context.stop();
173
+ }
174
+ }, _callee, this);
175
+ }));
176
+ function setValue(_x, _x2) {
177
+ return _setValue.apply(this, arguments);
178
+ }
179
+ return setValue;
180
+ }())
181
+ }, {
182
+ key: "loadValues",
183
+ value: /* -------------------------------------------------------------------------- */
184
+ /* Load */
185
+ /* -------------------------------------------------------------------------- */
228
186
  /**
229
187
  * load all properties in `initObject` into the current object
230
188
  * @param initObject
231
189
  * @param fullUpdate
232
190
  * @returns
233
- */
234
- loadValues(initObject, fullUpdate = false) {
235
- return SimpleObject.loadValues(this, initObject, fullUpdate);
236
- }
237
- static loadValues(obj, initObject, fullUpdate = false) {
238
- if (typeof initObject === "number" || typeof initObject === "string") {
239
- initObject = { _id: initObject };
240
- }
241
- SimpleObject.factoryClear(obj);
242
- if (initObject == null) {
243
- for (const key in obj) {
244
- if (typeof obj[key] === "function")
245
- continue;
246
- if (key.startsWith("__"))
247
- continue;
248
- delete obj[key];
249
- }
250
- }
251
- else if (typeof initObject == "object") {
252
- for (const key in initObject) {
253
- if (key.startsWith("__"))
254
- continue;
255
- if (fullUpdate ||
256
- Object.prototype.hasOwnProperty.call(obj, key)) {
257
- obj[key] = initObject[key];
258
- }
259
- }
260
- }
261
- return true;
262
- }
263
- async setTemp(key, value, sendChanges = false) {
264
- let isValueChanged = await super.setTemp(key, value);
265
- if (sendChanges === true ||
266
- (sendChanges === "auto" && isValueChanged)) {
267
- await this.notifyChanged("temp", key, value);
268
- }
269
- return isValueChanged;
270
- }
271
- /* ---------------------------- refresh functions --------------------------- */
272
- async clearRefreshFunction() {
273
- this.__refresh_functions = undefined;
274
- }
275
- async setRefreshFunction(new_fnc) {
276
- return await this.addRefreshFunction(new_fnc);
277
- }
278
- async addRefreshFunction(key, new_fnc) {
279
- if (typeof key === "function") {
280
- new_fnc = key;
281
- key = "main";
282
- }
283
- key = key || "main";
284
- let oldFunctions = this.__refresh_functions;
285
- if (oldFunctions == null || typeof oldFunctions !== "object")
286
- oldFunctions = {};
287
- oldFunctions[key] = new_fnc;
288
- this.__refresh_functions = oldFunctions;
289
- }
290
- async runRefreshFunction(...params) {
291
- let oldFunctions = this.__refresh_functions;
292
- if (oldFunctions == null || typeof oldFunctions !== "object")
293
- return;
294
- const fncArray = Object.values(oldFunctions);
295
- for (let index = 0; index < fncArray.length; index++) {
296
- const fnc = fncArray[index];
297
- await fn_eval(fnc, ...params);
298
- }
299
- }
300
- /* -------------------------------------------------------------------------- */
191
+ */
192
+ function loadValues(initObject) {
193
+ var fullUpdate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
194
+ return SimpleObject.loadValues(this, initObject, fullUpdate);
195
+ }
196
+ }, {
197
+ key: "setTemp",
198
+ value: function () {
199
+ var _setTemp = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(key, value) {
200
+ var sendChanges,
201
+ isValueChanged,
202
+ _args2 = arguments;
203
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
204
+ while (1) switch (_context2.prev = _context2.next) {
205
+ case 0:
206
+ sendChanges = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : false;
207
+ _context2.next = 3;
208
+ return _get(_getPrototypeOf(SimpleObject.prototype), "setTemp", this).call(this, key, value);
209
+ case 3:
210
+ isValueChanged = _context2.sent;
211
+ if (!(sendChanges === true || sendChanges === "auto" && isValueChanged)) {
212
+ _context2.next = 7;
213
+ break;
214
+ }
215
+ _context2.next = 7;
216
+ return this.notifyChanged("temp", key, value);
217
+ case 7:
218
+ return _context2.abrupt("return", isValueChanged);
219
+ case 8:
220
+ case "end":
221
+ return _context2.stop();
222
+ }
223
+ }, _callee2, this);
224
+ }));
225
+ function setTemp(_x3, _x4) {
226
+ return _setTemp.apply(this, arguments);
227
+ }
228
+ return setTemp;
229
+ }() /* ---------------------------- refresh functions --------------------------- */
230
+ }, {
231
+ key: "clearRefreshFunction",
232
+ value: (function () {
233
+ var _clearRefreshFunction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
234
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
235
+ while (1) switch (_context3.prev = _context3.next) {
236
+ case 0:
237
+ this.__refresh_functions = undefined;
238
+ case 1:
239
+ case "end":
240
+ return _context3.stop();
241
+ }
242
+ }, _callee3, this);
243
+ }));
244
+ function clearRefreshFunction() {
245
+ return _clearRefreshFunction.apply(this, arguments);
246
+ }
247
+ return clearRefreshFunction;
248
+ }())
249
+ }, {
250
+ key: "setRefreshFunction",
251
+ value: function () {
252
+ var _setRefreshFunction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(new_fnc) {
253
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
254
+ while (1) switch (_context4.prev = _context4.next) {
255
+ case 0:
256
+ _context4.next = 2;
257
+ return this.addRefreshFunction(new_fnc);
258
+ case 2:
259
+ return _context4.abrupt("return", _context4.sent);
260
+ case 3:
261
+ case "end":
262
+ return _context4.stop();
263
+ }
264
+ }, _callee4, this);
265
+ }));
266
+ function setRefreshFunction(_x5) {
267
+ return _setRefreshFunction.apply(this, arguments);
268
+ }
269
+ return setRefreshFunction;
270
+ }()
271
+ }, {
272
+ key: "addRefreshFunction",
273
+ value: function () {
274
+ var _addRefreshFunction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(key, new_fnc) {
275
+ var oldFunctions;
276
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
277
+ while (1) switch (_context5.prev = _context5.next) {
278
+ case 0:
279
+ if (typeof key === "function") {
280
+ new_fnc = key;
281
+ key = "main";
282
+ }
283
+ key = key || "main";
284
+ oldFunctions = this.__refresh_functions;
285
+ if (oldFunctions == null || _typeof(oldFunctions) !== "object") oldFunctions = {};
286
+ oldFunctions[key] = new_fnc;
287
+ this.__refresh_functions = oldFunctions;
288
+ case 6:
289
+ case "end":
290
+ return _context5.stop();
291
+ }
292
+ }, _callee5, this);
293
+ }));
294
+ function addRefreshFunction(_x6, _x7) {
295
+ return _addRefreshFunction.apply(this, arguments);
296
+ }
297
+ return addRefreshFunction;
298
+ }()
299
+ }, {
300
+ key: "runRefreshFunction",
301
+ value: function () {
302
+ var _runRefreshFunction = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
303
+ var oldFunctions,
304
+ fncArray,
305
+ _len,
306
+ params,
307
+ _key,
308
+ index,
309
+ fnc,
310
+ _args6 = arguments;
311
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
312
+ while (1) switch (_context6.prev = _context6.next) {
313
+ case 0:
314
+ oldFunctions = this.__refresh_functions;
315
+ if (!(oldFunctions == null || _typeof(oldFunctions) !== "object")) {
316
+ _context6.next = 3;
317
+ break;
318
+ }
319
+ return _context6.abrupt("return");
320
+ case 3:
321
+ fncArray = Object.values(oldFunctions);
322
+ for (_len = _args6.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
323
+ params[_key] = _args6[_key];
324
+ }
325
+ index = 0;
326
+ case 6:
327
+ if (!(index < fncArray.length)) {
328
+ _context6.next = 13;
329
+ break;
330
+ }
331
+ fnc = fncArray[index];
332
+ _context6.next = 10;
333
+ return _object.fn_eval.apply(void 0, [fnc].concat(params));
334
+ case 10:
335
+ index++;
336
+ _context6.next = 6;
337
+ break;
338
+ case 13:
339
+ case "end":
340
+ return _context6.stop();
341
+ }
342
+ }, _callee6, this);
343
+ }));
344
+ function runRefreshFunction() {
345
+ return _runRefreshFunction.apply(this, arguments);
346
+ }
347
+ return runRefreshFunction;
348
+ }()
349
+ /* -------------------------------------------------------------------------- */
301
350
  /**
302
351
  * calls `setValue` function for all props of `setObj`
303
352
  * @param setObj
304
353
  * @param changeablesOnly only updates the `__changeables` fields
305
354
  * @returns
306
- */
307
- async setAll(setObj, changeablesOnly = true) {
308
- let finalRes = true;
309
- const checkKeys = changeablesOnly
310
- ? array_intersection(this.__changeables, Object.keys(setObj))
311
- : Object.keys(setObj);
312
- for (let index = 0; index < checkKeys.length; index++) {
313
- const key = checkKeys[index];
314
- if (!(await this.setValue(key, setObj[key])))
315
- finalRes = false;
316
- }
317
- return finalRes;
318
- }
319
- /**
320
- * set value of a key only if the value is empty (null,undef,"")
321
- * @param key
322
- * @param value
323
- * @returns
324
- */
325
- setDefaultValue(key, value) {
326
- return this.#setDefaultProperty(key, value);
327
- }
328
- /**
329
- * set value of a key to now date time
330
- * @param key
331
- * @param value
332
- * @returns
333
- */
334
- setDefaultValueNow = this.#setDefaultPropertyNow;
355
+ */
356
+ }, {
357
+ key: "setAll",
358
+ value: (function () {
359
+ var _setAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(setObj) {
360
+ var changeablesOnly,
361
+ finalRes,
362
+ checkKeys,
363
+ index,
364
+ key,
365
+ _args7 = arguments;
366
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
367
+ while (1) switch (_context7.prev = _context7.next) {
368
+ case 0:
369
+ changeablesOnly = _args7.length > 1 && _args7[1] !== undefined ? _args7[1] : true;
370
+ finalRes = true;
371
+ checkKeys = changeablesOnly ? (0, _object.array_intersection)(this.__changeables, Object.keys(setObj)) : Object.keys(setObj);
372
+ index = 0;
373
+ case 4:
374
+ if (!(index < checkKeys.length)) {
375
+ _context7.next = 13;
376
+ break;
377
+ }
378
+ key = checkKeys[index];
379
+ _context7.next = 8;
380
+ return this.setValue(key, setObj[key]);
381
+ case 8:
382
+ if (_context7.sent) {
383
+ _context7.next = 10;
384
+ break;
385
+ }
386
+ finalRes = false;
387
+ case 10:
388
+ index++;
389
+ _context7.next = 4;
390
+ break;
391
+ case 13:
392
+ return _context7.abrupt("return", finalRes);
393
+ case 14:
394
+ case "end":
395
+ return _context7.stop();
396
+ }
397
+ }, _callee7, this);
398
+ }));
399
+ function setAll(_x8) {
400
+ return _setAll.apply(this, arguments);
401
+ }
402
+ return setAll;
403
+ }()
335
404
  /**
336
405
  * set value of a key only if the value is empty (null,undef,"")
337
406
  * @param key
338
407
  * @param value
339
408
  * @returns
340
- */
341
- setDefault = this.setDefaultValue;
409
+ */
410
+ )
411
+ }, {
412
+ key: "setDefaultValue",
413
+ value: function setDefaultValue(key, value) {
414
+ return _assertClassBrand(_SimpleObject_brand, this, _setDefaultProperty).call(this, key, value);
415
+ }
416
+ }, {
417
+ key: "setSubValue",
418
+ value: (
342
419
  /**
343
420
  * set key of subObject
344
421
  * @param key
345
422
  * @param value
346
423
  * @param subvalue
347
424
  * @return true if changed , false otherwise
348
- */
349
- async setSubValue(key, objKey, subvalue, sendChanges = "auto") {
350
- if (!this.canModify) {
351
- if (this.__throw_errors !== false)
352
- throw Error(`Cant use setSubValue (${String(key)}) when on readonly mode`);
353
- else
354
- return false;
355
- }
356
- let isValueChanged = false;
357
- try {
358
- //if accessing subval of an object, make sure the object is not null
359
- const subval = this.#getSubProperty(key, objKey);
360
- if (!object_equals(subval, subvalue, {
361
- ignoredStartingWords: "_",
362
- strictArrayCheck: true,
363
- })) {
364
- this.#setSubProperty(key, objKey, subvalue);
365
- isValueChanged = true;
366
- }
367
- else
368
- isValueChanged = false;
369
- }
370
- finally {
371
- if (sendChanges === true ||
372
- (sendChanges === "auto" && isValueChanged)) {
373
- await this.notifyChanged("", key, objKey, subvalue);
374
- }
375
- }
376
- return isValueChanged;
377
- }
378
- getSubValue = this.#getSubProperty;
425
+ */
426
+ function () {
427
+ var _setSubValue = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(key, objKey, subvalue) {
428
+ var sendChanges,
429
+ isValueChanged,
430
+ subval,
431
+ _args8 = arguments;
432
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
433
+ while (1) switch (_context8.prev = _context8.next) {
434
+ case 0:
435
+ sendChanges = _args8.length > 3 && _args8[3] !== undefined ? _args8[3] : "auto";
436
+ if (this.canModify) {
437
+ _context8.next = 7;
438
+ break;
439
+ }
440
+ if (!(this.__throw_errors !== false)) {
441
+ _context8.next = 6;
442
+ break;
443
+ }
444
+ throw Error("Cant use setSubValue (".concat(String(key), ") when on readonly mode"));
445
+ case 6:
446
+ return _context8.abrupt("return", false);
447
+ case 7:
448
+ isValueChanged = false;
449
+ _context8.prev = 8;
450
+ //if accessing subval of an object, make sure the object is not null
451
+ subval = _assertClassBrand(_SimpleObject_brand, this, _getSubProperty).call(this, key, objKey);
452
+ if (!(0, _object.object_equals)(subval, subvalue, {
453
+ ignoredStartingWords: "_",
454
+ strictArrayCheck: true
455
+ })) {
456
+ _assertClassBrand(_SimpleObject_brand, this, _setSubProperty).call(this, key, objKey, subvalue);
457
+ isValueChanged = true;
458
+ } else isValueChanged = false;
459
+ case 11:
460
+ _context8.prev = 11;
461
+ if (!(sendChanges === true || sendChanges === "auto" && isValueChanged)) {
462
+ _context8.next = 15;
463
+ break;
464
+ }
465
+ _context8.next = 15;
466
+ return this.notifyChanged("", key, objKey, subvalue);
467
+ case 15:
468
+ return _context8.finish(11);
469
+ case 16:
470
+ return _context8.abrupt("return", isValueChanged);
471
+ case 17:
472
+ case "end":
473
+ return _context8.stop();
474
+ }
475
+ }, _callee8, this, [[8,, 11, 16]]);
476
+ }));
477
+ function setSubValue(_x9, _x10, _x11) {
478
+ return _setSubValue.apply(this, arguments);
479
+ }
480
+ return setSubValue;
481
+ }())
482
+ }, {
483
+ key: "pushValue",
484
+ value: (
379
485
  /**
380
486
  * Push a single value to array property
381
487
  * @param key
382
488
  * @param value
383
489
  * @param sendChanges
384
490
  * @returns
385
- */
386
- async pushValue(key, value, sendChanges = "auto") {
387
- return await this.pushValues(key, [value], sendChanges);
388
- }
491
+ */
492
+ function () {
493
+ var _pushValue = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(key, value) {
494
+ var sendChanges,
495
+ _args9 = arguments;
496
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
497
+ while (1) switch (_context9.prev = _context9.next) {
498
+ case 0:
499
+ sendChanges = _args9.length > 2 && _args9[2] !== undefined ? _args9[2] : "auto";
500
+ _context9.next = 3;
501
+ return this.pushValues(key, [value], sendChanges);
502
+ case 3:
503
+ return _context9.abrupt("return", _context9.sent);
504
+ case 4:
505
+ case "end":
506
+ return _context9.stop();
507
+ }
508
+ }, _callee9, this);
509
+ }));
510
+ function pushValue(_x12, _x13) {
511
+ return _pushValue.apply(this, arguments);
512
+ }
513
+ return pushValue;
514
+ }()
389
515
  /**
390
516
  * Push a list of values to array property
391
517
  * @param key
392
518
  * @param values
393
519
  * @param sendChanges
394
520
  * @returns
395
- */
396
- async pushValues(key, values, sendChanges = "auto") {
397
- if (array_isEmpty(values))
398
- return false;
399
- if (!this.canModify) {
400
- if (this.__throw_errors !== false)
401
- throw Error(`Cant use pushValues (${String(key)}) when on readonly mode`);
402
- else
403
- return false;
404
- }
405
- if (this.#getProperty(key) == null)
406
- this.#setProperty(key, []);
407
- let tempArray = this.#getProperty(key);
408
- if (!Array.isArray(tempArray)) {
409
- if (this.__throw_errors !== false)
410
- throw Error(`Cant use pushValues (${String(key)}) on non Array keys`);
411
- else
412
- return false;
413
- }
414
- this.#setProperty(key, array_merge(tempArray, values));
415
- if (sendChanges === true || sendChanges === "auto")
416
- await this.notifyChanged("", key, values);
417
- return tempArray;
418
- }
521
+ */
522
+ )
523
+ }, {
524
+ key: "pushValues",
525
+ value: (function () {
526
+ var _pushValues = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(key, values) {
527
+ var sendChanges,
528
+ tempArray,
529
+ _args10 = arguments;
530
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
531
+ while (1) switch (_context10.prev = _context10.next) {
532
+ case 0:
533
+ sendChanges = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : "auto";
534
+ if (!(0, _object.array_isEmpty)(values)) {
535
+ _context10.next = 3;
536
+ break;
537
+ }
538
+ return _context10.abrupt("return", false);
539
+ case 3:
540
+ if (this.canModify) {
541
+ _context10.next = 9;
542
+ break;
543
+ }
544
+ if (!(this.__throw_errors !== false)) {
545
+ _context10.next = 8;
546
+ break;
547
+ }
548
+ throw Error("Cant use pushValues (".concat(String(key), ") when on readonly mode"));
549
+ case 8:
550
+ return _context10.abrupt("return", false);
551
+ case 9:
552
+ if (_assertClassBrand(_SimpleObject_brand, this, _getProperty).call(this, key) == null) _assertClassBrand(_SimpleObject_brand, this, _setProperty).call(this, key, []);
553
+ tempArray = _assertClassBrand(_SimpleObject_brand, this, _getProperty).call(this, key);
554
+ if (Array.isArray(tempArray)) {
555
+ _context10.next = 17;
556
+ break;
557
+ }
558
+ if (!(this.__throw_errors !== false)) {
559
+ _context10.next = 16;
560
+ break;
561
+ }
562
+ throw Error("Cant use pushValues (".concat(String(key), ") on non Array keys"));
563
+ case 16:
564
+ return _context10.abrupt("return", false);
565
+ case 17:
566
+ _assertClassBrand(_SimpleObject_brand, this, _setProperty).call(this, key, (0, _object.array_merge)(tempArray, values));
567
+ if (!(sendChanges === true || sendChanges === "auto")) {
568
+ _context10.next = 21;
569
+ break;
570
+ }
571
+ _context10.next = 21;
572
+ return this.notifyChanged("", key, values);
573
+ case 21:
574
+ return _context10.abrupt("return", tempArray);
575
+ case 22:
576
+ case "end":
577
+ return _context10.stop();
578
+ }
579
+ }, _callee10, this);
580
+ }));
581
+ function pushValues(_x14, _x15) {
582
+ return _pushValues.apply(this, arguments);
583
+ }
584
+ return pushValues;
585
+ }()
419
586
  /**
420
587
  *
421
588
  * @param type
@@ -423,623 +590,1037 @@ export class SimpleObject extends MapableObject {
423
590
  * @param value
424
591
  * @param subvalue
425
592
  * @returns true if manager was notified. false otherwise
426
- */
427
- async notifyChanged(type, key, value, subvalue) {
428
- if (value === undefined && type == "")
429
- value = this.getValue(key);
430
- let lc = "";
431
- if (type)
432
- lc += `$${String(type)}$`;
433
- if (subvalue) {
434
- lc += `${String(key)}.${value}`;
435
- }
436
- else {
437
- lc += `${String(key)}`;
438
- }
439
- this.__lastChanged = lc;
440
- /* --------------------------- notify the manager --------------------------- */
441
- const manager_class = this.manager;
442
- if (typeof manager_class?.notifyChanged === "function") {
443
- return await manager_class.notifyChanged(type, key, value, subvalue);
444
- }
445
- return false;
446
- }
593
+ */
594
+ )
595
+ }, {
596
+ key: "notifyChanged",
597
+ value: (function () {
598
+ var _notifyChanged = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(type, key, value, subvalue) {
599
+ var lc, manager_class;
600
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
601
+ while (1) switch (_context11.prev = _context11.next) {
602
+ case 0:
603
+ if (value === undefined && type == "") value = this.getValue(key);
604
+ lc = "";
605
+ if (type) lc += "$".concat(String(type), "$");
606
+ if (subvalue) {
607
+ lc += "".concat(String(key), ".").concat(value);
608
+ } else {
609
+ lc += "".concat(String(key));
610
+ }
611
+ this.__lastChanged = lc;
612
+ /* --------------------------- notify the manager --------------------------- */
613
+ manager_class = this.manager;
614
+ if (!(typeof (manager_class === null || manager_class === void 0 ? void 0 : manager_class.notifyChanged) === "function")) {
615
+ _context11.next = 10;
616
+ break;
617
+ }
618
+ _context11.next = 9;
619
+ return manager_class.notifyChanged(type, key, value, subvalue);
620
+ case 9:
621
+ return _context11.abrupt("return", _context11.sent);
622
+ case 10:
623
+ return _context11.abrupt("return", false);
624
+ case 11:
625
+ case "end":
626
+ return _context11.stop();
627
+ }
628
+ }, _callee11, this);
629
+ }));
630
+ function notifyChanged(_x16, _x17, _x18, _x19) {
631
+ return _notifyChanged.apply(this, arguments);
632
+ }
633
+ return notifyChanged;
634
+ }()
447
635
  /**
448
636
  * check if a key can be changed on an object
449
637
  * @param object the object we are checking
450
638
  * @param key key of the object
451
639
  * @param useExtraProps should we check extra props?
452
640
  * @returns
453
- */
454
- static canChangeKey(object, key, options) {
455
- if (!object || !key)
456
- return false;
457
- const { useExtraProps, ignoreProtected } = options;
458
- if (useExtraProps) {
459
- const __extraProperties = object.constructor["__extraProperties"];
460
- if (__extraProperties != null && __extraProperties.includes(key))
461
- return true;
462
- }
463
- /* -------------------------------------------------------------------------- */
464
- const __changeables = object.constructor["__changeables"];
465
- const __unchangeables = object.constructor["__unchangeables"];
466
- /* -------------------------------------------------------------------------- */
467
- if (__changeables != null && !__changeables.includes(key))
468
- return false;
469
- if (__unchangeables != null && __unchangeables.includes(key))
470
- return false;
471
- if (ignoreProtected !== true) {
472
- const __protectedProperties = object.constructor["__protectedProperties"];
473
- if (__protectedProperties != null &&
474
- __protectedProperties.includes(key)) {
475
- //protected keys can only be changed by the creator of this object
476
- //make sure you set the is_current_user function correctly
477
- if (object.isNew)
478
- return true;
479
- const creatorOfObject = object.get("_creator");
480
- return Boolean(gfunc.run_action_sync("is_current_user", creatorOfObject));
481
- }
482
- }
483
- return true;
484
- }
485
- static hasIllegalChanges(referenceObject, changesObject, updaterUser) {
486
- const __protectedProperties = object_getStatic(referenceObject, "__protectedProperties");
487
- if (object_isEmpty(__protectedProperties))
488
- return false;
489
- const referenceUser = object_isClass(referenceObject)
490
- ? object_get(changesObject, "__user", "_creator")
491
- : object_get(referenceObject, "__user", "_creator");
492
- if (Boolean(SimpleObject.getID(referenceUser) ==
493
- SimpleObject.getID(updaterUser))) {
494
- return false;
495
- }
496
- for (const key in changesObject) {
497
- if (__protectedProperties.includes(key)) {
498
- return true;
499
- }
500
- }
501
- return false;
502
- }
503
- /* -------------------------------------------------------------------------- */
504
- /* Changes */
505
- /* -------------------------------------------------------------------------- */
641
+ */
642
+ )
643
+ }, {
644
+ key: "apply_changes",
645
+ value: /* -------------------------------------------------------------------------- */
646
+ /* Changes */
647
+ /* -------------------------------------------------------------------------- */
506
648
  /**
507
649
  * Apply all the changes and empty the delta object
508
- */
509
- apply_changes() {
510
- try {
511
- if (!this.__use_delta) {
512
- //in this mode , we track changes by updating the original product
513
- this.setOriginal();
514
- return true;
515
- }
516
- if (!this.isChanged) {
517
- console.warn("No changes to apply");
518
- return false;
519
- }
520
- for (const key in this.__delta) {
521
- const nv = this.__delta[key];
522
- this[key] = nv;
523
- }
524
- return true;
525
- }
526
- finally {
527
- this.clear_changes();
528
- }
529
- }
650
+ */
651
+ function apply_changes() {
652
+ try {
653
+ if (!this.__use_delta) {
654
+ //in this mode , we track changes by updating the original product
655
+ this.setOriginal();
656
+ return true;
657
+ }
658
+ if (!this.isChanged) {
659
+ console.warn("No changes to apply");
660
+ return false;
661
+ }
662
+ for (var key in this.__delta) {
663
+ var nv = this.__delta[key];
664
+ this[key] = nv;
665
+ }
666
+ return true;
667
+ } finally {
668
+ this.clear_changes();
669
+ }
670
+ }
530
671
  /**
531
672
  * apply changes for the given fields
532
673
  * @param key
533
674
  * @returns
534
- */
535
- apply(keylist) {
536
- wrap_array(keylist).forEach((key) => {
537
- try {
538
- if (!this.__use_delta) {
539
- //in this mode , we track changes by updating the original
540
- return true;
541
- }
542
- if (this.__delta != null && key in this.__delta) {
543
- this[key] = this.__delta[key];
544
- delete this.__delta[key];
545
- }
546
- return true;
547
- }
548
- finally {
549
- }
550
- });
551
- return true;
552
- }
675
+ */
676
+ }, {
677
+ key: "apply",
678
+ value: function apply(keylist) {
679
+ var _this2 = this;
680
+ (0, _object.wrap_array)(keylist).forEach(function (key) {
681
+ try {
682
+ if (!_this2.__use_delta) {
683
+ //in this mode , we track changes by updating the original
684
+ return true;
685
+ }
686
+ if (_this2.__delta != null && key in _this2.__delta) {
687
+ _this2[key] = _this2.__delta[key];
688
+ delete _this2.__delta[key];
689
+ }
690
+ return true;
691
+ } finally {}
692
+ });
693
+ return true;
694
+ }
553
695
  /**
554
696
  * set the value to its original
555
697
  * @param key
556
- */
557
- async reset(key, shouldNotify = "auto") {
558
- return await this.setValue(key, this[key], shouldNotify);
559
- }
698
+ */
699
+ }, {
700
+ key: "reset",
701
+ value: (function () {
702
+ var _reset = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(key) {
703
+ var shouldNotify,
704
+ _args12 = arguments;
705
+ return _regeneratorRuntime().wrap(function _callee12$(_context12) {
706
+ while (1) switch (_context12.prev = _context12.next) {
707
+ case 0:
708
+ shouldNotify = _args12.length > 1 && _args12[1] !== undefined ? _args12[1] : "auto";
709
+ _context12.next = 3;
710
+ return this.setValue(key, this[key], shouldNotify);
711
+ case 3:
712
+ return _context12.abrupt("return", _context12.sent);
713
+ case 4:
714
+ case "end":
715
+ return _context12.stop();
716
+ }
717
+ }, _callee12, this);
718
+ }));
719
+ function reset(_x20) {
720
+ return _reset.apply(this, arguments);
721
+ }
722
+ return reset;
723
+ }()
560
724
  /**
561
725
  * reset all values back to their original
562
726
  * difference between this and clear_changes is that this function also calls on_change
563
727
  * @param key
564
- */
565
- async resetAll(shouldNotify = "auto") {
566
- let fr = true;
567
- for (const key in this.__delta) {
568
- fr = fr && (await this.reset(key, shouldNotify));
569
- }
570
- return fr;
571
- }
728
+ */
729
+ )
730
+ }, {
731
+ key: "resetAll",
732
+ value: (function () {
733
+ var _resetAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
734
+ var shouldNotify,
735
+ fr,
736
+ key,
737
+ _args13 = arguments;
738
+ return _regeneratorRuntime().wrap(function _callee13$(_context13) {
739
+ while (1) switch (_context13.prev = _context13.next) {
740
+ case 0:
741
+ shouldNotify = _args13.length > 0 && _args13[0] !== undefined ? _args13[0] : "auto";
742
+ fr = true;
743
+ _context13.t0 = _regeneratorRuntime().keys(this.__delta);
744
+ case 3:
745
+ if ((_context13.t1 = _context13.t0()).done) {
746
+ _context13.next = 13;
747
+ break;
748
+ }
749
+ key = _context13.t1.value;
750
+ _context13.t2 = fr;
751
+ if (!_context13.t2) {
752
+ _context13.next = 10;
753
+ break;
754
+ }
755
+ _context13.next = 9;
756
+ return this.reset(key, shouldNotify);
757
+ case 9:
758
+ _context13.t2 = _context13.sent;
759
+ case 10:
760
+ fr = _context13.t2;
761
+ _context13.next = 3;
762
+ break;
763
+ case 13:
764
+ return _context13.abrupt("return", fr);
765
+ case 14:
766
+ case "end":
767
+ return _context13.stop();
768
+ }
769
+ }, _callee13, this);
770
+ }));
771
+ function resetAll() {
772
+ return _resetAll.apply(this, arguments);
773
+ }
774
+ return resetAll;
775
+ }()
572
776
  /**
573
777
  * clear and discard all changes
574
778
  * does not call any on_change functions
575
779
  * @returns
576
- */
577
- clear_changes() {
578
- this.__delta = {};
579
- this.__has_delta = false;
580
- return true;
581
- }
780
+ */
781
+ )
782
+ }, {
783
+ key: "clear_changes",
784
+ value: function clear_changes() {
785
+ this.__delta = {};
786
+ this.__has_delta = false;
787
+ return true;
788
+ }
582
789
  /**
583
790
  * clear and discard all temp values
584
791
  * @returns
585
- */
586
- clear_cache() {
587
- delete this.__temp;
588
- return true;
589
- }
792
+ */
793
+ }, {
794
+ key: "clear_cache",
795
+ value: function clear_cache() {
796
+ delete this.__temp;
797
+ return true;
798
+ }
590
799
  /**
591
800
  * Sets readonly property on given objects
592
801
  * @param objs array of given objects or a single object
593
802
  * @param readonlyval readonly
594
803
  * @param clearChanges should we clean the changes after setting readonly
595
- */
596
- static setReadonly(objs, readonlyval = true, clearChanges = false) {
597
- let checkObjs = [];
598
- if (!objs)
599
- objs = [];
600
- if (!Array.isArray(objs))
601
- checkObjs = [objs];
602
- else
603
- checkObjs = objs;
604
- for (let index = 0; index < checkObjs.length; index++) {
605
- const obj = checkObjs[index];
606
- obj.__is_readonly = readonlyval;
607
- if (obj instanceof SimpleObject) {
608
- if (obj.canModify) {
609
- if (!obj.getOriginal())
610
- obj.setOriginal();
611
- }
612
- else {
613
- if (clearChanges)
614
- obj.clear_changes();
615
- //we dont need the original object in readonly mode
616
- obj.setOriginal(null);
617
- }
618
- }
619
- }
620
- }
621
- static setThrowErrors(objs, throwErr = true) {
622
- let checkObjs = [];
623
- if (!objs)
624
- objs = [];
625
- if (!Array.isArray(objs))
626
- checkObjs = [objs];
627
- else
628
- checkObjs = objs;
629
- for (let index = 0; index < checkObjs.length; index++) {
630
- const element = checkObjs[index];
631
- element.__throw_errors = throwErr;
632
- }
633
- }
634
- /* -------------------------------------------------------------------------- */
635
- /* Get/Check */
636
- /* -------------------------------------------------------------------------- */
637
- get __object_name() {
638
- return this.constructor["__object_name"] || this.__db_name;
639
- }
640
- get __db_name() {
641
- return this.constructor["__db_name"] || this.constructor["__web_path"];
642
- }
643
- get isChanged() {
644
- ////const deltaIsBlank = object_isEmpty(this.__delta);
645
- ////if (!deltaIsBlank) return true; //delta is not blank, definitely changed
646
- ////if (this.__use_delta) return !deltaIsBlank; //trust the delta.
647
- if (this.__use_delta) {
648
- return this.__has_delta;
649
- ////for (const key in this.__delta) {
650
- //// return true;
651
- ////}
652
- ////return false;
653
- }
654
- /* --------------------- check the properties one by one -------------------- */
655
- const orig = this.getOriginal();
656
- if (!orig)
657
- return false; //we dont know if anything has changed
658
- /**
659
- * if object is not using delta mode,
660
- * check changes by comparing changable fields with original object
661
- */
662
- if (Array.isArray(this.__changeables)) {
663
- return (this.__changeables.find((c) => {
664
- return !object_equals(this[c], orig[c]);
665
- }) != undefined);
666
- }
667
- else {
668
- /* check all fields */
669
- for (const key in this) {
670
- if (key.startsWith("__"))
671
- continue;
672
- if (!object_equals(this[key], orig[key]))
673
- return true;
674
- }
675
- return false;
676
- }
677
- }
678
- get isArchived() {
679
- return this._archived || false;
680
- }
681
- set isArchived(v) {
682
- this._archived = v;
683
- }
684
- get isDeleted() {
685
- return this.get("_deleted") || false;
686
- }
687
- get isLocked() {
688
- return this.get("_locked") || false;
689
- }
690
- set isLocked(v) {
691
- this._locked = v;
692
- }
693
- set isDeleted(v) {
694
- this._deleted = v;
695
- }
696
- get d() {
697
- return this.__use_delta ? this.__delta : this;
698
- }
699
- /* all updated properties */
700
- get allproperties() {
701
- const result = { ...this, ...this.__delta };
702
- result.__delta = {};
703
- return result;
704
- }
705
- get canModify() {
706
- return !Boolean(this.__is_readonly);
707
- }
708
- get __changeables() {
709
- return this.constructor["__changeables"];
710
- }
711
- get __unchangeables() {
712
- return this.constructor["__unchangeables"];
713
- }
714
- get __use_delta() {
715
- return this.constructor["__use_delta"];
716
- }
717
- get isSaved() {
718
- return Boolean(this.__saveResultIsSuccess ?? false);
719
- }
720
- get isNew() {
721
- const mid = this.getID();
722
- if (!mid)
723
- return true;
724
- if (typeof mid === "string")
725
- return mid === "" || mid === "-1";
726
- if (typeof mid === "number")
727
- return mid === -1;
728
- return false;
729
- }
730
- get lastChanged() {
731
- return this.__lastChanged || "";
732
- }
733
- get manager() {
734
- return this.get_managerClass() || undefined;
735
- }
736
- get_managerClass = () => undefined;
804
+ */
805
+ }, {
806
+ key: "__object_name",
807
+ get: /* -------------------------------------------------------------------------- */
808
+ /* Get/Check */
809
+ /* -------------------------------------------------------------------------- */
810
+ function get() {
811
+ return this.constructor["__object_name"] || this.__db_name;
812
+ }
813
+ }, {
814
+ key: "__db_name",
815
+ get: function get() {
816
+ return this.constructor["__db_name"] || this.constructor["__web_path"];
817
+ }
818
+ }, {
819
+ key: "isChanged",
820
+ get: function get() {
821
+ var _this3 = this;
822
+ ////const deltaIsBlank = object_isEmpty(this.__delta);
823
+ ////if (!deltaIsBlank) return true; //delta is not blank, definitely changed
824
+ ////if (this.__use_delta) return !deltaIsBlank; //trust the delta.
825
+ if (this.__use_delta) {
826
+ return this.__has_delta;
827
+ ////for (const key in this.__delta) {
828
+ //// return true;
829
+ ////}
830
+ ////return false;
831
+ }
832
+ /* --------------------- check the properties one by one -------------------- */
833
+ var orig = this.getOriginal();
834
+ if (!orig) return false; //we dont know if anything has changed
835
+ /**
836
+ * if object is not using delta mode,
837
+ * check changes by comparing changable fields with original object
838
+ */
839
+ if (Array.isArray(this.__changeables)) {
840
+ return this.__changeables.find(function (c) {
841
+ return !(0, _object.object_equals)(_this3[c], orig[c]);
842
+ }) != undefined;
843
+ } else {
844
+ /* check all fields */
845
+ for (var key in this) {
846
+ if (key.startsWith("__")) continue;
847
+ if (!(0, _object.object_equals)(this[key], orig[key])) return true;
848
+ }
849
+ return false;
850
+ }
851
+ }
852
+ }, {
853
+ key: "isArchived",
854
+ get: function get() {
855
+ return this._archived || false;
856
+ },
857
+ set: function set(v) {
858
+ this._archived = v;
859
+ }
860
+ }, {
861
+ key: "isDeleted",
862
+ get: function get() {
863
+ return this.get("_deleted") || false;
864
+ },
865
+ set: function set(v) {
866
+ this._deleted = v;
867
+ }
868
+ }, {
869
+ key: "isLocked",
870
+ get: function get() {
871
+ return this.get("_locked") || false;
872
+ },
873
+ set: function set(v) {
874
+ this._locked = v;
875
+ }
876
+ }, {
877
+ key: "d",
878
+ get: function get() {
879
+ return this.__use_delta ? this.__delta : this;
880
+ }
881
+ /* all updated properties */
882
+ }, {
883
+ key: "allproperties",
884
+ get: function get() {
885
+ var result = _objectSpread(_objectSpread({}, this), this.__delta);
886
+ result.__delta = {};
887
+ return result;
888
+ }
889
+ }, {
890
+ key: "canModify",
891
+ get: function get() {
892
+ return !Boolean(this.__is_readonly);
893
+ }
894
+ }, {
895
+ key: "__changeables",
896
+ get: function get() {
897
+ return this.constructor["__changeables"];
898
+ }
899
+ }, {
900
+ key: "__unchangeables",
901
+ get: function get() {
902
+ return this.constructor["__unchangeables"];
903
+ }
904
+ }, {
905
+ key: "__use_delta",
906
+ get: function get() {
907
+ return this.constructor["__use_delta"];
908
+ }
909
+ }, {
910
+ key: "isSaved",
911
+ get: function get() {
912
+ var _this$__saveResultIsS;
913
+ return Boolean((_this$__saveResultIsS = this.__saveResultIsSuccess) !== null && _this$__saveResultIsS !== void 0 ? _this$__saveResultIsS : false);
914
+ }
915
+ }, {
916
+ key: "isNew",
917
+ get: function get() {
918
+ var mid = this.getID();
919
+ if (!mid) return true;
920
+ if (typeof mid === "string") return mid === "" || mid === "-1";
921
+ if (typeof mid === "number") return mid === -1;
922
+ return false;
923
+ }
924
+ }, {
925
+ key: "lastChanged",
926
+ get: function get() {
927
+ return this.__lastChanged || "";
928
+ }
929
+ }, {
930
+ key: "manager",
931
+ get: function get() {
932
+ return this.get_managerClass() || undefined;
933
+ }
934
+ }, {
935
+ key: "set_managerClass",
936
+ value:
737
937
  /**
738
938
  * @deprecated use setManager
739
939
  * @param mng
740
- */
741
- set_managerClass(mng) {
742
- return this.setManager(mng);
743
- }
940
+ */
941
+ function set_managerClass(mng) {
942
+ return this.setManager(mng);
943
+ }
744
944
  /**
745
945
  * update the manager class for this object
746
946
  * @param mng
747
- */
748
- setManager(mng) {
749
- if (mng instanceof ObjectManager &&
750
- typeof mng.setObject === "function") {
751
- //make sure the manager knows what object it is managing
752
- return mng.setObject(this);
753
- }
754
- else {
755
- console.error("set_managerClass was used with a bad manager. make sure the input to this function is a correct ObjectManager");
756
- }
757
- }
758
- static isDeleted(obj) {
759
- if (obj instanceof SimpleObject)
760
- return obj.isDeleted === true;
761
- return false;
762
- }
763
- getIndexOrID() {
764
- return SimpleObject.getIndexOrID(this);
765
- }
766
- getIndex() {
767
- return SimpleObject.getIndex(this);
768
- }
769
- getID() {
770
- return SimpleObject.getID(this);
771
- }
772
- static getIndexOrID(o) {
773
- let res = SimpleObject.getIndex(o);
774
- if (!res || res == -1)
775
- res = SimpleObject.getID(o);
776
- return res || -1;
777
- }
778
- static getIndex(o) {
779
- if (o == null || o == undefined)
780
- return -1;
781
- switch (typeof o) {
782
- case "object":
783
- return o._index_value || -1;
784
- case "number":
785
- case "string":
786
- return Number(o);
787
- }
788
- return -1;
789
- }
790
- static getID(o) {
791
- if (o == null || o == undefined)
792
- return "";
793
- switch (typeof o) {
794
- case "object":
795
- if (o._bsontype == "ObjectID" &&
796
- typeof o.toString === "function") {
797
- return o.toString();
798
- }
799
- return String(o._id || o.insertedId || o.ID || o.id || o.__data_id || "");
800
- case "string":
801
- case "number":
802
- return String(o);
803
- }
804
- return "";
805
- }
806
- /* -------------------------------------------------------------------------- */
807
- /* Events */
808
- /* -------------------------------------------------------------------------- */
809
- async event_run(eventName, ...params) {
810
- if (!this.manager) {
811
- if (this.__showErrors === true)
812
- console.error(`you cant use events (${eventName}) if your object has no manager`);
813
- return;
814
- }
815
- return await this.manager.event_run(eventName, ...params);
816
- }
817
- event_use(name, runEvent, onlyOn = null, runOnce = false) {
818
- if (!this.manager) {
819
- if (this.__showErrors === true)
820
- console.error(`you cant use events (${name}) if your object has no manager`);
821
- return;
822
- }
823
- return this.manager.event_use(name, runEvent, onlyOn, runOnce);
824
- }
825
- event_remove(name, uname) {
826
- if (!this.manager) {
827
- if (this.__showErrors === true)
828
- console.error(`you cant use events (${name}) if your object has no manager`);
829
- return;
830
- }
831
- return this.manager.event_remove(name, uname);
832
- }
833
- async event_add(name, uname, runEvent, onlyOn = null, runOnce = false) {
834
- if (!this.manager) {
835
- if (this.__showErrors === true)
836
- console.error(`you cant use events (${name}) if your object has no manager`);
837
- return;
838
- }
839
- await this.manager.event_add(name, uname, runEvent, onlyOn, runOnce);
840
- }
841
- /* -------------------------------------------------------------------------- */
842
- /* Original */
843
- /* -------------------------------------------------------------------------- */
844
- getOriginal() {
845
- return this.__originalobject || undefined;
846
- }
847
- setOriginal(toObj = 0) {
848
- if (toObj === null) {
849
- delete this.__originalobject;
850
- return;
851
- }
852
- if (!this.canModify) {
853
- console.warn("Cant use setOriginal on non original OR readonly objects ! this will create an infinite loop");
854
- delete this.__originalobject;
855
- return;
856
- }
857
- if (toObj === 0) {
858
- toObj = this;
859
- }
860
- const cloneObject = object_clone(toObj);
861
- SimpleObject.setReadonly(cloneObject, true);
862
- this.__originalobject = object_removeKeys(cloneObject, ["__"]);
863
- return this;
864
- }
865
- static setOriginal(obj, toObj = 0) {
866
- if (!(obj instanceof SimpleObject))
867
- return undefined;
868
- return obj.setOriginal(toObj);
869
- }
870
- static getOriginal(obj) {
871
- if (obj instanceof SimpleObject)
872
- return obj.getOriginal();
873
- return obj?.__originalobject;
874
- }
875
- /* -------------------------------------------------------------------------- */
876
- /* Errors */
877
- /* -------------------------------------------------------------------------- */
878
- addError(title, message, sendToRegister = "auto") {
879
- return SimpleObject.addError(this, title, message, sendToRegister);
880
- }
881
- clearErrors() {
882
- return SimpleObject.clearErrors(this);
883
- }
884
- getErrors() {
885
- return SimpleObject.getErrors(this);
886
- }
887
- static addError(o, title, message, sendToRegister = "auto") {
888
- const errStr = combine_if_not_blank(title, " : ", message);
889
- if (o) {
890
- if (!Array.isArray(o.__errors))
891
- o.__errors = [];
892
- o.__errors.push(errStr);
893
- if (o.__showErrors === false)
894
- return;
895
- }
896
- else if (sendToRegister === "auto")
897
- sendToRegister = true;
898
- if (sendToRegister === true) {
899
- console.error("[REGISTER-ERROR]", errStr);
900
- gfunc.run_action("add_error", {
901
- title: title,
902
- message: message,
903
- type: "error",
904
- });
905
- }
906
- else {
907
- console.error("[ERROR]", errStr);
908
- }
909
- return o?.__errors || [];
910
- }
911
- static getErrors(o) {
912
- return o?.__errors || [];
913
- }
914
- static clearErrors(o) {
915
- if (o)
916
- o.__errors = [];
917
- return true;
918
- }
919
- /* -------------------------------------------------------------------------- */
920
- /* Save Results */
921
- /* -------------------------------------------------------------------------- */
922
- /**
923
- * Checks if the given object is a valid and truthy save result
924
- * @param result_object
925
- * @returns
926
- */
927
- static isResultValid(result_object, sub = false) {
928
- if (object_isEmpty(result_object))
929
- return false;
930
- switch (typeof result_object) {
931
- case "string":
932
- try {
933
- result_object = JSON.parse(result_object);
934
- return this.isResultValid(result_object);
935
- }
936
- catch {
937
- return false;
938
- }
939
- case "object":
940
- /* --------- maybe website sends the result as array with 1 element --------- */
941
- if (Array.isArray(result_object)) {
942
- if (sub)
943
- return false; //avoid going too deep
944
- return this.isResultValid(array_first(result_object), true);
945
- }
946
- /* -------------------------- check if acknowledged ------------------------- */
947
- if (object_isTrue(result_object.acknowledged)) {
948
- return true;
949
- }
950
- if (result_object.expected && result_object.result) {
951
- return object_equals(result_object.expected, result_object.result);
952
- }
953
- return !string_isEmpty(this.getID(result_object));
954
- case "number":
955
- return result_object > 0;
956
- case "boolean":
957
- return result_object === true;
958
- }
959
- return false;
960
- }
947
+ */
948
+ }, {
949
+ key: "setManager",
950
+ value: function setManager(mng) {
951
+ if (mng instanceof _ObjectManager["default"] && typeof mng.setObject === "function") {
952
+ //make sure the manager knows what object it is managing
953
+ return mng.setObject(this);
954
+ } else {
955
+ console.error("set_managerClass was used with a bad manager. make sure the input to this function is a correct ObjectManager");
956
+ }
957
+ }
958
+ }, {
959
+ key: "getIndexOrID",
960
+ value: function getIndexOrID() {
961
+ return SimpleObject.getIndexOrID(this);
962
+ }
963
+ }, {
964
+ key: "getIndex",
965
+ value: function getIndex() {
966
+ return SimpleObject.getIndex(this);
967
+ }
968
+ }, {
969
+ key: "getID",
970
+ value: function getID() {
971
+ return SimpleObject.getID(this);
972
+ }
973
+ }, {
974
+ key: "event_run",
975
+ value: (
976
+ /* -------------------------------------------------------------------------- */
977
+ /* Events */
978
+ /* -------------------------------------------------------------------------- */
979
+ function () {
980
+ var _event_run = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(eventName) {
981
+ var _this$manager2;
982
+ var _len2,
983
+ params,
984
+ _key2,
985
+ _args14 = arguments;
986
+ return _regeneratorRuntime().wrap(function _callee14$(_context14) {
987
+ while (1) switch (_context14.prev = _context14.next) {
988
+ case 0:
989
+ if (this.manager) {
990
+ _context14.next = 3;
991
+ break;
992
+ }
993
+ if (this.__showErrors === true) console.error("you cant use events (".concat(eventName, ") if your object has no manager"));
994
+ return _context14.abrupt("return");
995
+ case 3:
996
+ for (_len2 = _args14.length, params = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
997
+ params[_key2 - 1] = _args14[_key2];
998
+ }
999
+ _context14.next = 6;
1000
+ return (_this$manager2 = this.manager).event_run.apply(_this$manager2, [eventName].concat(params));
1001
+ case 6:
1002
+ return _context14.abrupt("return", _context14.sent);
1003
+ case 7:
1004
+ case "end":
1005
+ return _context14.stop();
1006
+ }
1007
+ }, _callee14, this);
1008
+ }));
1009
+ function event_run(_x21) {
1010
+ return _event_run.apply(this, arguments);
1011
+ }
1012
+ return event_run;
1013
+ }())
1014
+ }, {
1015
+ key: "event_use",
1016
+ value: function event_use(name, runEvent) {
1017
+ var onlyOn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
1018
+ var runOnce = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
1019
+ if (!this.manager) {
1020
+ if (this.__showErrors === true) console.error("you cant use events (".concat(name, ") if your object has no manager"));
1021
+ return;
1022
+ }
1023
+ return this.manager.event_use(name, runEvent, onlyOn, runOnce);
1024
+ }
1025
+ }, {
1026
+ key: "event_remove",
1027
+ value: function event_remove(name, uname) {
1028
+ if (!this.manager) {
1029
+ if (this.__showErrors === true) console.error("you cant use events (".concat(name, ") if your object has no manager"));
1030
+ return;
1031
+ }
1032
+ return this.manager.event_remove(name, uname);
1033
+ }
1034
+ }, {
1035
+ key: "event_add",
1036
+ value: function () {
1037
+ var _event_add = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15(name, uname, runEvent) {
1038
+ var onlyOn,
1039
+ runOnce,
1040
+ _args15 = arguments;
1041
+ return _regeneratorRuntime().wrap(function _callee15$(_context15) {
1042
+ while (1) switch (_context15.prev = _context15.next) {
1043
+ case 0:
1044
+ onlyOn = _args15.length > 3 && _args15[3] !== undefined ? _args15[3] : null;
1045
+ runOnce = _args15.length > 4 && _args15[4] !== undefined ? _args15[4] : false;
1046
+ if (this.manager) {
1047
+ _context15.next = 5;
1048
+ break;
1049
+ }
1050
+ if (this.__showErrors === true) console.error("you cant use events (".concat(name, ") if your object has no manager"));
1051
+ return _context15.abrupt("return");
1052
+ case 5:
1053
+ _context15.next = 7;
1054
+ return this.manager.event_add(name, uname, runEvent, onlyOn, runOnce);
1055
+ case 7:
1056
+ case "end":
1057
+ return _context15.stop();
1058
+ }
1059
+ }, _callee15, this);
1060
+ }));
1061
+ function event_add(_x22, _x23, _x24) {
1062
+ return _event_add.apply(this, arguments);
1063
+ }
1064
+ return event_add;
1065
+ }()
1066
+ /* -------------------------------------------------------------------------- */
1067
+ /* Original */
1068
+ /* -------------------------------------------------------------------------- */
1069
+ }, {
1070
+ key: "getOriginal",
1071
+ value: function getOriginal() {
1072
+ return this.__originalobject || undefined;
1073
+ }
1074
+ }, {
1075
+ key: "setOriginal",
1076
+ value: function setOriginal() {
1077
+ var toObj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
1078
+ if (toObj === null) {
1079
+ delete this.__originalobject;
1080
+ return;
1081
+ }
1082
+ if (!this.canModify) {
1083
+ console.warn("Cant use setOriginal on non original OR readonly objects ! this will create an infinite loop");
1084
+ delete this.__originalobject;
1085
+ return;
1086
+ }
1087
+ if (toObj === 0) {
1088
+ toObj = this;
1089
+ }
1090
+ var cloneObject = (0, _object.object_clone)(toObj);
1091
+ SimpleObject.setReadonly(cloneObject, true);
1092
+ this.__originalobject = (0, _object.object_removeKeys)(cloneObject, ["__"]);
1093
+ return this;
1094
+ }
1095
+ }, {
1096
+ key: "addError",
1097
+ value: /* -------------------------------------------------------------------------- */
1098
+ /* Errors */
1099
+ /* -------------------------------------------------------------------------- */
1100
+ function addError(title, message) {
1101
+ var sendToRegister = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "auto";
1102
+ return SimpleObject.addError(this, title, message, sendToRegister);
1103
+ }
1104
+ }, {
1105
+ key: "clearErrors",
1106
+ value: function clearErrors() {
1107
+ return SimpleObject.clearErrors(this);
1108
+ }
1109
+ }, {
1110
+ key: "getErrors",
1111
+ value: function getErrors() {
1112
+ return SimpleObject.getErrors(this);
1113
+ }
1114
+ }, {
1115
+ key: "setSaveResult",
1116
+ value:
961
1117
  /**
962
1118
  * set the response of last save from site
963
1119
  * and its type
964
1120
  * @param toObj
965
- */
966
- setSaveResult(toObj, type = "auto", registerErrors = false) {
967
- this.#setSaveResultObject(toObj);
968
- //check if the given object is a valid save result
969
- if (type === "error")
970
- this.__saveResultIsSuccess = false;
971
- else
972
- this.__saveResultIsSuccess = Boolean(type === "nothing" || this.constructor["isResultValid"](toObj));
973
- if (type === "auto") {
974
- type = this.__saveResultIsSuccess ? "normal" : "error";
975
- }
976
- if (registerErrors && !this.__saveResultIsSuccess) {
977
- console.error("bad save result : ", this.__saveResultObject);
978
- SimpleObject.addError(this, "save result is not valid", "the response from server was not valid", true);
979
- }
980
- this.__saveResultType = type;
981
- return type;
982
- }
1121
+ */
1122
+ function setSaveResult(toObj) {
1123
+ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "auto";
1124
+ var registerErrors = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1125
+ _assertClassBrand(_SimpleObject_brand, this, _setSaveResultObject).call(this, toObj);
1126
+ //check if the given object is a valid save result
1127
+ if (type === "error") this.__saveResultIsSuccess = false;else this.__saveResultIsSuccess = Boolean(type === "nothing" || this.constructor["isResultValid"](toObj));
1128
+ if (type === "auto") {
1129
+ type = this.__saveResultIsSuccess ? "normal" : "error";
1130
+ }
1131
+ if (registerErrors && !this.__saveResultIsSuccess) {
1132
+ console.error("bad save result : ", this.__saveResultObject);
1133
+ SimpleObject.addError(this, "save result is not valid", "the response from server was not valid", true);
1134
+ }
1135
+ this.__saveResultType = type;
1136
+ return type;
1137
+ }
983
1138
  /**
984
1139
  * set the response of last save from site
985
1140
  * @param toObj
986
- */
987
- #setSaveResultObject(toObj) {
988
- if (toObj == null)
989
- toObj = null; //null or undefined
990
- this.__saveResultObject = toObj;
991
- return true;
992
- }
993
- getSaveResult(original = false) {
994
- if (this.isSaved)
995
- return this.__saveResultObject;
996
- return false;
997
- }
998
- getSaveResultType() {
999
- return this.__saveResultType || "nothing";
1000
- }
1001
- /** did we create on the last save */
1002
- getIsLastSaveCreate() {
1003
- return this.isSaved && this.getSaveResult().insertedId;
1004
- }
1005
- /* -------------------------------------------------------------------------- */
1006
- /* statics */
1007
- /* -------------------------------------------------------------------------- */
1008
- static async setTemp(obj, key, value, sendChanges = false) {
1009
- if (typeof obj !== "object" || !obj || Array.isArray(obj))
1010
- return;
1011
- if (obj instanceof SimpleObject)
1012
- return await obj.setTemp(key, value, sendChanges);
1013
- //
1014
- if (obj.__temp == null)
1015
- obj.__temp = {};
1016
- object_set(obj.__temp, key, value);
1017
- }
1018
- static factoryClear(obj) {
1019
- obj.clear_changes();
1020
- SimpleObject.clearErrors(obj);
1021
- }
1022
- static async delete_cache(context) {
1023
- if (!this.__db_name)
1024
- return;
1025
- if (!context)
1026
- await gcache.delete_session_cache(this.__db_name);
1027
- else {
1028
- const cacheKey = `${this.__db_name}-${context}`;
1029
- await gcache.delete_session_cache(cacheKey);
1030
- }
1031
- }
1032
- static get_canModify(o) {
1033
- if (o instanceof SimpleObject)
1034
- return o.canModify;
1035
- return false;
1036
- }
1037
- /* -------------------------------------------------------------------------- */
1038
- static equals(a, b) {
1039
- console.warn("Dont use the default equals method !");
1040
- if (a == undefined || b == undefined)
1041
- return false;
1042
- return a === b;
1043
- }
1044
- }
1045
- export default SimpleObject;
1141
+ */
1142
+ }, {
1143
+ key: "getSaveResult",
1144
+ value: function getSaveResult() {
1145
+ var original = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
1146
+ if (this.isSaved) return this.__saveResultObject;
1147
+ return false;
1148
+ }
1149
+ }, {
1150
+ key: "getSaveResultType",
1151
+ value: function getSaveResultType() {
1152
+ return this.__saveResultType || "nothing";
1153
+ }
1154
+ /** did we create on the last save */
1155
+ }, {
1156
+ key: "getIsLastSaveCreate",
1157
+ value: function getIsLastSaveCreate() {
1158
+ return this.isSaved && this.getSaveResult().insertedId;
1159
+ }
1160
+ /* -------------------------------------------------------------------------- */
1161
+ /* statics */
1162
+ /* -------------------------------------------------------------------------- */
1163
+ }], [{
1164
+ key: "getValue",
1165
+ value: function getValue(o, key) {
1166
+ var original = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1167
+ if (o == null) return undefined;
1168
+ if (o instanceof SimpleObject && typeof o.getValue === "function") return o.getValue(key, original);
1169
+ if (!original && o.__use_delta != false && _typeof(o.__delta) === "object" && key in o.__delta) {
1170
+ //@ts-ignore
1171
+ return o.__delta[key];
1172
+ }
1173
+ return o[key];
1174
+ }
1175
+ /* -------------------------------------------------------------------------- */
1176
+ /* setValue */
1177
+ /* -------------------------------------------------------------------------- */
1178
+ }, {
1179
+ key: "loadValues",
1180
+ value: function loadValues(obj, initObject) {
1181
+ var fullUpdate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1182
+ if (typeof initObject === "number" || typeof initObject === "string") {
1183
+ initObject = {
1184
+ _id: initObject
1185
+ };
1186
+ }
1187
+ SimpleObject.factoryClear(obj);
1188
+ if (initObject == null) {
1189
+ for (var key in obj) {
1190
+ if (typeof obj[key] === "function") continue;
1191
+ if (key.startsWith("__")) continue;
1192
+ delete obj[key];
1193
+ }
1194
+ } else if (_typeof(initObject) == "object") {
1195
+ for (var _key3 in initObject) {
1196
+ if (_key3.startsWith("__")) continue;
1197
+ if (fullUpdate || Object.prototype.hasOwnProperty.call(obj, _key3)) {
1198
+ obj[_key3] = initObject[_key3];
1199
+ }
1200
+ }
1201
+ }
1202
+ return true;
1203
+ }
1204
+ }, {
1205
+ key: "canChangeKey",
1206
+ value: function canChangeKey(object, key, options) {
1207
+ if (!object || !key) return false;
1208
+ var useExtraProps = options.useExtraProps,
1209
+ ignoreProtected = options.ignoreProtected;
1210
+ if (useExtraProps) {
1211
+ var __extraProperties = object.constructor["__extraProperties"];
1212
+ if (__extraProperties != null && __extraProperties.includes(key)) return true;
1213
+ }
1214
+ /* -------------------------------------------------------------------------- */
1215
+ var __changeables = object.constructor["__changeables"];
1216
+ var __unchangeables = object.constructor["__unchangeables"];
1217
+ /* -------------------------------------------------------------------------- */
1218
+ if (__changeables != null && !__changeables.includes(key)) return false;
1219
+ if (__unchangeables != null && __unchangeables.includes(key)) return false;
1220
+ if (ignoreProtected !== true) {
1221
+ var __protectedProperties = object.constructor["__protectedProperties"];
1222
+ if (__protectedProperties != null && __protectedProperties.includes(key)) {
1223
+ //protected keys can only be changed by the creator of this object
1224
+ //make sure you set the is_current_user function correctly
1225
+ if (object.isNew) return true;
1226
+ var creatorOfObject = object.get("_creator");
1227
+ return Boolean(_global.gfunc.run_action_sync("is_current_user", creatorOfObject));
1228
+ }
1229
+ }
1230
+ return true;
1231
+ }
1232
+ }, {
1233
+ key: "hasIllegalChanges",
1234
+ value: function hasIllegalChanges(referenceObject, changesObject, updaterUser) {
1235
+ var __protectedProperties = (0, _object.object_getStatic)(referenceObject, "__protectedProperties");
1236
+ if ((0, _object.object_isEmpty)(__protectedProperties)) return false;
1237
+ var referenceUser = (0, _object.object_isClass)(referenceObject) ? (0, _object.object_get)(changesObject, "__user", "_creator") : (0, _object.object_get)(referenceObject, "__user", "_creator");
1238
+ if (Boolean(SimpleObject.getID(referenceUser) == SimpleObject.getID(updaterUser))) {
1239
+ return false;
1240
+ }
1241
+ for (var key in changesObject) {
1242
+ if (__protectedProperties.includes(key)) {
1243
+ return true;
1244
+ }
1245
+ }
1246
+ return false;
1247
+ }
1248
+ }, {
1249
+ key: "setReadonly",
1250
+ value: function setReadonly(objs) {
1251
+ var readonlyval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
1252
+ var clearChanges = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1253
+ var checkObjs = [];
1254
+ if (!objs) objs = [];
1255
+ if (!Array.isArray(objs)) checkObjs = [objs];else checkObjs = objs;
1256
+ for (var index = 0; index < checkObjs.length; index++) {
1257
+ var obj = checkObjs[index];
1258
+ obj.__is_readonly = readonlyval;
1259
+ if (obj instanceof SimpleObject) {
1260
+ if (obj.canModify) {
1261
+ if (!obj.getOriginal()) obj.setOriginal();
1262
+ } else {
1263
+ if (clearChanges) obj.clear_changes();
1264
+ //we dont need the original object in readonly mode
1265
+ obj.setOriginal(null);
1266
+ }
1267
+ }
1268
+ }
1269
+ }
1270
+ }, {
1271
+ key: "setThrowErrors",
1272
+ value: function setThrowErrors(objs) {
1273
+ var throwErr = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
1274
+ var checkObjs = [];
1275
+ if (!objs) objs = [];
1276
+ if (!Array.isArray(objs)) checkObjs = [objs];else checkObjs = objs;
1277
+ for (var index = 0; index < checkObjs.length; index++) {
1278
+ var element = checkObjs[index];
1279
+ element.__throw_errors = throwErr;
1280
+ }
1281
+ }
1282
+ }, {
1283
+ key: "isDeleted",
1284
+ value: function isDeleted(obj) {
1285
+ if (obj instanceof SimpleObject) return obj.isDeleted === true;
1286
+ return false;
1287
+ }
1288
+ }, {
1289
+ key: "getIndexOrID",
1290
+ value: function getIndexOrID(o) {
1291
+ var res = SimpleObject.getIndex(o);
1292
+ if (!res || res == -1) res = SimpleObject.getID(o);
1293
+ return res || -1;
1294
+ }
1295
+ }, {
1296
+ key: "getIndex",
1297
+ value: function getIndex(o) {
1298
+ if (o == null || o == undefined) return -1;
1299
+ switch (_typeof(o)) {
1300
+ case "object":
1301
+ return o._index_value || -1;
1302
+ case "number":
1303
+ case "string":
1304
+ return Number(o);
1305
+ }
1306
+ return -1;
1307
+ }
1308
+ }, {
1309
+ key: "getID",
1310
+ value: function getID(o) {
1311
+ if (o == null || o == undefined) return "";
1312
+ switch (_typeof(o)) {
1313
+ case "object":
1314
+ if (o._bsontype == "ObjectID" && typeof o.toString === "function") {
1315
+ return o.toString();
1316
+ }
1317
+ return String(o._id || o.insertedId || o.ID || o.id || o.__data_id || "");
1318
+ case "string":
1319
+ case "number":
1320
+ return String(o);
1321
+ }
1322
+ return "";
1323
+ }
1324
+ }, {
1325
+ key: "setOriginal",
1326
+ value: function setOriginal(obj) {
1327
+ var toObj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
1328
+ if (!(obj instanceof SimpleObject)) return undefined;
1329
+ return obj.setOriginal(toObj);
1330
+ }
1331
+ }, {
1332
+ key: "getOriginal",
1333
+ value: function getOriginal(obj) {
1334
+ if (obj instanceof SimpleObject) return obj.getOriginal();
1335
+ return obj === null || obj === void 0 ? void 0 : obj.__originalobject;
1336
+ }
1337
+ }, {
1338
+ key: "addError",
1339
+ value: function addError(o, title, message) {
1340
+ var sendToRegister = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "auto";
1341
+ var errStr = (0, _string.combine_if_not_blank)(title, " : ", message);
1342
+ if (o) {
1343
+ if (!Array.isArray(o.__errors)) o.__errors = [];
1344
+ o.__errors.push(errStr);
1345
+ if (o.__showErrors === false) return;
1346
+ } else if (sendToRegister === "auto") sendToRegister = true;
1347
+ if (sendToRegister === true) {
1348
+ console.error("[REGISTER-ERROR]", errStr);
1349
+ _global.gfunc.run_action("add_error", {
1350
+ title: title,
1351
+ message: message,
1352
+ type: "error"
1353
+ });
1354
+ } else {
1355
+ console.error("[ERROR]", errStr);
1356
+ }
1357
+ return (o === null || o === void 0 ? void 0 : o.__errors) || [];
1358
+ }
1359
+ }, {
1360
+ key: "getErrors",
1361
+ value: function getErrors(o) {
1362
+ return (o === null || o === void 0 ? void 0 : o.__errors) || [];
1363
+ }
1364
+ }, {
1365
+ key: "clearErrors",
1366
+ value: function clearErrors(o) {
1367
+ if (o) o.__errors = [];
1368
+ return true;
1369
+ }
1370
+ /* -------------------------------------------------------------------------- */
1371
+ /* Save Results */
1372
+ /* -------------------------------------------------------------------------- */
1373
+ /**
1374
+ * Checks if the given object is a valid and truthy save result
1375
+ * @param result_object
1376
+ * @returns
1377
+ */
1378
+ }, {
1379
+ key: "isResultValid",
1380
+ value: function isResultValid(result_object) {
1381
+ var sub = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1382
+ if ((0, _object.object_isEmpty)(result_object)) return false;
1383
+ switch (_typeof(result_object)) {
1384
+ case "string":
1385
+ try {
1386
+ result_object = JSON.parse(result_object);
1387
+ return this.isResultValid(result_object);
1388
+ } catch (_unused) {
1389
+ return false;
1390
+ }
1391
+ case "object":
1392
+ /* --------- maybe website sends the result as array with 1 element --------- */
1393
+ if (Array.isArray(result_object)) {
1394
+ if (sub) return false; //avoid going too deep
1395
+ return this.isResultValid((0, _object.array_first)(result_object), true);
1396
+ }
1397
+ /* -------------------------- check if acknowledged ------------------------- */
1398
+ if ((0, _object.object_isTrue)(result_object.acknowledged)) {
1399
+ return true;
1400
+ }
1401
+ if (result_object.expected && result_object.result) {
1402
+ return (0, _object.object_equals)(result_object.expected, result_object.result);
1403
+ }
1404
+ return !(0, _string.string_isEmpty)(this.getID(result_object));
1405
+ case "number":
1406
+ return result_object > 0;
1407
+ case "boolean":
1408
+ return result_object === true;
1409
+ }
1410
+ return false;
1411
+ }
1412
+ }, {
1413
+ key: "setTemp",
1414
+ value: (function () {
1415
+ var _setTemp2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(obj, key, value) {
1416
+ var sendChanges,
1417
+ _args16 = arguments;
1418
+ return _regeneratorRuntime().wrap(function _callee16$(_context16) {
1419
+ while (1) switch (_context16.prev = _context16.next) {
1420
+ case 0:
1421
+ sendChanges = _args16.length > 3 && _args16[3] !== undefined ? _args16[3] : false;
1422
+ if (!(_typeof(obj) !== "object" || !obj || Array.isArray(obj))) {
1423
+ _context16.next = 3;
1424
+ break;
1425
+ }
1426
+ return _context16.abrupt("return");
1427
+ case 3:
1428
+ if (!(obj instanceof SimpleObject)) {
1429
+ _context16.next = 7;
1430
+ break;
1431
+ }
1432
+ _context16.next = 6;
1433
+ return obj.setTemp(key, value, sendChanges);
1434
+ case 6:
1435
+ return _context16.abrupt("return", _context16.sent);
1436
+ case 7:
1437
+ //
1438
+ if (obj.__temp == null) obj.__temp = {};
1439
+ (0, _object.object_set)(obj.__temp, key, value);
1440
+ case 9:
1441
+ case "end":
1442
+ return _context16.stop();
1443
+ }
1444
+ }, _callee16);
1445
+ }));
1446
+ function setTemp(_x25, _x26, _x27) {
1447
+ return _setTemp2.apply(this, arguments);
1448
+ }
1449
+ return setTemp;
1450
+ }())
1451
+ }, {
1452
+ key: "factoryClear",
1453
+ value: function factoryClear(obj) {
1454
+ obj.clear_changes();
1455
+ SimpleObject.clearErrors(obj);
1456
+ }
1457
+ }, {
1458
+ key: "delete_cache",
1459
+ value: function () {
1460
+ var _delete_cache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17(context) {
1461
+ var cacheKey;
1462
+ return _regeneratorRuntime().wrap(function _callee17$(_context17) {
1463
+ while (1) switch (_context17.prev = _context17.next) {
1464
+ case 0:
1465
+ if (this.__db_name) {
1466
+ _context17.next = 2;
1467
+ break;
1468
+ }
1469
+ return _context17.abrupt("return");
1470
+ case 2:
1471
+ if (context) {
1472
+ _context17.next = 7;
1473
+ break;
1474
+ }
1475
+ _context17.next = 5;
1476
+ return _global.gcache.delete_session_cache(this.__db_name);
1477
+ case 5:
1478
+ _context17.next = 10;
1479
+ break;
1480
+ case 7:
1481
+ cacheKey = "".concat(this.__db_name, "-").concat(context);
1482
+ _context17.next = 10;
1483
+ return _global.gcache.delete_session_cache(cacheKey);
1484
+ case 10:
1485
+ case "end":
1486
+ return _context17.stop();
1487
+ }
1488
+ }, _callee17, this);
1489
+ }));
1490
+ function delete_cache(_x28) {
1491
+ return _delete_cache.apply(this, arguments);
1492
+ }
1493
+ return delete_cache;
1494
+ }()
1495
+ }, {
1496
+ key: "get_canModify",
1497
+ value: function get_canModify(o) {
1498
+ if (o instanceof SimpleObject) return o.canModify;
1499
+ return false;
1500
+ }
1501
+ /* -------------------------------------------------------------------------- */
1502
+ }, {
1503
+ key: "equals",
1504
+ value: function equals(a, b) {
1505
+ console.warn("Dont use the default equals method !");
1506
+ if (a == undefined || b == undefined) return false;
1507
+ return a === b;
1508
+ }
1509
+ }]);
1510
+ }(_MapableObject2.MapableObject);
1511
+ function _setDefaultProperty(key, value) {
1512
+ //@ts-ignore
1513
+ if (this[key] == undefined || this[key] === "" || this[key] === 0) {
1514
+ this[key] = value;
1515
+ return true;
1516
+ }
1517
+ return false;
1518
+ }
1519
+ function _setDefaultPropertyNow(key) {
1520
+ //@ts-ignore
1521
+ if (this[key] == undefined || this[key] === "" || this[key] === 0) {
1522
+ this[key] = new Date();
1523
+ return true;
1524
+ }
1525
+ return false;
1526
+ }
1527
+ /* -------------------------------------------------------------------------- */
1528
+ /* getProperty */
1529
+ /* -------------------------------------------------------------------------- */
1530
+ function _getProperty(key) {
1531
+ var original = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1532
+ //check if we should treat the prop as a function
1533
+ var is_fnc = this.constructor["__functionProperties"] != null && this.constructor["__functionProperties"].includes(key);
1534
+ //check if we should use delta version or normal version for this property
1535
+ var use_orig = original || !this.__use_delta || !(key in this.__delta);
1536
+ if (is_fnc) {
1537
+ if (use_orig) return (0, _object.fn_eval_this)(this, this[key]);
1538
+ return (0, _object.fn_eval_this)(this, this.__delta[key]);
1539
+ }
1540
+ if (use_orig) return this[key];
1541
+ return this.__delta[key];
1542
+ }
1543
+ function _setSubProperty(key, subkey, value) {
1544
+ var subobj = _assertClassBrand(_SimpleObject_brand, this, _getProperty).call(this, key);
1545
+ //@ts-ignore
1546
+ if (subobj == null || _typeof(subobj) !== "object") subobj = {};else subobj = _objectSpread({}, subobj); //make a clone so we dont update the original
1547
+ subobj[subkey] = value;
1548
+ _assertClassBrand(_SimpleObject_brand, this, _setProperty).call(this, key, subobj);
1549
+ }
1550
+ function _getSubProperty(key, subkey) {
1551
+ var original = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1552
+ var subobj = _assertClassBrand(_SimpleObject_brand, this, _getProperty).call(this, key, original);
1553
+ if (subobj == null || _typeof(subobj) !== "object") return undefined;
1554
+ return subobj[subkey];
1555
+ }
1556
+ /**
1557
+ * update a property on this object by updating the `__delta`
1558
+ * or updating the object property directly depending on `__use_delta` setting
1559
+ *
1560
+ * @param key
1561
+ * @param value
1562
+ * @returns true if state of `__has_delta` is changed, false otherwise
1563
+ */
1564
+ function _setProperty(key, value) {
1565
+ var checkIsChanged = false;
1566
+ if (this.__use_delta) {
1567
+ if (this[key] === value) {
1568
+ /**
1569
+ * we set something back to its original value
1570
+ * no need to save it as delta
1571
+ */
1572
+ delete this.__delta[key];
1573
+ checkIsChanged = true; //check if we should set __has_delta to false
1574
+ } else {
1575
+ this.__delta[key] = value; //always update delta to keep track of changes
1576
+ }
1577
+ } else {
1578
+ this[key] = value; //on noDelta mode update the object itself
1579
+ this.__delta[key] = value; //always update delta to keep track of changes
1580
+ }
1581
+ if (checkIsChanged || this.__has_delta === false) {
1582
+ /**
1583
+ * if any key exists in the `__delta` then the object is changed
1584
+ */
1585
+ var next__has_delta = false;
1586
+ for (var _key4 in this.__delta) {
1587
+ next__has_delta = true;
1588
+ break;
1589
+ }
1590
+ var previous__has_delta = this.__has_delta;
1591
+ this.__has_delta = next__has_delta;
1592
+ /**
1593
+ * if the state of __has_delta is changed we return true
1594
+ * so we can run the needed events
1595
+ */
1596
+ return previous__has_delta != this.__has_delta;
1597
+ }
1598
+ return false;
1599
+ }
1600
+ function _setSaveResultObject(toObj) {
1601
+ if (toObj == null) toObj = null; //null or undefined
1602
+ this.__saveResultObject = toObj;
1603
+ return true;
1604
+ }
1605
+ _defineProperty(SimpleObject, "__db_name", "");
1606
+ _defineProperty(SimpleObject, "__object_name", "");
1607
+ //these properties should not be changed and are usually set by server
1608
+ _defineProperty(SimpleObject, "__unchangeables", void 0);
1609
+ //these properties can be directly changed and are included in deltaObject
1610
+ _defineProperty(SimpleObject, "__changeables", void 0);
1611
+ //these properties can only be changed by the _creator
1612
+ _defineProperty(SimpleObject, "__protectedProperties", void 0);
1613
+ //these properties will be evaluated when needed
1614
+ _defineProperty(SimpleObject, "__functionProperties", void 0);
1615
+ //apply these string formats
1616
+ _defineProperty(SimpleObject, "__formatList", void 0);
1617
+ //these properties usually are not directly changed and sometimes are included in deltaObject
1618
+ _defineProperty(SimpleObject, "__extraProperties", void 0);
1619
+ //types for children can be used when importing children
1620
+ _defineProperty(SimpleObject, "__childTypes", void 0);
1621
+ //these fields are used when importing
1622
+ _defineProperty(SimpleObject, "__importColumns", void 0);
1623
+ //these fields can be used when importing and checking if an item exists
1624
+ _defineProperty(SimpleObject, "__checkProperties", void 0);
1625
+ _defineProperty(SimpleObject, "__use_delta", true);
1626
+ var _default = exports["default"] = SimpleObject;