-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.js
67 lines (59 loc) · 1.6 KB
/
events.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict'
const compiler = require('@nx-js/compiler-util')
const secret = {
handlers: Symbol('event handlers')
}
const handlerCache = new Map()
function events (elem) {
if (elem.nodeType !== 1) return
const handlers = getEventHandlers(elem)
if (handlers) {
handlers.forEach(addEventHandlers, elem)
elem[secret.handlers] = handlers
}
}
events.$name = 'events'
module.exports = events
function getEventHandlers (elem) {
const cloneId = elem.getAttribute('clone-id')
if (cloneId) {
let handlers = handlerCache.get(cloneId)
if (handlers === undefined) {
handlers = createEventHandlers(elem)
handlerCache.set(cloneId, handlers)
}
return handlers
}
return createEventHandlers(elem)
}
function createEventHandlers (elem) {
let handlers = false
const attributes = elem.attributes
let i = attributes.length
while (i--) {
const attribute = attributes[i]
if (attribute.name[0] === '#') {
handlers = handlers || new Map()
const handler = compiler.compileCode(attribute.value)
const names = attribute.name.slice(1).split(',')
for (let name of names) {
let typeHandlers = handlers.get(name)
if (!typeHandlers) {
typeHandlers = new Set()
handlers.set(name, typeHandlers)
}
typeHandlers.add(handler)
}
}
}
return handlers
}
function addEventHandlers (handlers, type) {
this.addEventListener(type, listener, true)
}
function listener (ev) {
const handlers = this[secret.handlers].get(ev.type)
for (let handler of handlers) {
handler(this.$contextState, { $event: ev })
}
}