UNPKG

weex-nuke

Version:

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

222 lines (186 loc) 7.93 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 _nukeTouchable = require('../Touchable/index.js'); var _nukeTouchable2 = _interopRequireDefault(_nukeTouchable); var _nukeEnv = require('../Env/index.js'); var _nukeUtil = require('../Util/index.js'); var _bridge = require('./bridge.js'); var _bridge2 = _interopRequireDefault(_bridge); var _styleda = require('./styleda.js'); var _styleda2 = _interopRequireDefault(_styleda); 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 defaultLineHeightOptimizer = function defaultLineHeightOptimizer(fontSize) { return Math.ceil(9.24 * Math.pow(10, -5) * Math.pow(fontSize, 2) + 1.492 * fontSize + 2.174); }; var Link = function (_Component) { _inherits(Link, _Component); function Link(props, context) { _classCallCheck(this, Link); var _this = _possibleConstructorReturn(this, (Link.__proto__ || Object.getPrototypeOf(Link)).call(this, props)); _this.pushNext = function () { if (_nukeEnv.isWeex) { var navigator = require('@weex-module/navigator'); try { navigator.push({ url: _this.props.href }, function (e) {}); } catch (e) {} } }; _this.onTextPress = function (e) { // 由于 native 端事件绑了2遍,因此 web 端需要去重 if (_nukeEnv.isWeb) { e.stopPropagation(); } _this.pushNext(); }; _this.pushto = function (url) { (0, _bridge2.default)(url); }; _this.fixedFont = context.commonConfigs && context.commonConfigs.fixedFont; _this.optimizeLineHeight = context.commonConfigs && context.commonConfigs.optimizeLineHeight; if ('fixedFont' in props) { _this.fixedFont = props.fixedFont; } if ('optimizeLineHeight' in props) { _this.optimizeLineHeight = props.optimizeLineHeight; } return _this; } _createClass(Link, [{ key: 'convertURL', value: function convertURL(url) { if (url.indexOf('qap://') > -1) { var options = { refer: window.location.href }; if (typeof window.__QAP__ !== 'undefined' && __QAP__.package && __QAP__.package.config && __QAP__.package.config.WebRootPath) { options.webRootPath = __QAP__.package.config.WebRootPath; } if (!options.webRootPath.endsWith('/')) { options.webRootPath += '/'; } // qap降级情况下,根据webrootPath计算获得qap协议对应的实际降级页面地址 url = options.webRootPath + url.replace(/qap:\/{2,3}/, ''); var WxParam = ['_wx_tpl', '_h5_tpl', 'dd_wx_tpl']; WxParam.map(function (param) { if (options.refer.indexOf(param) > -1) { options[param] = url; } }); url = _nukeUtil.urlHelper.setSearchParameter(url.replace('.js', '.html'), options, { keepOrigin: true, addQueryPrefix: false, encode: false }); } return url; } }, { key: 'render', value: function render() { var TextAttrArr = ['color', 'fontSize', 'fontStyle', 'fontWeight', 'textAlign', 'textDecoration']; var _props = this.props, children = _props.children, href = _props.href, _props$target = _props.target, target = _props$target === undefined ? '_self' : _props$target, webUrl = _props.webUrl, activeStyle = _props.activeStyle; var style = _extends({}, styles.initial, this.props.style); var textStyle = {}; var content = void 0; var activeObj = {}; for (var k in activeStyle) { activeObj[k + ':active'] = activeStyle[k]; } style = Object.assign(style, activeObj); if (_nukeEnv.isWeex) { if (typeof children === 'string') { TextAttrArr.forEach(function (item) { if (style[item]) { textStyle[item] = style[item]; } if (style[item + ':active']) { textStyle[item + ':active'] = style[item + ':active']; } }); content = (0, _rax.createElement)( _nukeText2.default, { fixedFont: this.fixedFont, optimizeLineHeight: this.optimizeLineHeight, onPress: this.onTextPress, style: textStyle }, children ); } return (0, _rax.createElement)( _nukeTouchable2.default, _extends({}, this.props, { activeStyle: activeStyle, style: style, onPress: this.pushNext }), typeof children !== 'string' ? children : content ); } else { var linkURL = webUrl || (href ? this.convertURL(href) : ''); if (_nukeEnv.isQNWeb && linkURL && target === '_blank') { return (0, _rax.createElement)( _nukeTouchable2.default, _extends({}, this.props, { style: style, onPress: this.pushto(linkURL) }), children || content ); } else { return (0, _rax.createElement)( _styleda2.default, _extends({}, this.props, { lineHeightOptimizer: defaultLineHeightOptimizer, target: target, style: [{ display: 'flex' }, style], href: linkURL }), this.props.children ); } } } }]); return Link; }(_rax.Component); var styles = { initial: { textDecoration: 'none' } }; Link.contextTypes = { androidConfigs: _rax.PropTypes.any, commonConfigs: _rax.PropTypes.any }; Link.propTypes = { optimizeLineHeight: _rax.PropTypes.boolean, fixedFont: _rax.PropTypes.boolean, lineHeightOptimizer: _rax.PropTypes.func }; Link.defaultProps = { style: {}, lineHeightOptimizer: defaultLineHeightOptimizer }; exports.default = Link; module.exports = exports['default'];