dom-primitive-bindings 1.4.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.
- package/README.md +153 -163
- package/package.json +1 -1
- package/src/index.js +2 -2
- package/src/{v3 → v5}/index.js +2 -3
- package/src/v5/prepare/index.js +23 -0
- package/src/{v4 → v5}/register/meta.js +1 -1
- package/src/v6/guards/actions/index.js +21 -0
- package/src/v6/guards/bindings/index.js +5 -0
- package/src/v6/guards/bindings/validateArray.js +23 -0
- package/src/v6/guards/bindings/validateBinding.js +22 -0
- package/src/{v4 → v6}/guards/index.js +19 -15
- package/src/{v2 → v6}/index.js +21 -22
- package/src/v6/prepare/index.js +23 -0
- package/src/{v2 → v6}/register/meta.js +6 -6
- package/src/v7/guards/actions/index.js +21 -0
- package/src/v7/guards/bindings/index.js +5 -0
- package/src/v7/guards/bindings/validateArray.js +23 -0
- package/src/v7/guards/bindings/validateBinding.js +22 -0
- package/src/{v3 → v7}/guards/index.js +19 -15
- package/src/v7/index.js +43 -0
- package/src/v7/prepare/index.js +23 -0
- package/src/{v4 → v7}/register/index.js +18 -18
- package/src/{v3 → v7}/register/meta.js +6 -6
- package/src/v7/sample/index.js +22 -0
- package/src/v2/prepare/index.js +0 -8
- package/src/v3/guards/bindings/index.js +0 -11
- package/src/v3/guards/bindings/validateArray.js +0 -10
- package/src/v3/prepare/index.js +0 -8
- package/src/v4/guards/bindings/index.js +0 -11
- package/src/v4/guards/bindings/validateArray.js +0 -10
- package/src/v4/index.js +0 -22
- package/src/v4/prepare/index.js +0 -8
- /package/src/{v2 → v5}/dispatch/executeBinding.js +0 -0
- /package/src/{v2 → v5}/dispatch/getClientData.js +0 -0
- /package/src/{v2 → v5}/dispatch/getStartInput.js +0 -0
- /package/src/{v2 → v5}/dispatch/index.js +0 -0
- /package/src/{v2 → v5}/guards/bindings/index.js +0 -0
- /package/src/{v2 → v5}/guards/bindings/validateArray.js +0 -0
- /package/src/{v2 → v5}/guards/container/index.js +0 -0
- /package/src/{v2 → v5}/guards/container/resolveElement.js +0 -0
- /package/src/{v2 → v5}/guards/index.js +0 -0
- /package/src/{v2 → v5}/listeners/attachListener.js +0 -0
- /package/src/{v2 → v5}/listeners/index.js +0 -0
- /package/src/{v2 → v5}/prepare/groupBindings.js +0 -0
- /package/src/{v2 → v5}/register/index.js +0 -0
- /package/src/{v3 → v6}/dispatch/executeBinding.js +0 -0
- /package/src/{v3 → v6}/dispatch/getClientData.js +0 -0
- /package/src/{v3 → v6}/dispatch/getStartInput.js +0 -0
- /package/src/{v3 → v6}/dispatch/index.js +0 -0
- /package/src/{v3 → v6}/guards/container/index.js +0 -0
- /package/src/{v3 → v6}/guards/container/resolveElement.js +0 -0
- /package/src/{v3 → v6}/listeners/attachListener.js +0 -0
- /package/src/{v3 → v6}/listeners/index.js +0 -0
- /package/src/{v3 → v6}/prepare/groupBindings.js +0 -0
- /package/src/{v3 → v6}/register/index.js +0 -0
- /package/src/{v4 → v7}/dispatch/executeBinding.js +0 -0
- /package/src/{v4 → v7}/dispatch/getClientData.js +0 -0
- /package/src/{v4 → v7}/dispatch/getStartInput.js +0 -0
- /package/src/{v4 → v7}/dispatch/index.js +0 -0
- /package/src/{v4 → v7}/guards/container/index.js +0 -0
- /package/src/{v4 → v7}/guards/container/resolveElement.js +0 -0
- /package/src/{v4 → v7}/listeners/attachListener.js +0 -0
- /package/src/{v4 → v7}/listeners/index.js +0 -0
- /package/src/{v4 → v7}/prepare/groupBindings.js +0 -0
package/README.md
CHANGED
|
@@ -1,163 +1,153 @@
|
|
|
1
|
-
# DOM Primitive Bindings
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/dom-primitive-bindings)
|
|
6
|
-
[](https://opensource.org/licenses/MIT)
|
|
7
|
-
[](https://www.npmjs.com/package/dom-primitive-bindings)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://keshavsoft.github.io/dom-primitive-bindings/)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 🌐 Live Documentation & Interactive Story
|
|
12
|
+
|
|
13
|
+
Explore the complete interactive story and live samples on **[GitHub Pages](https://keshavsoft.github.io/dom-primitive-bindings/)**:
|
|
14
|
+
|
|
15
|
+
1. **[Chapter 1: The Problem (Why?)](https://keshavsoft.github.io/dom-primitive-bindings/why/)** ([local](./docs/why/index.html))
|
|
16
|
+
Why attaching listeners to individual elements causes memory leaks, double-trigger cascades, and re-render dead zones.
|
|
17
|
+
2. **[Chapter 2: How We Achieved It](https://keshavsoft.github.io/dom-primitive-bindings/how/)** ([local](./docs/how/index.html))
|
|
18
|
+
The delegation epiphany: leveraging browser event bubbling across a decoupled 4-stage pipeline (`guards` $\to$ `prepare` $\to$ `listeners` $\to$ `dispatch`).
|
|
19
|
+
3. **[Chapter 3: Declarative JSON](https://keshavsoft.github.io/dom-primitive-bindings/declarative/)** ([local](./docs/declarative/index.html))
|
|
20
|
+
Separating intent from execution: why JSON instructions make UI code human-readable, statically guardable, and self-documenting.
|
|
21
|
+
4. **[Usage & Available Builds](https://keshavsoft.github.io/dom-primitive-bindings/usage/)** ([local](./docs/usage/index.html))
|
|
22
|
+
Available builds hosted on GitHub Pages CDN with one-click copyable script tags and ES Module imports.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 🎯 The Philosophy: The Wiring Layer
|
|
27
|
+
|
|
28
|
+
A common mistake in UI architecture is conflating **event wiring** with **DOM manipulation** or **data extraction**:
|
|
29
|
+
|
|
30
|
+
- **`dom-primitive-bindings` (This Repository):**
|
|
31
|
+
Pure **Wiring Layer**. Attaches one container listener per event type, matches bubbling targets via `closest(selector)`, and delivers `{ event, targetElement }`. It does **not** manipulate DOM elements and does **not** extract schemas.
|
|
32
|
+
- **`dom-primitive-engine` (Sibling Repository):**
|
|
33
|
+
Pure **Extraction Layer**. Receives the matched target element and executes declarative instruction pipelines (e.g. `closest("tr")`, `querySelectorAll("td")`) to produce clean JSON.
|
|
34
|
+
|
|
35
|
+
```text
|
|
36
|
+
[ User Action in Browser ]
|
|
37
|
+
│
|
|
38
|
+
▼
|
|
39
|
+
┌───────────────────────────────┐
|
|
40
|
+
│ dom-primitive-bindings │ ◄── Wiring Layer: Catches bubbling event & matches target
|
|
41
|
+
│ (guards → prepare → dispatch)│
|
|
42
|
+
└──────────────┬────────────────┘
|
|
43
|
+
│ delivers { event, targetElement }
|
|
44
|
+
▼
|
|
45
|
+
┌───────────────────────────────┐
|
|
46
|
+
│ Consumer / App Logic │ ◄── Extraction (dom-primitive-engine) or Action Handler
|
|
47
|
+
└───────────────────────────────┘
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## ⚡ Quick Start & Distribution
|
|
53
|
+
|
|
54
|
+
### 1. Direct Script Tag (Zero Install via GitHub Pages CDN)
|
|
55
|
+
|
|
56
|
+
```html
|
|
57
|
+
<script type="module" src="https://keshavsoft.github.io/dom-primitive-bindings/dist/v4/min.js"></script>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Or always use the latest build:
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<script type="module" src="https://keshavsoft.github.io/dom-primitive-bindings/dist/min.js"></script>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 2. ES Module Import in JavaScript
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
import { bindEvents } from "https://keshavsoft.github.io/dom-primitive-bindings/dist/v4/min.js";
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. NPM Package
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
npm install dom-primitive-bindings
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## 📦 Usage Example
|
|
81
|
+
|
|
82
|
+
### 1. Declare `bindings.json`
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
[
|
|
86
|
+
{
|
|
87
|
+
"event": "click",
|
|
88
|
+
"selector": "#rakaBtn",
|
|
89
|
+
"narration": "Load and populate Raka columns JSON into editor"
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
"event": "click",
|
|
93
|
+
"selector": "#templateBtn",
|
|
94
|
+
"narration": "Load and populate template JSON into editor"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
"event": "click",
|
|
98
|
+
"selector": "#buildBtn",
|
|
99
|
+
"narration": "Transform columns with template and display output in editor"
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 2. Delegate with `bindEvents`
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
import { bindEvents } from "https://keshavsoft.github.io/dom-primitive-bindings/dist/v4/min.js";
|
|
108
|
+
import bindings from "./bindings.json" with { type: "json" };
|
|
109
|
+
|
|
110
|
+
bindEvents("app-container", bindings);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## 🏗️ Staged Pipeline Architecture
|
|
116
|
+
|
|
117
|
+
The engine enforces a decoupled pipeline where every directory contains an `index.js` public gateway:
|
|
118
|
+
|
|
119
|
+
```text
|
|
120
|
+
src/v4/
|
|
121
|
+
├── guards/ # Validates container & bindings array before execution
|
|
122
|
+
├── prepare/ # Groups bindings by event type into Map
|
|
123
|
+
├── listeners/ # Attaches single container listener per event type
|
|
124
|
+
├── dispatch/ # Handles event bubbling, target matching (closest) & execution
|
|
125
|
+
├── register/ # Exposes module onto window.ks.domPrimitiveBindings
|
|
126
|
+
└── index.js # Orchestrator
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## 🧪 Testing & Build
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# Run unit tests
|
|
135
|
+
npm test
|
|
136
|
+
|
|
137
|
+
# Build production bundle using build-cli
|
|
138
|
+
npx @keshavsoft/build-cli build
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## 🔗 Useful Links
|
|
144
|
+
|
|
145
|
+
- **Live Documentation**: [https://keshavsoft.github.io/dom-primitive-bindings/](https://keshavsoft.github.io/dom-primitive-bindings/)
|
|
146
|
+
- **Copyable Script Links**: [https://keshavsoft.github.io/dom-primitive-bindings/usage/](https://keshavsoft.github.io/dom-primitive-bindings/usage/)
|
|
147
|
+
- **NPM Package**: [https://www.npmjs.com/package/dom-primitive-bindings](https://www.npmjs.com/package/dom-primitive-bindings)
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## 📄 License
|
|
152
|
+
|
|
153
|
+
MIT © KeshavSoft
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./v7/index.js";
|
|
2
2
|
|
|
3
|
-
export { default } from "./
|
|
3
|
+
export { default } from "./v7/index.js";
|
package/src/{v3 → v5}/index.js
RENAMED
|
@@ -3,12 +3,12 @@ import runGuards from "./guards/index.js";
|
|
|
3
3
|
import prepareBindings from "./prepare/index.js";
|
|
4
4
|
import attachListeners from "./listeners/index.js";
|
|
5
5
|
|
|
6
|
-
const bindEvents = (containerOrId, bindings = []) => {
|
|
6
|
+
const bindEvents = (containerOrId, bindings = [], actions = {}) => {
|
|
7
7
|
const guarded = runGuards(containerOrId, bindings);
|
|
8
8
|
if (!guarded) return null;
|
|
9
9
|
|
|
10
10
|
const { container, bindings: validBindings } = guarded;
|
|
11
|
-
const eventGroups = prepareBindings(validBindings);
|
|
11
|
+
const eventGroups = prepareBindings(validBindings, actions);
|
|
12
12
|
|
|
13
13
|
attachListeners(container, eventGroups);
|
|
14
14
|
|
|
@@ -19,4 +19,3 @@ export { bindEvents };
|
|
|
19
19
|
export default bindEvents;
|
|
20
20
|
|
|
21
21
|
registerGlobal(bindEvents);
|
|
22
|
-
|
|
@@ -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,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,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;
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import checkContainer from "./container/index.js";
|
|
2
|
-
import checkBindings from "./bindings/index.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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;
|
package/src/{v2 → v6}/index.js
RENAMED
|
@@ -1,22 +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 = []) => {
|
|
7
|
-
const guarded = runGuards(containerOrId, bindings);
|
|
8
|
-
if (!guarded) return null;
|
|
9
|
-
|
|
10
|
-
const { container, bindings: validBindings } = guarded;
|
|
11
|
-
const eventGroups = prepareBindings(validBindings);
|
|
12
|
-
|
|
13
|
-
attachListeners(container, eventGroups);
|
|
14
|
-
|
|
15
|
-
return container;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export { bindEvents };
|
|
19
|
-
export default bindEvents;
|
|
20
|
-
|
|
21
|
-
registerGlobal(bindEvents);
|
|
22
|
-
|
|
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,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,6 +1,6 @@
|
|
|
1
|
-
export const meta = {
|
|
2
|
-
name: "dom-primitive-bindings",
|
|
3
|
-
version: "
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export default meta;
|
|
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
|
+
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,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;
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import checkContainer from "./container/index.js";
|
|
2
|
-
import checkBindings from "./bindings/index.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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;
|
package/src/v7/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
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, {
|
|
6
|
+
sampleContainer,
|
|
7
|
+
sampleBindings,
|
|
8
|
+
sampleActions
|
|
9
|
+
} from "./sample/index.js";
|
|
10
|
+
|
|
11
|
+
const check = (containerOrId, bindings = [], actions = {}) =>
|
|
12
|
+
runGuards(containerOrId, bindings, actions);
|
|
13
|
+
|
|
14
|
+
const bindEvents = (containerOrId, bindings = [], actions = {}) => {
|
|
15
|
+
const guarded = check(containerOrId, bindings, actions);
|
|
16
|
+
if (!guarded) return null;
|
|
17
|
+
|
|
18
|
+
const { container, bindings: validBindings } = guarded;
|
|
19
|
+
const eventGroups = prepareBindings(validBindings, guarded.actions);
|
|
20
|
+
|
|
21
|
+
attachListeners(container, eventGroups);
|
|
22
|
+
|
|
23
|
+
return container;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
bindEvents,
|
|
28
|
+
check,
|
|
29
|
+
sample,
|
|
30
|
+
sampleContainer,
|
|
31
|
+
sampleBindings,
|
|
32
|
+
sampleActions
|
|
33
|
+
};
|
|
34
|
+
export default bindEvents;
|
|
35
|
+
|
|
36
|
+
registerGlobal({
|
|
37
|
+
bindEvents,
|
|
38
|
+
check,
|
|
39
|
+
sample,
|
|
40
|
+
sampleContainer,
|
|
41
|
+
sampleBindings,
|
|
42
|
+
sampleActions
|
|
43
|
+
});
|
|
@@ -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 = (
|
|
4
|
-
if (!
|
|
5
|
-
|
|
6
|
-
globalThis.ks ??= {};
|
|
7
|
-
|
|
8
|
-
globalThis.ks.domPrimitiveBindings = {
|
|
9
|
-
meta: metadata,
|
|
10
|
-
|
|
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 = (api, metadata = meta) => {
|
|
4
|
+
if (!api || typeof globalThis === "undefined") return;
|
|
5
|
+
|
|
6
|
+
globalThis.ks ??= {};
|
|
7
|
+
|
|
8
|
+
globalThis.ks.domPrimitiveBindings = {
|
|
9
|
+
meta: metadata,
|
|
10
|
+
...api
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
globalThis.ks.bindEvents ??= api.bindEvents;
|
|
14
|
+
|
|
15
|
+
return globalThis.ks.domPrimitiveBindings;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default registerGlobal;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export const meta = {
|
|
2
|
-
name: "dom-primitive-bindings",
|
|
3
|
-
version: "
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export default meta;
|
|
1
|
+
export const meta = {
|
|
2
|
+
name: "dom-primitive-bindings",
|
|
3
|
+
version: "5.0.0"
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export default meta;
|
|
@@ -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;
|
package/src/v2/prepare/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const validateArray = (bindings) => {
|
|
2
|
-
if (!Array.isArray(bindings)) return [];
|
|
3
|
-
|
|
4
|
-
return bindings.filter((binding) => (
|
|
5
|
-
binding && typeof binding === "object" && typeof binding.event === "string" && typeof binding.selector === "string"
|
|
6
|
-
));
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export default validateArray;
|
|
10
|
-
|
package/src/v3/prepare/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const validateArray = (bindings) => {
|
|
2
|
-
if (!Array.isArray(bindings)) return [];
|
|
3
|
-
|
|
4
|
-
return bindings.filter((binding) => (
|
|
5
|
-
binding && typeof binding === "object" && typeof binding.event === "string" && typeof binding.selector === "string"
|
|
6
|
-
));
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export default validateArray;
|
|
10
|
-
|
package/src/v4/index.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
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 = []) => {
|
|
7
|
-
const guarded = runGuards(containerOrId, bindings);
|
|
8
|
-
if (!guarded) return null;
|
|
9
|
-
|
|
10
|
-
const { container, bindings: validBindings } = guarded;
|
|
11
|
-
const eventGroups = prepareBindings(validBindings);
|
|
12
|
-
|
|
13
|
-
attachListeners(container, eventGroups);
|
|
14
|
-
|
|
15
|
-
return container;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export { bindEvents };
|
|
19
|
-
export default bindEvents;
|
|
20
|
-
|
|
21
|
-
registerGlobal(bindEvents);
|
|
22
|
-
|
package/src/v4/prepare/index.js
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|