choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
188 lines (164 loc) • 4.88 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import React from 'react';
import isPromise from 'is-promise';
import { NotificationManager } from 'choerodon-ui/shared';
import Icon from '../icon';
import { newNotificationInstance } from './Notification';
import { getPrefixCls } from '../configure/utils';
var config = NotificationManager.config;
function setNotificationConfig(options) {
var duration = options.duration,
placement = options.placement,
bottom = options.bottom,
top = options.top,
getContainer = options.getContainer,
maxCount = options.maxCount,
foldCount = options.foldCount;
if (duration !== undefined) {
config.duration = duration;
}
if (placement !== undefined) {
config.placement = placement;
}
if (bottom !== undefined) {
config.bottom = bottom;
}
if (top !== undefined) {
config.top = top;
}
if (getContainer !== undefined) {
config.getContainer = getContainer;
}
if (maxCount !== undefined) {
config.maxCount = maxCount;
}
if (foldCount !== undefined) {
config.foldCount = foldCount;
}
}
function getPlacementStyle(placement) {
var style;
switch (placement) {
case 'topLeft':
style = {
left: 0,
top: config.top,
bottom: 'auto'
};
break;
case 'topRight':
style = {
right: 0,
top: config.top,
bottom: 'auto'
};
break;
case 'bottomLeft':
style = {
left: 0,
top: 'auto',
bottom: config.bottom
};
break;
default:
style = {
right: 0,
top: 'auto',
bottom: config.bottom
};
break;
}
return style;
}
function getNotificationInstance(prefixCls, placement, callback) {
var cacheKey = "".concat(prefixCls, "-").concat(placement);
var instance = NotificationManager.instances.get(cacheKey);
if (instance) {
if (isPromise(instance)) {
instance.then(callback);
} else {
callback(instance);
}
return;
}
var config = NotificationManager.config;
NotificationManager.instances.set(cacheKey, new Promise(function (resolve) {
newNotificationInstance({
prefixCls: prefixCls,
className: cacheKey,
style: getPlacementStyle(placement),
getContainer: config.getContainer,
maxCount: config.maxCount,
foldCount: config.foldCount
}, function (notification) {
resolve(notification);
NotificationManager.instances.set(cacheKey, notification);
callback(notification);
});
}));
}
var typeToIcon = {
success: 'check',
info: 'info',
error: 'error',
warning: 'warning'
};
function notice(args) {
var outerPrefixCls = getPrefixCls('notification', args.prefixCls);
var prefixCls = "".concat(outerPrefixCls, "-notice");
var duration = args.duration === undefined ? config.duration : args.duration;
var iconNode = null;
if (args.icon) {
iconNode = /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-icon")
}, args.icon);
} else if (args.type) {
var iconType = typeToIcon[args.type];
iconNode = /*#__PURE__*/React.createElement(Icon, {
className: "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(args.type),
type: iconType
});
}
var autoMarginTag = !args.description && iconNode ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-message-single-line-auto-margin")
}) : null;
getNotificationInstance(outerPrefixCls, args.placement || config.placement, function (notification) {
notification.notice({
children: /*#__PURE__*/React.createElement("div", {
className: iconNode ? "".concat(prefixCls, "-with-icon") : ''
}, iconNode, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-message")
}, autoMarginTag, args.message), args.description ? /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-description")
}, args.description) : null, args.btn ? /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-btn")
}, args.btn) : null),
duration: duration,
closable: true,
onClose: args.onClose,
key: args.key,
style: args.style || {},
className: args.className
});
});
}
var api = {
open: notice,
close: function close(key) {
NotificationManager.remove(key);
},
config: setNotificationConfig,
destroy: function destroy() {
NotificationManager.clear();
}
};
['success', 'info', 'warning', 'error'].forEach(function (type) {
api[type] = function (args) {
return api.open(_objectSpread(_objectSpread({}, args), {}, {
type: type
}));
};
});
api.warn = api.warning;
export default api;
//# sourceMappingURL=index.js.map