UNPKG

weex-nuke

Version:

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

187 lines (149 loc) 7.14 kB
/** @jsx createElement */ 'use strict'; 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 _nukeText = require('../../Text/index.js'); var _nukeText2 = _interopRequireDefault(_nukeText); var _nukeEnv = require('../../Env/index.js'); var _nukeTouchable = require('../../Touchable/index.js'); var _nukeTouchable2 = _interopRequireDefault(_nukeTouchable); var _nukeImage = require('../../Image/index.js'); var _nukeImage2 = _interopRequireDefault(_nukeImage); var _nukeIconfont = require('../../Iconfont/index.js'); var _nukeIconfont2 = _interopRequireDefault(_nukeIconfont); 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 _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 Icon = function (_Component) { _inherits(Icon, _Component); function Icon(props) { _classCallCheck(this, Icon); var _this = _possibleConstructorReturn(this, (Icon.__proto__ || Object.getPrototypeOf(Icon)).call(this, props)); var themeStyle = props.themeStyle; _this.fontURL = themeStyle['icon-font-path'].raw || themeStyle['icon-font-path']; _this.fontName = themeStyle['icon-font-family'].raw || themeStyle['icon-font-family']; return _this; } _createClass(Icon, [{ key: 'componentDidMount', value: function componentDidMount() { var type = this.getType(); if (type === 'iconfont') { var tmpFontURL = this.fontURL; if (this.fontURL.indexOf('.ttf') <= -1) { tmpFontURL += '.ttf'; } (0, _nukeIconfont2.default)({ name: this.fontName, url: tmpFontURL }); } } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { var type = this.getType(); if (type === 'iconfont') { var themeStyle = nextProps.themeStyle; var curFontUrl = themeStyle['icon-font-path'].raw || themeStyle['icon-font-path']; var curFontName = themeStyle['icon-font-family'].raw || themeStyle['icon-font-family']; if (curFontUrl !== this.fontURL || curFontName !== this.fontName) { this.fontURL = curFontUrl; this.fontName = curFontName; var tmpFontURL = this.fontURL; if (this.fontURL.indexOf('.ttf') <= -1) { tmpFontURL += '.ttf'; } (0, _nukeIconfont2.default)({ name: this.fontName, url: tmpFontURL }); } } } }, { key: 'getType', value: function getType() { var _props$name = this.props.name, name = _props$name === undefined ? '' : _props$name; var _props$type = this.props.type, type = _props$type === undefined ? 'image' : _props$type; if (type === 'image' && name !== '') { type = 'iconfont'; } return type; } }, { key: 'render', value: function render() { var _props = this.props, size = _props.size, _props$name2 = _props.name, name = _props$name2 === undefined ? '' : _props$name2, style = _props.style, onPress = _props.onPress, fixedFont = _props.fixedFont, others = _objectWithoutProperties(_props, ['size', 'name', 'style', 'onPress', 'fixedFont']); var styles = this.props.themeStyle; var type = this.getType(); if (type === 'iconfont') { var textStyle = Object.assign({}, styles['iconfont-' + size], style, { fontFamily: this.fontName }); var unicode = styles['icon-content-' + name] && (0, _nukeIconfont.formatUnicode)(styles['icon-content-' + name], 16); return (0, _rax.createElement)( _nukeText2.default, _extends({ onClick: onPress }, others, { fixedFont: fixedFont, style: textStyle }), unicode ); } var wrapStyle = Object.assign({}, styles['icon-image'], style); var sizeStyle = styles['image-' + size]; return (0, _rax.createElement)( _nukeTouchable2.default, _extends({ onPress: onPress, style: wrapStyle }, others), (0, _rax.createElement)(_nukeImage2.default, { source: { uri: this.props.src }, style: sizeStyle, resizeMode: 'cover' }) ); } }]); return Icon; }(_rax.Component); Icon.displayName = 'Icon'; Icon.defaultProps = { size: 'medium', style: {}, name: '', onPress: function onPress() {}, src: '', type: 'image', fixedFont: false }; Icon.propTypes = { onPress: _rax.PropTypes.func, name: _rax.PropTypes.any, themeStyle: _rax.PropTypes.any, style: _rax.PropTypes.any, src: _rax.PropTypes.string, type: _rax.PropTypes.string, size: _rax.PropTypes.oneOf(['xs', 'small', 'medium', 'large']), fixedFont: _rax.PropTypes.boolean }; Icon.contextTypes = { parentPath: _rax.PropTypes.any, parentStyle: _rax.PropTypes.any }; var StyledIcon = (0, _nukeThemeProvider.connectStyle)(_styles2.default)(Icon); exports.default = StyledIcon; module.exports = exports['default'];