@domql/state 2.3.118 → 2.3.120
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/createState.js +91 -0
- package/dist/cjs/createState.js +96 -0
- package/dist/cjs/ignore.js +41 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/methods.js +109 -0
- package/dist/cjs/package.json +4 -0
- package/dist/cjs/updateState.js +102 -0
- package/dist/cjs/utils.js +89 -0
- package/ignore.js +2 -2
- package/index.js +5 -278
- package/methods.js +90 -0
- package/package.json +22 -4
- package/updateState.js +96 -0
- package/utils.js +66 -0
package/createState.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { triggerEventOn } from '@domql/event'
|
|
4
|
+
import { deepClone, exec, is, isFunction, isObject } from '@domql/utils'
|
|
5
|
+
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
|
+
import { add, apply, clean, destroy, parse, remove, rootUpdate, toggle } from './methods'
|
|
7
|
+
import { updateState } from './updateState'
|
|
8
|
+
import { checkIfInherits, createInheritedState } from './utils'
|
|
9
|
+
|
|
10
|
+
export const createState = function (element, parent, opts) {
|
|
11
|
+
const skip = (opts && opts.skip) ? opts.skip : false
|
|
12
|
+
let { state, __ref: __elementRef } = element
|
|
13
|
+
|
|
14
|
+
state = element.state = checkForTypes(element)
|
|
15
|
+
|
|
16
|
+
triggerEventOn('stateInit', element)
|
|
17
|
+
|
|
18
|
+
if (checkIfInherits(element)) {
|
|
19
|
+
state = element.state = createInheritedState(element, parent) || {}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!state) {
|
|
23
|
+
if (parent && parent.state) return parent.state
|
|
24
|
+
return {}
|
|
25
|
+
} else {
|
|
26
|
+
__elementRef.__hasRootState = true
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// reference other state
|
|
30
|
+
// TODO: check why __ref is assigned with element
|
|
31
|
+
// /docs/intro
|
|
32
|
+
const { __ref } = state
|
|
33
|
+
if (__ref) {
|
|
34
|
+
state = deepClone(__ref, IGNORE_STATE_PARAMS)
|
|
35
|
+
if (isObject(__ref.__depends)) {
|
|
36
|
+
__ref.__depends[element.key] = state
|
|
37
|
+
} else __ref.__depends = { [element.key]: state }
|
|
38
|
+
} else {
|
|
39
|
+
state = deepClone(state, IGNORE_STATE_PARAMS)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
element.state = state
|
|
43
|
+
|
|
44
|
+
// NOTE: Only true when 'onlyResolveExtends' option is set to true
|
|
45
|
+
if (skip) return state
|
|
46
|
+
|
|
47
|
+
applyMethods(element, state)
|
|
48
|
+
|
|
49
|
+
// trigger `on.stateCreated`
|
|
50
|
+
triggerEventOn('stateCreated', element)
|
|
51
|
+
|
|
52
|
+
return state
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const checkForTypes = (element) => {
|
|
56
|
+
const { state, __ref: __elementRef } = element
|
|
57
|
+
if (isFunction(state)) {
|
|
58
|
+
__elementRef.__state = state
|
|
59
|
+
return exec(state, element)
|
|
60
|
+
}
|
|
61
|
+
if (is(state)('string', 'number')) {
|
|
62
|
+
__elementRef.__state = state
|
|
63
|
+
return {}
|
|
64
|
+
}
|
|
65
|
+
if (state === true) {
|
|
66
|
+
__elementRef.__state = element.key
|
|
67
|
+
return {}
|
|
68
|
+
}
|
|
69
|
+
return state
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const applyMethods = (element, state) => {
|
|
73
|
+
const __elementRef = element.__ref
|
|
74
|
+
|
|
75
|
+
state.clean = clean
|
|
76
|
+
state.parse = parse
|
|
77
|
+
state.destroy = destroy
|
|
78
|
+
state.update = updateState
|
|
79
|
+
state.rootUpdate = rootUpdate
|
|
80
|
+
state.create = createState
|
|
81
|
+
state.add = add
|
|
82
|
+
state.toggle = toggle
|
|
83
|
+
state.remove = remove
|
|
84
|
+
state.apply = apply
|
|
85
|
+
state.parent = element.parent.state
|
|
86
|
+
state.__element = element
|
|
87
|
+
state.__children = {}
|
|
88
|
+
state.__root = __elementRef.__root ? __elementRef.__root.state : state
|
|
89
|
+
|
|
90
|
+
if (state.parent) state.parent.__children[element.key] = state
|
|
91
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var createState_exports = {};
|
|
20
|
+
__export(createState_exports, {
|
|
21
|
+
createState: () => createState
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(createState_exports);
|
|
24
|
+
var import_event = require("@domql/event");
|
|
25
|
+
var import_utils = require("@domql/utils");
|
|
26
|
+
var import_ignore = require("./ignore");
|
|
27
|
+
var import_methods = require("./methods");
|
|
28
|
+
var import_updateState = require("./updateState");
|
|
29
|
+
var import_utils2 = require("./utils");
|
|
30
|
+
const createState = function(element, parent, opts) {
|
|
31
|
+
const skip = opts && opts.skip ? opts.skip : false;
|
|
32
|
+
let { state, __ref: __elementRef } = element;
|
|
33
|
+
state = element.state = checkForTypes(element);
|
|
34
|
+
(0, import_event.triggerEventOn)("stateInit", element);
|
|
35
|
+
if ((0, import_utils2.checkIfInherits)(element)) {
|
|
36
|
+
state = element.state = (0, import_utils2.createInheritedState)(element, parent) || {};
|
|
37
|
+
}
|
|
38
|
+
if (!state) {
|
|
39
|
+
if (parent && parent.state)
|
|
40
|
+
return parent.state;
|
|
41
|
+
return {};
|
|
42
|
+
} else {
|
|
43
|
+
__elementRef.__hasRootState = true;
|
|
44
|
+
}
|
|
45
|
+
const { __ref } = state;
|
|
46
|
+
if (__ref) {
|
|
47
|
+
state = (0, import_utils.deepClone)(__ref, import_ignore.IGNORE_STATE_PARAMS);
|
|
48
|
+
if ((0, import_utils.isObject)(__ref.__depends)) {
|
|
49
|
+
__ref.__depends[element.key] = state;
|
|
50
|
+
} else
|
|
51
|
+
__ref.__depends = { [element.key]: state };
|
|
52
|
+
} else {
|
|
53
|
+
state = (0, import_utils.deepClone)(state, import_ignore.IGNORE_STATE_PARAMS);
|
|
54
|
+
}
|
|
55
|
+
element.state = state;
|
|
56
|
+
if (skip)
|
|
57
|
+
return state;
|
|
58
|
+
applyMethods(element, state);
|
|
59
|
+
(0, import_event.triggerEventOn)("stateCreated", element);
|
|
60
|
+
return state;
|
|
61
|
+
};
|
|
62
|
+
const checkForTypes = (element) => {
|
|
63
|
+
const { state, __ref: __elementRef } = element;
|
|
64
|
+
if ((0, import_utils.isFunction)(state)) {
|
|
65
|
+
__elementRef.__state = state;
|
|
66
|
+
return (0, import_utils.exec)(state, element);
|
|
67
|
+
}
|
|
68
|
+
if ((0, import_utils.is)(state)("string", "number")) {
|
|
69
|
+
__elementRef.__state = state;
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
if (state === true) {
|
|
73
|
+
__elementRef.__state = element.key;
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
return state;
|
|
77
|
+
};
|
|
78
|
+
const applyMethods = (element, state) => {
|
|
79
|
+
const __elementRef = element.__ref;
|
|
80
|
+
state.clean = import_methods.clean;
|
|
81
|
+
state.parse = import_methods.parse;
|
|
82
|
+
state.destroy = import_methods.destroy;
|
|
83
|
+
state.update = import_updateState.updateState;
|
|
84
|
+
state.rootUpdate = import_methods.rootUpdate;
|
|
85
|
+
state.create = createState;
|
|
86
|
+
state.add = import_methods.add;
|
|
87
|
+
state.toggle = import_methods.toggle;
|
|
88
|
+
state.remove = import_methods.remove;
|
|
89
|
+
state.apply = import_methods.apply;
|
|
90
|
+
state.parent = element.parent.state;
|
|
91
|
+
state.__element = element;
|
|
92
|
+
state.__children = {};
|
|
93
|
+
state.__root = __elementRef.__root ? __elementRef.__root.state : state;
|
|
94
|
+
if (state.parent)
|
|
95
|
+
state.parent.__children[element.key] = state;
|
|
96
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var ignore_exports = {};
|
|
20
|
+
__export(ignore_exports, {
|
|
21
|
+
IGNORE_STATE_PARAMS: () => IGNORE_STATE_PARAMS
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(ignore_exports);
|
|
24
|
+
const IGNORE_STATE_PARAMS = [
|
|
25
|
+
"update",
|
|
26
|
+
"parse",
|
|
27
|
+
"clean",
|
|
28
|
+
"create",
|
|
29
|
+
"destroy",
|
|
30
|
+
"add",
|
|
31
|
+
"toggle",
|
|
32
|
+
"remove",
|
|
33
|
+
"apply",
|
|
34
|
+
"rootUpdate",
|
|
35
|
+
"parent",
|
|
36
|
+
"__element",
|
|
37
|
+
"__depends",
|
|
38
|
+
"__ref",
|
|
39
|
+
"__children",
|
|
40
|
+
"__root"
|
|
41
|
+
];
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var state_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(state_exports);
|
|
18
|
+
__reExport(state_exports, require("./ignore"), module.exports);
|
|
19
|
+
__reExport(state_exports, require("./createState"), module.exports);
|
|
20
|
+
__reExport(state_exports, require("./updateState"), module.exports);
|
|
21
|
+
__reExport(state_exports, require("./methods"), module.exports);
|
|
22
|
+
__reExport(state_exports, require("./utils"), module.exports);
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var methods_exports = {};
|
|
20
|
+
__export(methods_exports, {
|
|
21
|
+
add: () => add,
|
|
22
|
+
apply: () => apply,
|
|
23
|
+
clean: () => clean,
|
|
24
|
+
destroy: () => destroy,
|
|
25
|
+
parse: () => parse,
|
|
26
|
+
remove: () => remove,
|
|
27
|
+
rootUpdate: () => rootUpdate,
|
|
28
|
+
toggle: () => toggle
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(methods_exports);
|
|
31
|
+
var import_utils = require("@domql/utils");
|
|
32
|
+
var import_ignore = require("./ignore");
|
|
33
|
+
const parse = function() {
|
|
34
|
+
const state = this;
|
|
35
|
+
if ((0, import_utils.isObject)(state)) {
|
|
36
|
+
const obj = {};
|
|
37
|
+
for (const param in state) {
|
|
38
|
+
if (!import_ignore.IGNORE_STATE_PARAMS.includes(param)) {
|
|
39
|
+
obj[param] = state[param];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return obj;
|
|
43
|
+
} else if ((0, import_utils.isArray)(state)) {
|
|
44
|
+
return state.filter((item) => !import_ignore.IGNORE_STATE_PARAMS.includes(item));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const clean = function(options = {}) {
|
|
48
|
+
const state = this;
|
|
49
|
+
for (const param in state) {
|
|
50
|
+
if (!import_ignore.IGNORE_STATE_PARAMS.includes(param)) {
|
|
51
|
+
delete state[param];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
state.update(state, { skipOverwrite: true, options });
|
|
55
|
+
return state;
|
|
56
|
+
};
|
|
57
|
+
const destroy = function() {
|
|
58
|
+
const state = this;
|
|
59
|
+
const element = state.__element;
|
|
60
|
+
delete element.state;
|
|
61
|
+
element.state = state.parent;
|
|
62
|
+
if (state.parent) {
|
|
63
|
+
delete state.parent.__children[element.key];
|
|
64
|
+
}
|
|
65
|
+
if (state.__children) {
|
|
66
|
+
for (const key in state.__children) {
|
|
67
|
+
const child = state.__children[key];
|
|
68
|
+
if (child.state) {
|
|
69
|
+
child.parent = state.parent;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
element.state.update();
|
|
74
|
+
return element.state;
|
|
75
|
+
};
|
|
76
|
+
const rootUpdate = function(obj, options = {}) {
|
|
77
|
+
const state = this;
|
|
78
|
+
if (!state)
|
|
79
|
+
return;
|
|
80
|
+
const rootState = state.__element.__ref.__root.state;
|
|
81
|
+
return rootState.update(obj, options);
|
|
82
|
+
};
|
|
83
|
+
const add = function(value, options = {}) {
|
|
84
|
+
const state = this;
|
|
85
|
+
if ((0, import_utils.isArray)(state)) {
|
|
86
|
+
state.push(value);
|
|
87
|
+
state.update(state.parse(), { skipOverwrite: true, ...options });
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const toggle = function(key, options = {}) {
|
|
91
|
+
const state = this;
|
|
92
|
+
state[key] = !state[key];
|
|
93
|
+
state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options });
|
|
94
|
+
};
|
|
95
|
+
const remove = function(key, options = {}) {
|
|
96
|
+
const state = this;
|
|
97
|
+
if ((0, import_utils.isArray)(state))
|
|
98
|
+
(0, import_utils.removeFromArray)(state, key);
|
|
99
|
+
if ((0, import_utils.isObject)(state))
|
|
100
|
+
(0, import_utils.removeFromObject)(state, key);
|
|
101
|
+
return state.update(state.parse(), { skipOverwrite: true, ...options });
|
|
102
|
+
};
|
|
103
|
+
const apply = function(func, options = {}) {
|
|
104
|
+
const state = this;
|
|
105
|
+
if ((0, import_utils.isFunction)(func)) {
|
|
106
|
+
func(state);
|
|
107
|
+
return state.update(state, { skipOverwrite: true, ...options });
|
|
108
|
+
}
|
|
109
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var updateState_exports = {};
|
|
20
|
+
__export(updateState_exports, {
|
|
21
|
+
updateState: () => updateState
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(updateState_exports);
|
|
24
|
+
var import_report = require("@domql/report");
|
|
25
|
+
var import_event = require("@domql/event");
|
|
26
|
+
var import_ignore = require("./ignore");
|
|
27
|
+
var import_utils = require("@domql/utils");
|
|
28
|
+
var import_utils2 = require("./utils");
|
|
29
|
+
const updateState = function(obj, options = {}) {
|
|
30
|
+
const state = this;
|
|
31
|
+
const element = state.__element;
|
|
32
|
+
if (!state.__element)
|
|
33
|
+
(0, import_report.report)("ElementOnStateIsNotDefined");
|
|
34
|
+
if (!options.preventInitStateUpdateListener) {
|
|
35
|
+
const initStateUpdateReturns = (0, import_event.triggerEventOn)("initStateUpdated", element, obj);
|
|
36
|
+
if (initStateUpdateReturns === false)
|
|
37
|
+
return element;
|
|
38
|
+
}
|
|
39
|
+
applyOverwrite(state, obj, options);
|
|
40
|
+
hoistStateUpdate(state, obj, options);
|
|
41
|
+
applyDependentState(state, obj, options);
|
|
42
|
+
applyElementUpdate(state, obj, options);
|
|
43
|
+
if (!options.preventStateUpdateListener) {
|
|
44
|
+
(0, import_event.triggerEventOn)("stateUpdated", element, obj);
|
|
45
|
+
}
|
|
46
|
+
return state;
|
|
47
|
+
};
|
|
48
|
+
const applyOverwrite = (state, obj, options) => {
|
|
49
|
+
const { skipOverwrite, shallow } = options;
|
|
50
|
+
if (skipOverwrite === "merge") {
|
|
51
|
+
(0, import_utils.deepMerge)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!skipOverwrite) {
|
|
55
|
+
const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
|
|
56
|
+
overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const hoistStateUpdate = (state, obj, options) => {
|
|
60
|
+
const element = state.__element;
|
|
61
|
+
const __elementRef = element.__ref;
|
|
62
|
+
const stateKey = __elementRef.__state;
|
|
63
|
+
const hasParentState = (0, import_utils2.checkIfInherits)(element);
|
|
64
|
+
const shouldPropagateState = stateKey && hasParentState && !options.stopStatePropagation;
|
|
65
|
+
const parentState = element.parent.state;
|
|
66
|
+
if (shouldPropagateState) {
|
|
67
|
+
const isStringState = __elementRef.__stateType === "string";
|
|
68
|
+
const value = isStringState ? state.value : state.parse();
|
|
69
|
+
const passedValue = isStringState ? state.value : obj;
|
|
70
|
+
parentState[stateKey] = value;
|
|
71
|
+
parentState.update({ [stateKey]: passedValue }, {
|
|
72
|
+
skipOverwrite: true,
|
|
73
|
+
preventUpdate: options.preventHoistElementUpdate,
|
|
74
|
+
...options
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const applyDependentState = (state, obj, options) => {
|
|
79
|
+
if (state.__depends) {
|
|
80
|
+
for (const el in state.__depends) {
|
|
81
|
+
const findElement = state.__depends[el];
|
|
82
|
+
findElement.clean().update(state.parse(), options);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const applyElementUpdate = (state, obj, options) => {
|
|
87
|
+
const element = state.__element;
|
|
88
|
+
if (!options.preventUpdate) {
|
|
89
|
+
element.update({}, {
|
|
90
|
+
...options,
|
|
91
|
+
updateByState: true,
|
|
92
|
+
preventUpdateTriggerStateUpdate: true
|
|
93
|
+
});
|
|
94
|
+
} else if (options.preventUpdate === "recursive") {
|
|
95
|
+
element.update({}, {
|
|
96
|
+
...options,
|
|
97
|
+
preventUpdateTriggerStateUpdate: true,
|
|
98
|
+
updateByState: true,
|
|
99
|
+
preventUpdate: true
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var utils_exports = {};
|
|
20
|
+
__export(utils_exports, {
|
|
21
|
+
checkIfInherits: () => checkIfInherits,
|
|
22
|
+
createInheritedState: () => createInheritedState,
|
|
23
|
+
getChildStateInKey: () => getChildStateInKey,
|
|
24
|
+
getParentStateInKey: () => getParentStateInKey,
|
|
25
|
+
isState: () => isState
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(utils_exports);
|
|
28
|
+
var import_utils = require("@domql/utils");
|
|
29
|
+
const getParentStateInKey = (stateKey, parentState) => {
|
|
30
|
+
const arr = stateKey.split("../");
|
|
31
|
+
const arrLength = arr.length - 1;
|
|
32
|
+
for (let i = 0; i < arrLength; i++) {
|
|
33
|
+
if (!parentState.parent)
|
|
34
|
+
return null;
|
|
35
|
+
parentState = parentState.parent;
|
|
36
|
+
}
|
|
37
|
+
return parentState;
|
|
38
|
+
};
|
|
39
|
+
const getChildStateInKey = (stateKey, parentState) => {
|
|
40
|
+
const arr = stateKey.split("/");
|
|
41
|
+
const arrLength = arr.length - 1;
|
|
42
|
+
for (let i = 0; i < arrLength; i++) {
|
|
43
|
+
const childKey = arr[i];
|
|
44
|
+
const grandChildKey = arr[i + 1];
|
|
45
|
+
const childInParent = parentState[childKey];
|
|
46
|
+
if (childInParent && childInParent[grandChildKey]) {
|
|
47
|
+
stateKey = grandChildKey;
|
|
48
|
+
parentState = childInParent;
|
|
49
|
+
} else
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
return parentState[stateKey];
|
|
53
|
+
};
|
|
54
|
+
const createInheritedState = (element, parent) => {
|
|
55
|
+
const __elementRef = element.__ref;
|
|
56
|
+
let stateKey = __elementRef.__state;
|
|
57
|
+
if (!stateKey || (0, import_utils.isNot)(stateKey)("number", "string"))
|
|
58
|
+
return element.state;
|
|
59
|
+
let parentState = parent.state;
|
|
60
|
+
if (stateKey.includes("../")) {
|
|
61
|
+
parentState = getParentStateInKey(stateKey, parent.state);
|
|
62
|
+
stateKey = stateKey.replaceAll("../", "");
|
|
63
|
+
}
|
|
64
|
+
if (!parentState)
|
|
65
|
+
return;
|
|
66
|
+
const keyInParentState = getChildStateInKey(stateKey, parentState);
|
|
67
|
+
if (!keyInParentState)
|
|
68
|
+
return;
|
|
69
|
+
if ((0, import_utils.is)(keyInParentState)("object", "array")) {
|
|
70
|
+
return (0, import_utils.deepClone)(keyInParentState);
|
|
71
|
+
} else if ((0, import_utils.is)(keyInParentState)("string", "number")) {
|
|
72
|
+
__elementRef.__stateType = "string";
|
|
73
|
+
return { value: keyInParentState };
|
|
74
|
+
}
|
|
75
|
+
console.warn(stateKey, "is not present. Replacing with", {});
|
|
76
|
+
};
|
|
77
|
+
const checkIfInherits = (element) => {
|
|
78
|
+
const __elementRef = element.__ref;
|
|
79
|
+
const stateKey = __elementRef.__state;
|
|
80
|
+
if (!stateKey || (0, import_utils.isNot)(stateKey)("number", "string"))
|
|
81
|
+
return false;
|
|
82
|
+
return true;
|
|
83
|
+
};
|
|
84
|
+
const isState = function(state) {
|
|
85
|
+
if (!(0, import_utils.isObjectLike)(state))
|
|
86
|
+
return false;
|
|
87
|
+
const keys = Object.keys(state);
|
|
88
|
+
return (0, import_utils.arrayContainsOtherArray)(keys, ["update", "parse", "clean", "create", "parent", "rootUpdate"]);
|
|
89
|
+
};
|
package/ignore.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
export const IGNORE_STATE_PARAMS = [
|
|
4
|
-
'update', 'parse', 'clean', 'create', 'destroy', 'add', '
|
|
5
|
-
'parent', '__element', '__depends', '__ref', '__children', '__root'
|
|
4
|
+
'update', 'parse', 'clean', 'create', 'destroy', 'add', 'toggle', 'remove', 'apply',
|
|
5
|
+
'rootUpdate', 'parent', '__element', '__depends', '__ref', '__children', '__root'
|
|
6
6
|
]
|
package/index.js
CHANGED
|
@@ -1,280 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
isObject,
|
|
9
|
-
exec,
|
|
10
|
-
isFunction,
|
|
11
|
-
isUndefined,
|
|
12
|
-
arrayContainsOtherArray,
|
|
13
|
-
isObjectLike,
|
|
14
|
-
isArray,
|
|
15
|
-
removeFromArray,
|
|
16
|
-
removeFromObject,
|
|
17
|
-
deepClone,
|
|
18
|
-
overwriteShallow,
|
|
19
|
-
overwriteDeep
|
|
20
|
-
} from '@domql/utils'
|
|
21
|
-
|
|
22
|
-
export const parse = function () {
|
|
23
|
-
const state = this
|
|
24
|
-
if (isObject(state)) {
|
|
25
|
-
const obj = {}
|
|
26
|
-
for (const param in state) {
|
|
27
|
-
if (!IGNORE_STATE_PARAMS.includes(param)) {
|
|
28
|
-
obj[param] = state[param]
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return obj
|
|
32
|
-
} else if (isArray(state)) {
|
|
33
|
-
return state.filter(item => !IGNORE_STATE_PARAMS.includes(item))
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export const clean = function (options = {}) {
|
|
38
|
-
const state = this
|
|
39
|
-
for (const param in state) {
|
|
40
|
-
if (!IGNORE_STATE_PARAMS.includes(param)) {
|
|
41
|
-
delete state[param]
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
state.update(state, { skipOverwrite: true, options })
|
|
45
|
-
return state
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export const destroy = function () {
|
|
49
|
-
const state = this
|
|
50
|
-
const element = state.__element
|
|
51
|
-
delete element.state
|
|
52
|
-
element.state = state.parent
|
|
53
|
-
|
|
54
|
-
if (state.parent) {
|
|
55
|
-
delete state.parent.__children[element.key]
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (state.__children) {
|
|
59
|
-
for (const key in state.__children) {
|
|
60
|
-
const child = state.__children[key]
|
|
61
|
-
if (child.state) {
|
|
62
|
-
child.parent = state.parent
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
element.state.update()
|
|
68
|
-
return element.state
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export const rootUpdate = function (obj, options = {}) {
|
|
72
|
-
const state = this
|
|
73
|
-
if (!state) return
|
|
74
|
-
const rootState = (state.__element.__ref.__root).state
|
|
75
|
-
return rootState.update(obj, options)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export const updateState = function (obj, options = {}) {
|
|
79
|
-
const state = this
|
|
80
|
-
const element = state.__element
|
|
81
|
-
const __elementRef = element.__ref
|
|
82
|
-
const parentState = element.parent.state
|
|
83
|
-
state.parent = parentState
|
|
84
|
-
|
|
85
|
-
for (const param in state) {
|
|
86
|
-
if (isUndefined(state[param])) {
|
|
87
|
-
delete state[param]
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (!state.__element) {
|
|
92
|
-
create(element, element.parent)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const initStateUpdateReturns = triggerEventOn('initStateUpdated', element, obj)
|
|
96
|
-
if (initStateUpdateReturns === false) return element
|
|
97
|
-
|
|
98
|
-
if (!options.skipOverwrite) {
|
|
99
|
-
if (options.shallow) {
|
|
100
|
-
overwriteShallow(state, obj, IGNORE_STATE_PARAMS)
|
|
101
|
-
} else {
|
|
102
|
-
overwriteDeep(state, obj, IGNORE_STATE_PARAMS)
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const stateKey = __elementRef.__state
|
|
107
|
-
const shouldPropagateState = stateKey && parentState && parentState[stateKey] && !options.stopStatePropagation
|
|
108
|
-
if (shouldPropagateState) {
|
|
109
|
-
const isStringState = (__elementRef.__stateType === 'string')
|
|
110
|
-
const value = isStringState ? state.value : state.parse()
|
|
111
|
-
parentState[stateKey] = value
|
|
112
|
-
parentState.update(value, { skipOverwrite: true, ...options })
|
|
113
|
-
return state
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (!options.preventUpdate) {
|
|
117
|
-
element.update({}, options)
|
|
118
|
-
} else if (options.preventUpdate === 'recursive') {
|
|
119
|
-
element.update({}, { ...options, preventUpdate: true })
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (state.__depends) {
|
|
123
|
-
for (const el in state.__depends) {
|
|
124
|
-
const findElement = state.__depends[el]
|
|
125
|
-
findElement.clean().update(state.parse(), options)
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (!options.preventUpdateListener) {
|
|
130
|
-
triggerEventOn('stateUpdated', element, obj)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return state
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export const remove = function (key, options) {
|
|
137
|
-
const state = this
|
|
138
|
-
if (isArray(state)) removeFromArray(state, key)
|
|
139
|
-
if (isObject(state)) removeFromObject(state, key)
|
|
140
|
-
return state.update(state, { skipOverwrite: true, options })
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export const apply = function (func, options) {
|
|
144
|
-
const state = this
|
|
145
|
-
if (isFunction(func)) {
|
|
146
|
-
func(state)
|
|
147
|
-
return state.update(state, { skipOverwrite: true, options })
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const getParentStateInKey = (stateKey, parentState) => {
|
|
152
|
-
const arr = stateKey.split('../')
|
|
153
|
-
const arrLength = arr.length - 1
|
|
154
|
-
for (let i = 0; i < arrLength; i++) {
|
|
155
|
-
if (!parentState.parent) return null
|
|
156
|
-
parentState = parentState.parent
|
|
157
|
-
}
|
|
158
|
-
return parentState
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const getChildStateInKey = (stateKey, parentState) => {
|
|
162
|
-
const arr = stateKey.split('/')
|
|
163
|
-
const arrLength = arr.length - 1
|
|
164
|
-
for (let i = 0; i < arrLength; i++) {
|
|
165
|
-
const childKey = arr[i]
|
|
166
|
-
const grandChildKey = arr[i + 1]
|
|
167
|
-
const childInParent = parentState[childKey]
|
|
168
|
-
if (childInParent && childInParent[grandChildKey]) {
|
|
169
|
-
stateKey = grandChildKey
|
|
170
|
-
parentState = childInParent
|
|
171
|
-
} else return
|
|
172
|
-
}
|
|
173
|
-
return parentState[stateKey]
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const createInheritedState = function (element, parent) {
|
|
177
|
-
const __elementRef = element.__ref
|
|
178
|
-
let stateKey = __elementRef.__state
|
|
179
|
-
if (!stateKey) return element.state
|
|
180
|
-
|
|
181
|
-
let parentState = parent.state
|
|
182
|
-
if (stateKey.includes('../')) {
|
|
183
|
-
parentState = getParentStateInKey(stateKey, parent.state)
|
|
184
|
-
stateKey = stateKey.replaceAll('../', '')
|
|
185
|
-
}
|
|
186
|
-
if (!parentState) return {}
|
|
187
|
-
|
|
188
|
-
const keyInParentState = getChildStateInKey(stateKey, parentState)
|
|
189
|
-
if (!keyInParentState) return {}
|
|
190
|
-
|
|
191
|
-
if (is(keyInParentState)('object', 'array')) {
|
|
192
|
-
return deepClone(keyInParentState, IGNORE_STATE_PARAMS)
|
|
193
|
-
} else if (is(keyInParentState)('string', 'number')) {
|
|
194
|
-
__elementRef.__stateType = 'string'
|
|
195
|
-
return { value: keyInParentState }
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
console.warn(stateKey, 'is not present. Replacing with', {})
|
|
199
|
-
return {}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export const createState = function (element, parent, opts) {
|
|
203
|
-
const skip = (opts && opts.skip) ? opts.skip : false
|
|
204
|
-
let { state, __ref: __elementRef } = element
|
|
205
|
-
|
|
206
|
-
if (isFunction(state)) element.state = exec(state, element)
|
|
207
|
-
|
|
208
|
-
if (is(state)('string', 'number')) {
|
|
209
|
-
__elementRef.__state = state
|
|
210
|
-
element.state = {}
|
|
211
|
-
}
|
|
212
|
-
if (state === true) {
|
|
213
|
-
__elementRef.__state = element.key
|
|
214
|
-
element.state = {}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// trigger `on.stateInit`
|
|
218
|
-
triggerEventOn('stateInit', element)
|
|
219
|
-
|
|
220
|
-
state = element.state = createInheritedState(element, parent)
|
|
221
|
-
|
|
222
|
-
if (!state) {
|
|
223
|
-
if (parent && parent.state) return parent.state
|
|
224
|
-
return {}
|
|
225
|
-
} else {
|
|
226
|
-
__elementRef.__hasRootState = true
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
// reference other state
|
|
230
|
-
// TODO: check why __ref is assigned with element
|
|
231
|
-
// /docs/intro
|
|
232
|
-
const { __ref } = state
|
|
233
|
-
if (__ref) {
|
|
234
|
-
state = deepClone(__ref, IGNORE_STATE_PARAMS)
|
|
235
|
-
if (isObject(__ref.__depends)) {
|
|
236
|
-
__ref.__depends[element.key] = state
|
|
237
|
-
} else __ref.__depends = { [element.key]: state }
|
|
238
|
-
} else {
|
|
239
|
-
state = deepClone(state, IGNORE_STATE_PARAMS)
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
element.state = state
|
|
243
|
-
|
|
244
|
-
// NOTE: Only true when 'onlyResolveExtends' option is set to true
|
|
245
|
-
if (skip) return state
|
|
246
|
-
|
|
247
|
-
applyMethods(element, state)
|
|
248
|
-
|
|
249
|
-
// trigger `on.stateCreated`
|
|
250
|
-
triggerEventOn('stateCreated', element)
|
|
251
|
-
|
|
252
|
-
return state
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export const isState = function (state) {
|
|
256
|
-
if (!isObjectLike(state)) return false
|
|
257
|
-
const keys = Object.keys(state)
|
|
258
|
-
return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const applyMethods = (element, state) => {
|
|
262
|
-
const __elementRef = element.__ref
|
|
263
|
-
|
|
264
|
-
state.clean = clean
|
|
265
|
-
state.parse = parse
|
|
266
|
-
state.destroy = destroy
|
|
267
|
-
state.update = updateState
|
|
268
|
-
state.rootUpdate = rootUpdate
|
|
269
|
-
state.create = createState
|
|
270
|
-
state.remove = remove
|
|
271
|
-
state.apply = apply
|
|
272
|
-
state.parent = element.parent.state
|
|
273
|
-
state.__element = element
|
|
274
|
-
state.__children = {}
|
|
275
|
-
state.__root = __elementRef.__root ? __elementRef.__root.state : state
|
|
276
|
-
|
|
277
|
-
if (state.parent) state.parent.__children[element.key] = state
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
export default createState
|
|
3
|
+
export * from './ignore'
|
|
4
|
+
export * from './createState'
|
|
5
|
+
export * from './updateState'
|
|
6
|
+
export * from './methods'
|
|
7
|
+
export * from './utils'
|
package/methods.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { isArray, isFunction, isObject, removeFromArray, removeFromObject } from '@domql/utils'
|
|
4
|
+
|
|
5
|
+
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
|
+
|
|
7
|
+
export const parse = function () {
|
|
8
|
+
const state = this
|
|
9
|
+
if (isObject(state)) {
|
|
10
|
+
const obj = {}
|
|
11
|
+
for (const param in state) {
|
|
12
|
+
if (!IGNORE_STATE_PARAMS.includes(param)) {
|
|
13
|
+
obj[param] = state[param]
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return obj
|
|
17
|
+
} else if (isArray(state)) {
|
|
18
|
+
return state.filter(item => !IGNORE_STATE_PARAMS.includes(item))
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const clean = function (options = {}) {
|
|
23
|
+
const state = this
|
|
24
|
+
for (const param in state) {
|
|
25
|
+
if (!IGNORE_STATE_PARAMS.includes(param)) {
|
|
26
|
+
delete state[param]
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
state.update(state, { skipOverwrite: true, options })
|
|
30
|
+
return state
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const destroy = function () {
|
|
34
|
+
const state = this
|
|
35
|
+
const element = state.__element
|
|
36
|
+
delete element.state
|
|
37
|
+
element.state = state.parent
|
|
38
|
+
|
|
39
|
+
if (state.parent) {
|
|
40
|
+
delete state.parent.__children[element.key]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (state.__children) {
|
|
44
|
+
for (const key in state.__children) {
|
|
45
|
+
const child = state.__children[key]
|
|
46
|
+
if (child.state) {
|
|
47
|
+
child.parent = state.parent
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
element.state.update()
|
|
53
|
+
return element.state
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const rootUpdate = function (obj, options = {}) {
|
|
57
|
+
const state = this
|
|
58
|
+
if (!state) return
|
|
59
|
+
const rootState = (state.__element.__ref.__root).state
|
|
60
|
+
return rootState.update(obj, options)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const add = function (value, options = {}) {
|
|
64
|
+
const state = this
|
|
65
|
+
if (isArray(state)) {
|
|
66
|
+
state.push(value)
|
|
67
|
+
state.update(state.parse(), { skipOverwrite: true, ...options })
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const toggle = function (key, options = {}) {
|
|
72
|
+
const state = this
|
|
73
|
+
state[key] = !state[key]
|
|
74
|
+
state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options })
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const remove = function (key, options = {}) {
|
|
78
|
+
const state = this
|
|
79
|
+
if (isArray(state)) removeFromArray(state, key)
|
|
80
|
+
if (isObject(state)) removeFromObject(state, key)
|
|
81
|
+
return state.update(state.parse(), { skipOverwrite: true, ...options })
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const apply = function (func, options = {}) {
|
|
85
|
+
const state = this
|
|
86
|
+
if (isFunction(func)) {
|
|
87
|
+
func(state)
|
|
88
|
+
return state.update(state, { skipOverwrite: true, ...options })
|
|
89
|
+
}
|
|
90
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.3.
|
|
4
|
-
"main": "index.js",
|
|
3
|
+
"version": "2.3.120",
|
|
5
4
|
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"unpkg": "dist/iife/index.js",
|
|
8
|
+
"jsdelivr": "dist/iife/index.js",
|
|
9
|
+
"main": "dist/esm/index.js",
|
|
10
|
+
"exports": "./dist/cjs/index.js",
|
|
11
|
+
"source": "index.js",
|
|
12
|
+
"files": [
|
|
13
|
+
"*.js",
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"copy:package:cjs": "cp ../../.build/package-cjs.json dist/cjs/package.json",
|
|
18
|
+
"build:esm": "npx esbuild *.js --target=es2019 --format=esm --outdir=dist/esm",
|
|
19
|
+
"build:cjs": "npx esbuild *.js --target=node16 --format=cjs --outdir=dist/cjs",
|
|
20
|
+
"build:iife": "npx esbuild *.js --target=node16 --format=iife --outdir=dist/iife",
|
|
21
|
+
"build": "yarn build:cjs",
|
|
22
|
+
"prepublish": "rimraf -I dist && yarn build && yarn copy:package:cjs"
|
|
23
|
+
},
|
|
6
24
|
"dependencies": {
|
|
7
25
|
"@domql/event": "latest",
|
|
26
|
+
"@domql/report": "latest",
|
|
8
27
|
"@domql/utils": "latest"
|
|
9
28
|
},
|
|
10
|
-
"gitHead": "
|
|
11
|
-
"source": "index.js"
|
|
29
|
+
"gitHead": "6e932a813b8e6ee0a57a40ebc6dc757cc44e6427"
|
|
12
30
|
}
|
package/updateState.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { report } from '@domql/report'
|
|
4
|
+
import { triggerEventOn } from '@domql/event'
|
|
5
|
+
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
|
+
import { deepMerge, overwriteDeep, overwriteShallow } from '@domql/utils'
|
|
7
|
+
import { checkIfInherits } from './utils'
|
|
8
|
+
|
|
9
|
+
export const updateState = function (obj, options = {}) {
|
|
10
|
+
const state = this
|
|
11
|
+
const element = state.__element
|
|
12
|
+
|
|
13
|
+
if (!state.__element) report('ElementOnStateIsNotDefined')
|
|
14
|
+
|
|
15
|
+
if (!options.preventInitStateUpdateListener) {
|
|
16
|
+
const initStateUpdateReturns = triggerEventOn('initStateUpdated', element, obj)
|
|
17
|
+
if (initStateUpdateReturns === false) return element
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
applyOverwrite(state, obj, options)
|
|
21
|
+
|
|
22
|
+
hoistStateUpdate(state, obj, options)
|
|
23
|
+
|
|
24
|
+
applyDependentState(state, obj, options)
|
|
25
|
+
|
|
26
|
+
applyElementUpdate(state, obj, options)
|
|
27
|
+
|
|
28
|
+
if (!options.preventStateUpdateListener) {
|
|
29
|
+
triggerEventOn('stateUpdated', element, obj)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return state
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const applyOverwrite = (state, obj, options) => {
|
|
36
|
+
const { skipOverwrite, shallow } = options
|
|
37
|
+
|
|
38
|
+
if (skipOverwrite === 'merge') {
|
|
39
|
+
deepMerge(state, obj, IGNORE_STATE_PARAMS)
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!skipOverwrite) {
|
|
44
|
+
const overwriteFunc = shallow ? overwriteShallow : overwriteDeep
|
|
45
|
+
overwriteFunc(state, obj, IGNORE_STATE_PARAMS)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const hoistStateUpdate = (state, obj, options) => {
|
|
50
|
+
const element = state.__element
|
|
51
|
+
const __elementRef = element.__ref
|
|
52
|
+
const stateKey = __elementRef.__state
|
|
53
|
+
const hasParentState = checkIfInherits(element)
|
|
54
|
+
const shouldPropagateState = stateKey && hasParentState && !options.stopStatePropagation
|
|
55
|
+
const parentState = element.parent.state
|
|
56
|
+
|
|
57
|
+
if (shouldPropagateState) {
|
|
58
|
+
const isStringState = (__elementRef.__stateType === 'string')
|
|
59
|
+
const value = isStringState ? state.value : state.parse()
|
|
60
|
+
const passedValue = isStringState ? state.value : obj
|
|
61
|
+
|
|
62
|
+
parentState[stateKey] = value
|
|
63
|
+
parentState.update({ [stateKey]: passedValue }, {
|
|
64
|
+
skipOverwrite: true,
|
|
65
|
+
preventUpdate: options.preventHoistElementUpdate,
|
|
66
|
+
...options
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const applyDependentState = (state, obj, options) => {
|
|
72
|
+
if (state.__depends) {
|
|
73
|
+
for (const el in state.__depends) {
|
|
74
|
+
const findElement = state.__depends[el]
|
|
75
|
+
findElement.clean().update(state.parse(), options)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const applyElementUpdate = (state, obj, options) => {
|
|
81
|
+
const element = state.__element
|
|
82
|
+
if (!options.preventUpdate) {
|
|
83
|
+
element.update({}, {
|
|
84
|
+
...options,
|
|
85
|
+
updateByState: true,
|
|
86
|
+
preventUpdateTriggerStateUpdate: true
|
|
87
|
+
})
|
|
88
|
+
} else if (options.preventUpdate === 'recursive') {
|
|
89
|
+
element.update({}, {
|
|
90
|
+
...options,
|
|
91
|
+
preventUpdateTriggerStateUpdate: true,
|
|
92
|
+
updateByState: true,
|
|
93
|
+
preventUpdate: true
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
}
|
package/utils.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
import { arrayContainsOtherArray, deepClone, is, isNot, isObjectLike } from '@domql/utils'
|
|
4
|
+
|
|
5
|
+
export const getParentStateInKey = (stateKey, parentState) => {
|
|
6
|
+
const arr = stateKey.split('../')
|
|
7
|
+
const arrLength = arr.length - 1
|
|
8
|
+
for (let i = 0; i < arrLength; i++) {
|
|
9
|
+
if (!parentState.parent) return null
|
|
10
|
+
parentState = parentState.parent
|
|
11
|
+
}
|
|
12
|
+
return parentState
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const getChildStateInKey = (stateKey, parentState) => {
|
|
16
|
+
const arr = stateKey.split('/')
|
|
17
|
+
const arrLength = arr.length - 1
|
|
18
|
+
for (let i = 0; i < arrLength; i++) {
|
|
19
|
+
const childKey = arr[i]
|
|
20
|
+
const grandChildKey = arr[i + 1]
|
|
21
|
+
const childInParent = parentState[childKey]
|
|
22
|
+
if (childInParent && childInParent[grandChildKey]) {
|
|
23
|
+
stateKey = grandChildKey
|
|
24
|
+
parentState = childInParent
|
|
25
|
+
} else return
|
|
26
|
+
}
|
|
27
|
+
return parentState[stateKey]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const createInheritedState = (element, parent) => {
|
|
31
|
+
const __elementRef = element.__ref
|
|
32
|
+
let stateKey = __elementRef.__state
|
|
33
|
+
if (!stateKey || isNot(stateKey)('number', 'string')) return element.state
|
|
34
|
+
|
|
35
|
+
let parentState = parent.state
|
|
36
|
+
if (stateKey.includes('../')) {
|
|
37
|
+
parentState = getParentStateInKey(stateKey, parent.state)
|
|
38
|
+
stateKey = stateKey.replaceAll('../', '')
|
|
39
|
+
}
|
|
40
|
+
if (!parentState) return
|
|
41
|
+
|
|
42
|
+
const keyInParentState = getChildStateInKey(stateKey, parentState)
|
|
43
|
+
if (!keyInParentState) return
|
|
44
|
+
|
|
45
|
+
if (is(keyInParentState)('object', 'array')) {
|
|
46
|
+
return deepClone(keyInParentState)
|
|
47
|
+
} else if (is(keyInParentState)('string', 'number')) {
|
|
48
|
+
__elementRef.__stateType = 'string'
|
|
49
|
+
return { value: keyInParentState }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
console.warn(stateKey, 'is not present. Replacing with', {})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const checkIfInherits = (element) => {
|
|
56
|
+
const __elementRef = element.__ref
|
|
57
|
+
const stateKey = __elementRef.__state
|
|
58
|
+
if (!stateKey || isNot(stateKey)('number', 'string')) return false
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const isState = function (state) {
|
|
63
|
+
if (!isObjectLike(state)) return false
|
|
64
|
+
const keys = Object.keys(state)
|
|
65
|
+
return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
|
|
66
|
+
}
|