-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon.js
124 lines (110 loc) · 2.44 KB
/
on.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { on, off, warn } from '../../util/index'
// keyCode aliases
const keyCodes = {
esc: 27,
tab: 9,
enter: 13,
space: 32,
'delete': 46,
up: 38,
left: 37,
right: 39,
down: 40
}
function keyFilter (handler, keys) {
var codes = keys.map(function (key) {
var charCode = key.charCodeAt(0)
if (charCode > 47 && charCode < 58) {
return parseInt(key, 10)
}
if (key.length === 1) {
charCode = key.toUpperCase().charCodeAt(0)
if (charCode > 64 && charCode < 91) {
return charCode
}
}
return keyCodes[key]
})
return function keyHandler (e) {
if (codes.indexOf(e.keyCode) > -1) {
return handler.call(this, e)
}
}
}
function stopFilter (handler) {
return function stopHandler (e) {
e.stopPropagation()
return handler.call(this, e)
}
}
function preventFilter (handler) {
return function preventHandler (e) {
e.preventDefault()
return handler.call(this, e)
}
}
export default {
acceptStatement: true,
priority: 700,
bind () {
// deal with iframes
if (
this.el.tagName === 'IFRAME' &&
this.arg !== 'load'
) {
var self = this
this.iframeBind = function () {
on(self.el.contentWindow, self.arg, self.handler)
}
this.on('load', this.iframeBind)
}
},
update (handler) {
// stub a noop for v-on with no value,
// e.g. @mousedown.prevent
if (!this.descriptor.raw) {
handler = function () {}
}
if (typeof handler !== 'function') {
process.env.NODE_ENV !== 'production' && warn(
'v-on:' + this.arg + '="' +
this.expression + '" expects a function value, ' +
'got ' + handler
)
return
}
// apply modifiers
if (this.modifiers.stop) {
handler = stopFilter(handler)
}
if (this.modifiers.prevent) {
handler = preventFilter(handler)
}
// key filter
var keys = Object.keys(this.modifiers)
.filter(function (key) {
return key !== 'stop' && key !== 'prevent'
})
if (keys.length) {
handler = keyFilter(handler, keys)
}
this.reset()
this.handler = handler
if (this.iframeBind) {
this.iframeBind()
} else {
on(this.el, this.arg, this.handler)
}
},
reset () {
var el = this.iframeBind
? this.el.contentWindow
: this.el
if (this.handler) {
off(el, this.arg, this.handler)
}
},
unbind () {
this.reset()
}
}