1- var utils = require ( '../utils' )
2-
3- function delegateCheck ( el , root , identifier ) {
4- while ( el && el !== root ) {
5- if ( el [ identifier ] ) return el
6- el = el . parentNode
7- }
8- }
1+ var warn = require ( 'utils' ) . warn
92
103module . exports = {
114
125 isFn : true ,
136
147 bind : function ( ) {
15- if ( this . compiler . repeat ) {
16- // attach an identifier to the el
17- // so it can be matched during event delegation
18- this . el [ this . expression ] = true
19- // attach the owner viewmodel of this directive
20- this . el . vue_viewmodel = this . vm
8+ // blur and focus events do not bubble
9+ // so they can't be delegated
10+ this . bubbles = this . arg !== 'blur' && this . arg !== 'focus'
11+ if ( this . bubbles ) {
12+ this . compiler . addListener ( this )
2113 }
2214 } ,
2315
2416 update : function ( handler ) {
25- this . reset ( )
2617 if ( typeof handler !== 'function' ) {
27- return utils . warn ( 'Directive "on" expects a function value.' )
18+ return warn ( 'Directive "on" expects a function value.' )
2819 }
29-
30- var compiler = this . compiler ,
31- event = this . arg ,
20+ var targetVM = this . vm ,
21+ ownerVM = this . binding . compiler . vm ,
3222 isExp = this . binding . isExp ,
33- ownerVM = this . binding . compiler . vm
34-
35- if ( compiler . repeat &&
36- // do not delegate if the repeat is combined with an extended VM
37- ! this . vm . constructor . super &&
38- // blur and focus events do not bubble
39- event !== 'blur' && event !== 'focus' ) {
40-
41- // for each blocks, delegate for better performance
42- // focus and blur events dont bubble so exclude them
43- var delegator = compiler . delegator ,
44- identifier = this . expression ,
45- dHandler = delegator . vue_dHandlers [ identifier ]
46-
47- if ( dHandler ) return
48-
49- // the following only gets run once for the entire each block
50- dHandler = delegator . vue_dHandlers [ identifier ] = function ( e ) {
51- var target = delegateCheck ( e . target , delegator , identifier )
52- if ( target ) {
53- e . el = target
54- e . targetVM = target . vue_viewmodel
55- handler . call ( isExp ? e . targetVM : ownerVM , e )
56- }
23+ newHandler = function ( e ) {
24+ e . targetVM = targetVM
25+ handler . call ( isExp ? targetVM : ownerVM , e )
5726 }
58- dHandler . event = event
59- delegator . addEventListener ( event , dHandler )
60-
61- } else {
62-
63- // a normal, single element handler
64- var vm = this . vm
65- this . handler = function ( e ) {
66- e . el = e . currentTarget
67- e . targetVM = vm
68- handler . call ( ownerVM , e )
69- }
70- this . el . addEventListener ( event , this . handler )
71-
27+ if ( ! this . bubbles ) {
28+ this . reset ( )
29+ this . el . addEventListener ( this . arg , newHandler )
7230 }
31+ this . handler = newHandler
7332 } ,
7433
7534 reset : function ( ) {
7635 this . el . removeEventListener ( this . arg , this . handler )
77- this . handler = null
7836 } ,
79-
37+
8038 unbind : function ( ) {
81- this . reset ( )
82- this . el . vue_viewmodel = null
39+ if ( this . bubbles ) {
40+ this . compiler . removeListener ( this )
41+ } else {
42+ this . reset ( )
43+ }
8344 }
8445}
0 commit comments