zent
Version:
一套前端设计语言和基于React的实现
147 lines (123 loc) • 5.14 kB
JavaScript
'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 = require('react');
var _react2 = _interopRequireDefault(_react);
var _getPosition = require('./getPosition');
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'];