dom-primitive-bindings 1.0.0 → 1.4.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/LICENSE +22 -22
- package/README.md +163 -126
- package/package.json +27 -36
- package/src/index.js +3 -3
- package/src/{v1 → v2}/dispatch/executeBinding.js +21 -21
- package/src/{v1 → v2}/dispatch/getClientData.js +8 -8
- package/src/{v1 → v2}/dispatch/getStartInput.js +10 -10
- package/src/{v1 → v2}/dispatch/index.js +15 -15
- package/src/{v1 → v2}/guards/bindings/index.js +11 -11
- package/src/{v1 → v2}/guards/bindings/validateArray.js +10 -10
- package/src/{v1 → v2}/guards/container/index.js +13 -13
- package/src/{v1 → v2}/guards/container/resolveElement.js +14 -14
- package/src/{v1 → v2}/guards/index.js +15 -15
- package/src/{v1 → v2}/index.js +22 -22
- package/src/{v1 → v2}/listeners/attachListener.js +10 -10
- package/src/{v1 → v2}/listeners/index.js +12 -12
- package/src/{v1 → v2}/prepare/groupBindings.js +14 -14
- package/src/{v1 → v2}/prepare/index.js +8 -8
- package/src/{v1 → v2}/register/index.js +18 -17
- package/src/{v1 → v2}/register/meta.js +6 -7
- package/src/v3/dispatch/executeBinding.js +21 -0
- package/src/v3/dispatch/getClientData.js +8 -0
- package/src/v3/dispatch/getStartInput.js +10 -0
- package/src/v3/dispatch/index.js +15 -0
- package/src/v3/guards/bindings/index.js +11 -0
- package/src/v3/guards/bindings/validateArray.js +10 -0
- package/src/v3/guards/container/index.js +13 -0
- package/src/v3/guards/container/resolveElement.js +14 -0
- package/src/v3/guards/index.js +15 -0
- package/src/v3/index.js +22 -0
- package/src/v3/listeners/attachListener.js +10 -0
- package/src/v3/listeners/index.js +12 -0
- package/src/v3/prepare/groupBindings.js +14 -0
- package/src/v3/prepare/index.js +8 -0
- package/src/v3/register/index.js +18 -0
- package/src/v3/register/meta.js +6 -0
- package/src/v4/dispatch/executeBinding.js +21 -0
- package/src/v4/dispatch/getClientData.js +8 -0
- package/src/v4/dispatch/getStartInput.js +10 -0
- package/src/v4/dispatch/index.js +15 -0
- package/src/v4/guards/bindings/index.js +11 -0
- package/src/v4/guards/bindings/validateArray.js +10 -0
- package/src/v4/guards/container/index.js +13 -0
- package/src/v4/guards/container/resolveElement.js +14 -0
- package/src/v4/guards/index.js +15 -0
- package/src/v4/index.js +22 -0
- package/src/v4/listeners/attachListener.js +10 -0
- package/src/v4/listeners/index.js +12 -0
- package/src/v4/prepare/groupBindings.js +14 -0
- package/src/v4/prepare/index.js +8 -0
- package/src/v4/register/index.js +18 -0
- package/src/v4/register/meta.js +6 -0
- package/docs/dist/min.js +0 -63
- package/docs/dist/v1/min.js +0 -63
- package/docs/index.html +0 -185
|
@@ -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;
|
package/docs/dist/min.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const u = {
|
|
2
|
-
name: "dom-primitive-bindings",
|
|
3
|
-
version: "1.0.0"
|
|
4
|
-
}, c = (t, n = u) => {
|
|
5
|
-
if (!(!t || typeof globalThis > "u"))
|
|
6
|
-
return globalThis.ks ?? (globalThis.ks = {}), globalThis.ks.domPrimitiveBindings = {
|
|
7
|
-
meta: n,
|
|
8
|
-
bindEvents: t
|
|
9
|
-
}, globalThis.ks.domPrimitiveBindings;
|
|
10
|
-
}, l = (t) => typeof t == "string" ? document.getElementById(t) || document.querySelector(t) : t && typeof t == "object" && "addEventListener" in t ? t : null, a = (t) => {
|
|
11
|
-
if (!t) return null;
|
|
12
|
-
const n = l(t);
|
|
13
|
-
return n || null;
|
|
14
|
-
}, f = (t) => Array.isArray(t) ? t.filter((n) => n && typeof n == "object" && typeof n.event == "string" && typeof n.selector == "string") : [], g = (t) => {
|
|
15
|
-
const n = f(t);
|
|
16
|
-
return n.length === 0 ? null : n;
|
|
17
|
-
}, p = (t, n) => {
|
|
18
|
-
const e = a(t);
|
|
19
|
-
if (!e) return null;
|
|
20
|
-
const r = g(n);
|
|
21
|
-
return r ? { container: e, bindings: r } : null;
|
|
22
|
-
}, d = (t) => {
|
|
23
|
-
const n = /* @__PURE__ */ new Map();
|
|
24
|
-
for (const e of t) {
|
|
25
|
-
const r = n.get(e.event) ?? [];
|
|
26
|
-
r.push(e), n.set(e.event, r);
|
|
27
|
-
}
|
|
28
|
-
return n;
|
|
29
|
-
}, y = (t) => d(t), m = (t, n, e) => {
|
|
30
|
-
var r;
|
|
31
|
-
return t.root === "event" || (r = n == null ? void 0 : n.type) != null && r.startsWith("key") ? n : e;
|
|
32
|
-
}, h = (t, n, e) => ({
|
|
33
|
-
...t.clientData,
|
|
34
|
-
event: n,
|
|
35
|
-
targetElement: e
|
|
36
|
-
}), v = ({ binding: t, event: n, matchElement: e }) => {
|
|
37
|
-
const r = m(t, n, e), i = h(t, n, e), o = t.runner, s = typeof o == "function" ? o(r, t.instructions, i) : r;
|
|
38
|
-
return s != null && typeof t.onExtract == "function" && t.onExtract(s, n, e), s;
|
|
39
|
-
}, B = ({ bindings: t, event: n, container: e }) => {
|
|
40
|
-
var r, i;
|
|
41
|
-
for (const o of t) {
|
|
42
|
-
const s = (i = (r = n.target) == null ? void 0 : r.closest) == null ? void 0 : i.call(r, o.selector);
|
|
43
|
-
!s || !e.contains(s) || v({ binding: o, event: n, matchElement: s });
|
|
44
|
-
}
|
|
45
|
-
}, E = (t, n, e) => {
|
|
46
|
-
t.addEventListener(n, (r) => {
|
|
47
|
-
B({ bindings: e, event: r, container: t });
|
|
48
|
-
});
|
|
49
|
-
}, k = (t, n) => {
|
|
50
|
-
for (const [e, r] of n.entries())
|
|
51
|
-
E(t, e, r);
|
|
52
|
-
return t;
|
|
53
|
-
}, T = (t, n = []) => {
|
|
54
|
-
const e = p(t, n);
|
|
55
|
-
if (!e) return null;
|
|
56
|
-
const { container: r, bindings: i } = e, o = y(i);
|
|
57
|
-
return k(r, o), r;
|
|
58
|
-
};
|
|
59
|
-
c(T);
|
|
60
|
-
export {
|
|
61
|
-
T as bindEvents,
|
|
62
|
-
T as default
|
|
63
|
-
};
|
package/docs/dist/v1/min.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
const u = {
|
|
2
|
-
name: "dom-primitive-bindings",
|
|
3
|
-
version: "1.0.0"
|
|
4
|
-
}, c = (t, n = u) => {
|
|
5
|
-
if (!(!t || typeof globalThis > "u"))
|
|
6
|
-
return globalThis.ks ?? (globalThis.ks = {}), globalThis.ks.domPrimitiveBindings = {
|
|
7
|
-
meta: n,
|
|
8
|
-
bindEvents: t
|
|
9
|
-
}, globalThis.ks.domPrimitiveBindings;
|
|
10
|
-
}, l = (t) => typeof t == "string" ? document.getElementById(t) || document.querySelector(t) : t && typeof t == "object" && "addEventListener" in t ? t : null, a = (t) => {
|
|
11
|
-
if (!t) return null;
|
|
12
|
-
const n = l(t);
|
|
13
|
-
return n || null;
|
|
14
|
-
}, f = (t) => Array.isArray(t) ? t.filter((n) => n && typeof n == "object" && typeof n.event == "string" && typeof n.selector == "string") : [], g = (t) => {
|
|
15
|
-
const n = f(t);
|
|
16
|
-
return n.length === 0 ? null : n;
|
|
17
|
-
}, p = (t, n) => {
|
|
18
|
-
const e = a(t);
|
|
19
|
-
if (!e) return null;
|
|
20
|
-
const r = g(n);
|
|
21
|
-
return r ? { container: e, bindings: r } : null;
|
|
22
|
-
}, d = (t) => {
|
|
23
|
-
const n = /* @__PURE__ */ new Map();
|
|
24
|
-
for (const e of t) {
|
|
25
|
-
const r = n.get(e.event) ?? [];
|
|
26
|
-
r.push(e), n.set(e.event, r);
|
|
27
|
-
}
|
|
28
|
-
return n;
|
|
29
|
-
}, y = (t) => d(t), m = (t, n, e) => {
|
|
30
|
-
var r;
|
|
31
|
-
return t.root === "event" || (r = n == null ? void 0 : n.type) != null && r.startsWith("key") ? n : e;
|
|
32
|
-
}, h = (t, n, e) => ({
|
|
33
|
-
...t.clientData,
|
|
34
|
-
event: n,
|
|
35
|
-
targetElement: e
|
|
36
|
-
}), v = ({ binding: t, event: n, matchElement: e }) => {
|
|
37
|
-
const r = m(t, n, e), i = h(t, n, e), o = t.runner, s = typeof o == "function" ? o(r, t.instructions, i) : r;
|
|
38
|
-
return s != null && typeof t.onExtract == "function" && t.onExtract(s, n, e), s;
|
|
39
|
-
}, B = ({ bindings: t, event: n, container: e }) => {
|
|
40
|
-
var r, i;
|
|
41
|
-
for (const o of t) {
|
|
42
|
-
const s = (i = (r = n.target) == null ? void 0 : r.closest) == null ? void 0 : i.call(r, o.selector);
|
|
43
|
-
!s || !e.contains(s) || v({ binding: o, event: n, matchElement: s });
|
|
44
|
-
}
|
|
45
|
-
}, E = (t, n, e) => {
|
|
46
|
-
t.addEventListener(n, (r) => {
|
|
47
|
-
B({ bindings: e, event: r, container: t });
|
|
48
|
-
});
|
|
49
|
-
}, k = (t, n) => {
|
|
50
|
-
for (const [e, r] of n.entries())
|
|
51
|
-
E(t, e, r);
|
|
52
|
-
return t;
|
|
53
|
-
}, T = (t, n = []) => {
|
|
54
|
-
const e = p(t, n);
|
|
55
|
-
if (!e) return null;
|
|
56
|
-
const { container: r, bindings: i } = e, o = y(i);
|
|
57
|
-
return k(r, o), r;
|
|
58
|
-
};
|
|
59
|
-
c(T);
|
|
60
|
-
export {
|
|
61
|
-
T as bindEvents,
|
|
62
|
-
T as default
|
|
63
|
-
};
|
package/docs/index.html
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
|
|
4
|
-
<head>
|
|
5
|
-
<meta charset="UTF-8">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
-
<title>DOM Primitive Bindings (v1) — Architecture & Story</title>
|
|
8
|
-
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
9
|
-
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
|
|
10
|
-
<style>
|
|
11
|
-
body {
|
|
12
|
-
background-color: #f8fafc;
|
|
13
|
-
color: #334155;
|
|
14
|
-
font-family: system-ui, -apple-system, sans-serif;
|
|
15
|
-
line-height: 1.6;
|
|
16
|
-
}
|
|
17
|
-
.hero-banner {
|
|
18
|
-
background: linear-gradient(135deg, #065f46 0%, #047857 100%);
|
|
19
|
-
color: #ffffff;
|
|
20
|
-
padding: 56px 0;
|
|
21
|
-
margin-bottom: 40px;
|
|
22
|
-
}
|
|
23
|
-
.feature-card {
|
|
24
|
-
background: #ffffff;
|
|
25
|
-
border: 1px solid #e2e8f0;
|
|
26
|
-
border-radius: 12px;
|
|
27
|
-
padding: 24px;
|
|
28
|
-
height: 100%;
|
|
29
|
-
}
|
|
30
|
-
pre {
|
|
31
|
-
background-color: #0f172a;
|
|
32
|
-
color: #f1f5f9;
|
|
33
|
-
padding: 16px;
|
|
34
|
-
border-radius: 8px;
|
|
35
|
-
font-size: 0.9rem;
|
|
36
|
-
overflow-x: auto;
|
|
37
|
-
}
|
|
38
|
-
code:not(pre code) {
|
|
39
|
-
background-color: #e2e8f0;
|
|
40
|
-
color: #0f172a;
|
|
41
|
-
padding: 2px 6px;
|
|
42
|
-
border-radius: 4px;
|
|
43
|
-
font-size: 0.88em;
|
|
44
|
-
}
|
|
45
|
-
</style>
|
|
46
|
-
</head>
|
|
47
|
-
|
|
48
|
-
<body>
|
|
49
|
-
|
|
50
|
-
<header class="hero-banner shadow-sm">
|
|
51
|
-
<div class="container">
|
|
52
|
-
<div class="d-flex align-items-center justify-content-between flex-wrap gap-3">
|
|
53
|
-
<div>
|
|
54
|
-
<span class="badge bg-warning text-dark mb-2">v1 · Delegated Event Engine</span>
|
|
55
|
-
<h1 class="display-6 fw-bold mb-2">DOM Primitive Bindings</h1>
|
|
56
|
-
<p class="lead mb-0 text-white opacity-75">
|
|
57
|
-
Single-listener container delegation with zero overhead. Modern, declarative, and dynamic.
|
|
58
|
-
</p>
|
|
59
|
-
</div>
|
|
60
|
-
<div class="d-flex gap-2">
|
|
61
|
-
<a href="../samples/v1/index.html" class="btn btn-outline-light btn-sm">
|
|
62
|
-
<i class="bi bi-play-circle me-1"></i> Interactive Samples
|
|
63
|
-
</a>
|
|
64
|
-
<a href="../index.html" class="btn btn-light btn-sm text-dark">
|
|
65
|
-
<i class="bi bi-house me-1"></i> Home
|
|
66
|
-
</a>
|
|
67
|
-
</div>
|
|
68
|
-
</div>
|
|
69
|
-
</div>
|
|
70
|
-
</header>
|
|
71
|
-
|
|
72
|
-
<main class="container pb-5" style="max-width: 1000px;">
|
|
73
|
-
|
|
74
|
-
<!-- Section 1: The Core Mental Model -->
|
|
75
|
-
<section class="card shadow-sm border-0 p-4 mb-4">
|
|
76
|
-
<h2 class="h5 fw-bold text-dark mb-3">
|
|
77
|
-
<i class="bi bi-diagram-3-fill text-success me-2"></i> The Event Delegation Story
|
|
78
|
-
</h2>
|
|
79
|
-
<p>
|
|
80
|
-
Traditional web apps attach <code>addEventListener</code> to individual buttons, inputs, and rows. When rows are re-rendered, paginated, or filtered, listeners are either lost or accidentally duplicated, creating memory leaks and stale event handlers.
|
|
81
|
-
</p>
|
|
82
|
-
<p class="mb-0">
|
|
83
|
-
<strong>DOM Primitive Bindings</strong> inverts this pattern. Exactly <strong>one listener per event type</strong> is attached to the parent container (e.g. <code>#table-container</code> or <code>#form-container</code>). When an event occurs, it bubbles to the container, checks for matched selectors via <code>closest(selector)</code>, and dispatches to the registered runner. New DOM elements added at runtime work immediately with zero re-binding!
|
|
84
|
-
</p>
|
|
85
|
-
</section>
|
|
86
|
-
|
|
87
|
-
<!-- Section 2: Staged Architecture -->
|
|
88
|
-
<section class="card shadow-sm border-0 p-4 mb-4">
|
|
89
|
-
<h2 class="h5 fw-bold text-dark mb-3">
|
|
90
|
-
<i class="bi bi-layers-fill text-primary me-2"></i> Staged Architecture in <code>src/v1/</code>
|
|
91
|
-
</h2>
|
|
92
|
-
|
|
93
|
-
<div class="row g-3">
|
|
94
|
-
<div class="col-md-6">
|
|
95
|
-
<div class="feature-card">
|
|
96
|
-
<h6 class="fw-bold text-success mb-2">
|
|
97
|
-
<i class="bi bi-shield-check me-1"></i> 1. guards/
|
|
98
|
-
</h6>
|
|
99
|
-
<p class="small text-muted mb-0">
|
|
100
|
-
Validates inputs before any DOM mutation occurs. Separated into <code>container/</code> (validates first input) and <code>bindings/</code> (validates second input array).
|
|
101
|
-
</p>
|
|
102
|
-
</div>
|
|
103
|
-
</div>
|
|
104
|
-
|
|
105
|
-
<div class="col-md-6">
|
|
106
|
-
<div class="feature-card">
|
|
107
|
-
<h6 class="fw-bold text-info mb-2">
|
|
108
|
-
<i class="bi bi-collection me-1"></i> 2. prepare/
|
|
109
|
-
</h6>
|
|
110
|
-
<p class="small text-muted mb-0">
|
|
111
|
-
Groups bindings by event type into a <code>Map<eventType, bindings[]></code>, ensuring only a single listener is registered per event type.
|
|
112
|
-
</p>
|
|
113
|
-
</div>
|
|
114
|
-
</div>
|
|
115
|
-
|
|
116
|
-
<div class="col-md-6">
|
|
117
|
-
<div class="feature-card">
|
|
118
|
-
<h6 class="fw-bold text-primary mb-2">
|
|
119
|
-
<i class="bi bi-earbuds me-1"></i> 3. listeners/
|
|
120
|
-
</h6>
|
|
121
|
-
<p class="small text-muted mb-0">
|
|
122
|
-
Attaches container event listeners that intercept bubbling browser events and pass them down into the dispatch pipeline.
|
|
123
|
-
</p>
|
|
124
|
-
</div>
|
|
125
|
-
</div>
|
|
126
|
-
|
|
127
|
-
<div class="col-md-6">
|
|
128
|
-
<div class="feature-card">
|
|
129
|
-
<h6 class="fw-bold text-warning mb-2">
|
|
130
|
-
<i class="bi bi-lightning-charge me-1"></i> 4. dispatch/
|
|
131
|
-
</h6>
|
|
132
|
-
<p class="small text-muted mb-0">
|
|
133
|
-
Finds the matching target via <code>closest(selector)</code>, resolves start inputs, executes the runner, and emits extracted data to <code>onExtract</code>.
|
|
134
|
-
</p>
|
|
135
|
-
</div>
|
|
136
|
-
</div>
|
|
137
|
-
</div>
|
|
138
|
-
</section>
|
|
139
|
-
|
|
140
|
-
<!-- Section 3: Browser API & Window.ks -->
|
|
141
|
-
<section class="card shadow-sm border-0 p-4 mb-4">
|
|
142
|
-
<h2 class="h5 fw-bold text-dark mb-2">
|
|
143
|
-
<i class="bi bi-globe2 text-info me-2"></i> Browser Usage via <code>window.ks</code>
|
|
144
|
-
</h2>
|
|
145
|
-
<p class="small text-muted mb-3">
|
|
146
|
-
Include the bundled distribution or ES module, then call <code>bindEvents</code>:
|
|
147
|
-
</p>
|
|
148
|
-
|
|
149
|
-
<pre class="mb-3"><code><script type="module" src="/docs/dist/v1/min.js"></script>
|
|
150
|
-
<script type="module">
|
|
151
|
-
const bindings = [
|
|
152
|
-
{
|
|
153
|
-
event: "click",
|
|
154
|
-
selector: ".btn-delete",
|
|
155
|
-
runner: (target) => ({ id: target.dataset.id }),
|
|
156
|
-
onExtract: (data) => console.log("Deleted:", data)
|
|
157
|
-
}
|
|
158
|
-
];
|
|
159
|
-
|
|
160
|
-
window.ks.domPrimitiveBindings.bindEvents("container-id", bindings);
|
|
161
|
-
</script></code></pre>
|
|
162
|
-
</section>
|
|
163
|
-
|
|
164
|
-
<!-- Section 4: Design Rules & Zero Comments -->
|
|
165
|
-
<section class="card shadow-sm border-0 p-4">
|
|
166
|
-
<h2 class="h5 fw-bold text-dark mb-3">
|
|
167
|
-
<i class="bi bi-check2-circle text-success me-2"></i> Code Conventions
|
|
168
|
-
</h2>
|
|
169
|
-
<ul class="list-unstyled mb-0 small text-muted">
|
|
170
|
-
<li class="mb-2"><i class="bi bi-check-lg text-success me-2"></i><strong>Every folder has an <code>index.js</code></strong> file acting as its public gateway.</li>
|
|
171
|
-
<li class="mb-2"><i class="bi bi-check-lg text-success me-2"></i><strong>Single-function default export</strong> per file (no default export objects).</li>
|
|
172
|
-
<li class="mb-2"><i class="bi bi-check-lg text-success me-2"></i><strong>Zero comments in code:</strong> Folders and file names tell the story directly.</li>
|
|
173
|
-
<li><i class="bi bi-check-lg text-success me-2"></i><strong>Independent sibling package:</strong> Focused purely on event delegation without mixing extraction concerns.</li>
|
|
174
|
-
</ul>
|
|
175
|
-
</section>
|
|
176
|
-
|
|
177
|
-
</main>
|
|
178
|
-
|
|
179
|
-
<footer class="text-center text-muted py-4 border-top small">
|
|
180
|
-
DOM Primitive Bindings · Single Source of Truth: <code>v1</code> · MIT License
|
|
181
|
-
</footer>
|
|
182
|
-
|
|
183
|
-
</body>
|
|
184
|
-
|
|
185
|
-
</html>
|