UNPKG

6.55 kBJavaScriptView Raw
1'use strict';
2
3var _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; };
4
5var _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; }; }();
6
7var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8
9var _g2PluginSlider = require('@antv/g2-plugin-slider');
10
11var _g2PluginSlider2 = _interopRequireDefault(_g2PluginSlider);
12
13var _react = require('react');
14
15var _react2 = _interopRequireDefault(_react);
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19function _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; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23function _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; }
24
25function _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; }
26
27var SliderAttrs = ['width', 'height', 'padding', 'xAis', 'yAxis', 'start', 'end', 'fillerStyle', 'backgroundStyle', 'scales', 'textStyle', 'handleStyle', 'backgroundChart'];
28
29function isArray(obj) {
30 return Array.isArray(obj);
31}
32function isObject(obj) {
33 return (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && obj !== null;
34}
35
36function length(obj) {
37 if (isArray(obj)) {
38 return obj.length;
39 } else if (isObject(obj)) {
40 return Object.keys(obj).length;
41 }
42
43 return 0;
44}
45
46function is(x, y) {
47 if (x === y) {
48 return x !== 0 || y !== 0 || 1 / x === 1 / y;
49 }
50 return x !== x && y !== y; // NaN == NaN
51}
52
53function each(obj, fun) {
54 if (obj.forEach) {
55 obj.forEach(fun);
56 }
57 if (isObject(obj)) {
58 Object.keys(obj).forEach(function (key) {
59 fun(obj[key], key);
60 });
61 }
62}
63
64function shallowEqual(objA, objB) {
65 if (is(objA, objB)) {
66 return true;
67 }
68
69 if ((typeof objA === 'undefined' ? 'undefined' : _typeof(objA)) !== 'object' || objA === null || (typeof objB === 'undefined' ? 'undefined' : _typeof(objB)) !== 'object' || objB === null) {
70 return false;
71 }
72
73 if (isArray(objA) !== isArray(objB)) {
74 return false;
75 }
76
77 if (length(objA) !== length(objB)) {
78 return false;
79 }
80
81 var ret = true;
82
83 each(objA, function (v, k) {
84 if (!is(v, objB[k])) {
85 return ret = false;
86 }
87 return true;
88 });
89
90 return ret;
91}
92
93function sliderNeedRebuild(props, nextProps) {
94 if (props.onChange !== nextProps.onChange) {
95 return true;
96 }
97
98 for (var i = 0; i < SliderAttrs.length; i += 1) {
99 var attr = SliderAttrs[i];
100 if (!shallowEqual(props[attr], nextProps[attr])) {
101 console.log('need rebuild');
102 return true;
103 }
104 }
105
106 return false;
107}
108
109var Slider = function (_Component) {
110 _inherits(Slider, _Component);
111
112 function Slider() {
113 var _ref;
114
115 var _temp, _this, _ret;
116
117 _classCallCheck(this, Slider);
118
119 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
120 args[_key] = arguments[_key];
121 }
122
123 return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Slider.__proto__ || Object.getPrototypeOf(Slider)).call.apply(_ref, [this].concat(args))), _this), _this.refHandle = function (cw) {
124 // chart container wrap for reset operation
125 if (!_this.container) {
126 _this.container = cw;
127 }
128 }, _temp), _possibleConstructorReturn(_this, _ret);
129 }
130
131 _createClass(Slider, [{
132 key: 'componentDidMount',
133 value: function componentDidMount() {
134 var slider = this.createG2Instance();
135 slider.render();
136 }
137 }, {
138 key: 'componentWillReceiveProps',
139 value: function componentWillReceiveProps(nextProps) {
140 var _props = this.props,
141 data = _props.data,
142 others = _objectWithoutProperties(_props, ['data']);
143
144 var nextData = nextProps.data,
145 nextOthers = _objectWithoutProperties(nextProps, ['data']);
146 // refrence compare
147
148
149 if (data !== nextData) {
150 this.slider.changeData(nextData);
151 this.slider.repaint();
152 }
153
154 if (sliderNeedRebuild(others, nextOthers)) {
155 this.reBuild = true;
156 }
157 }
158 }, {
159 key: 'componentDidUpdate',
160 value: function componentDidUpdate() {
161 if (!this.reBuild) {
162 return;
163 }
164
165 this.slider.destroy();
166 var slider = this.createG2Instance();
167 slider.render();
168 this.reBuild = false;
169 }
170 }, {
171 key: 'componentWillUnmount',
172 value: function componentWillUnmount() {
173 this.slider.destroy();
174 }
175 }, {
176 key: 'createG2Instance',
177 value: function createG2Instance() {
178 this.slider = new _g2PluginSlider2.default(_extends({ container: this.container }, this.props));
179 return this.slider;
180 }
181 }, {
182 key: 'render',
183 value: function render() {
184 return _react2.default.createElement('div', { ref: this.refHandle });
185 }
186 }]);
187
188 return Slider;
189}(_react.Component);
190
191exports.default = Slider;
192module.exports = exports['default'];
\No newline at end of file