github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/vue-1.0.24/src/directives/public/on.js (about)

     1  import { on, off, warn } from '../../util/index'
     2  import { ON } from '../priorities'
     3  
     4  // keyCode aliases
     5  const keyCodes = {
     6    esc: 27,
     7    tab: 9,
     8    enter: 13,
     9    space: 32,
    10    'delete': [8, 46],
    11    up: 38,
    12    left: 37,
    13    right: 39,
    14    down: 40
    15  }
    16  
    17  function keyFilter (handler, keys) {
    18    var codes = keys.map(function (key) {
    19      var charCode = key.charCodeAt(0)
    20      if (charCode > 47 && charCode < 58) {
    21        return parseInt(key, 10)
    22      }
    23      if (key.length === 1) {
    24        charCode = key.toUpperCase().charCodeAt(0)
    25        if (charCode > 64 && charCode < 91) {
    26          return charCode
    27        }
    28      }
    29      return keyCodes[key]
    30    })
    31    codes = [].concat.apply([], codes)
    32    return function keyHandler (e) {
    33      if (codes.indexOf(e.keyCode) > -1) {
    34        return handler.call(this, e)
    35      }
    36    }
    37  }
    38  
    39  function stopFilter (handler) {
    40    return function stopHandler (e) {
    41      e.stopPropagation()
    42      return handler.call(this, e)
    43    }
    44  }
    45  
    46  function preventFilter (handler) {
    47    return function preventHandler (e) {
    48      e.preventDefault()
    49      return handler.call(this, e)
    50    }
    51  }
    52  
    53  function selfFilter (handler) {
    54    return function selfHandler (e) {
    55      if (e.target === e.currentTarget) {
    56        return handler.call(this, e)
    57      }
    58    }
    59  }
    60  
    61  export default {
    62  
    63    priority: ON,
    64    acceptStatement: true,
    65    keyCodes,
    66  
    67    bind () {
    68      // deal with iframes
    69      if (
    70        this.el.tagName === 'IFRAME' &&
    71        this.arg !== 'load'
    72      ) {
    73        var self = this
    74        this.iframeBind = function () {
    75          on(
    76            self.el.contentWindow,
    77            self.arg,
    78            self.handler,
    79            self.modifiers.capture
    80          )
    81        }
    82        this.on('load', this.iframeBind)
    83      }
    84    },
    85  
    86    update (handler) {
    87      // stub a noop for v-on with no value,
    88      // e.g. @mousedown.prevent
    89      if (!this.descriptor.raw) {
    90        handler = function () {}
    91      }
    92  
    93      if (typeof handler !== 'function') {
    94        process.env.NODE_ENV !== 'production' && warn(
    95          'v-on:' + this.arg + '="' +
    96          this.expression + '" expects a function value, ' +
    97          'got ' + handler,
    98          this.vm
    99        )
   100        return
   101      }
   102  
   103      // apply modifiers
   104      if (this.modifiers.stop) {
   105        handler = stopFilter(handler)
   106      }
   107      if (this.modifiers.prevent) {
   108        handler = preventFilter(handler)
   109      }
   110      if (this.modifiers.self) {
   111        handler = selfFilter(handler)
   112      }
   113      // key filter
   114      var keys = Object.keys(this.modifiers)
   115        .filter(function (key) {
   116          return key !== 'stop' &&
   117            key !== 'prevent' &&
   118            key !== 'self' &&
   119            key !== 'capture'
   120        })
   121      if (keys.length) {
   122        handler = keyFilter(handler, keys)
   123      }
   124  
   125      this.reset()
   126      this.handler = handler
   127  
   128      if (this.iframeBind) {
   129        this.iframeBind()
   130      } else {
   131        on(
   132          this.el,
   133          this.arg,
   134          this.handler,
   135          this.modifiers.capture
   136        )
   137      }
   138    },
   139  
   140    reset () {
   141      var el = this.iframeBind
   142        ? this.el.contentWindow
   143        : this.el
   144      if (this.handler) {
   145        off(el, this.arg, this.handler)
   146      }
   147    },
   148  
   149    unbind () {
   150      this.reset()
   151    }
   152  }