UNPKG

weex-nuke

Version:

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

293 lines (258 loc) 9.53 kB
'use strict'; /** @jsx createElement */ Object.defineProperty(exports, "__esModule", { value: true }); 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 _rax = require('rax'); var _nukeView = require('../View/index.js'); var _nukeView2 = _interopRequireDefault(_nukeView); var _nukeEnv = require('../Env/index.js'); var _nukeTransition = require('../Transition/index.js'); var _nukeTransition2 = _interopRequireDefault(_nukeTransition); var _nukeDimensions = require('../Dimensions/index.js'); var _nukeDimensions2 = _interopRequireDefault(_nukeDimensions); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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 Mask = function (_Component) { _inherits(Mask, _Component); function Mask(props) { _classCallCheck(this, Mask); var _this = _possibleConstructorReturn(this, (Mask.__proto__ || Object.getPrototypeOf(Mask)).call(this, props)); var visible = true; if ('defaultVisible' in props) { visible = props.defaultVisible; } _this.state = { visible: visible, style: visible ? styles.transparent : styles.shown }; return _this; } _createClass(Mask, [{ key: 'componentDidMount', value: function componentDidMount() { if (this.state.visible) { if (this.props.animate) { this.doAnimShow(); } else { this.show(); } } } }, { key: 'show', value: function show() { if (this.props.animate) { this.setState({ visible: true, style: styles.transparent }); return; } this.setState({ visible: true, style: styles.shown }); this.props.onShow && this.props.onShow(); } }, { key: 'change', value: function change(e) { this.setState({ visible: e.visible || !this.state.visible }); this.props.onVisibleChanged && this.props.onVisibleChanged(e); } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps, prevState) { // if (this.state.visible && this.props.animate) { this.doAnimShow(); } } }, { key: 'doAnimShow', value: function doAnimShow() { var _this2 = this; var _props = this.props, effect = _props.effect, duration = _props.duration; var box = (0, _rax.findDOMNode)(this.maskNode); if (this.state.visible && box && this.state.style.opacity == 0) { (0, _nukeTransition2.default)(box, styles.shown, { timingFunction: effect, duration: duration }, function () { _this2.setState({ style: styles.shown }); _this2.props.onShow && _this2.props.onShow(); }); } } }, { key: 'hide', value: function hide() { var _this3 = this; var _props2 = this.props, animate = _props2.animate, effect = _props2.effect, duration = _props2.duration; if (animate) { var box = this.maskNode; if (box) { setTimeout(function () { (0, _nukeTransition2.default)(box, styles.transparent, { timingFunction: effect, duration: duration }, function () { _this3.setState({ visible: false }); _this3.props.onHide && _this3.props.onHide(); }); }, 10); } } else { this.setState({ visible: false }); this.props.onHide && this.props.onHide(); } } }, { key: 'maskPress', value: function maskPress(e) { if (this.props.maskClosable) { this.hide(); } this.props.onMaskPress && this.props.onMaskPress(); } }, { key: 'emptyEvent', value: function emptyEvent(e) { // EmptyEvent consumes events in android, so it would not be bubbled to mask press. if (_nukeEnv.isWeb) { e.stopPropagation(); } } }, { key: 'getChildren', value: function getChildren() { var _this4 = this; var _props3 = this.props, children = _props3.children, _props3$contentStyle = _props3.contentStyle, contentStyle = _props3$contentStyle === undefined ? {} : _props3$contentStyle; if (!children) return null; if (!Array.isArray(children)) { return (0, _rax.cloneElement)(children, { onClick: function onClick(e) { _this4.emptyEvent(e); } }); } if (children.length === 1) { return (0, _rax.cloneElement)(children[0], { onClick: function onClick(e) { _this4.emptyEvent(e); } }); } return (0, _rax.createElement)( _nukeView2.default, { style: contentStyle, x: 'autowrap', onClick: function onClick(e) { return _this4.emptyEvent(e); } }, children ); } }, { key: 'render', value: function render() { var _this5 = this; var visible = this.state.visible; var _props4 = this.props, content = _props4.content, children = _props4.children, _props4$style = _props4.style, style = _props4$style === undefined ? {} : _props4$style, onVisibleChanged = _props4.onVisibleChanged, onClick = _props4.onClick, _props4$noPress = _props4.noPress, noPress = _props4$noPress === undefined ? false : _props4$noPress, others = _objectWithoutProperties(_props4, ['content', 'children', 'style', 'onVisibleChanged', 'onClick', 'noPress']); if (_nukeEnv.isWeex) { var nativeAttrs = _extends({}, others, { style: [_nukeEnv.appInfo.platform === 'iOS' ? styles.maskNativeIOS : {}, style], onClick: function onClick(e) { return _this5.maskPress(e); }, onVisibleChanged: function onVisibleChanged(e) { return _this5.change(e); } }); if (noPress) { delete nativeAttrs.onClick; } var maskWeex = visible ? (0, _rax.createElement)( 'mask', _extends({ ref: function ref(n) { _this5.maskNode = (0, _rax.findDOMNode)(n); } }, nativeAttrs), this.getChildren() ) : null; return maskWeex; } return visible ? (0, _rax.createElement)( 'div', _extends({}, others, { ref: function ref(n) { _this5.maskNode = (0, _rax.findDOMNode)(n); }, style: [styles.maskWeb, this.state.style, style], onClick: function onClick(e) { return _this5.maskPress(e); } }), this.getChildren() ) : null; } }]); return Mask; }(_rax.Component); var styles = { // In Android width & height & position will be auto calculated while in iOS won't. maskNativeIOS: { position: 'absolute', width: 750, left: 0, top: 0, // To get fullscreen mask , we set screen height height: Math.round(_nukeDimensions2.default.get('screen').height / window.devicePixelRatio) + 'wx' }, maskWeb: { display: 'flex', boxSizing: 'border-box', flexDirection: 'column', alignContent: 'flex-start', flexShrink: 0, position: 'fixed', top: 0, left: 0, right: 0, bottom: 0, zIndex: 100 }, transparent: { opacity: 0 }, shown: { opacity: 1 } }; Mask.propTypes = {}; Mask.defaultProps = { animate: true, effect: 'ease-in-out', duration: 200, contentStyle: {} }; exports.default = Mask; module.exports = exports['default'];