on.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var _ = require('../../util')
  2. // modifiers
  3. var stopRE = /\.stop\b/
  4. var preventRE = /\.prevent\b/
  5. // keyCode aliases
  6. var keyCodes = {
  7. esc: 27,
  8. tab: 9,
  9. enter: 13,
  10. space: 32,
  11. 'delete': 46,
  12. up: 38,
  13. left: 37,
  14. right: 39,
  15. down: 40
  16. }
  17. function keyFilter (handler, key) {
  18. var code = keyCodes[key]
  19. if (!code) {
  20. code = parseInt(key, 10)
  21. }
  22. return function (e) {
  23. if (e.keyCode === code) {
  24. return handler.call(this, e)
  25. }
  26. }
  27. }
  28. function stopFilter (handler) {
  29. return function (e) {
  30. e.stopPropagation()
  31. return handler.call(this, e)
  32. }
  33. }
  34. function preventFilter (handler) {
  35. return function (e) {
  36. e.preventDefault()
  37. return handler.call(this, e)
  38. }
  39. }
  40. module.exports = {
  41. acceptStatement: true,
  42. priority: 700,
  43. bind: function () {
  44. // 1.0.0 key filter
  45. var event = this.arg
  46. // stop modifier
  47. if (stopRE.test(event)) {
  48. this.stop = true
  49. event = event.replace(stopRE, '')
  50. }
  51. // prevent modifier
  52. if (preventRE.test(event)) {
  53. this.prevent = true
  54. event = event.replace(preventRE, '')
  55. }
  56. // key modifier
  57. var keyIndex = event.indexOf('.')
  58. if (keyIndex > -1) {
  59. this.event = event.slice(0, keyIndex)
  60. this.key = event.slice(keyIndex + 1)
  61. } else {
  62. this.event = event
  63. }
  64. // deal with iframes
  65. if (
  66. this.el.tagName === 'IFRAME' &&
  67. this.event !== 'load'
  68. ) {
  69. var self = this
  70. this.iframeBind = function () {
  71. _.on(self.el.contentWindow, self.event, self.handler)
  72. }
  73. this.on('load', this.iframeBind)
  74. }
  75. },
  76. update: function (handler) {
  77. if (typeof handler !== 'function') {
  78. process.env.NODE_ENV !== 'production' && _.warn(
  79. 'v-on:' + this.event + '="' +
  80. this.expression + '" expects a function value, ' +
  81. 'got ' + handler
  82. )
  83. return
  84. }
  85. // apply modifiers
  86. if (this.stop) {
  87. handler = stopFilter(handler)
  88. }
  89. if (this.prevent) {
  90. handler = preventFilter(handler)
  91. }
  92. if (this.key) {
  93. handler = keyFilter(handler, this.key)
  94. }
  95. this.reset()
  96. var scope = this._scope || this.vm
  97. this.handler = function (e) {
  98. scope.$event = e
  99. var res = handler(e)
  100. scope.$event = null
  101. return res
  102. }
  103. if (this.iframeBind) {
  104. this.iframeBind()
  105. } else {
  106. _.on(this.el, this.event, this.handler)
  107. }
  108. },
  109. reset: function () {
  110. var el = this.iframeBind
  111. ? this.el.contentWindow
  112. : this.el
  113. if (this.handler) {
  114. _.off(el, this.event, this.handler)
  115. }
  116. },
  117. unbind: function () {
  118. this.reset()
  119. }
  120. }