zent
Version:
一套前端设计语言和基于React的实现
138 lines (109 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.success = success;
exports.error = error;
exports.clear = clear;
exports.config = config;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _reactDom2 = _interopRequireDefault(_reactDom);
var _isBrowser = require('../utils/isBrowser');
var _isBrowser2 = _interopRequireDefault(_isBrowser);
var _NotifyContent = require('./NotifyContent');
var _NotifyContent2 = _interopRequireDefault(_NotifyContent);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var containerList = {};
var id = 0;
var durationDefault = 2000;
function createContainerId() {
return ++id;
}
/**
* 执行notify结束callback
* @param {Function} callback 关闭notify回调
*/
var closeNotifyCallback = function closeNotifyCallback(callback) {
if (typeof callback === 'function') {
callback();
}
};
/**
* 关闭notify
* @param {[type]} containerId notify容器Id
* @param {Function} callback notify消失时回调
*/
var closeNotify = function closeNotify(containerId) {
var containerObj = containerList[containerId];
if (!containerObj) {
return;
}
var container = containerObj.container,
callback = containerObj.callback,
timeOutId = containerObj.timeOutId;
clearTimeout(timeOutId);
_reactDom2['default'].unmountComponentAtNode(container);
delete containerList[containerId];
closeNotifyCallback(callback);
};
/**
* 关闭所有notify
*/
var closeAllNotify = function closeAllNotify() {
Object.keys(containerList).forEach(function (containerId) {
closeNotify(containerId);
});
};
/**
* 显示notify
* @param {[type]} container notify容器
* @param {[type]} props notify属性
* @param {Function} callback notify消失时回调
*/
var showNotify = function showNotify(container, props, callback) {
_reactDom2['default'].render(_react2['default'].createElement(_NotifyContent2['default'], props), container);
var containerId = createContainerId();
var timeOutId = setTimeout(function () {
closeNotify(containerId);
}, props.duration || durationDefault);
containerList[containerId] = { container: container, callback: callback, timeOutId: timeOutId };
return containerId;
};
/**
* notify显示前初始化
* @param {[type]} text 显示文案
* @param {[type]} duration 显示时长
* @param {[type]} status notify状态
* @param {Function} callback notify消失时回调
*/
var readyToShow = function readyToShow(text, duration, status, callback) {
if (!_isBrowser2['default']) return;
var container = document.createElement('div');
var props = {
visible: true,
text: text,
duration: duration,
status: status
};
return showNotify(container, props, callback);
};
function success(text, duration, callback) {
return readyToShow(text, duration, 'success', callback);
}
function error(text, duration, callback) {
return readyToShow(text, duration, 'error', callback);
}
function clear(containerId) {
if (containerId) {
closeNotify(containerId);
} else {
closeAllNotify();
}
}
function config(options) {
if (options.duration) {
durationDefault = options.duration;
}
}