UNPKG

8.72 kBJavaScriptView Raw
1var _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
3var _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
5function _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
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9function _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
11function _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// Copyright (c) 2016 - 2017 Uber Technologies, Inc.
14//
15// Permission is hereby granted, free of charge, to any person obtaining a copy
16// of this software and associated documentation files (the "Software"), to deal
17// in the Software without restriction, including without limitation the rights
18// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19// copies of the Software, and to permit persons to whom the Software is
20// furnished to do so, subject to the following conditions:
21//
22// The above copyright notice and this permission notice shall be included in
23// all copies or substantial portions of the Software.
24//
25// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31// THE SOFTWARE.
32
33import React from 'react';
34import window from 'global/window';
35
36import XYPlot from './plot/xy-plot';
37import { getDOMNode } from './utils/react-utils';
38
39var CONTAINER_REF = 'container';
40
41// As a performance enhancement, we want to only listen once
42var resizeSubscribers = [];
43var DEBOUNCE_DURATION = 100;
44var timeoutId = null;
45
46/**
47 * Calls each subscriber, debounced to the
48 */
49function debounceEmitResize() {
50 window.clearTimeout(timeoutId);
51 timeoutId = window.setTimeout(emitResize, DEBOUNCE_DURATION);
52}
53
54/**
55 * Calls each subscriber once syncronously.
56 */
57function emitResize() {
58 resizeSubscribers.forEach(function (cb) {
59 return cb();
60 });
61}
62
63/**
64 * Add the given callback to the list of subscribers to be caled when the
65 * window resizes. Returns a function that, when called, removes the given
66 * callback from the list of subscribers. This function is also resposible for
67 * adding and removing the resize listener on `window`.
68 *
69 * @param {Function} cb - Subscriber callback function
70 * @returns {Function} Unsubscribe function
71 */
72function subscribeToDebouncedResize(cb) {
73 resizeSubscribers.push(cb);
74
75 // if we go from zero to one Flexible components instances, add the listener
76 if (resizeSubscribers.length === 1) {
77 window.addEventListener('resize', debounceEmitResize);
78 }
79 return function unsubscribe() {
80 removeSubscriber(cb);
81
82 // if we have no Flexible components, remove the listener
83 if (resizeSubscribers.length === 0) {
84 window.clearTimeout(timeoutId);
85 window.removeEventListener('resize', debounceEmitResize);
86 }
87 };
88}
89
90/**
91 * Helper for removing the given callback from the list of subscribers.
92 *
93 * @param {Function} cb - Subscriber callback function
94 */
95function removeSubscriber(cb) {
96 var index = resizeSubscribers.indexOf(cb);
97 if (index > -1) {
98 resizeSubscribers.splice(index, 1);
99 }
100}
101
102/**
103 * Helper for getting a display name for the child component
104 * @param {*} Component React class for the child component.
105 * @returns {String} The child components name
106 */
107function getDisplayName(Component) {
108 return Component.displayName || Component.name || 'Component';
109}
110
111/**
112 * Add the ability to stretch the visualization on window resize.
113 * @param {*} Component React class for the child component.
114 * @returns {*} Flexible component.
115 */
116
117function 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']); // eslint-disable-line no-unused-vars
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 * Get the width of the container and assign the width.
150 * @private
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
213export function makeHeightFlexible(component) {
214 return makeFlexible(component, false, true);
215}
216
217export function makeVisFlexible(component) {
218 return makeFlexible(component, true, true);
219}
220
221export function makeWidthFlexible(component) {
222 return makeFlexible(component, true, false);
223}
224
225export var FlexibleWidthXYPlot = makeWidthFlexible(XYPlot);
226export var FlexibleHeightXYPlot = makeHeightFlexible(XYPlot);
227export var FlexibleXYPlot = makeVisFlexible(XYPlot);
\No newline at end of file