1 | 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; };
|
2 |
|
3 | 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; }; }();
|
4 |
|
5 | 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; }
|
6 |
|
7 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
8 |
|
9 | 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; }
|
10 |
|
11 | 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; }
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | import React from 'react';
|
34 | import window from 'global/window';
|
35 |
|
36 | import XYPlot from './plot/xy-plot';
|
37 | import { getDOMNode } from './utils/react-utils';
|
38 |
|
39 | var CONTAINER_REF = 'container';
|
40 |
|
41 |
|
42 | var resizeSubscribers = [];
|
43 | var DEBOUNCE_DURATION = 100;
|
44 | var timeoutId = null;
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function debounceEmitResize() {
|
50 | window.clearTimeout(timeoutId);
|
51 | timeoutId = window.setTimeout(emitResize, DEBOUNCE_DURATION);
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function emitResize() {
|
58 | resizeSubscribers.forEach(function (cb) {
|
59 | return cb();
|
60 | });
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function subscribeToDebouncedResize(cb) {
|
73 | resizeSubscribers.push(cb);
|
74 |
|
75 |
|
76 | if (resizeSubscribers.length === 1) {
|
77 | window.addEventListener('resize', debounceEmitResize);
|
78 | }
|
79 | return function unsubscribe() {
|
80 | removeSubscriber(cb);
|
81 |
|
82 |
|
83 | if (resizeSubscribers.length === 0) {
|
84 | window.clearTimeout(timeoutId);
|
85 | window.removeEventListener('resize', debounceEmitResize);
|
86 | }
|
87 | };
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | function removeSubscriber(cb) {
|
96 | var index = resizeSubscribers.indexOf(cb);
|
97 | if (index > -1) {
|
98 | resizeSubscribers.splice(index, 1);
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | function getDisplayName(Component) {
|
108 | return Component.displayName || Component.name || 'Component';
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function makeFlexible(Component, isWidthFlexible, isHeightFlexible) {
|
118 |
|
119 | var ResultClass = function (_React$Component) {
|
120 | _inherits(ResultClass, _React$Component);
|
121 |
|
122 | _createClass(ResultClass, null, [{
|
123 | key: 'propTypes',
|
124 | get: function get() {
|
125 | var _Component$propTypes = Component.propTypes,
|
126 | height = _Component$propTypes.height,
|
127 | width = _Component$propTypes.width,
|
128 | otherPropTypes = _objectWithoutProperties(_Component$propTypes, ['height', 'width']);
|
129 |
|
130 |
|
131 | return otherPropTypes;
|
132 | }
|
133 | }]);
|
134 |
|
135 | function ResultClass(props) {
|
136 | _classCallCheck(this, ResultClass);
|
137 |
|
138 | var _this = _possibleConstructorReturn(this, (ResultClass.__proto__ || Object.getPrototypeOf(ResultClass)).call(this, props));
|
139 |
|
140 | _this.state = {
|
141 | height: 0,
|
142 | width: 0
|
143 | };
|
144 | _this._onResize = _this._onResize.bind(_this);
|
145 | return _this;
|
146 | }
|
147 |
|
148 | |
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | _createClass(ResultClass, [{
|
155 | key: '_onResize',
|
156 | value: function _onResize() {
|
157 | var containerElement = getDOMNode(this.refs[CONTAINER_REF]);
|
158 | var offsetHeight = containerElement.offsetHeight,
|
159 | offsetWidth = containerElement.offsetWidth;
|
160 |
|
161 |
|
162 | var newHeight = this.state.height === offsetHeight ? {} : { height: offsetHeight };
|
163 |
|
164 | var newWidth = this.state.width === offsetWidth ? {} : { width: offsetWidth };
|
165 |
|
166 | this.setState(_extends({}, newHeight, newWidth));
|
167 | }
|
168 | }, {
|
169 | key: 'componentDidMount',
|
170 | value: function componentDidMount() {
|
171 | this._onResize();
|
172 | this.cancelSubscription = subscribeToDebouncedResize(this._onResize);
|
173 | }
|
174 | }, {
|
175 | key: 'componentWillReceiveProps',
|
176 | value: function componentWillReceiveProps() {
|
177 | this._onResize();
|
178 | }
|
179 | }, {
|
180 | key: 'componentWillUnmount',
|
181 | value: function componentWillUnmount() {
|
182 | this.cancelSubscription();
|
183 | }
|
184 | }, {
|
185 | key: 'render',
|
186 | value: function render() {
|
187 | var _state = this.state,
|
188 | height = _state.height,
|
189 | width = _state.width;
|
190 |
|
191 | var props = _extends({}, this.props, { animation: height === 0 && width === 0 ? null : this.props.animation });
|
192 |
|
193 | var updatedDimensions = _extends({}, isHeightFlexible ? { height: height } : {}, isWidthFlexible ? { width: width } : {});
|
194 |
|
195 | return React.createElement(
|
196 | 'div',
|
197 | {
|
198 | ref: CONTAINER_REF,
|
199 | style: { width: '100%', height: '100%' } },
|
200 | React.createElement(Component, _extends({}, updatedDimensions, props))
|
201 | );
|
202 | }
|
203 | }]);
|
204 |
|
205 | return ResultClass;
|
206 | }(React.Component);
|
207 |
|
208 | ResultClass.displayName = 'Flexible' + getDisplayName(Component);
|
209 |
|
210 | return ResultClass;
|
211 | }
|
212 |
|
213 | export function makeHeightFlexible(component) {
|
214 | return makeFlexible(component, false, true);
|
215 | }
|
216 |
|
217 | export function makeVisFlexible(component) {
|
218 | return makeFlexible(component, true, true);
|
219 | }
|
220 |
|
221 | export function makeWidthFlexible(component) {
|
222 | return makeFlexible(component, true, false);
|
223 | }
|
224 |
|
225 | export var FlexibleWidthXYPlot = makeWidthFlexible(XYPlot);
|
226 | export var FlexibleHeightXYPlot = makeHeightFlexible(XYPlot);
|
227 | export var FlexibleXYPlot = makeVisFlexible(XYPlot); |
\ | No newline at end of file |