|
|
@@ -1,84 +1,45 @@
|
|
|
-var utils = require('../utils')
|
|
|
-
|
|
|
-function delegateCheck (el, root, identifier) {
|
|
|
- while (el && el !== root) {
|
|
|
- if (el[identifier]) return el
|
|
|
- el = el.parentNode
|
|
|
- }
|
|
|
-}
|
|
|
+var warn = require('utils').warn
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
isFn: true,
|
|
|
|
|
|
bind: function () {
|
|
|
- if (this.compiler.repeat) {
|
|
|
- // attach an identifier to the el
|
|
|
- // so it can be matched during event delegation
|
|
|
- this.el[this.expression] = true
|
|
|
- // attach the owner viewmodel of this directive
|
|
|
- this.el.vue_viewmodel = this.vm
|
|
|
+ // blur and focus events do not bubble
|
|
|
+ // so they can't be delegated
|
|
|
+ this.bubbles = this.arg !== 'blur' && this.arg !== 'focus'
|
|
|
+ if (this.bubbles) {
|
|
|
+ this.compiler.addListener(this)
|
|
|
}
|
|
|
},
|
|
|
|
|
|
update: function (handler) {
|
|
|
- this.reset()
|
|
|
if (typeof handler !== 'function') {
|
|
|
- return utils.warn('Directive "on" expects a function value.')
|
|
|
+ return warn('Directive "on" expects a function value.')
|
|
|
}
|
|
|
-
|
|
|
- var compiler = this.compiler,
|
|
|
- event = this.arg,
|
|
|
+ var targetVM = this.vm,
|
|
|
+ ownerVM = this.binding.compiler.vm,
|
|
|
isExp = this.binding.isExp,
|
|
|
- ownerVM = this.binding.compiler.vm
|
|
|
-
|
|
|
- if (compiler.repeat &&
|
|
|
- // do not delegate if the repeat is combined with an extended VM
|
|
|
- !this.vm.constructor.super &&
|
|
|
- // blur and focus events do not bubble
|
|
|
- event !== 'blur' && event !== 'focus') {
|
|
|
-
|
|
|
- // for each blocks, delegate for better performance
|
|
|
- // focus and blur events dont bubble so exclude them
|
|
|
- var delegator = compiler.delegator,
|
|
|
- identifier = this.expression,
|
|
|
- dHandler = delegator.vue_dHandlers[identifier]
|
|
|
-
|
|
|
- if (dHandler) return
|
|
|
-
|
|
|
- // the following only gets run once for the entire each block
|
|
|
- dHandler = delegator.vue_dHandlers[identifier] = function (e) {
|
|
|
- var target = delegateCheck(e.target, delegator, identifier)
|
|
|
- if (target) {
|
|
|
- e.el = target
|
|
|
- e.targetVM = target.vue_viewmodel
|
|
|
- handler.call(isExp ? e.targetVM : ownerVM, e)
|
|
|
- }
|
|
|
+ newHandler = function (e) {
|
|
|
+ e.targetVM = targetVM
|
|
|
+ handler.call(isExp ? targetVM : ownerVM, e)
|
|
|
}
|
|
|
- dHandler.event = event
|
|
|
- delegator.addEventListener(event, dHandler)
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- // a normal, single element handler
|
|
|
- var vm = this.vm
|
|
|
- this.handler = function (e) {
|
|
|
- e.el = e.currentTarget
|
|
|
- e.targetVM = vm
|
|
|
- handler.call(ownerVM, e)
|
|
|
- }
|
|
|
- this.el.addEventListener(event, this.handler)
|
|
|
-
|
|
|
+ if (!this.bubbles) {
|
|
|
+ this.reset()
|
|
|
+ this.el.addEventListener(this.arg, newHandler)
|
|
|
}
|
|
|
+ this.handler = newHandler
|
|
|
},
|
|
|
|
|
|
reset: function () {
|
|
|
this.el.removeEventListener(this.arg, this.handler)
|
|
|
- this.handler = null
|
|
|
},
|
|
|
-
|
|
|
+
|
|
|
unbind: function () {
|
|
|
- this.reset()
|
|
|
- this.el.vue_viewmodel = null
|
|
|
+ if (this.bubbles) {
|
|
|
+ this.compiler.removeListener(this)
|
|
|
+ } else {
|
|
|
+ this.reset()
|
|
|
+ }
|
|
|
}
|
|
|
}
|