UNPKG

yylib-quick-mobile

Version:

yylib-quick-mobile

129 lines (107 loc) 5.86 kB
'use strict'; 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 _classnames2 = require('classnames'); var _classnames3 = _interopRequireDefault(_classnames2); require('./YYRotary.less'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 YYRotary = function (_Component) { _inherits(YYRotary, _Component); function YYRotary() { var _config; _classCallCheck(this, YYRotary); var _this2 = _possibleConstructorReturn(this, (YYRotary.__proto__ || Object.getPrototypeOf(YYRotary)).apply(this, arguments)); var config = (_config = { initPageX: 0 }, _defineProperty(_config, 'initPageX', 0), _defineProperty(_config, 'preAngle', 0), _defineProperty(_config, 'curAngle', 0), _defineProperty(_config, 'centerPointX', 0), _defineProperty(_config, 'centerPointY', 0), _defineProperty(_config, 'transferAngle', 0), _defineProperty(_config, 'currentDeg', 0), _config); Object.assign(_this2, config); return _this2; } _createClass(YYRotary, [{ key: 'componentDidMount', value: function componentDidMount() { var size = this.props.size; var _this = this; if (this.refs.YYRotary) { var rect = this.refs.YYRotary.getBoundingClientRect(); this.centerPointX = rect.x + size / 2; this.centerPointY = rect.y + size / 2; this.refs.YYRotary.addEventListener('touchstart', function (event) { var cX = event.touches[0].pageX; var cY = event.touches[0].pageY; _this.initPageX = cX - _this.centerPointX; _this.initPageY = cY - _this.centerPointY; _this.preAngle = Math.atan2(_this.initPageY, _this.initPageX); }); this.refs.YYRotary.addEventListener('touchmove', function (event) { var cX = event.touches[0].pageX; var cY = event.touches[0].pageY; _this.curAngle = Math.atan2(cY - _this.centerPointY, cX - _this.centerPointX); _this.transferAngle = _this.curAngle - _this.preAngle; var value = _this.transferAngle * 180 / Math.PI; _this.currentDeg = _this.currentDeg + value; _this.refs.YYRotary.style.transform = 'rotate(' + _this.currentDeg + 'deg)'; _this.initPageX = cX; _this.initPageY = cY; _this.preAngle = _this.curAngle; _this.props.rotaryCallback && _this.props.rotaryCallback(_this.currentDeg); }); } } }, { key: 'render', value: function render() { var _classnames; var _props = this.props, size = _props.size, borderRadius = _props.borderRadius, className = _props.className, type = _props.type, align = _props.align, borderColor = _props.borderColor, borderWidth = _props.borderWidth, borderStyle = _props.borderStyle, background = _props.background, backgroundColor = _props.backgroundColor, children = _props.children; var wrapClz = (0, _classnames3.default)((_classnames = { 'yy-rotary': true }, _defineProperty(_classnames, '' + type, true), _defineProperty(_classnames, '' + align, true), _defineProperty(_classnames, 'className', className), _classnames)); var rotaryStyle = { width: size + 'px', height: size + 'px', borderRadius: borderRadius, border: borderWidth + 'px ' + borderStyle + ' ' + borderColor, backgroundColor: background ? backgroundColor ? backgroundColor : borderColor : null }; if (align === 'right') { rotaryStyle = Object.assign(rotaryStyle, { left: 'calc(100% - ' + size + 'px)' }); } return _react2.default.createElement( 'div', { ref: 'YYRotary', className: wrapClz, style: rotaryStyle }, children ); } }]); return YYRotary; }(_react.Component); YYRotary.defaultProps = { size: 200, borderRadius: 'none', type: 'circular', borderColor: '#ccc', borderWidth: 1, backgroundColor: '#ccc', background: false, align: 'left', borderStyle: 'solid' }; module.exports = YYRotary;