@azrico/webobject 1.5.14 → 1.5.15

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