ant-design-vue/components/trigger/Popup.vue

193 lines
5.0 KiB
Vue
Raw Normal View History

2017-12-22 10:43:28 +00:00
<script>
2017-12-25 10:08:36 +00:00
import PropTypes from '../_util/vue-types'
2017-12-22 10:43:28 +00:00
import Align from '../align'
import PopupInner from './PopupInner'
import LazyRenderBox from './LazyRenderBox'
export default {
props: {
visible: PropTypes.bool,
getClassNameFromAlign: PropTypes.func,
getRootDomNode: PropTypes.func,
align: PropTypes.any,
destroyPopupOnHide: PropTypes.bool,
prefixCls: PropTypes.string,
2017-12-25 10:08:36 +00:00
getContainer: PropTypes.func,
2017-12-26 11:04:28 +00:00
transitionName: PropTypes.string,
animation: PropTypes.any,
maskAnimation: PropTypes.string,
maskTransitionName: PropTypes.string,
mask: PropTypes.bool,
zIndex: PropTypes.number,
2017-12-22 10:43:28 +00:00
},
2017-12-27 08:13:26 +00:00
data () {
return {
destroyPopup: false,
}
},
2017-12-22 10:43:28 +00:00
mounted () {
this.rootNode = this.getPopupDomNode()
2017-12-25 10:08:36 +00:00
this._container = this.getContainer()
2017-12-27 08:13:26 +00:00
this._container.appendChild(this.$el)
this.$nextTick(() => {
this.$refs.alignInstance.forceAlign()
})
2017-12-22 10:43:28 +00:00
},
methods: {
onAlign (popupDomNode, align) {
const props = this.$props
const currentAlignClassName = props.getClassNameFromAlign(align)
// FIX: https://github.com/react-component/trigger/issues/56
// FIX: https://github.com/react-component/tooltip/issues/79
if (this.currentAlignClassName !== currentAlignClassName) {
this.currentAlignClassName = currentAlignClassName
popupDomNode.className = this.getClassName(currentAlignClassName)
}
this.$emit('align', popupDomNode, align)
},
getPopupDomNode () {
2017-12-26 11:04:28 +00:00
return this.$refs.popupInstance ? this.$refs.popupInstance.$el : null
2017-12-22 10:43:28 +00:00
},
getTarget () {
return this.$props.getRootDomNode()
},
getMaskTransitionName () {
const props = this.$props
let transitionName = props.maskTransitionName
const animation = props.maskAnimation
if (!transitionName && animation) {
transitionName = `${props.prefixCls}-${animation}`
}
return transitionName
},
getTransitionName () {
const props = this.$props
let transitionName = props.transitionName
if (!transitionName && props.animation) {
transitionName = `${props.prefixCls}-${props.animation}`
}
2017-12-27 08:45:32 +00:00
return transitionName
2017-12-22 10:43:28 +00:00
},
getClassName (currentAlignClassName) {
return `${this.$props.prefixCls} ${currentAlignClassName}`
},
onMouseEnter (e) {
this.$emit('mouseenter', e)
},
onMouseLeave (e) {
this.$emit('mouseleave', e)
},
2017-12-27 08:13:26 +00:00
beforeEnter (el) {
this.$refs.alignInstance && this.$refs.alignInstance.forceAlign()
},
afterLeave (el) {
if (this.destroyPopupOnHide) {
this.destroyPopup = true
}
},
2017-12-22 10:43:28 +00:00
getPopupElement () {
const { $props: props, onMouseEnter, onMouseLeave, $slots } = this
2017-12-27 08:13:26 +00:00
const { align, visible, prefixCls } = props
2017-12-22 10:43:28 +00:00
const className = this.getClassName(this.currentAlignClassName ||
props.getClassNameFromAlign(align))
2017-12-27 08:13:26 +00:00
// const hiddenClassName = `${prefixCls}-hidden`
2017-12-22 10:43:28 +00:00
if (!visible) {
this.currentAlignClassName = null
}
2017-12-25 10:08:36 +00:00
// visible = true
2017-12-22 10:43:28 +00:00
const popupInnerProps = {
props: {
prefixCls,
visible,
},
class: className,
on: {
mouseenter: onMouseEnter,
mouseleave: onMouseLeave,
},
ref: 'popupInstance',
style: { ...this.getZIndexStyle() },
}
2017-12-25 10:08:36 +00:00
return (<transition
2017-12-27 08:13:26 +00:00
appear
2017-12-25 10:08:36 +00:00
name={this.getTransitionName()}
2017-12-27 08:13:26 +00:00
onBeforeEnter={this.beforeEnter}
onAfterLeave={this.afterLeave}
2017-12-22 10:43:28 +00:00
>
<Align
2017-12-26 11:04:28 +00:00
v-show={visible}
2017-12-22 10:43:28 +00:00
target={this.getTarget}
key='popup'
ref='alignInstance'
monitorWindowResize
disabled={!visible}
align={align}
onAlign={this.onAlign}
>
<PopupInner
{...popupInnerProps}
>
{$slots.default}
</PopupInner>
</Align>
2017-12-27 08:13:26 +00:00
2017-12-25 10:08:36 +00:00
</transition>)
2017-12-22 10:43:28 +00:00
},
getZIndexStyle () {
const style = {}
const props = this.$props
if (props.zIndex !== undefined) {
style.zIndex = props.zIndex
}
return style
},
getMaskElement () {
const props = this.$props
let maskElement
if (props.mask) {
2017-12-27 08:45:32 +00:00
const maskTransition = this.getMaskTransitionName()
2017-12-22 10:43:28 +00:00
maskElement = (
<LazyRenderBox
2017-12-27 08:13:26 +00:00
v-show={props.visible}
2017-12-22 10:43:28 +00:00
style={this.getZIndexStyle()}
key='mask'
class={`${props.prefixCls}-mask`}
visible={props.visible}
/>
)
if (maskTransition) {
maskElement = (
2017-12-25 10:08:36 +00:00
<transition
2017-12-27 08:13:26 +00:00
appear
2017-12-25 10:08:36 +00:00
name={maskTransition}
2017-12-22 10:43:28 +00:00
>
{maskElement}
2017-12-25 10:08:36 +00:00
</transition>
2017-12-22 10:43:28 +00:00
)
}
}
return maskElement
},
},
render () {
2017-12-27 08:13:26 +00:00
const { destroyPopup, getMaskElement, getPopupElement } = this
2017-12-22 10:43:28 +00:00
return (
2017-12-27 08:13:26 +00:00
<div style='position: absolute; top: 0px; left: 0px; width: 100%;'>
{getMaskElement()}
{destroyPopup ? null : getPopupElement()}
2017-12-22 10:43:28 +00:00
</div>
)
},
}
</script>
2017-12-26 11:04:28 +00:00