2018-03-19 10:16:27 +08:00
|
|
|
|
2018-02-05 19:12:41 +08:00
|
|
|
import Vue from 'vue'
|
2018-02-06 15:32:32 +08:00
|
|
|
import PropTypes from '../_util/vue-types'
|
2018-02-24 18:12:24 +08:00
|
|
|
import { getStyle } from '../_util/props-util'
|
2018-02-06 15:32:32 +08:00
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
|
|
|
import createChainedFunction from '../_util/createChainedFunction'
|
|
|
|
import getTransitionProps from '../_util/getTransitionProps'
|
2018-02-05 19:12:41 +08:00
|
|
|
import Notice from './Notice'
|
|
|
|
|
|
|
|
let seed = 0
|
|
|
|
const now = Date.now()
|
|
|
|
|
|
|
|
function getUuid () {
|
|
|
|
return `rcNotification_${now}_${seed++}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const Notification = {
|
|
|
|
mixins: [BaseMixin],
|
|
|
|
props: {
|
|
|
|
prefixCls: PropTypes.string.def('rc-notification'),
|
|
|
|
transitionName: PropTypes.string,
|
|
|
|
animation: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).def('fade'),
|
2018-09-05 21:28:54 +08:00
|
|
|
maxCount: PropTypes.number,
|
2018-02-05 19:12:41 +08:00
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
notices: [],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getTransitionName () {
|
|
|
|
const props = this.$props
|
|
|
|
let transitionName = props.transitionName
|
|
|
|
if (!transitionName && props.animation) {
|
|
|
|
transitionName = `${props.prefixCls}-${props.animation}`
|
|
|
|
}
|
|
|
|
return transitionName
|
|
|
|
},
|
|
|
|
|
|
|
|
add (notice) {
|
|
|
|
const key = notice.key = notice.key || getUuid()
|
2018-09-05 21:28:54 +08:00
|
|
|
const { maxCount } = this.$props
|
2018-02-05 19:12:41 +08:00
|
|
|
this.setState(previousState => {
|
|
|
|
const notices = previousState.notices
|
2018-09-05 21:28:54 +08:00
|
|
|
const noticeIndex = notices.map(v => v.key).indexOf(key)
|
|
|
|
const updatedNotices = notices.concat()
|
|
|
|
if (noticeIndex !== -1) {
|
|
|
|
updatedNotices.splice(noticeIndex, 1, notice)
|
|
|
|
} else {
|
|
|
|
if (maxCount && notices.length >= maxCount) {
|
|
|
|
// XXX, use key of first item to update new added (let React to move exsiting
|
|
|
|
// instead of remove and mount). Same key was used before for both a) external
|
|
|
|
// manual control and b) internal react 'key' prop , which is not that good.
|
|
|
|
notice.updateKey = updatedNotices[0].updateKey || updatedNotices[0].key
|
|
|
|
updatedNotices.shift()
|
2018-02-05 19:12:41 +08:00
|
|
|
}
|
2018-09-05 21:28:54 +08:00
|
|
|
updatedNotices.push(notice)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
notices: updatedNotices,
|
2018-02-05 19:12:41 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
remove (key) {
|
|
|
|
this.setState(previousState => {
|
|
|
|
return {
|
|
|
|
notices: previousState.notices.filter(notice => notice.key !== key),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2018-02-06 15:21:13 +08:00
|
|
|
render (h) {
|
2018-02-05 19:12:41 +08:00
|
|
|
const { prefixCls, notices, remove, getTransitionName } = this
|
2018-02-06 15:21:13 +08:00
|
|
|
const transitionProps = getTransitionProps(getTransitionName())
|
2018-09-05 21:28:54 +08:00
|
|
|
const noticeNodes = notices.map((notice, index) => {
|
|
|
|
const update = Boolean(index === notices.length - 1 && notice.updateKey)
|
|
|
|
const key = notice.updateKey ? notice.updateKey : notice.key
|
|
|
|
|
|
|
|
const { content, duration, closable, onClose, style, class: className } = notice
|
2018-02-06 15:21:13 +08:00
|
|
|
const close = createChainedFunction(remove.bind(this, key), onClose)
|
2018-02-05 19:12:41 +08:00
|
|
|
const noticeProps = {
|
|
|
|
props: {
|
|
|
|
prefixCls,
|
2018-02-06 15:21:13 +08:00
|
|
|
duration,
|
|
|
|
closable,
|
2018-09-05 21:28:54 +08:00
|
|
|
update,
|
2018-02-05 19:12:41 +08:00
|
|
|
},
|
|
|
|
on: {
|
2018-02-06 15:21:13 +08:00
|
|
|
close,
|
2018-02-05 19:12:41 +08:00
|
|
|
},
|
2018-02-06 15:21:13 +08:00
|
|
|
style,
|
|
|
|
class: className,
|
|
|
|
key,
|
2018-02-05 19:12:41 +08:00
|
|
|
}
|
2018-02-06 15:21:13 +08:00
|
|
|
return (
|
|
|
|
<Notice
|
|
|
|
{...noticeProps}
|
|
|
|
>
|
2018-09-05 21:28:54 +08:00
|
|
|
{typeof content === 'function' ? content(h) : content}
|
2018-02-06 15:21:13 +08:00
|
|
|
</Notice>
|
|
|
|
)
|
2018-02-05 19:12:41 +08:00
|
|
|
})
|
|
|
|
const className = {
|
|
|
|
[prefixCls]: 1,
|
|
|
|
}
|
|
|
|
const style = getStyle(this)
|
|
|
|
return (
|
|
|
|
<div class={className} style={style || {
|
|
|
|
top: '65px',
|
|
|
|
left: '50%',
|
|
|
|
}}>
|
2018-02-06 15:21:13 +08:00
|
|
|
<transition-group {...transitionProps}>{noticeNodes}</transition-group>
|
2018-02-05 19:12:41 +08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Notification.newInstance = function newNotificationInstance (properties, callback) {
|
2018-02-06 15:21:13 +08:00
|
|
|
const { getContainer, style, class: className, ...props } = properties || {}
|
2018-02-05 19:12:41 +08:00
|
|
|
const div = document.createElement('div')
|
|
|
|
if (getContainer) {
|
|
|
|
const root = getContainer()
|
|
|
|
root.appendChild(div)
|
|
|
|
} else {
|
|
|
|
document.body.appendChild(div)
|
|
|
|
}
|
2018-02-06 15:21:13 +08:00
|
|
|
new Vue({
|
2018-02-05 19:12:41 +08:00
|
|
|
el: div,
|
|
|
|
mounted () {
|
|
|
|
const self = this
|
|
|
|
this.$nextTick(() => {
|
|
|
|
callback({
|
|
|
|
notice (noticeProps) {
|
|
|
|
self.$refs.notification.add(noticeProps)
|
|
|
|
},
|
|
|
|
removeNotice (key) {
|
|
|
|
self.$refs.notification.remove(key)
|
|
|
|
},
|
|
|
|
component: self,
|
|
|
|
destroy () {
|
2018-02-06 15:21:13 +08:00
|
|
|
self.$destroy()
|
|
|
|
self.$el.parentNode.removeChild(self.$el)
|
2018-02-05 19:12:41 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
render () {
|
|
|
|
const p = {
|
|
|
|
props,
|
|
|
|
ref: 'notification',
|
|
|
|
style,
|
|
|
|
class: className,
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Notification
|
|
|
|
{...p}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Notification
|
|
|
|
|