UNPKG

zent

Version:

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

174 lines (138 loc) 6.12 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _class, _temp; 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 _Loading = require('./Loading'); var _Loading2 = _interopRequireDefault(_Loading); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Instance = (_temp = _class = function (_Component) { _inherits(Instance, _Component); function Instance() { _classCallCheck(this, Instance); return _possibleConstructorReturn(this, (Instance.__proto__ || Object.getPrototypeOf(Instance)).apply(this, arguments)); } _createClass(Instance, [{ key: 'componentDidMount', value: function componentDidMount() { this.renderLoading(); } // 因为需要确保内部元素已经 render 完毕,所以使用 did,但是因此带来额外的对 show 字段的判断逻辑 // 对外暴露的静态初始方法 }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this.renderLoading(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.instance) { var loadingContainer = this.instance.container; _reactDom2['default'].unmountComponentAtNode(this.instance.container); loadingContainer.parentNode.removeChild(loadingContainer); } } // 通过以下函数与组件通信 }, { key: 'renderLoading', value: function renderLoading(target) { // static 方式,loading 将存在于文档流中 if (this.props['static']) { return; } if (!this.instance) { if (!target) { target = _reactDom2['default'].findDOMNode(this); } this.instance = Instance.newInstance(_extends({}, this.props, { target: target })); } if (this.instance) { this.instance.show(_extends({}, this.props)); } } }, { key: 'render', value: function render() { if (this.props['static']) { return _react2['default'].createElement( _Loading2['default'], this.props, this.props.children ); } return this.props.children; } }]); return Instance; }(_react.Component), _class.propTypes = { prefix: _react.PropTypes.string, className: _react.PropTypes.string, 'static': _react.PropTypes.bool, show: _react.PropTypes.bool, zIndex: _react.PropTypes.number, containerClass: _react.PropTypes.string }, _class.defaultProps = { prefix: 'zent', className: '', 'static': true, show: false, height: 160, zIndex: 9998, containerClass: '' }, _class.newInstance = function (props) { if (!_isBrowser2['default']) return; var div = document.createElement('div'); div.className = props.prefix + '-loading-container ' + props.containerClass; document.body.appendChild(div); var loading = _reactDom2['default'].render(_react2['default'].createElement(_Loading2['default'], props), div); return { show: loading.show, container: div }; }, _class.on = function () { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, _ref$prefix = _ref.prefix, prefix = _ref$prefix === undefined ? 'zent' : _ref$prefix, _ref$className = _ref.className, className = _ref$className === undefined ? '' : _ref$className, _ref$containerClass = _ref.containerClass, containerClass = _ref$containerClass === undefined ? '' : _ref$containerClass, _ref$zIndex = _ref.zIndex, zIndex = _ref$zIndex === undefined ? 9998 : _ref$zIndex; if (!_isBrowser2['default']) return; if (!this.instance) { this.instance = this.newInstance({ show: true, prefix: prefix, className: className, containerClass: containerClass, zIndex: zIndex }); } this.instance.show({ show: true }); }, _class.off = function () { if (!_isBrowser2['default']) return; if (!this.instance) return; this.instance.show({ show: false }); }, _temp); exports['default'] = Instance; module.exports = exports['default'];