antd
Version:
An enterprise-class UI design language and React-based implementation
183 lines (169 loc) • 5.79 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _react = require('react');
var React = _interopRequireWildcard(_react);
var _rcNotification = require('rc-notification');
var _rcNotification2 = _interopRequireDefault(_rcNotification);
var _icon = require('../icon');
var _icon2 = _interopRequireDefault(_icon);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var notificationInstance = {};
var defaultDuration = 4.5;
var defaultTop = 24;
var defaultBottom = 24;
var defaultPlacement = 'topRight';
var defaultGetContainer = void 0;
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 = void 0;
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 = prefixCls + '-' + placement;
if (notificationInstance[cacheKey]) {
callback(notificationInstance[cacheKey]);
return;
}
_rcNotification2['default'].newInstance({
prefixCls: prefixCls,
className: prefixCls + '-' + placement,
style: getPlacementStyle(placement),
getContainer: defaultGetContainer
}, function (notification) {
notificationInstance[cacheKey] = notification;
callback(notification);
});
}
var typeToIcon = {
success: 'check-circle-o',
info: 'info-circle-o',
error: 'cross-circle-o',
warning: 'exclamation-circle-o'
};
function notice(args) {
var outerPrefixCls = args.prefixCls || 'ant-notification';
var prefixCls = outerPrefixCls + '-notice';
var duration = args.duration === undefined ? defaultDuration : args.duration;
var iconNode = null;
if (args.icon) {
iconNode = React.createElement(
'span',
{ className: prefixCls + '-icon' },
args.icon
);
} else if (args.type) {
var iconType = typeToIcon[args.type];
iconNode = React.createElement(_icon2['default'], { className: prefixCls + '-icon ' + prefixCls + '-icon-' + args.type, type: iconType });
}
var autoMarginTag = !args.description && iconNode ? React.createElement('span', { className: prefixCls + '-message-single-line-auto-margin' }) : null;
getNotificationInstance(outerPrefixCls, args.placement || defaultPlacement, function (notification) {
notification.notice({
content: React.createElement(
'div',
{ className: iconNode ? prefixCls + '-with-icon' : '' },
iconNode,
React.createElement(
'div',
{ className: prefixCls + '-message' },
autoMarginTag,
args.message
),
React.createElement(
'div',
{ className: prefixCls + '-description' },
args.description
),
args.btn ? React.createElement(
'span',
{ className: 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, _extends3['default'])({}, args, { type: type }));
};
});
api.warn = api.warning;
exports['default'] = api;
module.exports = exports['default'];
;