-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (38 loc) · 1.23 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
39
40
41
42
import { Utils } from 'cable_ready'
const { before, operate, after, processElements, safeString } = Utils
export default {
invokeMethod: operation => {
processElements(operation, element => {
before(element, operation)
operate(operation, () => {
let firstObjectInChain
const { element, receiver, method, args } = operation
const chain = safeString(method).split('.')
switch (receiver) {
case 'window':
firstObjectInChain = window
break
case 'document':
firstObjectInChain = document
break
default:
firstObjectInChain = element
}
let lastObjectInChain = firstObjectInChain
const foundMethod = chain.reduce((lastTerm, nextTerm) => {
lastObjectInChain = lastTerm
return lastTerm[nextTerm] || {}
}, firstObjectInChain)
if (foundMethod instanceof Function) {
foundMethod.apply(lastObjectInChain, args || [])
} else {
console.warn(
`CableReady invoke_method operation failed due to missing '${method}' method for:`,
firstObjectInChain
)
}
})
after(element, operation)
})
}
}