dom-primitive-bindings 1.7.3 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -153
- package/package.json +4 -2
- package/src/index.js +2 -2
- package/src/v6/guards/actions/index.js +21 -21
- package/src/v6/guards/bindings/index.js +5 -5
- package/src/v6/guards/bindings/validateArray.js +23 -23
- package/src/v6/guards/bindings/validateBinding.js +22 -22
- package/src/v6/guards/index.js +19 -19
- package/src/v6/index.js +21 -21
- package/src/v6/prepare/index.js +23 -23
- package/src/v6/register/meta.js +6 -6
- package/src/v7/guards/actions/index.js +21 -21
- package/src/v7/guards/bindings/index.js +5 -5
- package/src/v7/guards/bindings/validateArray.js +23 -23
- package/src/v7/guards/bindings/validateBinding.js +22 -22
- package/src/v7/guards/index.js +19 -19
- package/src/v7/index.js +43 -43
- package/src/v7/prepare/index.js +23 -23
- package/src/v7/register/index.js +18 -18
- package/src/v7/register/meta.js +6 -6
- package/src/v7/sample/index.js +22 -22
- package/src/v8/dispatch/executeBinding.js +21 -0
- package/src/v8/dispatch/getClientData.js +8 -0
- package/src/v8/dispatch/getStartInput.js +10 -0
- package/src/v8/dispatch/index.js +15 -0
- package/src/v8/guards/actions/index.js +21 -0
- package/src/v8/guards/bindings/index.js +5 -0
- package/src/v8/guards/bindings/validateArray.js +23 -0
- package/src/v8/guards/bindings/validateBinding.js +22 -0
- package/src/v8/guards/container/index.js +13 -0
- package/src/v8/guards/container/resolveElement.js +14 -0
- package/src/v8/guards/index.js +19 -0
- package/src/v8/index.js +37 -0
- package/src/v8/listeners/attachListener.js +10 -0
- package/src/v8/listeners/index.js +12 -0
- package/src/v8/prepare/groupBindings.js +14 -0
- package/src/v8/prepare/index.js +23 -0
- package/src/v8/register/index.js +15 -0
- package/src/v8/register/meta.js +6 -0
- package/src/v8/sample/index.js +22 -0
- package/src/v9/dispatch/index.js +7 -0
- package/src/v9/guards/actions/index.js +27 -0
- package/src/v9/guards/bindings/index.js +5 -0
- package/src/v9/guards/bindings/validateArray.js +23 -0
- package/src/v9/guards/bindings/validateBinding.js +22 -0
- package/src/v9/guards/container/index.js +13 -0
- package/src/v9/guards/container/resolveElement.js +14 -0
- package/src/v9/guards/index.js +19 -0
- package/src/v9/index.js +21 -0
- package/src/v9/listeners/attachListener.js +9 -0
- package/src/v9/listeners/index.js +12 -0
- package/src/v9/prepare/groupBindings.js +14 -0
- package/src/v9/prepare/index.js +12 -0
- package/src/v9/register/index.js +15 -0
- package/src/v9/register/meta.js +2 -0
- package/src/v9/sample/index.js +12 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const groupBindings = (bindings) => {
|
|
2
|
+
const groups = new Map();
|
|
3
|
+
|
|
4
|
+
for (const binding of bindings) {
|
|
5
|
+
const list = groups.get(binding.event) ?? [];
|
|
6
|
+
list.push(binding);
|
|
7
|
+
groups.set(binding.event, list);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return groups;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default groupBindings;
|
|
14
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import groupBindings from "./groupBindings.js";
|
|
2
|
+
|
|
3
|
+
const prepareBindings = (bindings, actions = {}) => {
|
|
4
|
+
const hasActions = actions && typeof actions === "object" && Object.keys(actions).length > 0;
|
|
5
|
+
|
|
6
|
+
const configuredBindings = hasActions
|
|
7
|
+
? bindings.map((binding) => {
|
|
8
|
+
if (binding.action && typeof actions[binding.action] === "function" && typeof binding.onExtract !== "function") {
|
|
9
|
+
return {
|
|
10
|
+
...binding,
|
|
11
|
+
onExtract: (result, event, matchElement) => {
|
|
12
|
+
actions[binding.action](event, matchElement, result);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return binding;
|
|
17
|
+
})
|
|
18
|
+
: bindings;
|
|
19
|
+
|
|
20
|
+
return groupBindings(configuredBindings);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default prepareBindings;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import meta from "./meta.js";
|
|
2
|
+
|
|
3
|
+
const registerGlobal = (bindEvents) => {
|
|
4
|
+
if (!bindEvents || typeof globalThis === "undefined") return;
|
|
5
|
+
|
|
6
|
+
globalThis.ks ??= {};
|
|
7
|
+
globalThis.ks.domPrimitive ??= {};
|
|
8
|
+
|
|
9
|
+
globalThis.ks.domPrimitive.bindings = {
|
|
10
|
+
meta,
|
|
11
|
+
bindEvents
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default registerGlobal;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const sampleContainer = () => "app";
|
|
2
|
+
|
|
3
|
+
const sampleBindings = () => [
|
|
4
|
+
{
|
|
5
|
+
event: "click",
|
|
6
|
+
selector: "#loadBtn",
|
|
7
|
+
action: "loadData"
|
|
8
|
+
}
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const sampleActions = () => ({
|
|
12
|
+
loadData: () => {}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const sample = () => ({
|
|
16
|
+
containerOrId: sampleContainer(),
|
|
17
|
+
bindings: sampleBindings(),
|
|
18
|
+
actions: sampleActions()
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export { sample, sampleContainer, sampleBindings, sampleActions };
|
|
22
|
+
export default sample;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const isPlainObject = (value) => {
|
|
2
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
3
|
+
const prototype = Object.getPrototypeOf(value);
|
|
4
|
+
return prototype === Object.prototype || prototype === null;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const checkActions = (actions = {}, bindings = []) => {
|
|
8
|
+
if (!isPlainObject(actions)) {
|
|
9
|
+
throw new TypeError("actions must be a plain, flat object.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
for (const [name, action] of Object.entries(actions)) {
|
|
13
|
+
if (typeof action !== "function") {
|
|
14
|
+
throw new TypeError(`actions.${name} must be a function.`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
for (const binding of bindings) {
|
|
19
|
+
if (typeof actions[binding.action] !== "function") {
|
|
20
|
+
throw new TypeError(`actions.${binding.action} must be a function.`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return actions;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default checkActions;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import validateBinding from "./validateBinding.js";
|
|
2
|
+
|
|
3
|
+
const validateArray = (bindings) => {
|
|
4
|
+
if (!Array.isArray(bindings)) {
|
|
5
|
+
throw new TypeError("bindings must be an array.");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
if (bindings.length === 0) {
|
|
9
|
+
throw new TypeError("bindings must contain at least one binding.");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
bindings.forEach((binding, index) => {
|
|
13
|
+
if (!validateBinding(binding)) {
|
|
14
|
+
throw new TypeError(
|
|
15
|
+
`bindings[${index}] must be an object with exactly two non-empty string properties: event and action.`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return bindings;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default validateArray;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const bindingKeys = ["event", "action"];
|
|
2
|
+
|
|
3
|
+
const isPlainObject = (value) => {
|
|
4
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
5
|
+
const prototype = Object.getPrototypeOf(value);
|
|
6
|
+
return prototype === Object.prototype || prototype === null;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const validateBinding = (binding) => {
|
|
10
|
+
if (!isPlainObject(binding)) return false;
|
|
11
|
+
|
|
12
|
+
const keys = Object.keys(binding);
|
|
13
|
+
if (keys.length !== bindingKeys.length) return false;
|
|
14
|
+
if (!bindingKeys.every((key) => Object.hasOwn(binding, key))) return false;
|
|
15
|
+
|
|
16
|
+
return bindingKeys.every((key) => (
|
|
17
|
+
typeof binding[key] === "string" && binding[key].trim().length > 0
|
|
18
|
+
));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { bindingKeys, isPlainObject };
|
|
22
|
+
export default validateBinding;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import resolveElement from "./resolveElement.js";
|
|
2
|
+
|
|
3
|
+
const checkContainer = (containerOrId) => {
|
|
4
|
+
if (!containerOrId) return null;
|
|
5
|
+
|
|
6
|
+
const element = resolveElement(containerOrId);
|
|
7
|
+
if (!element) return null;
|
|
8
|
+
|
|
9
|
+
return element;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default checkContainer;
|
|
13
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const resolveElement = (containerOrId) => {
|
|
2
|
+
if (typeof containerOrId === "string") {
|
|
3
|
+
return document.getElementById(containerOrId) || document.querySelector(containerOrId);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (containerOrId && typeof containerOrId === "object" && "addEventListener" in containerOrId) {
|
|
7
|
+
return containerOrId;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return null;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default resolveElement;
|
|
14
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import checkContainer from "./container/index.js";
|
|
2
|
+
import checkBindings from "./bindings/index.js";
|
|
3
|
+
import checkActions from "./actions/index.js";
|
|
4
|
+
|
|
5
|
+
const runGuards = (containerOrId, bindings, actions = {}) => {
|
|
6
|
+
const container = checkContainer(containerOrId);
|
|
7
|
+
if (!container) return null;
|
|
8
|
+
|
|
9
|
+
const validBindings = checkBindings(bindings);
|
|
10
|
+
const validActions = checkActions(actions, validBindings);
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
container,
|
|
14
|
+
bindings: validBindings,
|
|
15
|
+
actions: validActions
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default runGuards;
|
package/src/v9/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import registerGlobal from "./register/index.js";
|
|
2
|
+
import runGuards from "./guards/index.js";
|
|
3
|
+
import prepareBindings from "./prepare/index.js";
|
|
4
|
+
import attachListeners from "./listeners/index.js";
|
|
5
|
+
import sample, { sampleContainer, sampleBindings, sampleActions } from "./sample/index.js";
|
|
6
|
+
|
|
7
|
+
const check = (containerOrId, bindings = [], actions = {}) =>
|
|
8
|
+
runGuards(containerOrId, bindings, actions);
|
|
9
|
+
|
|
10
|
+
const bindEvents = (containerOrId, bindings = [], actions = {}) => {
|
|
11
|
+
const guarded = check(containerOrId, bindings, actions);
|
|
12
|
+
if (!guarded) return null;
|
|
13
|
+
const { container, bindings: validBindings } = guarded;
|
|
14
|
+
const eventGroups = prepareBindings(validBindings, guarded.actions);
|
|
15
|
+
attachListeners(container, eventGroups);
|
|
16
|
+
return container;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { bindEvents, check, sample, sampleContainer, sampleBindings, sampleActions };
|
|
20
|
+
export default bindEvents;
|
|
21
|
+
registerGlobal(bindEvents);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import attachListener from "./attachListener.js";
|
|
2
|
+
|
|
3
|
+
const attachListeners = (container, eventGroups) => {
|
|
4
|
+
for (const [eventType, bindings] of eventGroups.entries()) {
|
|
5
|
+
attachListener(container, eventType, bindings);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return container;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default attachListeners;
|
|
12
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const groupBindings = (bindings) => {
|
|
2
|
+
const groups = new Map();
|
|
3
|
+
|
|
4
|
+
for (const binding of bindings) {
|
|
5
|
+
const list = groups.get(binding.event) ?? [];
|
|
6
|
+
list.push(binding);
|
|
7
|
+
groups.set(binding.event, list);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return groups;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default groupBindings;
|
|
14
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import groupBindings from "./groupBindings.js";
|
|
2
|
+
|
|
3
|
+
const prepareBindings = (bindings, actions = {}) => {
|
|
4
|
+
const configuredBindings = bindings.map((binding) => ({
|
|
5
|
+
...binding,
|
|
6
|
+
action: actions[binding.action]
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
return groupBindings(configuredBindings);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export default prepareBindings;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import meta from "./meta.js";
|
|
2
|
+
|
|
3
|
+
const registerGlobal = (bindEvents) => {
|
|
4
|
+
if (!bindEvents || typeof globalThis === "undefined") return;
|
|
5
|
+
|
|
6
|
+
globalThis.ks ??= {};
|
|
7
|
+
globalThis.ks.domPrimitive ??= {};
|
|
8
|
+
|
|
9
|
+
globalThis.ks.domPrimitive.bindings = {
|
|
10
|
+
meta,
|
|
11
|
+
bindEvents
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default registerGlobal;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const sampleContainer = () => "app";
|
|
2
|
+
const sampleBindings = () => [
|
|
3
|
+
{ event: "click", action: "handleClick" }
|
|
4
|
+
];
|
|
5
|
+
const sampleActions = () => ({ handleClick: () => {} });
|
|
6
|
+
const sample = () => ({
|
|
7
|
+
containerOrId: sampleContainer(),
|
|
8
|
+
bindings: sampleBindings(),
|
|
9
|
+
actions: sampleActions()
|
|
10
|
+
});
|
|
11
|
+
export { sample, sampleContainer, sampleBindings, sampleActions };
|
|
12
|
+
export default sample;
|