UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

169 lines (141 loc) 5.48 kB
/** @jsx createElement */ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 _rax = require('rax'); var _nukeView = require('../View/index.js'); var _nukeView2 = _interopRequireDefault(_nukeView); var _nukeEnv = require('../Env/index.js'); var _nukeThemeProvider = require('../ThemeProvider/index.js'); var _styles = require('./styles/index.js'); var _styles2 = _interopRequireDefault(_styles); 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 Progress = function (_Component) { _inherits(Progress, _Component); function Progress(props) { _classCallCheck(this, Progress); var _this = _possibleConstructorReturn(this, (Progress.__proto__ || Object.getPrototypeOf(Progress)).call(this, props)); _this.state = { innerWidth: 0 }; return _this; } _createClass(Progress, [{ key: 'componentDidMount', value: function componentDidMount() { this.setInnerWidth(this.props); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('rate' in nextProps) { this.setInnerWidth(nextProps); } } }, { key: 'getRect', value: function getRect() { var _this2 = this; return new Promise(function (resolve, reject) { var domEl = (0, _rax.findDOMNode)(_this2.progress); if (domEl && _nukeEnv.isWeex) { try { setTimeout(function () { var dom = require('@weex-module/dom'); dom.getComponentRect(domEl.ref, function (e) { if (e && e.size && e.size.width) { resolve(e.size.width.toFixed()); } else { reject(0); } }); }, 100); } catch (e) { reject(0); } } else { reject(0); } }); } }, { key: 'setInnerWidth', value: function setInnerWidth(props) { var _this3 = this; var style = props.style, rate = props.rate; var width = void 0; if (_nukeEnv.isWeb) { this.setState({ innerWidth: (rate * 100).toString() + '%' }); } else if (style.width) { this.setState({ innerWidth: rate * style.width }); } else { this.getRect().then(function (width) { if (width) { _this3.setState({ innerWidth: rate * width }); } else { // try again _this3.getRect().then(function (width) { if (width) { _this3.setState({ innerWidth: rate * width }); } }); } }); } } }, { key: 'render', value: function render() { var _this4 = this; var _props = this.props, style = _props.style, barStyle = _props.barStyle, rate = _props.rate; var innerWidth = this.state.innerWidth; var styles = this.props.themeStyle; var innerStyle = Object.assign({}, styles['progress-inner'], { width: innerWidth }, barStyle); return (0, _rax.createElement)( _nukeView2.default, { x: 'progress-wrap', ref: function ref(n) { _this4.progress = n; }, style: [styles['progress-wrap'], style] }, (0, _rax.createElement)(_nukeView2.default, { style: innerStyle }) ); } }]); return Progress; }(_rax.Component); Progress.propTypes = { rate: _rax.PropTypes.number, style: _rax.PropTypes.any, barStyle: _rax.PropTypes.any }; Progress.defaultProps = { rate: 1, style: {}, barStyle: {} }; Progress.displayName = 'Progress'; var StyledProgress = (0, _nukeThemeProvider.connectStyle)(_styles2.default, { withRef: true })(Progress); exports.default = StyledProgress; module.exports = exports['default'];