github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/instance/api/dom.js (about)

     1  import {
     2    nextTick,
     3    inDoc,
     4    removeNodeRange,
     5    mapNodeRange,
     6    before,
     7    remove
     8  } from '../../util/index'
     9  
    10  import {
    11    beforeWithTransition,
    12    appendWithTransition,
    13    removeWithTransition
    14  } from '../../transition/index'
    15  
    16  export default function (Vue) {
    17    /**
    18     * Convenience on-instance nextTick. The callback is
    19     * auto-bound to the instance, and this avoids component
    20     * modules having to rely on the global Vue.
    21     *
    22     * @param {Function} fn
    23     */
    24  
    25    Vue.prototype.$nextTick = function (fn) {
    26      nextTick(fn, this)
    27    }
    28  
    29    /**
    30     * Append instance to target
    31     *
    32     * @param {Node} target
    33     * @param {Function} [cb]
    34     * @param {Boolean} [withTransition] - defaults to true
    35     */
    36  
    37    Vue.prototype.$appendTo = function (target, cb, withTransition) {
    38      return insert(
    39        this, target, cb, withTransition,
    40        append, appendWithTransition
    41      )
    42    }
    43  
    44    /**
    45     * Prepend instance to target
    46     *
    47     * @param {Node} target
    48     * @param {Function} [cb]
    49     * @param {Boolean} [withTransition] - defaults to true
    50     */
    51  
    52    Vue.prototype.$prependTo = function (target, cb, withTransition) {
    53      target = query(target)
    54      if (target.hasChildNodes()) {
    55        this.$before(target.firstChild, cb, withTransition)
    56      } else {
    57        this.$appendTo(target, cb, withTransition)
    58      }
    59      return this
    60    }
    61  
    62    /**
    63     * Insert instance before target
    64     *
    65     * @param {Node} target
    66     * @param {Function} [cb]
    67     * @param {Boolean} [withTransition] - defaults to true
    68     */
    69  
    70    Vue.prototype.$before = function (target, cb, withTransition) {
    71      return insert(
    72        this, target, cb, withTransition,
    73        beforeWithCb, beforeWithTransition
    74      )
    75    }
    76  
    77    /**
    78     * Insert instance after target
    79     *
    80     * @param {Node} target
    81     * @param {Function} [cb]
    82     * @param {Boolean} [withTransition] - defaults to true
    83     */
    84  
    85    Vue.prototype.$after = function (target, cb, withTransition) {
    86      target = query(target)
    87      if (target.nextSibling) {
    88        this.$before(target.nextSibling, cb, withTransition)
    89      } else {
    90        this.$appendTo(target.parentNode, cb, withTransition)
    91      }
    92      return this
    93    }
    94  
    95    /**
    96     * Remove instance from DOM
    97     *
    98     * @param {Function} [cb]
    99     * @param {Boolean} [withTransition] - defaults to true
   100     */
   101  
   102    Vue.prototype.$remove = function (cb, withTransition) {
   103      if (!this.$el.parentNode) {
   104        return cb && cb()
   105      }
   106      var inDocument = this._isAttached && inDoc(this.$el)
   107      // if we are not in document, no need to check
   108      // for transitions
   109      if (!inDocument) withTransition = false
   110      var self = this
   111      var realCb = function () {
   112        if (inDocument) self._callHook('detached')
   113        if (cb) cb()
   114      }
   115      if (this._isFragment) {
   116        removeNodeRange(
   117          this._fragmentStart,
   118          this._fragmentEnd,
   119          this, this._fragment, realCb
   120        )
   121      } else {
   122        var op = withTransition === false
   123          ? removeWithCb
   124          : removeWithTransition
   125        op(this.$el, this, realCb)
   126      }
   127      return this
   128    }
   129  
   130    /**
   131     * Shared DOM insertion function.
   132     *
   133     * @param {Vue} vm
   134     * @param {Element} target
   135     * @param {Function} [cb]
   136     * @param {Boolean} [withTransition]
   137     * @param {Function} op1 - op for non-transition insert
   138     * @param {Function} op2 - op for transition insert
   139     * @return vm
   140     */
   141  
   142    function insert (vm, target, cb, withTransition, op1, op2) {
   143      target = query(target)
   144      var targetIsDetached = !inDoc(target)
   145      var op = withTransition === false || targetIsDetached
   146          ? op1
   147          : op2
   148      var shouldCallHook =
   149        !targetIsDetached &&
   150        !vm._isAttached &&
   151        !inDoc(vm.$el)
   152      if (vm._isFragment) {
   153        mapNodeRange(vm._fragmentStart, vm._fragmentEnd, function (node) {
   154          op(node, target, vm)
   155        })
   156        cb && cb()
   157      } else {
   158        op(vm.$el, target, vm, cb)
   159      }
   160      if (shouldCallHook) {
   161        vm._callHook('attached')
   162      }
   163      return vm
   164    }
   165  
   166    /**
   167     * Check for selectors
   168     *
   169     * @param {String|Element} el
   170     */
   171  
   172    function query (el) {
   173      return typeof el === 'string'
   174        ? document.querySelector(el)
   175        : el
   176    }
   177  
   178    /**
   179     * Append operation that takes a callback.
   180     *
   181     * @param {Node} el
   182     * @param {Node} target
   183     * @param {Vue} vm - unused
   184     * @param {Function} [cb]
   185     */
   186  
   187    function append (el, target, vm, cb) {
   188      target.appendChild(el)
   189      if (cb) cb()
   190    }
   191  
   192    /**
   193     * InsertBefore operation that takes a callback.
   194     *
   195     * @param {Node} el
   196     * @param {Node} target
   197     * @param {Vue} vm - unused
   198     * @param {Function} [cb]
   199     */
   200  
   201    function beforeWithCb (el, target, vm, cb) {
   202      before(el, target)
   203      if (cb) cb()
   204    }
   205  
   206    /**
   207     * Remove operation that takes a callback.
   208     *
   209     * @param {Node} el
   210     * @param {Vue} vm - unused
   211     * @param {Function} [cb]
   212     */
   213  
   214    function removeWithCb (el, vm, cb) {
   215      remove(el)
   216      if (cb) cb()
   217    }
   218  }