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

     1  import { SLOT } from '../priorities'
     2  import {
     3    extractContent,
     4    replace,
     5    remove
     6  } from '../../util/index'
     7  
     8  export default {
     9  
    10    priority: SLOT,
    11    params: ['name'],
    12  
    13    bind () {
    14      // this was resolved during component transclusion
    15      var name = this.params.name || 'default'
    16      var content = this.vm._slotContents && this.vm._slotContents[name]
    17      if (!content || !content.hasChildNodes()) {
    18        this.fallback()
    19      } else {
    20        this.compile(content.cloneNode(true), this.vm._context, this.vm)
    21      }
    22    },
    23  
    24    compile (content, context, host) {
    25      if (content && context) {
    26        if (
    27          this.el.hasChildNodes() &&
    28          content.childNodes.length === 1 &&
    29          content.childNodes[0].nodeType === 1 &&
    30          content.childNodes[0].hasAttribute('v-if')
    31        ) {
    32          // if the inserted slot has v-if
    33          // inject fallback content as the v-else
    34          const elseBlock = document.createElement('template')
    35          elseBlock.setAttribute('v-else', '')
    36          elseBlock.innerHTML = this.el.innerHTML
    37          // the else block should be compiled in child scope
    38          elseBlock._context = this.vm
    39          content.appendChild(elseBlock)
    40        }
    41        const scope = host
    42          ? host._scope
    43          : this._scope
    44        this.unlink = context.$compile(
    45          content, host, scope, this._frag
    46        )
    47      }
    48      if (content) {
    49        replace(this.el, content)
    50      } else {
    51        remove(this.el)
    52      }
    53    },
    54  
    55    fallback () {
    56      this.compile(extractContent(this.el, true), this.vm)
    57    },
    58  
    59    unbind () {
    60      if (this.unlink) {
    61        this.unlink()
    62      }
    63    }
    64  }