stimulus-use-actions 0.3.4 → 0.3.5
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 +5 -2
- package/index.js +32 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ means:
|
|
|
37
37
|
- Listeners are added on `connect()` and removed on `disconnect()`
|
|
38
38
|
- Target elements can appear and disappear in the DOM at any time -- events are
|
|
39
39
|
still captured
|
|
40
|
-
- No need to call
|
|
40
|
+
- No need to call `super.connect()` -- actions bind automatically
|
|
41
41
|
|
|
42
42
|
### Keys
|
|
43
43
|
|
|
@@ -76,7 +76,10 @@ export default class extends Controller {
|
|
|
76
76
|
|
|
77
77
|
connect() {
|
|
78
78
|
useActions(this, {
|
|
79
|
-
buttonTargets: [
|
|
79
|
+
buttonTargets: [
|
|
80
|
+
"click->submit",
|
|
81
|
+
"keyup->preview",
|
|
82
|
+
],
|
|
80
83
|
element: "submit->save",
|
|
81
84
|
window: "resize->reflow",
|
|
82
85
|
})
|
package/index.js
CHANGED
|
@@ -78,6 +78,27 @@ function parseDescriptor(descriptor) {
|
|
|
78
78
|
return { eventName, filter, methodName, options }
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
function camelize(str) {
|
|
82
|
+
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase())
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function typecast(value) {
|
|
86
|
+
try { return JSON.parse(value) } catch { return value }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function paramsForElement(element, identifier) {
|
|
90
|
+
const params = {}
|
|
91
|
+
const prefix = `data-${identifier}-`
|
|
92
|
+
const suffix = "-param"
|
|
93
|
+
for (const attr of element.attributes) {
|
|
94
|
+
if (attr.name.startsWith(prefix) && attr.name.endsWith(suffix)) {
|
|
95
|
+
const key = camelize(attr.name.slice(prefix.length, -suffix.length))
|
|
96
|
+
params[key] = typecast(attr.value)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return Object.freeze(params)
|
|
100
|
+
}
|
|
101
|
+
|
|
81
102
|
function bindDelegatedActions(controller) {
|
|
82
103
|
const actions = controller.constructor.actions
|
|
83
104
|
if (!actions || typeof actions !== "object") return []
|
|
@@ -113,15 +134,24 @@ function bindDelegatedActions(controller) {
|
|
|
113
134
|
guard = (event) => {
|
|
114
135
|
const matched = event.target.closest(selector)
|
|
115
136
|
if (!matched || !controller.element.contains(matched)) return false
|
|
116
|
-
|
|
137
|
+
if (options.includes("self") && event.target !== matched) return false
|
|
138
|
+
return matched
|
|
117
139
|
}
|
|
118
140
|
}
|
|
119
141
|
|
|
120
142
|
const handler = (event) => {
|
|
121
|
-
|
|
143
|
+
const match = guard(event)
|
|
144
|
+
if (!match) return
|
|
122
145
|
if (filter && event.key !== (KEY_MAP[filter] || filter)) return
|
|
123
146
|
if (options.includes("stop")) event.stopPropagation()
|
|
124
147
|
if (options.includes("prevent")) event.preventDefault()
|
|
148
|
+
if (match instanceof Element) {
|
|
149
|
+
Object.defineProperty(event, "currentTarget", { value: match, configurable: true })
|
|
150
|
+
Object.defineProperty(event, "params", {
|
|
151
|
+
value: paramsForElement(match, identifier),
|
|
152
|
+
configurable: true,
|
|
153
|
+
})
|
|
154
|
+
}
|
|
125
155
|
controller[methodName](event)
|
|
126
156
|
}
|
|
127
157
|
const capture = !isWindow && !isElement
|