| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- import _extends from "@babel/runtime/helpers/esm/extends";
- import { createVNode as _createVNode } from "vue";
- import Notification from '../vc-notification';
- import LoadingOutlined from "@ant-design/icons-vue/es/icons/LoadingOutlined";
- import ExclamationCircleFilled from "@ant-design/icons-vue/es/icons/ExclamationCircleFilled";
- import CloseCircleFilled from "@ant-design/icons-vue/es/icons/CloseCircleFilled";
- import CheckCircleFilled from "@ant-design/icons-vue/es/icons/CheckCircleFilled";
- import InfoCircleFilled from "@ant-design/icons-vue/es/icons/InfoCircleFilled";
- import classNames from '../_util/classNames';
- import useStyle from './style';
- import useMessage from './useMessage';
- let defaultDuration = 3;
- let defaultTop;
- let messageInstance;
- let key = 1;
- let localPrefixCls = '';
- let transitionName = 'move-up';
- let hasTransitionName = false;
- let getContainer = () => document.body;
- let maxCount;
- let rtl = false;
- export function getKeyThenIncreaseKey() {
- return key++;
- }
- function setMessageConfig(options) {
- if (options.top !== undefined) {
- defaultTop = options.top;
- messageInstance = null; // delete messageInstance for new defaultTop
- }
- if (options.duration !== undefined) {
- defaultDuration = options.duration;
- }
- if (options.prefixCls !== undefined) {
- localPrefixCls = options.prefixCls;
- }
- if (options.getContainer !== undefined) {
- getContainer = options.getContainer;
- messageInstance = null; // delete messageInstance for new getContainer
- }
- if (options.transitionName !== undefined) {
- transitionName = options.transitionName;
- messageInstance = null; // delete messageInstance for new transitionName
- hasTransitionName = true;
- }
- if (options.maxCount !== undefined) {
- maxCount = options.maxCount;
- messageInstance = null;
- }
- if (options.rtl !== undefined) {
- rtl = options.rtl;
- }
- }
- function getMessageInstance(args, callback) {
- if (messageInstance) {
- callback(messageInstance);
- return;
- }
- Notification.newInstance({
- appContext: args.appContext,
- prefixCls: args.prefixCls || localPrefixCls,
- rootPrefixCls: args.rootPrefixCls,
- transitionName,
- hasTransitionName,
- style: {
- top: defaultTop
- },
- getContainer: getContainer || args.getPopupContainer,
- maxCount,
- name: 'message',
- useStyle
- }, instance => {
- if (messageInstance) {
- callback(messageInstance);
- return;
- }
- messageInstance = instance;
- callback(instance);
- });
- }
- const typeToIcon = {
- info: InfoCircleFilled,
- success: CheckCircleFilled,
- error: CloseCircleFilled,
- warning: ExclamationCircleFilled,
- loading: LoadingOutlined
- };
- export const typeList = Object.keys(typeToIcon);
- function notice(args) {
- const duration = args.duration !== undefined ? args.duration : defaultDuration;
- const target = args.key || getKeyThenIncreaseKey();
- const closePromise = new Promise(resolve => {
- const callback = () => {
- if (typeof args.onClose === 'function') {
- args.onClose();
- }
- return resolve(true);
- };
- getMessageInstance(args, instance => {
- instance.notice({
- key: target,
- duration,
- style: args.style || {},
- class: args.class,
- content: _ref => {
- let {
- prefixCls
- } = _ref;
- const Icon = typeToIcon[args.type];
- const iconNode = Icon ? _createVNode(Icon, null, null) : '';
- const messageClass = classNames(`${prefixCls}-custom-content`, {
- [`${prefixCls}-${args.type}`]: args.type,
- [`${prefixCls}-rtl`]: rtl === true
- });
- return _createVNode("div", {
- "class": messageClass
- }, [typeof args.icon === 'function' ? args.icon() : args.icon || iconNode, _createVNode("span", null, [typeof args.content === 'function' ? args.content() : args.content])]);
- },
- onClose: callback,
- onClick: args.onClick
- });
- });
- });
- const result = () => {
- if (messageInstance) {
- messageInstance.removeNotice(target);
- }
- };
- result.then = (filled, rejected) => closePromise.then(filled, rejected);
- result.promise = closePromise;
- return result;
- }
- function isArgsProps(content) {
- return Object.prototype.toString.call(content) === '[object Object]' && !!content.content;
- }
- const api = {
- open: notice,
- config: setMessageConfig,
- destroy(messageKey) {
- if (messageInstance) {
- if (messageKey) {
- const {
- removeNotice
- } = messageInstance;
- removeNotice(messageKey);
- } else {
- const {
- destroy
- } = messageInstance;
- destroy();
- messageInstance = null;
- }
- }
- }
- };
- export function attachTypeApi(originalApi, type) {
- originalApi[type] = (content, duration, onClose) => {
- if (isArgsProps(content)) {
- return originalApi.open(_extends(_extends({}, content), {
- type
- }));
- }
- if (typeof duration === 'function') {
- onClose = duration;
- duration = undefined;
- }
- return originalApi.open({
- content,
- duration,
- type,
- onClose
- });
- };
- }
- typeList.forEach(type => attachTypeApi(api, type));
- api.warn = api.warning;
- api.useMessage = useMessage;
- /** @private test Only function. Not work on production */
- export const getInstance = () => process.env.NODE_ENV === 'test' ? messageInstance : null;
- export default api;
|