choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
193 lines (164 loc) • 5.1 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _react = _interopRequireDefault(require("react"));
var _icon = _interopRequireDefault(require("../icon"));
var _notification = _interopRequireDefault(require("../rc-components/notification"));
var _configure = require("../configure");
var notificationInstance = {};
var defaultDuration = 4.5;
var defaultTop = 24;
var defaultBottom = 24;
var defaultPlacement = 'topRight';
var defaultGetContainer;
function setNotificationConfig(options) {
var duration = options.duration,
placement = options.placement,
bottom = options.bottom,
top = options.top,
getContainer = options.getContainer;
if (duration !== undefined) {
defaultDuration = duration;
}
if (placement !== undefined) {
defaultPlacement = placement;
}
if (bottom !== undefined) {
defaultBottom = bottom;
}
if (top !== undefined) {
defaultTop = top;
}
if (getContainer !== undefined) {
defaultGetContainer = getContainer;
}
}
function getPlacementStyle(placement) {
var style;
switch (placement) {
case 'topLeft':
style = {
left: 0,
top: defaultTop,
bottom: 'auto'
};
break;
case 'topRight':
style = {
right: 0,
top: defaultTop,
bottom: 'auto'
};
break;
case 'bottomLeft':
style = {
left: 0,
top: 'auto',
bottom: defaultBottom
};
break;
default:
style = {
right: 0,
top: 'auto',
bottom: defaultBottom
};
break;
}
return style;
}
function getNotificationInstance(prefixCls, placement, callback) {
var cacheKey = "".concat(prefixCls, "-").concat(placement);
if (notificationInstance[cacheKey]) {
callback(notificationInstance[cacheKey]);
return;
}
_notification["default"].newInstance({
prefixCls: prefixCls,
className: "".concat(prefixCls, "-").concat(placement),
style: getPlacementStyle(placement),
getContainer: defaultGetContainer,
closeIcon: _react["default"].createElement(_icon["default"], {
className: "".concat(prefixCls, "-close-icon"),
type: "close"
})
}, function (notification) {
notificationInstance[cacheKey] = notification;
callback(notification);
});
}
var typeToIcon = {
success: 'check',
info: 'info',
error: 'error',
warning: 'warning'
};
function notice(args) {
var outerPrefixCls = (0, _configure.getPrefixCls)('notification', args.prefixCls);
var prefixCls = "".concat(outerPrefixCls, "-notice");
var duration = args.duration === undefined ? defaultDuration : args.duration;
var iconNode = null;
if (args.icon) {
iconNode = _react["default"].createElement("span", {
className: "".concat(prefixCls, "-icon")
}, args.icon);
} else if (args.type) {
var iconType = typeToIcon[args.type];
iconNode = _react["default"].createElement(_icon["default"], {
className: "".concat(prefixCls, "-icon ").concat(prefixCls, "-icon-").concat(args.type),
type: iconType
});
}
var autoMarginTag = !args.description && iconNode ? _react["default"].createElement("span", {
className: "".concat(prefixCls, "-message-single-line-auto-margin")
}) : null;
getNotificationInstance(outerPrefixCls, args.placement || defaultPlacement, function (notification) {
notification.notice({
content: _react["default"].createElement("div", {
className: iconNode ? "".concat(prefixCls, "-with-icon") : ''
}, iconNode, _react["default"].createElement("div", {
className: "".concat(prefixCls, "-message")
}, autoMarginTag, args.message), _react["default"].createElement("div", {
className: "".concat(prefixCls, "-description")
}, args.description), args.btn ? _react["default"].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) {
Object.keys(notificationInstance).forEach(function (cacheKey) {
return notificationInstance[cacheKey].removeNotice(key);
});
},
config: setNotificationConfig,
destroy: function destroy() {
Object.keys(notificationInstance).forEach(function (cacheKey) {
notificationInstance[cacheKey].destroy();
delete notificationInstance[cacheKey];
});
}
};
['success', 'info', 'warning', 'error'].forEach(function (type) {
api[type] = function (args) {
return api.open((0, _objectSpread2["default"])({}, args, {
type: type
}));
};
});
api.warn = api.warning;
var _default = api;
exports["default"] = _default;
//# sourceMappingURL=index.js.map