2019-01-12 03:33:27 +00:00
|
|
|
import Notification from '../vc-notification';
|
2020-03-26 04:00:35 +00:00
|
|
|
import CheckCircleOutlined from '@ant-design/icons-vue/CheckCircleOutlined';
|
|
|
|
import InfoCircleOutlined from '@ant-design/icons-vue/InfoCircleOutlined';
|
|
|
|
import CloseCircleOutlined from '@ant-design/icons-vue/CloseCircleOutlined';
|
|
|
|
import ExclamationCircleOutlined from '@ant-design/icons-vue/ExclamationCircleOutlined';
|
|
|
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
2018-02-06 07:21:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const notificationInstance = {};
|
|
|
|
let defaultDuration = 4.5;
|
|
|
|
let defaultTop = '24px';
|
|
|
|
let defaultBottom = '24px';
|
|
|
|
let defaultPlacement = 'topRight';
|
|
|
|
let defaultGetContainer = () => document.body;
|
2020-03-07 11:45:13 +00:00
|
|
|
let defaultCloseIcon = null;
|
2018-02-06 07:21:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function setNotificationConfig(options) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { duration, placement, bottom, top, getContainer, closeIcon } = options;
|
2018-02-06 07:21:13 +00:00
|
|
|
if (duration !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultDuration = duration;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (placement !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultPlacement = placement;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (bottom !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultBottom = typeof bottom === 'number' ? `${bottom}px` : bottom;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (top !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultTop = typeof top === 'number' ? `${top}px` : top;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
if (getContainer !== undefined) {
|
2019-01-12 03:33:27 +00:00
|
|
|
defaultGetContainer = getContainer;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
if (closeIcon !== undefined) {
|
|
|
|
defaultCloseIcon = closeIcon;
|
|
|
|
}
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
function getPlacementStyle(placement, top = defaultTop, bottom = defaultBottom) {
|
2019-01-12 03:33:27 +00:00
|
|
|
let style;
|
2018-02-06 07:21:13 +00:00
|
|
|
switch (placement) {
|
|
|
|
case 'topLeft':
|
|
|
|
style = {
|
2020-06-14 13:41:29 +00:00
|
|
|
left: '0px',
|
2020-03-07 11:45:13 +00:00
|
|
|
top,
|
2018-02-06 07:21:13 +00:00
|
|
|
bottom: 'auto',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
case 'topRight':
|
|
|
|
style = {
|
2020-06-14 13:41:29 +00:00
|
|
|
right: '0px',
|
2020-03-07 11:45:13 +00:00
|
|
|
top,
|
2018-02-06 07:21:13 +00:00
|
|
|
bottom: 'auto',
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
case 'bottomLeft':
|
|
|
|
style = {
|
2020-06-14 13:41:29 +00:00
|
|
|
left: '0px',
|
2018-02-06 07:21:13 +00:00
|
|
|
top: 'auto',
|
2020-03-07 11:45:13 +00:00
|
|
|
bottom,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
default:
|
|
|
|
style = {
|
2020-06-14 13:41:29 +00:00
|
|
|
right: '0px',
|
2018-02-06 07:21:13 +00:00
|
|
|
top: 'auto',
|
2020-03-07 11:45:13 +00:00
|
|
|
bottom,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
|
|
|
break;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
return style;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
function getNotificationInstance(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
placement = defaultPlacement,
|
|
|
|
getContainer = defaultGetContainer,
|
|
|
|
top,
|
|
|
|
bottom,
|
|
|
|
closeIcon = defaultCloseIcon,
|
|
|
|
},
|
|
|
|
callback,
|
|
|
|
) {
|
2019-01-12 03:33:27 +00:00
|
|
|
const cacheKey = `${prefixCls}-${placement}`;
|
2018-02-06 07:21:13 +00:00
|
|
|
if (notificationInstance[cacheKey]) {
|
2019-01-12 03:33:27 +00:00
|
|
|
callback(notificationInstance[cacheKey]);
|
|
|
|
return;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
Notification.newInstance(
|
|
|
|
{
|
|
|
|
prefixCls,
|
|
|
|
class: `${prefixCls}-${placement}`,
|
2020-03-07 11:45:13 +00:00
|
|
|
style: getPlacementStyle(placement, top, bottom),
|
|
|
|
getContainer,
|
2020-06-14 13:41:29 +00:00
|
|
|
closeIcon: () => {
|
|
|
|
const icon = typeof closeIcon === 'function' ? closeIcon() : closeIcon;
|
2020-03-07 11:45:13 +00:00
|
|
|
const closeIconToRender = (
|
|
|
|
<span class={`${prefixCls}-close-x`}>
|
2020-03-26 04:00:35 +00:00
|
|
|
{icon || <CloseOutlined class={`${prefixCls}-close-icon`} />}
|
2020-03-07 11:45:13 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
return closeIconToRender;
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
},
|
|
|
|
notification => {
|
|
|
|
notificationInstance[cacheKey] = notification;
|
|
|
|
callback(notification);
|
|
|
|
},
|
|
|
|
);
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const typeToIcon = {
|
2020-03-26 04:00:35 +00:00
|
|
|
success: CheckCircleOutlined,
|
|
|
|
info: InfoCircleOutlined,
|
|
|
|
error: CloseCircleOutlined,
|
|
|
|
warning: ExclamationCircleOutlined,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-02-06 07:21:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function notice(args) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { icon, type, description, message, btn } = args;
|
2019-01-12 03:33:27 +00:00
|
|
|
const outerPrefixCls = args.prefixCls || 'ant-notification';
|
|
|
|
const prefixCls = `${outerPrefixCls}-notice`;
|
|
|
|
const duration = args.duration === undefined ? defaultDuration : args.duration;
|
2018-02-06 07:21:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
let iconNode = null;
|
2018-02-06 07:21:13 +00:00
|
|
|
if (icon) {
|
2020-06-14 13:41:29 +00:00
|
|
|
iconNode = () => (
|
|
|
|
<span class={`${prefixCls}-icon`}>{typeof icon === 'function' ? icon() : icon}</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-02-06 07:21:13 +00:00
|
|
|
} else if (type) {
|
2020-03-26 04:00:35 +00:00
|
|
|
const Icon = typeToIcon[type];
|
2020-06-14 13:41:29 +00:00
|
|
|
iconNode = () => <Icon class={`${prefixCls}-icon ${prefixCls}-icon-${type}`} />;
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
const { placement, top, bottom, getContainer, closeIcon } = args;
|
|
|
|
getNotificationInstance(
|
|
|
|
{
|
|
|
|
prefixCls: outerPrefixCls,
|
|
|
|
placement,
|
|
|
|
top,
|
|
|
|
bottom,
|
|
|
|
getContainer,
|
|
|
|
closeIcon,
|
|
|
|
},
|
|
|
|
notification => {
|
|
|
|
notification.notice({
|
2020-06-14 13:41:29 +00:00
|
|
|
content: () => (
|
2020-03-07 11:45:13 +00:00
|
|
|
<div class={iconNode ? `${prefixCls}-with-icon` : ''}>
|
2020-08-03 03:22:52 +00:00
|
|
|
{iconNode && iconNode()}
|
2020-03-07 11:45:13 +00:00
|
|
|
<div class={`${prefixCls}-message`}>
|
|
|
|
{!description && iconNode ? (
|
|
|
|
<span class={`${prefixCls}-message-single-line-auto-margin`} />
|
|
|
|
) : null}
|
2020-06-14 13:41:29 +00:00
|
|
|
{typeof message === 'function' ? message() : message}
|
2020-03-07 11:45:13 +00:00
|
|
|
</div>
|
|
|
|
<div class={`${prefixCls}-description`}>
|
2020-06-14 13:41:29 +00:00
|
|
|
{typeof description === 'function' ? description() : description}
|
2020-03-07 11:45:13 +00:00
|
|
|
</div>
|
|
|
|
{btn ? (
|
2020-06-14 13:41:29 +00:00
|
|
|
<span class={`${prefixCls}-btn`}>{typeof btn === 'function' ? btn() : btn}</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
) : null}
|
2018-02-06 07:21:13 +00:00
|
|
|
</div>
|
2020-03-07 11:45:13 +00:00
|
|
|
),
|
|
|
|
duration,
|
|
|
|
closable: true,
|
|
|
|
onClose: args.onClose,
|
|
|
|
onClick: args.onClick,
|
|
|
|
key: args.key,
|
|
|
|
style: args.style || {},
|
|
|
|
class: args.class,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2018-02-06 07:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const api = {
|
|
|
|
open: notice,
|
2019-01-12 03:33:27 +00:00
|
|
|
close(key) {
|
|
|
|
Object.keys(notificationInstance).forEach(cacheKey =>
|
|
|
|
notificationInstance[cacheKey].removeNotice(key),
|
|
|
|
);
|
2018-02-06 07:21:13 +00:00
|
|
|
},
|
|
|
|
config: setNotificationConfig,
|
2019-01-12 03:33:27 +00:00
|
|
|
destroy() {
|
2018-02-06 07:21:13 +00:00
|
|
|
Object.keys(notificationInstance).forEach(cacheKey => {
|
2019-01-12 03:33:27 +00:00
|
|
|
notificationInstance[cacheKey].destroy();
|
|
|
|
delete notificationInstance[cacheKey];
|
|
|
|
});
|
2018-02-06 07:21:13 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
['success', 'info', 'warning', 'error'].forEach(type => {
|
|
|
|
api[type] = args =>
|
|
|
|
api.open({
|
|
|
|
...args,
|
|
|
|
type,
|
|
|
|
});
|
|
|
|
});
|
2018-02-06 07:21:13 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
api.warn = api.warning;
|
|
|
|
export default api;
|