1 |
|
2 |
|
3 | import {
|
4 | warn,
|
5 | invokeWithErrorHandling
|
6 | } from 'core/util/index'
|
7 | import {
|
8 | cached,
|
9 | isUndef,
|
10 | isTrue,
|
11 | isPlainObject
|
12 | } from 'shared/util'
|
13 |
|
14 | const normalizeEvent = cached((name: string): {
|
15 | name: string,
|
16 | once: boolean,
|
17 | capture: boolean,
|
18 | passive: boolean,
|
19 | handler?: Function,
|
20 | params?: Array<any>
|
21 | } => {
|
22 | const passive = name.charAt(0) === '&'
|
23 | name = passive ? name.slice(1) : name
|
24 | const once = name.charAt(0) === '~'
|
25 | name = once ? name.slice(1) : name
|
26 | const capture = name.charAt(0) === '!'
|
27 | name = capture ? name.slice(1) : name
|
28 | return {
|
29 | name,
|
30 | once,
|
31 | capture,
|
32 | passive
|
33 | }
|
34 | })
|
35 |
|
36 | export function createFnInvoker (fns: Function | Array<Function>, vm: ?Component): Function {
|
37 | function invoker () {
|
38 | const fns = invoker.fns
|
39 | if (Array.isArray(fns)) {
|
40 | const cloned = fns.slice()
|
41 | for (let i = 0; i < cloned.length; i++) {
|
42 | invokeWithErrorHandling(cloned[i], null, arguments, vm, `v-on handler`)
|
43 | }
|
44 | } else {
|
45 |
|
46 | return invokeWithErrorHandling(fns, null, arguments, vm, `v-on handler`)
|
47 | }
|
48 | }
|
49 | invoker.fns = fns
|
50 | return invoker
|
51 | }
|
52 |
|
53 | export function updateListeners (
|
54 | on: Object,
|
55 | oldOn: Object,
|
56 | add: Function,
|
57 | remove: Function,
|
58 | createOnceHandler: Function,
|
59 | vm: Component
|
60 | ) {
|
61 | let name, def, cur, old, event
|
62 | for (name in on) {
|
63 | def = cur = on[name]
|
64 | old = oldOn[name]
|
65 | event = normalizeEvent(name)
|
66 |
|
67 | if (__WEEX__ && isPlainObject(def)) {
|
68 | cur = def.handler
|
69 | event.params = def.params
|
70 | }
|
71 | if (isUndef(cur)) {
|
72 | process.env.NODE_ENV !== 'production' && warn(
|
73 | `Invalid handler for event "${event.name}": got ` + String(cur),
|
74 | vm
|
75 | )
|
76 | } else if (isUndef(old)) {
|
77 | if (isUndef(cur.fns)) {
|
78 | cur = on[name] = createFnInvoker(cur, vm)
|
79 | }
|
80 | if (isTrue(event.once)) {
|
81 | cur = on[name] = createOnceHandler(event.name, cur, event.capture)
|
82 | }
|
83 | add(event.name, cur, event.capture, event.passive, event.params)
|
84 | } else if (cur !== old) {
|
85 | old.fns = cur
|
86 | on[name] = old
|
87 | }
|
88 | }
|
89 | for (name in oldOn) {
|
90 | if (isUndef(on[name])) {
|
91 | event = normalizeEvent(name)
|
92 | remove(event.name, oldOn[name], event.capture)
|
93 | }
|
94 | }
|
95 | }
|