2017-12-25 18:08:36 +08:00
|
|
|
import Vue from 'vue'
|
2017-12-22 18:43:28 +08:00
|
|
|
function defaultGetContainer () {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
document.body.appendChild(container)
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function getContainerRenderMixin (config) {
|
|
|
|
const {
|
|
|
|
autoMount = true,
|
|
|
|
autoDestroy = true,
|
|
|
|
isVisible,
|
|
|
|
isForceRender,
|
|
|
|
getComponent,
|
|
|
|
getContainer = defaultGetContainer,
|
|
|
|
} = config
|
|
|
|
|
|
|
|
let mixin
|
|
|
|
if (autoMount) {
|
|
|
|
mixin = {
|
|
|
|
...mixin,
|
|
|
|
mounted () {
|
2017-12-25 18:08:36 +08:00
|
|
|
// this.renderComponent()
|
2017-12-22 18:43:28 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoDestroy) {
|
|
|
|
mixin = {
|
|
|
|
...mixin,
|
|
|
|
beforeDestroy () {
|
2017-12-25 18:08:36 +08:00
|
|
|
// this.removeContainer()
|
2017-12-22 18:43:28 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mixin = {
|
|
|
|
...mixin,
|
|
|
|
methods: {
|
|
|
|
removeContainer () {
|
|
|
|
if (this._container) {
|
|
|
|
const container = this._container
|
|
|
|
container.parentNode.removeChild(container)
|
|
|
|
this._container = null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
renderComponent (componentArg) {
|
|
|
|
if (
|
2017-12-25 18:08:36 +08:00
|
|
|
!isVisible || this._component || isVisible(this) ||
|
2017-12-22 18:43:28 +08:00
|
|
|
(isForceRender && isForceRender(this))
|
|
|
|
) {
|
|
|
|
if (!this._container) {
|
|
|
|
this._container = getContainer(this)
|
|
|
|
}
|
2017-12-25 18:08:36 +08:00
|
|
|
let component
|
|
|
|
if (this.getComponent) {
|
|
|
|
component = this.getComponent(componentArg)
|
|
|
|
} else {
|
|
|
|
component = getComponent(this, componentArg)
|
|
|
|
}
|
|
|
|
this._component = component
|
|
|
|
const vmC = document.createElement('div')
|
|
|
|
this._container.appendChild(vmC)
|
|
|
|
|
|
|
|
new Vue({
|
|
|
|
el: vmC,
|
|
|
|
render () {
|
|
|
|
return component
|
|
|
|
},
|
|
|
|
})
|
2017-12-22 18:43:28 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return mixin
|
|
|
|
}
|