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

523 lines
16 KiB
Vue
Raw Normal View History

2017-12-22 18:43:28 +08:00
<script>
2017-12-25 18:08:36 +08:00
import PropTypes from '../_util/vue-types'
2017-12-22 18:43:28 +08:00
import contains from '../_util/Dom/contains'
2018-01-12 16:10:41 +08:00
import hasProp from '../_util/props-util'
2017-12-22 18:43:28 +08:00
import addEventListener from '../_util/Dom/addEventListener'
2017-12-26 19:04:28 +08:00
import warning from '../_util/warning'
2017-12-22 18:43:28 +08:00
import Popup from './Popup'
2018-01-11 18:53:51 +08:00
import { getAlignFromPlacement, getPopupClassNameFromAlign, noop } from './utils'
2018-01-12 16:10:41 +08:00
import BaseMixin from '../_util/BaseMixin'
2018-01-17 16:12:53 +08:00
import { cloneElement, filterEmpty, getEvents } from '../_util/vnode'
2017-12-22 18:43:28 +08:00
function returnEmptyString () {
return ''
}
function returnDocument () {
return window.document
}
2017-12-25 18:08:36 +08:00
const ALL_HANDLERS = ['click', 'mousedown', 'touchStart', 'mouseenter',
'mouseleave', 'focus', 'blur', 'contextMenu']
2017-12-22 18:43:28 +08:00
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),
2018-01-11 18:53:51 +08:00
// onPopupVisibleChange: PropTypes.func.def(noop),
afterPopupVisibleChange: PropTypes.func.def(noop),
2017-12-22 18:43:28 +08:00
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),
2017-12-26 19:04:28 +08:00
getPopupContainer: PropTypes.func,
2017-12-22 18:43:28 +08:00
getDocument: PropTypes.func.def(returnDocument),
forceRender: PropTypes.bool,
destroyPopupOnHide: PropTypes.bool.def(false),
mask: PropTypes.bool.def(false),
maskClosable: PropTypes.bool.def(true),
2018-01-11 18:53:51 +08:00
// onPopupAlign: PropTypes.func.def(noop),
2017-12-22 18:43:28 +08:00
popupAlign: PropTypes.object.def({}),
popupVisible: PropTypes.bool,
2017-12-26 19:04:28 +08:00
defaultPopupVisible: PropTypes.bool.def(false),
maskTransitionName: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
]),
maskAnimation: PropTypes.string,
2017-12-22 18:43:28 +08:00
},
2018-01-12 16:10:41 +08:00
mixins: [BaseMixin],
2017-12-22 18:43:28 +08:00
data () {
const props = this.$props
let popupVisible
2017-12-27 18:15:11 +08:00
if (hasProp(this, 'popupVisible')) {
2017-12-22 18:43:28 +08:00
popupVisible = !!props.popupVisible
} else {
popupVisible = !!props.defaultPopupVisible
}
return {
sPopupVisible: popupVisible,
}
},
beforeCreate () {
ALL_HANDLERS.forEach((h) => {
this[`fire${h}`] = (e) => {
2018-01-17 16:12:53 +08:00
// const ev = `on${h[0].toUpperCase() + h.slice(1)}`
this.fireEvents(h, e)
2017-12-22 18:43:28 +08:00
}
})
},
mounted () {
2017-12-25 18:08:36 +08:00
this.updatedCal()
2017-12-22 18:43:28 +08:00
},
watch: {
popupVisible (val) {
if (val !== undefined) {
this.sPopupVisible = val
}
},
sPopupVisible (val) {
this.$nextTick(() => {
2018-01-11 18:53:51 +08:00
this.afterPopupVisibleChange(val)
2017-12-22 18:43:28 +08:00
})
},
},
2017-12-25 18:08:36 +08:00
updated () {
this.updatedCal()
2017-12-22 18:43:28 +08:00
},
beforeDestroy () {
this.clearDelayTimer()
this.clearOutsideHandler()
},
methods: {
2017-12-25 18:08:36 +08:00
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)
2017-12-22 18:43:28 +08:00
this.delaySetPopupVisible(true, this.$props.mouseEnterDelay)
},
2017-12-25 18:08:36 +08:00
onMouseleave (e) {
this.fireEvents('mouseleave', e)
2017-12-22 18:43:28 +08:00
this.delaySetPopupVisible(false, this.$props.mouseLeaveDelay)
},
2017-12-25 18:08:36 +08:00
onPopupMouseenter () {
2017-12-22 18:43:28 +08:00
this.clearDelayTimer()
},
2017-12-25 18:08:36 +08:00
onPopupMouseleave (e) {
2017-12-22 18:43:28 +08:00
if (e.relatedTarget && !e.relatedTarget.setTimeout &&
2017-12-26 19:04:28 +08:00
this.$refs.popup &&
this.$refs.popup.getPopupDomNode &&
contains(this.$refs.popup.getPopupDomNode(), e.relatedTarget)) {
2017-12-22 18:43:28 +08:00
return
}
this.delaySetPopupVisible(false, this.$props.mouseLeaveDelay)
},
onFocus (e) {
2017-12-25 18:08:36 +08:00
this.fireEvents('focus', e)
2017-12-22 18:43:28 +08:00
// incase focusin and focusout
this.clearDelayTimer()
if (this.isFocusToShow()) {
this.focusTime = Date.now()
this.delaySetPopupVisible(true, this.$props.focusDelay)
}
},
2017-12-25 18:08:36 +08:00
onMousedown (e) {
this.fireEvents('mousedown', e)
2017-12-22 18:43:28 +08:00
this.preClickTime = Date.now()
},
onTouchStart (e) {
2017-12-25 18:08:36 +08:00
this.fireEvents('touchStart', e)
2017-12-22 18:43:28 +08:00
this.preTouchTime = Date.now()
},
onBlur (e) {
2017-12-25 18:08:36 +08:00
this.fireEvents('blur', e)
2017-12-22 18:43:28 +08:00
this.clearDelayTimer()
if (this.isBlurToHide()) {
this.delaySetPopupVisible(false, this.$props.blurDelay)
}
},
onContextMenu (e) {
e.preventDefault()
2017-12-25 18:08:36 +08:00
this.fireEvents('contextMenu', e)
2017-12-22 18:43:28 +08:00
this.setPopupVisible(true)
},
onContextMenuClose () {
if (this.isContextMenuToShow()) {
this.close()
}
},
onClick (event) {
2017-12-25 18:08:36 +08:00
this.fireEvents('click', event)
2017-12-22 18:43:28 +08:00
// 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
2018-01-18 10:43:39 +08:00
event.preventDefault && event.preventDefault()
if (event.domEvent) {
event.domEvent.preventDefault()
}
2017-12-22 18:43:28 +08:00
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
2017-12-26 19:04:28 +08:00
if (this.$refs.popup && this.$refs.popup.getPopupDomNode) {
return this.$refs.popup.getPopupDomNode()
2017-12-22 18:43:28 +08:00
}
return null
},
getRootDomNode () {
2018-01-17 16:12:53 +08:00
return this.$el
2018-01-18 18:58:36 +08:00
// return this.$el.children[0] || this.$el
2017-12-22 18:43:28 +08:00
},
2018-01-11 18:53:51 +08:00
handleGetPopupClassFromAlign (align) {
2017-12-22 18:43:28 +08:00
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
},
2018-01-11 18:53:51 +08:00
getComponent (h) {
2017-12-22 18:43:28 +08:00
const mouseProps = {}
if (this.isMouseEnterToShow()) {
2017-12-25 18:08:36 +08:00
mouseProps.mouseenter = this.onPopupMouseenter
2017-12-22 18:43:28 +08:00
}
if (this.isMouseLeaveToHide()) {
2017-12-25 18:08:36 +08:00
mouseProps.mouseleave = this.onPopupMouseleave
2017-12-22 18:43:28 +08:00
}
const { prefixCls, destroyPopupOnHide, sPopupVisible,
2018-01-12 16:10:41 +08:00
popupStyle, popupClassName, action,
2018-01-11 18:53:51 +08:00
popupAnimation, handleGetPopupClassFromAlign, getRootDomNode,
2017-12-22 18:43:28 +08:00
mask, zIndex, popupTransitionName, getPopupAlign,
2017-12-25 18:08:36 +08:00
maskAnimation, maskTransitionName, popup, $slots, getContainer } = this
2017-12-22 18:43:28 +08:00
const popupProps = {
props: {
prefixCls,
destroyPopupOnHide,
visible: sPopupVisible,
action,
align: getPopupAlign(),
animation: popupAnimation,
2018-01-11 18:53:51 +08:00
getClassNameFromAlign: handleGetPopupClassFromAlign,
2017-12-22 18:43:28 +08:00
getRootDomNode,
mask,
zIndex,
transitionName: popupTransitionName,
maskAnimation,
maskTransitionName,
2017-12-25 18:08:36 +08:00
getContainer,
2018-01-03 18:30:12 +08:00
popupClassName,
2017-12-22 18:43:28 +08:00
},
on: {
2018-01-12 16:10:41 +08:00
align: this.$listeners.popupAlign || noop,
2017-12-22 18:43:28 +08:00
...mouseProps,
},
ref: 'popup',
style: popupStyle,
2018-01-18 18:58:36 +08:00
key: '_ANT_PORTAL',
2017-12-22 18:43:28 +08:00
}
return (
<Popup
{...popupProps}
>
2018-01-11 18:53:51 +08:00
{typeof popup === 'function' ? popup(h) : popup}
2017-12-22 18:43:28 +08:00
{popup === undefined ? $slots.popup : null}
</Popup>
)
},
getContainer () {
const { $props: props } = this
const mountNode = props.getPopupContainer
? props.getPopupContainer(this.$el) : props.getDocument().body
2017-12-27 16:13:26 +08:00
return mountNode
2017-12-22 18:43:28 +08:00
},
setPopupVisible (sPopupVisible) {
this.clearDelayTimer()
if (this.$data.sPopupVisible !== sPopupVisible) {
2017-12-27 18:15:11 +08:00
if (!hasProp(this, 'popupVisible')) {
2017-12-22 18:43:28 +08:00
this.setState({
sPopupVisible,
})
2017-12-25 18:08:36 +08:00
this.$forceUpdate()
2017-12-22 18:43:28 +08:00
}
2018-01-12 16:10:41 +08:00
this.$listeners.popupVisibleChange && this.$listeners.popupVisibleChange(sPopupVisible)
2017-12-22 18:43:28 +08:00
}
},
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
}
},
2018-01-18 18:58:36 +08:00
createTwoChains (event) {
2017-12-26 19:04:28 +08:00
let fn = () => {
}
2018-01-17 16:12:53 +08:00
const events = this.$listeners
if (this.childOriginEvents[event] && events[event]) {
2017-12-26 19:04:28 +08:00
return this[`fire${event}`]
2017-12-22 18:43:28 +08:00
}
2018-01-17 16:12:53 +08:00
fn = this.childOriginEvents[event] || events[event] || fn
2017-12-25 18:08:36 +08:00
return fn
2017-12-22 18:43:28 +08:00
},
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
2017-12-25 18:08:36 +08:00
return action.indexOf('hover') !== -1 || showAction.indexOf('mouseenter') !== -1
2017-12-22 18:43:28 +08:00
},
isMouseLeaveToHide () {
const { action, hideAction } = this.$props
2017-12-25 18:08:36 +08:00
return action.indexOf('hover') !== -1 || hideAction.indexOf('mouseleave') !== -1
2017-12-22 18:43:28 +08:00
},
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 () {
2017-12-26 19:04:28 +08:00
if (this.$data.sPopupVisible && this.$refs.popup && this.$refs.popup.$refs.alignInstance) {
this.$refs.popup.$refs.alignInstance.forceAlign()
2017-12-22 18:43:28 +08:00
}
},
fireEvents (type, e) {
2018-01-17 16:12:53 +08:00
if (this.childOriginEvents[type]) {
this.childOriginEvents[type](e)
2017-12-22 18:43:28 +08:00
}
2018-01-17 16:12:53 +08:00
this.__emit(type, e)
2017-12-22 18:43:28 +08:00
},
close () {
this.setPopupVisible(false)
},
},
2018-01-11 18:53:51 +08:00
render (h) {
2018-01-17 16:12:53 +08:00
const children = filterEmpty(this.$slots.default)
2017-12-26 19:04:28 +08:00
if (children.length > 1) {
warning(false, 'Trigger $slots.default.length > 1, just support only one default', true)
}
2017-12-22 18:43:28 +08:00
const child = children[0]
2018-01-18 18:58:36 +08:00
const events = getEvents(child)
// 黑科技vue暂未发现保留原事件的方法使用_ANT_EVENT_HACK来判断事件是否更新
if (!events._ANT_EVENT_HACK) {
this.childOriginEvents = events
}
2017-12-22 18:43:28 +08:00
const newChildProps = {
props: {},
2018-01-18 18:58:36 +08:00
on: { _ANT_EVENT_HACK: () => {} },
2017-12-22 18:43:28 +08:00
key: 'trigger',
}
if (this.isContextMenuToShow()) {
newChildProps.on.contextMenu = this.onContextMenu
} else {
2018-01-18 18:58:36 +08:00
newChildProps.on.contextMenu = this.createTwoChains('contextMenu')
2017-12-22 18:43:28 +08:00
}
if (this.isClickToHide() || this.isClickToShow()) {
newChildProps.on.click = this.onClick
2017-12-25 18:08:36 +08:00
newChildProps.on.mousedown = this.onMousedown
2017-12-22 18:43:28 +08:00
// newChildProps.on.touchStart = this.onTouchStart
} else {
2018-01-18 18:58:36 +08:00
newChildProps.on.click = this.createTwoChains('click')
newChildProps.on.mousedown = this.createTwoChains('mousedown')
// newChildProps.on.TouchStart = this.createTwoChains('onTouchStart')
2017-12-22 18:43:28 +08:00
}
if (this.isMouseEnterToShow()) {
2017-12-25 18:08:36 +08:00
newChildProps.on.mouseenter = this.onMouseenter
2017-12-22 18:43:28 +08:00
} else {
2018-01-18 18:58:36 +08:00
newChildProps.on.mouseenter = this.createTwoChains('mouseenter')
2017-12-22 18:43:28 +08:00
}
if (this.isMouseLeaveToHide()) {
2017-12-25 18:08:36 +08:00
newChildProps.on.mouseleave = this.onMouseleave
2017-12-22 18:43:28 +08:00
} else {
2018-01-18 18:58:36 +08:00
newChildProps.on.mouseleave = this.createTwoChains('mouseleave')
2017-12-22 18:43:28 +08:00
}
2018-01-08 18:31:04 +08:00
2017-12-22 18:43:28 +08:00
if (this.isFocusToShow() || this.isBlurToHide()) {
newChildProps.on.focus = this.onFocus
newChildProps.on.blur = this.onBlur
} else {
2018-01-18 18:58:36 +08:00
newChildProps.on.focus = this.createTwoChains('focus')
newChildProps.on.blur = this.createTwoChains('blur')
2017-12-22 18:43:28 +08:00
}
2017-12-26 19:04:28 +08:00
const { sPopupVisible, forceRender } = this
if (sPopupVisible || forceRender || this._component) {
2018-01-11 18:53:51 +08:00
this._component = this.getComponent(h)
2017-12-26 19:04:28 +08:00
} else {
this._component = null
}
2018-01-18 18:58:36 +08:00
this._component && (newChildProps.addChildren = [this._component])
const trigger = cloneElement(child, newChildProps)
2018-01-17 16:12:53 +08:00
return trigger
2017-12-22 18:43:28 +08:00
},
}
</script>