-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
38 lines (29 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { dataset } from '@newlogic-digital/utils-js'
export function initActions(parent, selectors) {
if (!parent) return
selectors.forEach(([selector, action]) => {
parent.querySelectorAll(selector).forEach((element) => {
dataset(element, 'action').add(action)
})
})
}
export function initControllers(parent, selectors) {
if (!parent) return
selectors.forEach((selector) => {
[...parent.getElementsByClassName(selector)].forEach((element) => {
dataset(element, 'controller').add(selector)
})
})
}
export const initStimulus = (element, { controllers, actions }) => {
initControllers(element, controllers)
initActions(element, actions)
}
export const useController = (controller, target, application) => {
const getController = application.getControllerForElementAndIdentifier(document.querySelector(target || `.${controller}`), controller) ?? {}
getController.invoke = (action, event) => {
return getController[action] ? getController[action](event ?? { params: {} }) : undefined
}
return getController
}
export default { initStimulus, initActions, initControllers, useController }