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.
ant-design-vue/components/trigger/index.vue

535 lines
16 KiB

7 years ago
<script>
7 years ago
import PropTypes from '../_util/vue-types'
7 years ago
import contains from '../_util/Dom/contains'
7 years ago
import hasProp from '../_util/props-util'
7 years ago
import addEventListener from '../_util/Dom/addEventListener'
7 years ago
import warning from '../_util/warning'
7 years ago
import Popup from './Popup'
7 years ago
import { getAlignFromPlacement, getPopupClassNameFromAlign, noop } from './utils'
7 years ago
import BaseMixin from '../_util/BaseMixin'
7 years ago
import { cloneElement, cloneVNode } from '../_util/vnode'
7 years ago
function returnEmptyString () {
return ''
}
function returnDocument () {
return window.document
}
7 years ago
const ALL_HANDLERS = ['click', 'mousedown', 'touchStart', 'mouseenter',
'mouseleave', 'focus', 'blur', 'contextMenu']
7 years ago
export default {
name: 'Trigger',
props: {
action: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]).def([]),
showAction: PropTypes.any.def([]),
hideAction: PropTypes.any.def([]),
getPopupClassNameFromAlign: PropTypes.any.def(returnEmptyString),
7 years ago
// onPopupVisibleChange: PropTypes.func.def(noop),
afterPopupVisibleChange: PropTypes.func.def(noop),
7 years ago
popup: PropTypes.any,
popupStyle: PropTypes.object.def({}),
prefixCls: PropTypes.string.def('rc-trigger-popup'),
popupClassName: PropTypes.string.def(''),
popupPlacement: PropTypes.string,
builtinPlacements: PropTypes.object,
popupTransitionName: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
]),
popupAnimation: PropTypes.any,
mouseEnterDelay: PropTypes.number.def(0),
mouseLeaveDelay: PropTypes.number.def(0.1),
zIndex: PropTypes.number,
focusDelay: PropTypes.number.def(0),
blurDelay: PropTypes.number.def(0.15),
7 years ago
getPopupContainer: PropTypes.func,
7 years ago
getDocument: PropTypes.func.def(returnDocument),
forceRender: PropTypes.bool,
destroyPopupOnHide: PropTypes.bool.def(false),
mask: PropTypes.bool.def(false),
maskClosable: PropTypes.bool.def(true),
7 years ago
// onPopupAlign: PropTypes.func.def(noop),
7 years ago
popupAlign: PropTypes.object.def({}),
popupVisible: PropTypes.bool,
7 years ago
defaultPopupVisible: PropTypes.bool.def(false),
maskTransitionName: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
]),
maskAnimation: PropTypes.string,
7 years ago
},
7 years ago
mixins: [BaseMixin],
7 years ago
data () {
const props = this.$props
let popupVisible
7 years ago
if (hasProp(this, 'popupVisible')) {
7 years ago
popupVisible = !!props.popupVisible
} else {
popupVisible = !!props.defaultPopupVisible
}
return {
sPopupVisible: popupVisible,
}
},
beforeCreate () {
ALL_HANDLERS.forEach((h) => {
this[`fire${h}`] = (e) => {
7 years ago
const ev = `on${h[0].toUpperCase() + h.slice(1)}`
this.fireEvents(ev, e)
7 years ago
}
})
},
mounted () {
7 years ago
this.updatedCal()
7 years ago
},
watch: {
popupVisible (val) {
if (val !== undefined) {
this.sPopupVisible = val
}
},
sPopupVisible (val) {
this.$nextTick(() => {
7 years ago
this.afterPopupVisibleChange(val)
7 years ago
})
},
},
7 years ago
updated () {
this.updatedCal()
7 years ago
},
beforeDestroy () {
this.clearDelayTimer()
this.clearOutsideHandler()
},
methods: {
7 years ago
updatedCal () {
const props = this.$props
const state = this.$data
// this.renderComponent()
// We must listen to `mousedown` or `touchstart`, edge case:
// https://github.com/ant-design/ant-design/issues/5804
// https://github.com/react-component/calendar/issues/250
// https://github.com/react-component/trigger/issues/50
if (state.sPopupVisible) {
let currentDocument
if (!this.clickOutsideHandler && (this.isClickToHide() || this.isContextMenuToShow())) {
currentDocument = props.getDocument()
this.clickOutsideHandler = addEventListener(currentDocument,
'mousedown', this.onDocumentClick)
}
// always hide on mobile
if (!this.touchOutsideHandler) {
currentDocument = currentDocument || props.getDocument()
this.touchOutsideHandler = addEventListener(currentDocument,
'touchstart', this.onDocumentClick)
}
// close popup when trigger type contains 'onContextMenu' and document is scrolling.
if (!this.contextMenuOutsideHandler1 && this.isContextMenuToShow()) {
currentDocument = currentDocument || props.getDocument()
this.contextMenuOutsideHandler1 = addEventListener(currentDocument,
'scroll', this.onContextMenuClose)
}
// close popup when trigger type contains 'onContextMenu' and window is blur.
if (!this.contextMenuOutsideHandler2 && this.isContextMenuToShow()) {
this.contextMenuOutsideHandler2 = addEventListener(window,
'blur', this.onContextMenuClose)
}
return
}
this.clearOutsideHandler()
},
onMouseenter (e) {
this.fireEvents('mouseenter', e)
7 years ago
this.delaySetPopupVisible(true, this.$props.mouseEnterDelay)
},
7 years ago
onMouseleave (e) {
this.fireEvents('mouseleave', e)
7 years ago
this.delaySetPopupVisible(false, this.$props.mouseLeaveDelay)
},
7 years ago
onPopupMouseenter () {
7 years ago
this.clearDelayTimer()
},
7 years ago
onPopupMouseleave (e) {
7 years ago
if (e.relatedTarget && !e.relatedTarget.setTimeout &&
7 years ago
this.$refs.popup &&
this.$refs.popup.getPopupDomNode &&
contains(this.$refs.popup.getPopupDomNode(), e.relatedTarget)) {
7 years ago
return
}
this.delaySetPopupVisible(false, this.$props.mouseLeaveDelay)
},
onFocus (e) {
7 years ago
this.fireEvents('focus', e)
7 years ago
// incase focusin and focusout
this.clearDelayTimer()
if (this.isFocusToShow()) {
this.focusTime = Date.now()
this.delaySetPopupVisible(true, this.$props.focusDelay)
}
},
7 years ago
onMousedown (e) {
this.fireEvents('mousedown', e)
7 years ago
this.preClickTime = Date.now()
},
onTouchStart (e) {
7 years ago
this.fireEvents('touchStart', e)
7 years ago
this.preTouchTime = Date.now()
},
onBlur (e) {
7 years ago
this.fireEvents('blur', e)
7 years ago
this.clearDelayTimer()
if (this.isBlurToHide()) {
this.delaySetPopupVisible(false, this.$props.blurDelay)
}
},
onContextMenu (e) {
e.preventDefault()
7 years ago
this.fireEvents('contextMenu', e)
7 years ago
this.setPopupVisible(true)
},
onContextMenuClose () {
if (this.isContextMenuToShow()) {
this.close()
}
},
onClick (event) {
7 years ago
this.fireEvents('click', event)
7 years ago
// focus will trigger click
if (this.focusTime) {
let preTime
if (this.preClickTime && this.preTouchTime) {
preTime = Math.min(this.preClickTime, this.preTouchTime)
} else if (this.preClickTime) {
preTime = this.preClickTime
} else if (this.preTouchTime) {
preTime = this.preTouchTime
}
if (Math.abs(preTime - this.focusTime) < 20) {
return
}
this.focusTime = 0
}
this.preClickTime = 0
this.preTouchTime = 0
event.preventDefault()
const nextVisible = !this.$data.sPopupVisible
if (this.isClickToHide() && !nextVisible || nextVisible && this.isClickToShow()) {
this.setPopupVisible(!this.$data.sPopupVisible)
}
},
onDocumentClick (event) {
if (this.$props.mask && !this.$props.maskClosable) {
return
}
const target = event.target
const root = this.$el
const popupNode = this.getPopupDomNode()
if (!contains(root, target) && !contains(popupNode, target)) {
this.close()
}
},
getPopupDomNode () {
// for test
7 years ago
if (this.$refs.popup && this.$refs.popup.getPopupDomNode) {
return this.$refs.popup.getPopupDomNode()
7 years ago
}
return null
},
getRootDomNode () {
7 years ago
return this.$el.children ? this.$el.children[0] : this.$el
7 years ago
},
7 years ago
handleGetPopupClassFromAlign (align) {
7 years ago
const className = []
const props = this.$props
const { popupPlacement, builtinPlacements, prefixCls } = props
if (popupPlacement && builtinPlacements) {
className.push(getPopupClassNameFromAlign(builtinPlacements, prefixCls, align))
}
if (props.getPopupClassNameFromAlign) {
className.push(props.getPopupClassNameFromAlign(align))
}
return className.join(' ')
},
getPopupAlign () {
const props = this.$props
const { popupPlacement, popupAlign, builtinPlacements } = props
if (popupPlacement && builtinPlacements) {
return getAlignFromPlacement(builtinPlacements, popupPlacement, popupAlign)
}
return popupAlign
},
7 years ago
getComponent (h) {
7 years ago
const mouseProps = {}
if (this.isMouseEnterToShow()) {
7 years ago
mouseProps.mouseenter = this.onPopupMouseenter
7 years ago
}
if (this.isMouseLeaveToHide()) {
7 years ago
mouseProps.mouseleave = this.onPopupMouseleave
7 years ago
}
const { prefixCls, destroyPopupOnHide, sPopupVisible,
7 years ago
popupStyle, popupClassName, action,
7 years ago
popupAnimation, handleGetPopupClassFromAlign, getRootDomNode,
7 years ago
mask, zIndex, popupTransitionName, getPopupAlign,
7 years ago
maskAnimation, maskTransitionName, popup, $slots, getContainer } = this
7 years ago
const popupProps = {
props: {
prefixCls,
destroyPopupOnHide,
visible: sPopupVisible,
action,
align: getPopupAlign(),
animation: popupAnimation,
7 years ago
getClassNameFromAlign: handleGetPopupClassFromAlign,
7 years ago
getRootDomNode,
mask,
zIndex,
transitionName: popupTransitionName,
maskAnimation,
maskTransitionName,
7 years ago
getContainer,
7 years ago
popupClassName,
7 years ago
},
on: {
7 years ago
align: this.$listeners.popupAlign || noop,
7 years ago
...mouseProps,
},
ref: 'popup',
style: popupStyle,
}
return (
<Popup
{...popupProps}
7 years ago
ref='popup'
7 years ago
>
7 years ago
{typeof popup === 'function' ? popup(h) : popup}
7 years ago
{popup === undefined ? $slots.popup : null}
</Popup>
)
},
getContainer () {
const { $props: props } = this
7 years ago
// const popupContainer = document.createElement('div')
// // Make sure default popup container will never cause scrollbar appearing
// // https://github.com/react-component/trigger/issues/41
// popupContainer.style.position = 'absolute'
// popupContainer.style.top = '0'
// popupContainer.style.left = '0'
// popupContainer.style.width = '100%'
7 years ago
const mountNode = props.getPopupContainer
? props.getPopupContainer(this.$el) : props.getDocument().body
7 years ago
// mountNode.appendChild(popupContainer)
return mountNode
7 years ago
},
setPopupVisible (sPopupVisible) {
this.clearDelayTimer()
if (this.$data.sPopupVisible !== sPopupVisible) {
7 years ago
if (!hasProp(this, 'popupVisible')) {
7 years ago
this.setState({
sPopupVisible,
})
7 years ago
this.$forceUpdate()
7 years ago
}
7 years ago
this.$listeners.popupVisibleChange && this.$listeners.popupVisibleChange(sPopupVisible)
7 years ago
}
},
delaySetPopupVisible (visible, delayS) {
const delay = delayS * 1000
this.clearDelayTimer()
if (delay) {
this.delayTimer = setTimeout(() => {
this.setPopupVisible(visible)
this.clearDelayTimer()
}, delay)
} else {
this.setPopupVisible(visible)
}
},
clearDelayTimer () {
if (this.delayTimer) {
clearTimeout(this.delayTimer)
this.delayTimer = null
}
},
clearOutsideHandler () {
if (this.clickOutsideHandler) {
this.clickOutsideHandler.remove()
this.clickOutsideHandler = null
}
if (this.contextMenuOutsideHandler1) {
this.contextMenuOutsideHandler1.remove()
this.contextMenuOutsideHandler1 = null
}
if (this.contextMenuOutsideHandler2) {
this.contextMenuOutsideHandler2.remove()
this.contextMenuOutsideHandler2 = null
}
if (this.touchOutsideHandler) {
this.touchOutsideHandler.remove()
this.touchOutsideHandler = null
}
},
createTwoChains (event) {
7 years ago
const child = this.$slots.default[0]
7 years ago
let fn = () => {
}
child.data = child.data || {}
child.data.on = child.data.on || {}
const childEvents = child.data.on
const events = (this.data ? this.data.on : {}) || {}
if (childEvents[event] && events[event]) {
return this[`fire${event}`]
7 years ago
}
7 years ago
fn = childEvents[event] || events[event] || fn
7 years ago
return fn
7 years ago
},
isClickToShow () {
const { action, showAction } = this.$props
return action.indexOf('click') !== -1 || showAction.indexOf('click') !== -1
},
isContextMenuToShow () {
const { action, showAction } = this.$props
return action.indexOf('contextMenu') !== -1 || showAction.indexOf('contextMenu') !== -1
},
isClickToHide () {
const { action, hideAction } = this.$props
return action.indexOf('click') !== -1 || hideAction.indexOf('click') !== -1
},
isMouseEnterToShow () {
const { action, showAction } = this.$props
7 years ago
return action.indexOf('hover') !== -1 || showAction.indexOf('mouseenter') !== -1
7 years ago
},
isMouseLeaveToHide () {
const { action, hideAction } = this.$props
7 years ago
return action.indexOf('hover') !== -1 || hideAction.indexOf('mouseleave') !== -1
7 years ago
},
isFocusToShow () {
const { action, showAction } = this.$props
return action.indexOf('focus') !== -1 || showAction.indexOf('focus') !== -1
},
isBlurToHide () {
const { action, hideAction } = this.$props
return action.indexOf('focus') !== -1 || hideAction.indexOf('blur') !== -1
},
forcePopupAlign () {
7 years ago
if (this.$data.sPopupVisible && this.$refs.popup && this.$refs.popup.$refs.alignInstance) {
this.$refs.popup.$refs.alignInstance.forceAlign()
7 years ago
}
},
fireEvents (type, e) {
7 years ago
const child = this.$slots.default[0]
7 years ago
if (child && child.data && child.data.on && child.data.on[type]) {
7 years ago
child.data.on[type](e)
7 years ago
}
7 years ago
if (this.data && this.data.on && this.data.on[type]) {
this.data.on[type](e)
7 years ago
}
},
close () {
this.setPopupVisible(false)
},
},
7 years ago
render (h) {
7 years ago
const children = this.$slots.default
7 years ago
if (children.length > 1) {
warning(false, 'Trigger $slots.default.length > 1, just support only one default', true)
}
7 years ago
const child = children[0]
const newChildProps = {
props: {},
on: {},
key: 'trigger',
}
if (this.isContextMenuToShow()) {
newChildProps.on.contextMenu = this.onContextMenu
} else {
7 years ago
newChildProps.on.contextMenu = this.createTwoChains('contextMenu')
7 years ago
}
if (this.isClickToHide() || this.isClickToShow()) {
newChildProps.on.click = this.onClick
7 years ago
newChildProps.on.mousedown = this.onMousedown
7 years ago
// newChildProps.on.touchStart = this.onTouchStart
} else {
7 years ago
newChildProps.on.click = this.createTwoChains('click')
newChildProps.on.mousedown = this.createTwoChains('mousedown')
7 years ago
// newChildProps.on.TouchStart = this.createTwoChains('onTouchStart')
}
if (this.isMouseEnterToShow()) {
7 years ago
newChildProps.on.mouseenter = this.onMouseenter
7 years ago
} else {
7 years ago
newChildProps.on.mouseenter = this.createTwoChains('mouseenter')
7 years ago
}
if (this.isMouseLeaveToHide()) {
7 years ago
newChildProps.on.mouseleave = this.onMouseleave
7 years ago
} else {
7 years ago
newChildProps.on.mouseleave = this.createTwoChains('mouseleave')
7 years ago
}
7 years ago
7 years ago
if (this.isFocusToShow() || this.isBlurToHide()) {
newChildProps.on.focus = this.onFocus
newChildProps.on.blur = this.onBlur
} else {
7 years ago
newChildProps.on.focus = this.createTwoChains('focus')
newChildProps.on.blur = this.createTwoChains('blur')
7 years ago
}
7 years ago
const trigger = cloneElement(cloneVNode(child), newChildProps)
const { sPopupVisible, forceRender } = this
if (sPopupVisible || forceRender || this._component) {
7 years ago
this._component = this.getComponent(h)
7 years ago
} else {
this._component = null
}
7 years ago
return (
<span>
{trigger}
7 years ago
{this._component}
7 years ago
</span>
)
7 years ago
},
}
</script>