data-vis-ui
Version:
## [使用文档](https://temp-static-domain.jd.com/data-vis-ui)
223 lines (189 loc) • 9.52 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/*
* @Author: 石丽丽
* @description : 滚动
* @@copyRight: 京东科技IOT
*/
import React from 'react';
var DEFAULT_PROPS = {
fps: 60,
speed: 30,
style: {},
className: '',
reverse: false,
mode: 'vertical'
};
var Scroll = /*#__PURE__*/function (_React$PureComponent) {
_inherits(Scroll, _React$PureComponent);
var _super = _createSuper(Scroll);
function Scroll() {
var _this;
_classCallCheck(this, Scroll);
_this = _super.apply(this, arguments);
_this.containerDom = null;
_this.wrapperDom = null;
_this.sliderDom = null;
_this.sliderDomCopy = null;
_this.requestID = 0;
_this.state = {
step: 1,
sliderSize: 0,
containerSize: 0
};
_this.init = function () {
var _this$props = _this.props,
_this$props$speed = _this$props.speed,
speed = _this$props$speed === void 0 ? DEFAULT_PROPS.speed : _this$props$speed,
_this$props$mode = _this$props.mode,
mode = _this$props$mode === void 0 ? DEFAULT_PROPS.mode : _this$props$mode;
var step = parseFloat((speed / DEFAULT_PROPS.fps).toFixed(2));
var sliderSize = mode === 'vertical' ? _this.sliderDom.offsetHeight : _this.sliderDom.offsetWidth;
var containerSize = mode === 'vertical' ? _this.containerDom.clientHeight : _this.containerDom.clientWidth;
_this.setState({
step: step,
sliderSize: sliderSize,
containerSize: containerSize
}, function () {
cancelAnimationFrame(_this.requestID);
if (sliderSize > containerSize) {
_this.sliderDomCopy.innerHTML = _this.sliderDom.innerHTML;
_this.requestID = requestAnimationFrame(_this.marquee);
_this.containerDom.onmouseover = function () {
cancelAnimationFrame(_this.requestID);
};
_this.containerDom.onmouseout = function () {
cancelAnimationFrame(_this.requestID);
_this.requestID = requestAnimationFrame(_this.marquee);
};
} else {
_this.sliderDomCopy.innerHTML = '';
_this.containerDom.onmouseover = null;
_this.containerDom.onmouseout = null;
}
});
};
/**marquee */
_this.marquee = function () {
var _this$state = _this.state,
sliderSize = _this$state.sliderSize,
step = _this$state.step;
var _this$props2 = _this.props,
_this$props2$reverse = _this$props2.reverse,
reverse = _this$props2$reverse === void 0 ? DEFAULT_PROPS.reverse : _this$props2$reverse,
_this$props2$mode = _this$props2.mode,
mode = _this$props2$mode === void 0 ? DEFAULT_PROPS.mode : _this$props2$mode;
var key = mode === 'vertical' ? 'top' : 'left';
var position = parseFloat(_this.getStyle(_this.wrapperDom, key));
if (reverse) {
if (position >= 0) {
_this.wrapperDom.style[key] = "-".concat(sliderSize, "px");
} else {
_this.wrapperDom.style[key] = "".concat(position + step, "px");
}
} else {
if (sliderSize - Math.abs(position) <= 0) {
_this.wrapperDom.style[key] = '0px';
} else {
_this.wrapperDom.style[key] = "".concat(position - step, "px");
}
}
cancelAnimationFrame(_this.requestID);
_this.requestID = requestAnimationFrame(_this.marquee);
};
_this.getStyle = function (ele, attr) {
if (window.getComputedStyle) {
return window.getComputedStyle(ele)[attr];
}
return ele.currentStyle[attr];
};
return _this;
}
_createClass(Scroll, [{
key: "componentDidMount",
value: function componentDidMount() {
this.init();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props3 = this.props,
children = _this$props3.children,
speed = _this$props3.speed,
style = _this$props3.style,
className = _this$props3.className,
mode = _this$props3.mode;
if (children !== prevProps.children || speed !== prevProps.speed || style !== prevProps.style || className !== prevProps.className || mode !== prevProps.mode) {
this.init();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
cancelAnimationFrame(this.requestID);
} // 检测dom宽高变更
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props4 = this.props,
children = _this$props4.children,
_this$props4$style = _this$props4.style,
style = _this$props4$style === void 0 ? DEFAULT_PROPS.style : _this$props4$style,
_this$props4$classNam = _this$props4.className,
className = _this$props4$classNam === void 0 ? DEFAULT_PROPS.className : _this$props4$classNam,
_this$props4$mode = _this$props4.mode,
mode = _this$props4$mode === void 0 ? DEFAULT_PROPS.mode : _this$props4$mode;
var containerStyle = {
position: 'relative',
overflow: 'hidden',
width: '100%',
height: '100%'
};
var WrapperStyle = {
position: 'absolute',
top: 0,
left: 0,
right: mode === 'vertical' ? 0 : 'auto',
bottom: mode === 'vertical' ? 'auto' : 0,
whiteSpace: mode === 'vertical' ? 'normal' : 'nowrap'
};
var sliderStyle = {
display: mode === 'vertical' ? 'block' : 'inline-block'
};
return /*#__PURE__*/React.createElement("div", {
className: className,
ref: function ref(dom) {
return _this2.containerDom = dom;
},
style: Object.assign(Object.assign({}, containerStyle), style)
}, /*#__PURE__*/React.createElement("div", {
ref: function ref(dom) {
return _this2.wrapperDom = dom;
},
style: WrapperStyle
}, /*#__PURE__*/React.createElement("div", {
style: sliderStyle,
ref: function ref(dom) {
return _this2.sliderDom = dom;
}
}, children), /*#__PURE__*/React.createElement("div", {
style: sliderStyle,
ref: function ref(dom) {
return _this2.sliderDomCopy = dom;
}
})));
}
}]);
return Scroll;
}(React.PureComponent);
export default Scroll;