You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.0 KiB
88 lines
2.0 KiB
7 years ago
|
|
||
7 years ago
|
import Vue from 'vue'
|
||
|
import PropTypes from './vue-types'
|
||
|
|
||
|
export default {
|
||
|
props: {
|
||
|
autoMount: PropTypes.bool.def(true),
|
||
|
autoDestroy: PropTypes.bool.def(true),
|
||
|
visible: PropTypes.bool,
|
||
|
forceRender: PropTypes.bool.def(false),
|
||
|
parent: PropTypes.any,
|
||
|
getComponent: PropTypes.func.isRequired,
|
||
|
getContainer: PropTypes.func.isRequired,
|
||
|
children: PropTypes.func.isRequired,
|
||
|
},
|
||
|
|
||
|
mounted () {
|
||
|
if (this.autoMount) {
|
||
|
this.renderComponent()
|
||
|
}
|
||
|
},
|
||
|
|
||
|
updated () {
|
||
|
if (this.autoMount) {
|
||
|
this.renderComponent()
|
||
|
}
|
||
|
},
|
||
|
|
||
|
beforeDestroy () {
|
||
|
if (this.autoDestroy) {
|
||
|
this.removeContainer()
|
||
|
}
|
||
|
},
|
||
|
methods: {
|
||
|
removeContainer () {
|
||
|
if (this.container) {
|
||
|
this._component && this._component.$destroy()
|
||
|
this.container.parentNode.removeChild(this.container)
|
||
|
this.container = null
|
||
|
}
|
||
|
},
|
||
|
|
||
7 years ago
|
renderComponent (props = {}, ready) {
|
||
7 years ago
|
const { visible, getComponent, forceRender, getContainer, parent } = this
|
||
|
const self = this
|
||
|
if (visible || parent.$refs._component || forceRender) {
|
||
7 years ago
|
let el = this.componentEl
|
||
7 years ago
|
if (!this.container) {
|
||
|
this.container = getContainer()
|
||
7 years ago
|
el = document.createElement('div')
|
||
|
this.componentEl = el
|
||
|
this.container.appendChild(el)
|
||
7 years ago
|
}
|
||
|
|
||
|
if (!this._component) {
|
||
|
this._component = new Vue({
|
||
7 years ago
|
data: {
|
||
|
comProps: props,
|
||
|
},
|
||
7 years ago
|
parent: self.parent,
|
||
7 years ago
|
el: el,
|
||
7 years ago
|
mounted () {
|
||
7 years ago
|
this.$nextTick(() => {
|
||
7 years ago
|
if (ready) {
|
||
|
ready.call(self)
|
||
|
}
|
||
|
})
|
||
|
},
|
||
|
render () {
|
||
7 years ago
|
return getComponent(this.comProps)
|
||
7 years ago
|
},
|
||
|
})
|
||
7 years ago
|
} else {
|
||
|
this._component.comProps = props
|
||
7 years ago
|
}
|
||
|
}
|
||
|
},
|
||
|
},
|
||
|
|
||
|
render () {
|
||
|
return this.children({
|
||
|
renderComponent: this.renderComponent,
|
||
|
removeContainer: this.removeContainer,
|
||
|
})
|
||
|
},
|
||
|
}
|
||
|
|