dom-primitive-bindings 1.3.1 → 1.7.3

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.
Files changed (69) hide show
  1. package/LICENSE +22 -22
  2. package/README.md +67 -77
  3. package/package.json +26 -35
  4. package/src/index.js +3 -3
  5. package/src/{v3 → v5}/dispatch/executeBinding.js +21 -21
  6. package/src/{v3 → v5}/dispatch/getClientData.js +8 -8
  7. package/src/{v3 → v5}/dispatch/getStartInput.js +10 -10
  8. package/src/{v3 → v5}/dispatch/index.js +15 -15
  9. package/src/{v3 → v5}/guards/bindings/index.js +11 -11
  10. package/src/{v3 → v5}/guards/bindings/validateArray.js +10 -10
  11. package/src/{v3 → v5}/guards/container/index.js +13 -13
  12. package/src/{v3 → v5}/guards/container/resolveElement.js +14 -14
  13. package/src/{v3 → v5}/guards/index.js +15 -15
  14. package/src/{v3 → v5}/index.js +21 -22
  15. package/src/{v3 → v5}/listeners/attachListener.js +10 -10
  16. package/src/{v3 → v5}/listeners/index.js +12 -12
  17. package/src/{v3 → v5}/prepare/groupBindings.js +14 -14
  18. package/src/v5/prepare/index.js +23 -0
  19. package/src/{v3 → v5}/register/index.js +18 -18
  20. package/src/v5/register/meta.js +6 -0
  21. package/src/v6/dispatch/executeBinding.js +21 -0
  22. package/src/v6/dispatch/getClientData.js +8 -0
  23. package/src/v6/dispatch/getStartInput.js +10 -0
  24. package/src/v6/dispatch/index.js +15 -0
  25. package/src/v6/guards/actions/index.js +21 -0
  26. package/src/v6/guards/bindings/index.js +5 -0
  27. package/src/v6/guards/bindings/validateArray.js +23 -0
  28. package/src/v6/guards/bindings/validateBinding.js +22 -0
  29. package/src/v6/guards/container/index.js +13 -0
  30. package/src/v6/guards/container/resolveElement.js +14 -0
  31. package/src/v6/guards/index.js +19 -0
  32. package/src/v6/index.js +21 -0
  33. package/src/v6/listeners/attachListener.js +10 -0
  34. package/src/v6/listeners/index.js +12 -0
  35. package/src/v6/prepare/groupBindings.js +14 -0
  36. package/src/v6/prepare/index.js +23 -0
  37. package/src/v6/register/index.js +18 -0
  38. package/src/{v3 → v6}/register/meta.js +1 -1
  39. package/src/v7/dispatch/executeBinding.js +21 -0
  40. package/src/v7/dispatch/getClientData.js +8 -0
  41. package/src/v7/dispatch/getStartInput.js +10 -0
  42. package/src/v7/dispatch/index.js +15 -0
  43. package/src/v7/guards/actions/index.js +21 -0
  44. package/src/v7/guards/bindings/index.js +5 -0
  45. package/src/v7/guards/bindings/validateArray.js +23 -0
  46. package/src/v7/guards/bindings/validateBinding.js +22 -0
  47. package/src/v7/guards/container/index.js +13 -0
  48. package/src/v7/guards/container/resolveElement.js +14 -0
  49. package/src/v7/guards/index.js +19 -0
  50. package/src/v7/index.js +43 -0
  51. package/src/v7/listeners/attachListener.js +10 -0
  52. package/src/v7/listeners/index.js +12 -0
  53. package/src/v7/prepare/groupBindings.js +14 -0
  54. package/src/v7/prepare/index.js +23 -0
  55. package/src/v7/register/index.js +18 -0
  56. package/src/v7/register/meta.js +6 -0
  57. package/src/v7/sample/index.js +22 -0
  58. package/docs/architecture.md +0 -111
  59. package/docs/binding-spec.md +0 -86
  60. package/docs/dist/min.js +0 -64
  61. package/docs/dist/v1/min.js +0 -63
  62. package/docs/dist/v2/min.js +0 -64
  63. package/docs/dist/v3/min.js +0 -64
  64. package/docs/index.html +0 -208
  65. package/docs/stories/01-the-listener-trap.html +0 -189
  66. package/docs/stories/02-the-delegation-epiphany.html +0 -185
  67. package/docs/stories/03-the-staged-pipeline.html +0 -162
  68. package/docs/stories/04-declarative-json-bindings.html +0 -159
  69. package/src/v3/prepare/index.js +0 -8
@@ -1,10 +1,10 @@
1
- import dispatchEvent from "../dispatch/index.js";
2
-
3
- const attachListener = (container, eventType, bindings) => {
4
- container.addEventListener(eventType, (event) => {
5
- dispatchEvent({ bindings, event, container });
6
- });
7
- };
8
-
9
- export default attachListener;
10
-
1
+ import dispatchEvent from "../dispatch/index.js";
2
+
3
+ const attachListener = (container, eventType, bindings) => {
4
+ container.addEventListener(eventType, (event) => {
5
+ dispatchEvent({ bindings, event, container });
6
+ });
7
+ };
8
+
9
+ export default attachListener;
10
+
@@ -1,12 +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
-
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
+
@@ -1,14 +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
-
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;
@@ -1,18 +1,18 @@
1
- import meta from "./meta.js";
2
-
3
- const registerGlobal = (bindEvents, metadata = meta) => {
4
- if (!bindEvents || typeof globalThis === "undefined") return;
5
-
6
- globalThis.ks ??= {};
7
-
8
- globalThis.ks.domPrimitiveBindings = {
9
- meta: metadata,
10
- bindEvents
11
- };
12
-
13
- globalThis.ks.bindEvents ??= bindEvents;
14
-
15
- return globalThis.ks.domPrimitiveBindings;
16
- };
17
-
18
- export default registerGlobal;
1
+ import meta from "./meta.js";
2
+
3
+ const registerGlobal = (bindEvents, metadata = meta) => {
4
+ if (!bindEvents || typeof globalThis === "undefined") return;
5
+
6
+ globalThis.ks ??= {};
7
+
8
+ globalThis.ks.domPrimitiveBindings = {
9
+ meta: metadata,
10
+ bindEvents
11
+ };
12
+
13
+ globalThis.ks.bindEvents ??= bindEvents;
14
+
15
+ return globalThis.ks.domPrimitiveBindings;
16
+ };
17
+
18
+ export default registerGlobal;
@@ -0,0 +1,6 @@
1
+ export const meta = {
2
+ name: "dom-primitive-bindings",
3
+ version: "5.0.0"
4
+ };
5
+
6
+ export default meta;
@@ -0,0 +1,21 @@
1
+ import getStartInput from "./getStartInput.js";
2
+ import getClientData from "./getClientData.js";
3
+
4
+ const executeBinding = ({ binding, event, matchElement }) => {
5
+ const startInput = getStartInput(binding, event, matchElement);
6
+ const clientData = getClientData(binding, event, matchElement);
7
+ const runner = binding.runner;
8
+
9
+ const result = typeof runner === "function"
10
+ ? runner(startInput, binding.instructions, clientData)
11
+ : startInput;
12
+
13
+ if (result !== null && result !== undefined && typeof binding.onExtract === "function") {
14
+ binding.onExtract(result, event, matchElement);
15
+ }
16
+
17
+ return result;
18
+ };
19
+
20
+ export default executeBinding;
21
+
@@ -0,0 +1,8 @@
1
+ const getClientData = (binding, event, matchElement) => ({
2
+ ...binding.clientData,
3
+ event,
4
+ targetElement: matchElement
5
+ });
6
+
7
+ export default getClientData;
8
+
@@ -0,0 +1,10 @@
1
+ const getStartInput = (binding, event, matchElement) => {
2
+ if (binding.root === "event" || event?.type?.startsWith("key")) {
3
+ return event;
4
+ }
5
+
6
+ return matchElement;
7
+ };
8
+
9
+ export default getStartInput;
10
+
@@ -0,0 +1,15 @@
1
+ import executeBinding from "./executeBinding.js";
2
+
3
+ const dispatchEvent = ({ bindings, event, container }) => {
4
+ for (const binding of bindings) {
5
+ const matchElement = event.target?.closest?.(binding.selector);
6
+ if (!matchElement || !container.contains(matchElement)) {
7
+ continue;
8
+ }
9
+
10
+ executeBinding({ binding, event, matchElement });
11
+ }
12
+ };
13
+
14
+ export default dispatchEvent;
15
+
@@ -0,0 +1,21 @@
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 = {}) => {
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
+ return actions;
19
+ };
20
+
21
+ export default checkActions;
@@ -0,0 +1,5 @@
1
+ import validateArray from "./validateArray.js";
2
+
3
+ const checkBindings = (bindings) => validateArray(bindings);
4
+
5
+ export default checkBindings;
@@ -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 three non-empty string properties: event, selector, and action.`
16
+ );
17
+ }
18
+ });
19
+
20
+ return bindings;
21
+ };
22
+
23
+ export default validateArray;
@@ -0,0 +1,22 @@
1
+ const bindingKeys = ["event", "selector", "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);
11
+
12
+ return {
13
+ container,
14
+ bindings: validBindings,
15
+ actions: validActions
16
+ };
17
+ };
18
+
19
+ export default runGuards;
@@ -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
+
6
+ const bindEvents = (containerOrId, bindings = [], actions = {}) => {
7
+ const guarded = runGuards(containerOrId, bindings, actions);
8
+ if (!guarded) return null;
9
+
10
+ const { container, bindings: validBindings } = guarded;
11
+ const eventGroups = prepareBindings(validBindings, guarded.actions);
12
+
13
+ attachListeners(container, eventGroups);
14
+
15
+ return container;
16
+ };
17
+
18
+ export { bindEvents };
19
+ export default bindEvents;
20
+
21
+ registerGlobal(bindEvents);
@@ -0,0 +1,10 @@
1
+ import dispatchEvent from "../dispatch/index.js";
2
+
3
+ const attachListener = (container, eventType, bindings) => {
4
+ container.addEventListener(eventType, (event) => {
5
+ dispatchEvent({ bindings, event, container });
6
+ });
7
+ };
8
+
9
+ export default attachListener;
10
+
@@ -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,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,18 @@
1
+ import meta from "./meta.js";
2
+
3
+ const registerGlobal = (bindEvents, metadata = meta) => {
4
+ if (!bindEvents || typeof globalThis === "undefined") return;
5
+
6
+ globalThis.ks ??= {};
7
+
8
+ globalThis.ks.domPrimitiveBindings = {
9
+ meta: metadata,
10
+ bindEvents
11
+ };
12
+
13
+ globalThis.ks.bindEvents ??= bindEvents;
14
+
15
+ return globalThis.ks.domPrimitiveBindings;
16
+ };
17
+
18
+ export default registerGlobal;
@@ -1,6 +1,6 @@
1
1
  export const meta = {
2
2
  name: "dom-primitive-bindings",
3
- version: "3.0.0"
3
+ version: "5.0.0"
4
4
  };
5
5
 
6
6
  export default meta;
@@ -0,0 +1,21 @@
1
+ import getStartInput from "./getStartInput.js";
2
+ import getClientData from "./getClientData.js";
3
+
4
+ const executeBinding = ({ binding, event, matchElement }) => {
5
+ const startInput = getStartInput(binding, event, matchElement);
6
+ const clientData = getClientData(binding, event, matchElement);
7
+ const runner = binding.runner;
8
+
9
+ const result = typeof runner === "function"
10
+ ? runner(startInput, binding.instructions, clientData)
11
+ : startInput;
12
+
13
+ if (result !== null && result !== undefined && typeof binding.onExtract === "function") {
14
+ binding.onExtract(result, event, matchElement);
15
+ }
16
+
17
+ return result;
18
+ };
19
+
20
+ export default executeBinding;
21
+
@@ -0,0 +1,8 @@
1
+ const getClientData = (binding, event, matchElement) => ({
2
+ ...binding.clientData,
3
+ event,
4
+ targetElement: matchElement
5
+ });
6
+
7
+ export default getClientData;
8
+
@@ -0,0 +1,10 @@
1
+ const getStartInput = (binding, event, matchElement) => {
2
+ if (binding.root === "event" || event?.type?.startsWith("key")) {
3
+ return event;
4
+ }
5
+
6
+ return matchElement;
7
+ };
8
+
9
+ export default getStartInput;
10
+
@@ -0,0 +1,15 @@
1
+ import executeBinding from "./executeBinding.js";
2
+
3
+ const dispatchEvent = ({ bindings, event, container }) => {
4
+ for (const binding of bindings) {
5
+ const matchElement = event.target?.closest?.(binding.selector);
6
+ if (!matchElement || !container.contains(matchElement)) {
7
+ continue;
8
+ }
9
+
10
+ executeBinding({ binding, event, matchElement });
11
+ }
12
+ };
13
+
14
+ export default dispatchEvent;
15
+
@@ -0,0 +1,21 @@
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 = {}) => {
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
+ return actions;
19
+ };
20
+
21
+ export default checkActions;
@@ -0,0 +1,5 @@
1
+ import validateArray from "./validateArray.js";
2
+
3
+ const checkBindings = (bindings) => validateArray(bindings);
4
+
5
+ export default checkBindings;
@@ -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 three non-empty string properties: event, selector, and action.`
16
+ );
17
+ }
18
+ });
19
+
20
+ return bindings;
21
+ };
22
+
23
+ export default validateArray;
@@ -0,0 +1,22 @@
1
+ const bindingKeys = ["event", "selector", "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);
11
+
12
+ return {
13
+ container,
14
+ bindings: validBindings,
15
+ actions: validActions
16
+ };
17
+ };
18
+
19
+ export default runGuards;