UNPKG

zent

Version:

一套前端设计语言和基于React的实现

123 lines (98 loc) 2.96 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.success = success; exports.error = error; exports.clear = clear; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _isBrowser = require('zent-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; 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, callback) { var container = containerList[containerId]; if (!container) { return; } _reactDom2['default'].unmountComponentAtNode(container); delete containerList[containerId]; closeNotifyCallback(callback); }; /** * 显示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(); containerList[containerId] = container; setTimeout(function () { closeNotify(containerId, callback); }, props.duration || 3000); return containerId; }; /** * 关闭所有notify */ var closeAllNotify = function closeAllNotify() { Object.keys(containerList).forEach(function (containerId) { closeNotify(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(); } }