2018-03-19 02:16:27 +00:00
|
|
|
|
2018-02-06 07:32:32 +00:00
|
|
|
import PropTypes from '../_util/vue-types'
|
2018-11-08 11:59:09 +00:00
|
|
|
import { getStyle, getComponentFromProp } from '../_util/props-util'
|
2018-02-06 07:32:32 +00:00
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-02-05 11:12:41 +00:00
|
|
|
|
2019-01-01 13:19:50 +00:00
|
|
|
function noop () {
|
|
|
|
}
|
|
|
|
|
2018-02-05 11:12:41 +00:00
|
|
|
export default {
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: {
|
|
|
|
duration: PropTypes.number.def(1.5),
|
2018-02-06 07:21:13 +00:00
|
|
|
closable: PropTypes.bool,
|
|
|
|
prefixCls: PropTypes.string,
|
2018-09-05 13:28:54 +00:00
|
|
|
update: PropTypes.bool,
|
2018-11-08 11:59:09 +00:00
|
|
|
closeIcon: PropTypes.any,
|
2018-02-05 11:12:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
mounted () {
|
|
|
|
this.startCloseTimer()
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
updated () {
|
|
|
|
if (this.update) {
|
|
|
|
this.restartCloseTimer()
|
|
|
|
}
|
|
|
|
},
|
2018-02-05 11:12:41 +00:00
|
|
|
|
2018-02-06 07:21:13 +00:00
|
|
|
beforeDestroy () {
|
2018-02-05 11:12:41 +00:00
|
|
|
this.clearCloseTimer()
|
2018-02-06 07:21:13 +00:00
|
|
|
this.willDestroy = true // beforeDestroy调用后依然会触发onMouseleave事件
|
2018-02-05 11:12:41 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
watch: {
|
|
|
|
duration () {
|
|
|
|
this.restartCloseTimer()
|
|
|
|
},
|
|
|
|
},
|
2018-02-05 11:12:41 +00:00
|
|
|
methods: {
|
|
|
|
close () {
|
|
|
|
this.clearCloseTimer()
|
2018-02-06 07:21:13 +00:00
|
|
|
this.__emit('close')
|
2018-02-05 11:12:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
startCloseTimer () {
|
2018-02-06 07:21:13 +00:00
|
|
|
this.clearCloseTimer()
|
|
|
|
if (!this.willDestroy && this.duration) {
|
2018-02-05 11:12:41 +00:00
|
|
|
this.closeTimer = setTimeout(() => {
|
|
|
|
this.close()
|
|
|
|
}, this.duration * 1000)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clearCloseTimer () {
|
|
|
|
if (this.closeTimer) {
|
|
|
|
clearTimeout(this.closeTimer)
|
|
|
|
this.closeTimer = null
|
|
|
|
}
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
restartCloseTimer () {
|
|
|
|
this.clearCloseTimer()
|
|
|
|
this.startCloseTimer()
|
|
|
|
},
|
2018-02-05 11:12:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
2019-01-01 13:19:50 +00:00
|
|
|
const { prefixCls, closable, clearCloseTimer, startCloseTimer, $slots, close, $listeners } = this
|
2018-02-05 11:12:41 +00:00
|
|
|
const componentClass = `${prefixCls}-notice`
|
|
|
|
const className = {
|
|
|
|
[`${componentClass}`]: 1,
|
|
|
|
[`${componentClass}-closable`]: closable,
|
|
|
|
}
|
|
|
|
const style = getStyle(this)
|
2018-11-08 11:59:09 +00:00
|
|
|
const closeIcon = getComponentFromProp(this, 'closeIcon')
|
2018-02-05 11:12:41 +00:00
|
|
|
return (
|
2019-01-01 13:19:50 +00:00
|
|
|
<div
|
|
|
|
class={className}
|
|
|
|
style={style || { right: '50%' } }
|
|
|
|
onMouseenter={clearCloseTimer}
|
2018-02-05 11:12:41 +00:00
|
|
|
onMouseleave={startCloseTimer}
|
2019-01-01 13:19:50 +00:00
|
|
|
onClick={$listeners.click || noop}
|
2018-02-05 11:12:41 +00:00
|
|
|
>
|
|
|
|
<div class={`${componentClass}-content`}>{$slots.default}</div>
|
|
|
|
{closable
|
|
|
|
? <a tabIndex='0' onClick={close} class={`${componentClass}-close`}>
|
2018-11-08 11:59:09 +00:00
|
|
|
{closeIcon || <span class={`${componentClass}-close-x`}/>}
|
2018-02-05 11:12:41 +00:00
|
|
|
</a> : null
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|