@domql/state 3.2.3 → 3.2.8

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.
@@ -0,0 +1,1051 @@
1
+ "use strict";
2
+ var DomqlState = (() => {
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // index.js
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ add: () => add,
25
+ apply: () => apply,
26
+ applyFunction: () => applyFunction,
27
+ applyInitialState: () => applyInitialState,
28
+ applyReplace: () => applyReplace,
29
+ applyStateMethods: () => applyStateMethods,
30
+ clean: () => clean,
31
+ createState: () => createState,
32
+ destroy: () => destroy,
33
+ getByPath: () => getByPath,
34
+ hoistStateUpdate: () => hoistStateUpdate,
35
+ keys: () => keys,
36
+ parentUpdate: () => parentUpdate,
37
+ parse: () => parse,
38
+ quietReplace: () => quietReplace,
39
+ quietUpdate: () => quietUpdate,
40
+ remove: () => remove,
41
+ removeByPath: () => removeByPath,
42
+ removePathCollection: () => removePathCollection,
43
+ replace: () => replace,
44
+ reset: () => reset,
45
+ rootUpdate: () => rootUpdate,
46
+ set: () => set,
47
+ setByPath: () => setByPath,
48
+ setPathCollection: () => setPathCollection,
49
+ toggle: () => toggle,
50
+ updateState: () => updateState,
51
+ values: () => values
52
+ });
53
+
54
+ // ../utils/dist/esm/globals.js
55
+ var window = globalThis;
56
+ var document2 = window.document;
57
+
58
+ // ../utils/dist/esm/node.js
59
+ var isNode = (obj) => {
60
+ return (typeof Node === "object" ? obj instanceof window.Node : obj && typeof obj === "object" && typeof obj.nodeType === "number" && typeof obj.nodeName === "string") || false;
61
+ };
62
+ var isHtmlElement = (obj) => {
63
+ return (typeof HTMLElement === "object" ? obj instanceof window.HTMLElement : obj && typeof obj === "object" && obj !== null && obj.nodeType === 1 && typeof obj.nodeName === "string") || false;
64
+ };
65
+ var isDOMNode = (obj) => {
66
+ return typeof window !== "undefined" && (obj instanceof window.Node || obj instanceof window.Window || obj === window || obj === document);
67
+ };
68
+
69
+ // ../utils/dist/esm/types.js
70
+ var isObject = (arg) => {
71
+ if (arg === null) return false;
72
+ return typeof arg === "object" && arg.constructor === Object;
73
+ };
74
+ var isString = (arg) => typeof arg === "string";
75
+ var isNumber = (arg) => typeof arg === "number";
76
+ var isFunction = (arg) => typeof arg === "function";
77
+ var isBoolean = (arg) => arg === true || arg === false;
78
+ var isNull = (arg) => arg === null;
79
+ var isArray = (arg) => Array.isArray(arg);
80
+ var isDate = (d) => d instanceof Date;
81
+ var isObjectLike = (arg) => {
82
+ if (arg === null) return false;
83
+ return typeof arg === "object";
84
+ };
85
+ var isDefined = (arg) => arg !== void 0;
86
+ var isUndefined = (arg) => arg === void 0;
87
+ var TYPES = {
88
+ boolean: isBoolean,
89
+ array: isArray,
90
+ object: isObject,
91
+ string: isString,
92
+ date: isDate,
93
+ number: isNumber,
94
+ null: isNull,
95
+ function: isFunction,
96
+ objectLike: isObjectLike,
97
+ node: isNode,
98
+ htmlElement: isHtmlElement,
99
+ defined: isDefined
100
+ };
101
+ var is = (arg) => {
102
+ return (...args) => args.some((val) => TYPES[val](arg));
103
+ };
104
+
105
+ // ../utils/dist/esm/array.js
106
+ var removeFromArray = (arr, index) => {
107
+ if (isString(index)) index = parseInt(index);
108
+ if (isNumber(index)) {
109
+ if (index < 0 || index >= arr.length || isNaN(index)) {
110
+ throw new Error("Invalid index");
111
+ }
112
+ arr.splice(index, 1);
113
+ } else {
114
+ throw new Error("Invalid index");
115
+ }
116
+ return arr;
117
+ };
118
+ var unstackArrayOfObjects = (arr, exclude = []) => {
119
+ return arr.reduce(
120
+ (a, c) => deepMerge(a, deepClone(c, { exclude }), exclude),
121
+ {}
122
+ );
123
+ };
124
+ var addProtoToArray = (state, proto) => {
125
+ for (const key in proto) {
126
+ Object.defineProperty(state, key, {
127
+ value: proto[key],
128
+ enumerable: false,
129
+ // Set this to true if you want the method to appear in for...in loops
130
+ configurable: true,
131
+ // Set this to true if you want to allow redefining/removing the property later
132
+ writable: true
133
+ // Set this to true if you want to allow changing the function later
134
+ });
135
+ }
136
+ };
137
+
138
+ // ../utils/dist/esm/keys.js
139
+ var STATE_METHODS = /* @__PURE__ */ new Set([
140
+ "update",
141
+ "parse",
142
+ "clean",
143
+ "create",
144
+ "destroy",
145
+ "add",
146
+ "toggle",
147
+ "remove",
148
+ "apply",
149
+ "set",
150
+ "reset",
151
+ "replace",
152
+ "quietReplace",
153
+ "quietUpdate",
154
+ "applyReplace",
155
+ "applyFunction",
156
+ "keys",
157
+ "values",
158
+ "ref",
159
+ "rootUpdate",
160
+ "parentUpdate",
161
+ "parent",
162
+ "__element",
163
+ "__depends",
164
+ "__ref",
165
+ "__children",
166
+ "root",
167
+ "setByPath",
168
+ "setPathCollection",
169
+ "removeByPath",
170
+ "removePathCollection",
171
+ "getByPath"
172
+ ]);
173
+ var PROPS_METHODS = /* @__PURE__ */ new Set(["update", "__element"]);
174
+ var METHODS = /* @__PURE__ */ new Set([
175
+ "set",
176
+ "reset",
177
+ "update",
178
+ "remove",
179
+ "updateContent",
180
+ "removeContent",
181
+ "lookup",
182
+ "lookdown",
183
+ "lookdownAll",
184
+ "getRef",
185
+ "getPath",
186
+ "setNodeStyles",
187
+ "spotByPath",
188
+ "keys",
189
+ "parse",
190
+ "setProps",
191
+ "parseDeep",
192
+ "variables",
193
+ "if",
194
+ "log",
195
+ "verbose",
196
+ "warn",
197
+ "error",
198
+ "call",
199
+ "nextElement",
200
+ "previousElement",
201
+ "getRootState",
202
+ "getRoot",
203
+ "getRootData",
204
+ "getRootContext",
205
+ "getContext",
206
+ "getChildren"
207
+ ]);
208
+ var METHODS_EXL = /* @__PURE__ */ new Set([
209
+ "node",
210
+ "context",
211
+ "extends",
212
+ "__element",
213
+ "__ref",
214
+ ...METHODS,
215
+ ...STATE_METHODS,
216
+ ...PROPS_METHODS
217
+ ]);
218
+
219
+ // ../utils/dist/esm/object.js
220
+ var _startsWithDunder = (e) => e.charCodeAt(0) === 95 && e.charCodeAt(1) === 95;
221
+ var exec = (param, element, state, context) => {
222
+ if (isFunction(param)) {
223
+ if (!element) return;
224
+ const result = param.call(
225
+ element,
226
+ element,
227
+ state || element.state,
228
+ context || element.context
229
+ );
230
+ if (result && typeof result.then === "function") {
231
+ let resolved;
232
+ result.then((value) => {
233
+ resolved = value;
234
+ });
235
+ return resolved;
236
+ }
237
+ return result;
238
+ }
239
+ return param;
240
+ };
241
+ var merge = (element, obj, excludeFrom = []) => {
242
+ const useSet = excludeFrom instanceof Set;
243
+ for (const e in obj) {
244
+ if (!Object.prototype.hasOwnProperty.call(obj, e)) continue;
245
+ if (_startsWithDunder(e)) continue;
246
+ if (useSet ? excludeFrom.has(e) : excludeFrom.includes(e)) continue;
247
+ if (element[e] === void 0) {
248
+ element[e] = obj[e];
249
+ }
250
+ }
251
+ return element;
252
+ };
253
+ var deepMerge = (element, extend, excludeFrom = METHODS_EXL) => {
254
+ const useSet = excludeFrom instanceof Set;
255
+ for (const e in extend) {
256
+ if (!Object.prototype.hasOwnProperty.call(extend, e)) continue;
257
+ if (_startsWithDunder(e)) continue;
258
+ if (useSet ? excludeFrom.has(e) : excludeFrom.includes(e)) continue;
259
+ const elementProp = element[e];
260
+ const extendProp = extend[e];
261
+ if (isObjectLike(elementProp) && isObjectLike(extendProp)) {
262
+ deepMerge(elementProp, extendProp, excludeFrom);
263
+ } else if (elementProp === void 0) {
264
+ element[e] = extendProp;
265
+ }
266
+ }
267
+ return element;
268
+ };
269
+ var deepClone = (obj, options = {}) => {
270
+ const {
271
+ exclude = [],
272
+ cleanUndefined = false,
273
+ cleanNull = false,
274
+ window: targetWindow,
275
+ visited = /* @__PURE__ */ new WeakMap(),
276
+ handleExtends = false
277
+ } = options;
278
+ const contentWindow = targetWindow || window || globalThis;
279
+ if (!isObjectLike(obj) || isDOMNode(obj)) {
280
+ return obj;
281
+ }
282
+ if (visited.has(obj)) {
283
+ return visited.get(obj);
284
+ }
285
+ const isArr = isArray(obj);
286
+ const clone2 = isArr ? [] : {};
287
+ visited.set(obj, clone2);
288
+ const excludeSet = exclude instanceof Set ? exclude : exclude.length > 3 ? new Set(exclude) : null;
289
+ for (const key in obj) {
290
+ if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
291
+ if (_startsWithDunder(key) || key === "__proto__") continue;
292
+ if (excludeSet ? excludeSet.has(key) : exclude.includes(key)) continue;
293
+ const value = obj[key];
294
+ if (cleanUndefined && value === void 0) continue;
295
+ if (cleanNull && value === null) continue;
296
+ if (isDOMNode(value)) {
297
+ clone2[key] = value;
298
+ continue;
299
+ }
300
+ if (handleExtends && key === "extends" && isArray(value)) {
301
+ clone2[key] = unstackArrayOfObjects(value, exclude);
302
+ continue;
303
+ }
304
+ if (isFunction(value) && options.window) {
305
+ clone2[key] = contentWindow.eval("(" + value.toString() + ")");
306
+ continue;
307
+ }
308
+ if (isObjectLike(value)) {
309
+ clone2[key] = deepClone(value, {
310
+ ...options,
311
+ visited
312
+ });
313
+ } else {
314
+ clone2[key] = value;
315
+ }
316
+ }
317
+ return clone2;
318
+ };
319
+ var overwriteShallow = (obj, params, excludeFrom = []) => {
320
+ const useSet = excludeFrom instanceof Set;
321
+ for (const e in params) {
322
+ if (_startsWithDunder(e)) continue;
323
+ if (useSet ? excludeFrom.has(e) : excludeFrom.includes(e)) continue;
324
+ obj[e] = params[e];
325
+ }
326
+ return obj;
327
+ };
328
+ var overwriteDeep = (obj, params, opts = {}, visited = /* @__PURE__ */ new WeakMap()) => {
329
+ if (!isObjectLike(obj) || !isObjectLike(params) || isDOMNode(obj) || isDOMNode(params)) {
330
+ return params;
331
+ }
332
+ if (visited.has(obj)) return visited.get(obj);
333
+ visited.set(obj, obj);
334
+ const excl = opts.exclude;
335
+ const exclSet = excl ? excl instanceof Set ? excl : new Set(excl) : null;
336
+ const forcedExclude = !opts.preventForce;
337
+ for (const e in params) {
338
+ if (!Object.prototype.hasOwnProperty.call(params, e)) continue;
339
+ if (exclSet && exclSet.has(e) || forcedExclude && _startsWithDunder(e)) continue;
340
+ const objProp = obj[e];
341
+ const paramsProp = params[e];
342
+ if (isDOMNode(paramsProp)) {
343
+ obj[e] = paramsProp;
344
+ } else if (isObjectLike(objProp) && isObjectLike(paramsProp)) {
345
+ obj[e] = overwriteDeep(objProp, paramsProp, opts, visited);
346
+ } else if (paramsProp !== void 0) {
347
+ obj[e] = paramsProp;
348
+ }
349
+ }
350
+ return obj;
351
+ };
352
+ var removeFromObject = (obj, props) => {
353
+ if (props === void 0 || props === null) return obj;
354
+ if (is(props)("string", "number")) {
355
+ delete obj[props];
356
+ } else if (isArray(props)) {
357
+ for (let i = 0; i < props.length; i++) delete obj[props[i]];
358
+ } else {
359
+ throw new Error(
360
+ "Invalid input: props must be a string or an array of strings"
361
+ );
362
+ }
363
+ return obj;
364
+ };
365
+ var createNestedObject = (arr, lastValue) => {
366
+ if (arr.length === 0) return lastValue;
367
+ const nestedObject = {};
368
+ let current = nestedObject;
369
+ for (let i = 0; i < arr.length; i++) {
370
+ if (i === arr.length - 1 && lastValue) {
371
+ current[arr[i]] = lastValue;
372
+ } else {
373
+ current[arr[i]] = {};
374
+ current = current[arr[i]];
375
+ }
376
+ }
377
+ return nestedObject;
378
+ };
379
+ var removeNestedKeyByPath = (obj, path) => {
380
+ if (!Array.isArray(path)) {
381
+ throw new Error("Path must be an array.");
382
+ }
383
+ let current = obj;
384
+ for (let i = 0; i < path.length - 1; i++) {
385
+ if (current[path[i]] === void 0) return;
386
+ current = current[path[i]];
387
+ }
388
+ const lastKey = path[path.length - 1];
389
+ if (current && Object.prototype.hasOwnProperty.call(current, lastKey)) {
390
+ delete current[lastKey];
391
+ }
392
+ };
393
+ var setInObjectByPath = (obj, path, value) => {
394
+ if (!Array.isArray(path)) {
395
+ throw new Error("Path must be an array.");
396
+ }
397
+ let current = obj;
398
+ for (let i = 0; i < path.length - 1; i++) {
399
+ if (!current[path[i]] || typeof current[path[i]] !== "object") {
400
+ current[path[i]] = {};
401
+ }
402
+ current = current[path[i]];
403
+ }
404
+ current[path[path.length - 1]] = value;
405
+ return obj;
406
+ };
407
+ var getInObjectByPath = (obj, path) => {
408
+ if (!Array.isArray(path)) {
409
+ throw new Error("Path must be an array.");
410
+ }
411
+ let current = obj;
412
+ for (let i = 0; i < path.length; i++) {
413
+ if (current === void 0 || current === null) {
414
+ return void 0;
415
+ }
416
+ current = current[path[i]];
417
+ }
418
+ return current;
419
+ };
420
+
421
+ // ../utils/dist/esm/state.js
422
+ var checkForStateTypes = (element) => {
423
+ const { state: orig, props, __ref: ref } = element;
424
+ const state = props?.state || orig;
425
+ if (isFunction(state)) {
426
+ ref.__state = state;
427
+ return exec(state, element);
428
+ } else if (is(state)("string", "number")) {
429
+ ref.__state = state;
430
+ return { value: state };
431
+ } else if (state === true) {
432
+ ref.__state = element.key;
433
+ return {};
434
+ } else if (state) {
435
+ ref.__hasRootState = true;
436
+ return state;
437
+ } else {
438
+ return false;
439
+ }
440
+ };
441
+ var getRootStateInKey = (stateKey, parentState) => {
442
+ if (!stateKey.includes("~/")) return;
443
+ const arr = stateKey.split("~/");
444
+ if (arr.length > 1) return parentState.root;
445
+ };
446
+ var getParentStateInKey = (stateKey, parentState) => {
447
+ if (!stateKey.includes("../")) return;
448
+ const arr = stateKey.split("../");
449
+ const arrLength = arr.length - 1;
450
+ for (let i = 0; i < arrLength; i++) {
451
+ if (!parentState.parent) return null;
452
+ parentState = parentState.parent;
453
+ }
454
+ return parentState;
455
+ };
456
+ var getChildStateInKey = (stateKey, parentState, options = {}) => {
457
+ const arr = isString(stateKey) ? stateKey.split("/") : [stateKey];
458
+ const arrLength = arr.length - 1;
459
+ for (let i = 0; i < arrLength; i++) {
460
+ const childKey = arr[i];
461
+ const grandChildKey = arr[i + 1];
462
+ if (childKey === "__proto__" || grandChildKey === "__proto__") return;
463
+ let childInParent = parentState[childKey];
464
+ if (!childInParent) childInParent = parentState[childKey] = {};
465
+ if (!childInParent[grandChildKey]) childInParent[grandChildKey] = {};
466
+ stateKey = grandChildKey;
467
+ parentState = childInParent;
468
+ }
469
+ if (options.returnParent) return parentState;
470
+ return parentState[stateKey];
471
+ };
472
+ var findInheritedState = (element, parent, options = {}) => {
473
+ const ref = element.__ref;
474
+ let stateKey = ref.__state;
475
+ if (!checkIfInherits(element)) return;
476
+ const rootState = getRootStateInKey(stateKey, parent.state);
477
+ let parentState = parent.state;
478
+ if (rootState) {
479
+ parentState = rootState;
480
+ stateKey = stateKey.replaceAll("~/", "");
481
+ } else {
482
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state);
483
+ if (findGrandParentState) {
484
+ parentState = findGrandParentState;
485
+ stateKey = stateKey.replaceAll("../", "");
486
+ }
487
+ }
488
+ if (!parentState) return;
489
+ return getChildStateInKey(stateKey, parentState, options);
490
+ };
491
+ var createInheritedState = (element, parent) => {
492
+ const ref = element.__ref;
493
+ const inheritedState = findInheritedState(element, parent);
494
+ if (inheritedState === void 0) return element.state;
495
+ if (is(inheritedState)("object", "array")) {
496
+ return deepClone(inheritedState);
497
+ } else if (is(inheritedState)("string", "number", "boolean")) {
498
+ ref.__stateType = typeof inheritedState;
499
+ return { value: inheritedState };
500
+ }
501
+ console.warn(ref.__state, "is not present. Replacing with", {});
502
+ };
503
+ var checkIfInherits = (element) => {
504
+ const { __ref: ref } = element;
505
+ const stateKey = ref?.__state;
506
+ if (stateKey && is(stateKey)("number", "string", "boolean")) return true;
507
+ return false;
508
+ };
509
+ var createNestedObjectByKeyPath = (path, value) => {
510
+ if (!path) {
511
+ return value || {};
512
+ }
513
+ const keys2 = path.split("/");
514
+ const obj = {};
515
+ let ref = obj;
516
+ const lastIdx = keys2.length - 1;
517
+ for (let i = 0; i <= lastIdx; i++) {
518
+ ref[keys2[i]] = i === lastIdx ? value || {} : {};
519
+ ref = ref[keys2[i]];
520
+ }
521
+ return obj;
522
+ };
523
+ var applyDependentState = (element, state) => {
524
+ const { __element } = state;
525
+ const origState = exec(__element?.state, element);
526
+ if (!origState) return;
527
+ const dependentState = deepClone(origState, STATE_METHODS);
528
+ const newDepends = { [element.key]: dependentState };
529
+ const __depends = isObject(origState.__depends) ? { ...origState.__depends, ...newDepends } : newDepends;
530
+ if (Array.isArray(origState)) {
531
+ addProtoToArray(origState, {
532
+ ...Object.getPrototypeOf(origState),
533
+ __depends
534
+ });
535
+ } else {
536
+ Object.setPrototypeOf(origState, {
537
+ ...Object.getPrototypeOf(origState),
538
+ __depends
539
+ });
540
+ }
541
+ return dependentState;
542
+ };
543
+ var overwriteState = (state, obj, options = {}) => {
544
+ const { overwrite } = options;
545
+ if (!overwrite) return;
546
+ const shallow = overwrite === "shallow";
547
+ const merge2 = overwrite === "merge";
548
+ if (merge2) {
549
+ deepMerge(state, obj, STATE_METHODS);
550
+ return;
551
+ }
552
+ const overwriteFunc = shallow ? overwriteShallow : overwriteDeep;
553
+ overwriteFunc(state, obj, STATE_METHODS);
554
+ };
555
+
556
+ // ../utils/dist/esm/triggerEvent.js
557
+ var getOnOrPropsEvent = (param, element) => {
558
+ const onEvent = element.on?.[param];
559
+ if (onEvent) return onEvent;
560
+ const props = element.props;
561
+ if (!props) return;
562
+ const propKey = "on" + param.charAt(0).toUpperCase() + param.slice(1);
563
+ return props[propKey];
564
+ };
565
+ var applyEvent = (param, element, state, context, options) => {
566
+ if (!isFunction(param)) return;
567
+ const result = param.call(
568
+ element,
569
+ element,
570
+ state || element.state,
571
+ context || element.context,
572
+ options
573
+ );
574
+ if (result && typeof result.then === "function") {
575
+ result.catch(() => {
576
+ });
577
+ }
578
+ return result;
579
+ };
580
+ var triggerEventOn = (param, element, options) => {
581
+ if (!element) {
582
+ throw new Error("Element is required");
583
+ }
584
+ const appliedFunction = getOnOrPropsEvent(param, element);
585
+ if (appliedFunction) {
586
+ const { state, context } = element;
587
+ return applyEvent(appliedFunction, element, state, context, options);
588
+ }
589
+ };
590
+ var applyEventUpdate = (param, updatedObj, element, state, context, options) => {
591
+ if (!isFunction(param)) return;
592
+ const result = param.call(
593
+ element,
594
+ updatedObj,
595
+ element,
596
+ state || element.state,
597
+ context || element.context,
598
+ options
599
+ );
600
+ if (result && typeof result.then === "function") {
601
+ result.catch(() => {
602
+ });
603
+ }
604
+ return result;
605
+ };
606
+ var triggerEventOnUpdate = (param, updatedObj, element, options) => {
607
+ const appliedFunction = getOnOrPropsEvent(param, element);
608
+ if (appliedFunction) {
609
+ const { state, context } = element;
610
+ return applyEventUpdate(
611
+ appliedFunction,
612
+ updatedObj,
613
+ element,
614
+ state,
615
+ context,
616
+ options
617
+ );
618
+ }
619
+ };
620
+
621
+ // ../../plugins/report/index.js
622
+ var ERRORS_REGISTRY = {
623
+ en: {
624
+ DocumentNotDefined: {
625
+ title: "Document is undefined",
626
+ description: "To tweak with DOM, you should use browser."
627
+ },
628
+ OverwriteToBuiltin: {
629
+ title: "Overwriting to builtin method",
630
+ description: "Overwriting a builtin method in the window define is not possible, please choose different name"
631
+ },
632
+ BrowserNotDefined: {
633
+ title: "Can't recognize environment",
634
+ description: "Environment should be browser application, that can run Javascript"
635
+ },
636
+ SetQuickPreferancesIsNotObject: {
637
+ title: "Quick preferances object is required",
638
+ description: 'Please pass a plain object with "lang", "culture" and "area" properties'
639
+ },
640
+ InvalidParams: {
641
+ title: "Params are invalid",
642
+ description: 'Please pass a plain object with "lang", "culture" and "area" properties'
643
+ },
644
+ CantCreateWithoutNode: {
645
+ title: "You must provide node",
646
+ description: "Can't create DOM element without setting node or text"
647
+ },
648
+ HTMLInvalidTag: {
649
+ title: "Element tag name (or DOM nodeName) is invalid",
650
+ description: "To create element, you must provide valid DOM node. See full list of them at here: http://www.w3schools.com/tags/"
651
+ },
652
+ HTMLInvalidAttr: {
653
+ title: "Attibutes object is invalid",
654
+ description: "Please pass a valid plain object to apply as an attributes for a DOM node"
655
+ },
656
+ HTMLInvalidData: {
657
+ title: "Data object is invalid",
658
+ description: "Please pass a valid plain object to apply as an dataset for a DOM node"
659
+ },
660
+ HTMLInvalidStyles: {
661
+ title: "Styles object is invalid",
662
+ description: "Please pass a valid plain object to apply as an style for a DOM node"
663
+ },
664
+ HTMLInvalidText: {
665
+ title: "Text string is invalid",
666
+ description: "Please pass a valid string to apply text to DOM node"
667
+ },
668
+ ElementOnStateIsNotDefined: {
669
+ title: "Element on state is not defined",
670
+ description: "Please check the element object"
671
+ }
672
+ }
673
+ };
674
+ var report = (err, arg, element) => {
675
+ const currentLang = "en";
676
+ let errObj;
677
+ if (err && typeof err === "string") errObj = ERRORS_REGISTRY[currentLang][err];
678
+ const description = errObj ? `
679
+
680
+ ${errObj.description}` : "";
681
+ const context = element ? `
682
+
683
+ ${element}` : "";
684
+ return new Error(`"${err}", "${arg}"${description}${context}`);
685
+ };
686
+
687
+ // updateState.js
688
+ var STATE_UPDATE_OPTIONS = {
689
+ overwrite: true,
690
+ preventHoistElementUpdate: false,
691
+ updateByState: true,
692
+ isHoisted: true,
693
+ execStateFunction: true
694
+ };
695
+ var updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
696
+ const state = this;
697
+ const element = state.__element;
698
+ if (options.onEach) options.onEach(element, state, element.context, options);
699
+ if (!options.updateByState) merge(options, STATE_UPDATE_OPTIONS);
700
+ if (!state.__element) report("ElementOnStateIsNotDefined");
701
+ if (options.preventInheritAtCurrentState === true) {
702
+ options.preventInheritAtCurrentState = state;
703
+ } else if (options.preventInheritAtCurrentState) return;
704
+ if (!options.preventBeforeStateUpdateListener) {
705
+ const beforeStateUpdateReturns = triggerEventOnUpdate(
706
+ "beforeStateUpdate",
707
+ obj,
708
+ element,
709
+ options
710
+ );
711
+ if (beforeStateUpdateReturns === false) return element;
712
+ }
713
+ overwriteState(state, obj, options);
714
+ const updateIsHoisted = hoistStateUpdate(state, obj, options);
715
+ if (updateIsHoisted) return state;
716
+ updateDependentState(state, obj, options);
717
+ applyElementUpdate(state, obj, options);
718
+ if (!options.preventStateUpdateListener) {
719
+ triggerEventOnUpdate("stateUpdate", obj, element, options);
720
+ }
721
+ return state;
722
+ };
723
+ var hoistStateUpdate = (state, obj, options) => {
724
+ const element = state.__element;
725
+ const { parent, __ref: ref } = element;
726
+ const stateKey = ref?.__state;
727
+ const stateType = ref?.__stateType;
728
+ if (!stateKey) return;
729
+ const asksForInherit = checkIfInherits(element);
730
+ const inheritedState = findInheritedState(element, parent, {
731
+ returnParent: true
732
+ });
733
+ const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
734
+ if (!shouldPropagateState) return;
735
+ const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
736
+ const value = isStringState ? state.value : state.parse();
737
+ const passedValue = isStringState ? state.value : obj;
738
+ const findRootState = getRootStateInKey(stateKey, parent.state);
739
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state);
740
+ const changesValue = createNestedObjectByKeyPath(stateKey, passedValue);
741
+ const targetParent = findRootState || findGrandParentState || parent.state;
742
+ if (options.replace) overwriteDeep(targetParent, changesValue || value);
743
+ targetParent.update(changesValue, {
744
+ execStateFunction: false,
745
+ isHoisted: true,
746
+ preventUpdate: options.preventHoistElementUpdate,
747
+ overwrite: !options.replace,
748
+ ...options
749
+ });
750
+ const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
751
+ if (!options.preventStateUpdateListener && hasNotUpdated) {
752
+ triggerEventOnUpdate("stateUpdate", obj, element, options);
753
+ }
754
+ return true;
755
+ };
756
+ var updateDependentState = (state, obj, options) => {
757
+ if (!state.__depends) return;
758
+ for (const el in state.__depends) {
759
+ const dependentState = state.__depends[el];
760
+ const cleanState = dependentState.clean();
761
+ cleanState.update(state.parse(), options);
762
+ }
763
+ };
764
+ var applyElementUpdate = (state, obj, options) => {
765
+ const element = state.__element;
766
+ if (options.preventUpdate !== true) {
767
+ element.update(
768
+ {},
769
+ {
770
+ ...options,
771
+ updateByState: true
772
+ }
773
+ );
774
+ } else if (options.preventUpdate === "recursive") {
775
+ element.update(
776
+ {},
777
+ {
778
+ ...options,
779
+ isHoisted: false,
780
+ updateByState: true,
781
+ preventUpdate: true
782
+ }
783
+ );
784
+ }
785
+ };
786
+
787
+ // methods.js
788
+ var parse = function() {
789
+ const state = this;
790
+ if (isObject(state)) {
791
+ const obj = {};
792
+ for (const param in state) {
793
+ if (!STATE_METHODS.has(param)) {
794
+ obj[param] = state[param];
795
+ }
796
+ }
797
+ return obj;
798
+ } else if (isArray(state)) {
799
+ return state.filter((item) => !STATE_METHODS.has(item));
800
+ }
801
+ };
802
+ var clean = function(options = {}) {
803
+ const state = this;
804
+ for (const param in state) {
805
+ if (!STATE_METHODS.has(param) && Object.prototype.hasOwnProperty.call(state, param)) {
806
+ delete state[param];
807
+ }
808
+ }
809
+ if (!options.preventStateUpdate) {
810
+ state.update(state, { replace: true, ...options });
811
+ }
812
+ return state;
813
+ };
814
+ var destroy = function(options = {}) {
815
+ const state = this;
816
+ const element = state.__element;
817
+ const stateKey = element.__ref.__state;
818
+ if (isString(stateKey)) {
819
+ element.parent.state.remove(stateKey, { isHoisted: true, ...options });
820
+ return element.state;
821
+ }
822
+ delete element.state;
823
+ element.state = state.parent;
824
+ if (state.parent) {
825
+ delete state.parent.__children[element.key];
826
+ }
827
+ if (state.__children) {
828
+ for (const key in state.__children) {
829
+ const child = state.__children[key];
830
+ if (child.state) {
831
+ if (isArray(child.state)) {
832
+ Object.defineProperty(child.state, "parent", {
833
+ value: state.parent,
834
+ enumerable: false,
835
+ // Set this to true if you want the method to appear in for...in loops
836
+ configurable: true,
837
+ // Set this to true if you want to allow redefining/removing the property later
838
+ writable: true
839
+ // Set this to true if you want to allow changing the function later
840
+ });
841
+ } else {
842
+ Object.setPrototypeOf(child, { parent: state.parent });
843
+ }
844
+ }
845
+ }
846
+ }
847
+ element.state.update({}, { isHoisted: true, ...options });
848
+ return element.state;
849
+ };
850
+ var parentUpdate = function(obj, options = {}) {
851
+ const state = this;
852
+ if (!state || !state.parent) return;
853
+ return state.parent.update(obj, { isHoisted: true, ...options });
854
+ };
855
+ var rootUpdate = function(obj, options = {}) {
856
+ const state = this;
857
+ if (!state) return;
858
+ const rootState = state.__element.__ref.root.state;
859
+ return rootState.update(obj, { isHoisted: false, ...options });
860
+ };
861
+ var add = function(value, options = {}) {
862
+ const state = this;
863
+ if (isArray(state)) {
864
+ state.push(value);
865
+ state.update(state.parse(), { overwrite: true, ...options });
866
+ } else if (isObject(state)) {
867
+ const key = Object.keys(state).length;
868
+ state.update({ [key]: value }, options);
869
+ }
870
+ };
871
+ var toggle = function(key, options = {}) {
872
+ const state = this;
873
+ state.update({ [key]: !state[key] }, options);
874
+ };
875
+ var remove = function(key, options = {}) {
876
+ const state = this;
877
+ if (isArray(state)) removeFromArray(state, key);
878
+ else if (isObject(state)) removeFromObject(state, key);
879
+ if (options.applyReset) {
880
+ return state.set(state.parse(), { replace: true, ...options });
881
+ }
882
+ return state.update({}, options);
883
+ };
884
+ var set = function(val, options = {}) {
885
+ const state = this;
886
+ const value = deepClone(val);
887
+ const cleanState = state.clean({ preventStateUpdate: true, ...options });
888
+ return cleanState.update(value, { replace: true, ...options });
889
+ };
890
+ var setByPath = function(path, val, options = {}) {
891
+ const state = this;
892
+ const value = deepClone(val);
893
+ if (!options.preventReplace) setInObjectByPath(state, path, val);
894
+ const update = createNestedObject(path, value);
895
+ if (options.preventStateUpdate) return update;
896
+ return state.update(update, options);
897
+ };
898
+ var setPathCollection = function(changes, options = {}) {
899
+ const state = this;
900
+ const update = changes.reduce((acc, change) => {
901
+ if (change[0] === "update") {
902
+ const result = setByPath.call(state, change[1], change[2], {
903
+ preventStateUpdate: true
904
+ });
905
+ return overwriteDeep(acc, result);
906
+ } else if (change[0] === "delete") {
907
+ removeByPath.call(state, change[1], {
908
+ ...options,
909
+ preventUpdate: true
910
+ });
911
+ }
912
+ return acc;
913
+ }, {});
914
+ return state.update(update, options);
915
+ };
916
+ var removeByPath = function(path, options = {}) {
917
+ const state = this;
918
+ removeNestedKeyByPath(state, path);
919
+ if (options.preventUpdate) return path;
920
+ return state.update({}, options);
921
+ };
922
+ var removePathCollection = function(changes, options = {}) {
923
+ const state = this;
924
+ for (let i = 0; i < changes.length; i++) {
925
+ removeByPath(changes[i], { preventUpdate: true });
926
+ }
927
+ return state.update({}, options);
928
+ };
929
+ var getByPath = function(path, options = {}) {
930
+ const state = this;
931
+ return getInObjectByPath(state, path);
932
+ };
933
+ var reset = function(options = {}) {
934
+ const state = this;
935
+ const value = deepClone(state.parse());
936
+ return state.set(value, { replace: true, ...options });
937
+ };
938
+ var apply = function(func, options = {}) {
939
+ const state = this;
940
+ if (isFunction(func)) {
941
+ const value = func(state);
942
+ return state.update(value, { replace: true, ...options });
943
+ }
944
+ };
945
+ var applyReplace = function(func, options = {}) {
946
+ const state = this;
947
+ if (isFunction(func)) {
948
+ const value = func(state);
949
+ return state.replace(value, options);
950
+ }
951
+ };
952
+ var applyFunction = function(func, options = {}) {
953
+ const state = this;
954
+ if (isFunction(func)) {
955
+ func(state);
956
+ return state.update(state.parse(), { replace: true, ...options });
957
+ }
958
+ };
959
+ var quietUpdate = function(obj, options = {}) {
960
+ const state = this;
961
+ return state.update(obj, { preventUpdate: true, ...options });
962
+ };
963
+ var replace = function(obj, options = {}) {
964
+ const state = this;
965
+ for (const param in obj) {
966
+ state[param] = obj[param];
967
+ }
968
+ return state.update(obj, options);
969
+ };
970
+ var quietReplace = function(obj, options = {}) {
971
+ const state = this;
972
+ return state.replace(obj, { preventUpdate: true, ...options });
973
+ };
974
+ var keys = function(obj, options = {}) {
975
+ const state = this;
976
+ return Object.keys(state);
977
+ };
978
+ var values = function(obj, options = {}) {
979
+ const state = this;
980
+ return Object.values(state);
981
+ };
982
+ var applyStateMethods = (element) => {
983
+ const state = element.state;
984
+ const ref = element.__ref;
985
+ const proto = {
986
+ clean: clean.bind(state),
987
+ parse: parse.bind(state),
988
+ destroy: destroy.bind(state),
989
+ update: updateState.bind(state),
990
+ rootUpdate: rootUpdate.bind(state),
991
+ parentUpdate: parentUpdate.bind(state),
992
+ create: createState.bind(state),
993
+ add: add.bind(state),
994
+ toggle: toggle.bind(state),
995
+ remove: remove.bind(state),
996
+ apply: apply.bind(state),
997
+ applyReplace: applyReplace.bind(state),
998
+ applyFunction: applyFunction.bind(state),
999
+ set: set.bind(state),
1000
+ quietUpdate: quietUpdate.bind(state),
1001
+ replace: replace.bind(state),
1002
+ quietReplace: quietReplace.bind(state),
1003
+ reset: reset.bind(state),
1004
+ parent: element.parent?.state || state,
1005
+ setByPath: setByPath.bind(state),
1006
+ setPathCollection: setPathCollection.bind(state),
1007
+ removeByPath: removeByPath.bind(state),
1008
+ removePathCollection: removePathCollection.bind(state),
1009
+ getByPath: getByPath.bind(state),
1010
+ keys: keys.bind(state),
1011
+ values: values.bind(state),
1012
+ __element: element,
1013
+ __children: {},
1014
+ root: ref?.root ? ref.root.state : state
1015
+ };
1016
+ if (isArray(state)) {
1017
+ addProtoToArray(state, proto);
1018
+ } else {
1019
+ Object.setPrototypeOf(state, proto);
1020
+ }
1021
+ if (state.parent && state.parent.__children) {
1022
+ state.parent.__children[element.key] = state;
1023
+ }
1024
+ };
1025
+
1026
+ // create.js
1027
+ var createState = function(element, parent, options) {
1028
+ element.state = applyInitialState(element, parent, options);
1029
+ return element.state;
1030
+ };
1031
+ var applyInitialState = function(element, parent, options) {
1032
+ const objectizeState = checkForStateTypes(element);
1033
+ if (objectizeState === false) return parent.state || {};
1034
+ else element.state = objectizeState;
1035
+ const whatInitReturns = triggerEventOn("stateInit", element, options);
1036
+ if (whatInitReturns === false) return element.state;
1037
+ if (checkIfInherits(element)) {
1038
+ const inheritedState = createInheritedState(element, parent);
1039
+ element.state = isUndefined(inheritedState) ? {} : inheritedState;
1040
+ }
1041
+ const dependentState = applyDependentState(
1042
+ element,
1043
+ element.state || parent.state || {}
1044
+ );
1045
+ if (dependentState) element.state = dependentState;
1046
+ applyStateMethods(element);
1047
+ triggerEventOn("stateCreated", element);
1048
+ return element.state;
1049
+ };
1050
+ return __toCommonJS(index_exports);
1051
+ })();