UNPKG

zent

Version:

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

469 lines (373 loc) 15.4 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof define === 'function' && define.amd) define(["react", "react-dom"], factory); else if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("react"), require("react-dom")); else if(typeof exports === 'object') exports["zent-loading"] = factory(require("react"), require("react-dom")); else root["zent-loading"] = factory(root["React"], root["ReactDOM"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_3__, __WEBPACK_EXTERNAL_MODULE_4__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { module.exports = __webpack_require__(1); /***/ }, /* 1 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = undefined; var _LoadingInstance = __webpack_require__(2); var _LoadingInstance2 = _interopRequireDefault(_LoadingInstance); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } exports['default'] = _LoadingInstance2['default']; module.exports = exports['default']; /***/ }, /* 2 */ /***/ function(module, exports, __webpack_require__) { '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 = __webpack_require__(3); var _react2 = _interopRequireDefault(_react); var _reactDom = __webpack_require__(4); var _reactDom2 = _interopRequireDefault(_reactDom); var _isBrowser = __webpack_require__(5); var _isBrowser2 = _interopRequireDefault(_isBrowser); var _Loading = __webpack_require__(6); 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']; /***/ }, /* 3 */ /***/ function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_3__; /***/ }, /* 4 */ /***/ function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_4__; /***/ }, /* 5 */ /***/ function(module, exports) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = !!(typeof window !== 'undefined' && window); module.exports = exports['default']; /***/ }, /* 6 */ /***/ function(module, exports, __webpack_require__) { 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports['default'] = undefined; 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 _react = __webpack_require__(3); var _react2 = _interopRequireDefault(_react); var _getPosition = __webpack_require__(7); 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 Loading = function (_Component) { _inherits(Loading, _Component); function Loading(props) { _classCallCheck(this, Loading); var _this = _possibleConstructorReturn(this, (Loading.__proto__ || Object.getPrototypeOf(Loading)).call(this, props)); _this.state = { show: _this.props.show }; ['show', 'setPosition', 'setWrapperRef'].forEach(function (item) { return _this[item] = _this[item].bind(_this); }); return _this; } _createClass(Loading, [{ key: 'show', value: function show(info) { if (info.show === this.state.show) { return; } if (info.show) { var target = this.props.target; if (target) { this.style = { left: (0, _getPosition.getElementLeft)(target), top: (0, _getPosition.getElementTop)(target), width: target.offsetWidth, height: target.offsetHeight }; } } this.setState({ show: info.show }); } }, { key: 'setPosition', value: function setPosition() { if (!this.wrapper) { return; } this.wrapper.style.zIndex = this.props.zIndex; if (this.style) { this.wrapper.style.top = this.style.top + 'px'; this.wrapper.style.left = this.style.left + 'px'; this.wrapper.style.width = this.style.width + 'px'; this.wrapper.style.height = this.style.height + 'px'; } else { this.wrapper.style.position = 'fixed'; } } }, { key: 'setWrapperRef', value: function setWrapperRef(name, el) { this[name] = el; } }, { key: 'componentDidMount', value: function componentDidMount() { this.setPosition(); } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this.setPosition(); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { this.setState({ show: nextProps.show }); } }, { key: 'render', value: function render() { var _props = this.props, prefix = _props.prefix, className = _props.className, containerClass = _props.containerClass; if (this.props['static']) { return _react2['default'].createElement( 'div', { className: prefix + '-loading-container ' + prefix + '-loading-container-static ' + containerClass, style: { height: this.props.children || !this.state.show ? 'initial' : this.props.height } }, this.props.children, this.state.show && _react2['default'].createElement( 'div', { className: prefix + '-page-loading ' + className }, _react2['default'].createElement('div', { className: prefix + '-page-mask' }) ) ); } return this.state.show && _react2['default'].createElement( 'div', { className: prefix + '-page-loading ' + className, ref: this.setWrapperRef.bind(null, 'wrapper') }, _react2['default'].createElement('div', { className: prefix + '-page-mask' }) ); } }]); return Loading; }(_react.Component); exports['default'] = Loading; module.exports = exports['default']; /***/ }, /* 7 */ /***/ function(module, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getElementLeft = getElementLeft; exports.getElementTop = getElementTop; function getElementLeft(element) { var actualLeft = element.offsetLeft; var current = element.offsetParent; while (current !== null) { actualLeft += current.offsetLeft; current = current.offsetParent; } return actualLeft; } function getElementTop(element) { var actualTop = element.offsetTop; var current = element.offsetParent; while (current !== null) { actualTop += current.offsetTop; current = current.offsetParent; } return actualTop; } /***/ } /******/ ]) }); ;