github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/directives/internal/class.js (about)

     1  import {
     2    addClass,
     3    removeClass,
     4    isArray,
     5    isObject
     6  } from '../../util/index'
     7  
     8  export default {
     9  
    10    deep: true,
    11  
    12    update (value) {
    13      if (!value) {
    14        this.cleanup()
    15      } else if (typeof value === 'string') {
    16        this.setClass(value.trim().split(/\s+/))
    17      } else {
    18        this.setClass(normalize(value))
    19      }
    20    },
    21  
    22    setClass (value) {
    23      this.cleanup(value)
    24      for (var i = 0, l = value.length; i < l; i++) {
    25        var val = value[i]
    26        if (val) {
    27          apply(this.el, val, addClass)
    28        }
    29      }
    30      this.prevKeys = value
    31    },
    32  
    33    cleanup (value) {
    34      const prevKeys = this.prevKeys
    35      if (!prevKeys) return
    36      var i = prevKeys.length
    37      while (i--) {
    38        var key = prevKeys[i]
    39        if (!value || value.indexOf(key) < 0) {
    40          apply(this.el, key, removeClass)
    41        }
    42      }
    43    }
    44  }
    45  
    46  /**
    47   * Normalize objects and arrays (potentially containing objects)
    48   * into array of strings.
    49   *
    50   * @param {Object|Array<String|Object>} value
    51   * @return {Array<String>}
    52   */
    53  
    54  function normalize (value) {
    55    const res = []
    56    if (isArray(value)) {
    57      for (var i = 0, l = value.length; i < l; i++) {
    58        const key = value[i]
    59        if (key) {
    60          if (typeof key === 'string') {
    61            res.push(key)
    62          } else {
    63            for (var k in key) {
    64              if (key[k]) res.push(k)
    65            }
    66          }
    67        }
    68      }
    69    } else if (isObject(value)) {
    70      for (var key in value) {
    71        if (value[key]) res.push(key)
    72      }
    73    }
    74    return res
    75  }
    76  
    77  /**
    78   * Add or remove a class/classes on an element
    79   *
    80   * @param {Element} el
    81   * @param {String} key The class name. This may or may not
    82   *                     contain a space character, in such a
    83   *                     case we'll deal with multiple class
    84   *                     names at once.
    85   * @param {Function} fn
    86   */
    87  
    88  function apply (el, key, fn) {
    89    key = key.trim()
    90    if (key.indexOf(' ') === -1) {
    91      fn(el, key)
    92      return
    93    }
    94    // The key contains one or more space characters.
    95    // Since a class name doesn't accept such characters, we
    96    // treat it as multiple classes.
    97    var keys = key.split(/\s+/)
    98    for (var i = 0, l = keys.length; i < l; i++) {
    99      fn(el, keys[i])
   100    }
   101  }