You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

231 lines
8.6 KiB

  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. 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; }; }();
  3. 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; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. 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; }
  6. 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; }
  7. // Copyright (c) 2016 - 2017 Uber Technologies, Inc.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import React from 'react';
  27. import window from 'global/window';
  28. import XYPlot from './plot/xy-plot';
  29. import { getDOMNode } from './utils/react-utils';
  30. var CONTAINER_REF = 'container';
  31. // As a performance enhancement, we want to only listen once
  32. var resizeSubscribers = [];
  33. var DEBOUNCE_DURATION = 100;
  34. var timeoutId = null;
  35. /**
  36. * Calls each subscriber, debounced to the
  37. */
  38. function debounceEmitResize() {
  39. window.clearTimeout(timeoutId);
  40. timeoutId = window.setTimeout(emitResize, DEBOUNCE_DURATION);
  41. }
  42. /**
  43. * Calls each subscriber once syncronously.
  44. */
  45. function emitResize() {
  46. resizeSubscribers.forEach(function (cb) {
  47. return cb();
  48. });
  49. }
  50. /**
  51. * Add the given callback to the list of subscribers to be caled when the
  52. * window resizes. Returns a function that, when called, removes the given
  53. * callback from the list of subscribers. This function is also resposible for
  54. * adding and removing the resize listener on `window`.
  55. *
  56. * @param {Function} cb - Subscriber callback function
  57. * @returns {Function} Unsubscribe function
  58. */
  59. function subscribeToDebouncedResize(cb) {
  60. resizeSubscribers.push(cb);
  61. // if we go from zero to one Flexible components instances, add the listener
  62. if (resizeSubscribers.length === 1) {
  63. window.addEventListener('resize', debounceEmitResize);
  64. }
  65. return function unsubscribe() {
  66. removeSubscriber(cb);
  67. // if we have no Flexible components, remove the listener
  68. if (resizeSubscribers.length === 0) {
  69. window.clearTimeout(timeoutId);
  70. window.removeEventListener('resize', debounceEmitResize);
  71. }
  72. };
  73. }
  74. /**
  75. * Helper for removing the given callback from the list of subscribers.
  76. *
  77. * @param {Function} cb - Subscriber callback function
  78. */
  79. function removeSubscriber(cb) {
  80. var index = resizeSubscribers.indexOf(cb);
  81. if (index > -1) {
  82. resizeSubscribers.splice(index, 1);
  83. }
  84. }
  85. /**
  86. * Helper for getting a display name for the child component
  87. * @param {*} Component React class for the child component.
  88. * @returns {String} The child components name
  89. */
  90. function getDisplayName(Component) {
  91. return Component.displayName || Component.name || 'Component';
  92. }
  93. /**
  94. * Add the ability to stretch the visualization on window resize.
  95. * @param {*} Component React class for the child component.
  96. * @returns {*} Flexible component.
  97. */
  98. function makeFlexible(Component, isWidthFlexible, isHeightFlexible) {
  99. var ResultClass = function (_React$Component) {
  100. _inherits(ResultClass, _React$Component);
  101. _createClass(ResultClass, null, [{
  102. key: 'propTypes',
  103. get: function get() {
  104. var _Component$propTypes = Component.propTypes,
  105. height = _Component$propTypes.height,
  106. width = _Component$propTypes.width,
  107. otherPropTypes = _objectWithoutProperties(_Component$propTypes, ['height', 'width']); // eslint-disable-line no-unused-vars
  108. return otherPropTypes;
  109. }
  110. }]);
  111. function ResultClass(props) {
  112. _classCallCheck(this, ResultClass);
  113. var _this = _possibleConstructorReturn(this, (ResultClass.__proto__ || Object.getPrototypeOf(ResultClass)).call(this, props));
  114. _this._onResize = function () {
  115. var containerElement = getDOMNode(_this[CONTAINER_REF]);
  116. var offsetHeight = containerElement.offsetHeight,
  117. offsetWidth = containerElement.offsetWidth;
  118. var newHeight = _this.state.height === offsetHeight ? {} : { height: offsetHeight };
  119. var newWidth = _this.state.width === offsetWidth ? {} : { width: offsetWidth };
  120. _this.setState(_extends({}, newHeight, newWidth));
  121. };
  122. _this.state = {
  123. height: 0,
  124. width: 0
  125. };
  126. return _this;
  127. }
  128. /**
  129. * Get the width of the container and assign the width.
  130. * @private
  131. */
  132. _createClass(ResultClass, [{
  133. key: 'componentDidMount',
  134. value: function componentDidMount() {
  135. this._onResize();
  136. this.cancelSubscription = subscribeToDebouncedResize(this._onResize);
  137. }
  138. }, {
  139. key: 'componentWillReceiveProps',
  140. value: function componentWillReceiveProps() {
  141. this._onResize();
  142. }
  143. }, {
  144. key: 'componentWillUnmount',
  145. value: function componentWillUnmount() {
  146. this.cancelSubscription();
  147. }
  148. }, {
  149. key: 'render',
  150. value: function render() {
  151. var _this2 = this;
  152. var _state = this.state,
  153. height = _state.height,
  154. width = _state.width;
  155. var props = _extends({}, this.props, {
  156. animation: height === 0 && width === 0 ? null : this.props.animation
  157. });
  158. var updatedDimensions = _extends({}, isHeightFlexible ? { height: height } : {}, isWidthFlexible ? { width: width } : {});
  159. return React.createElement(
  160. 'div',
  161. {
  162. ref: function ref(_ref) {
  163. return _this2[CONTAINER_REF] = _ref;
  164. },
  165. style: { width: '100%', height: '100%' }
  166. },
  167. React.createElement(Component, _extends({}, updatedDimensions, props))
  168. );
  169. }
  170. }]);
  171. return ResultClass;
  172. }(React.Component);
  173. ResultClass.displayName = 'Flexible' + getDisplayName(Component);
  174. return ResultClass;
  175. }
  176. export function makeHeightFlexible(component) {
  177. return makeFlexible(component, false, true);
  178. }
  179. export function makeVisFlexible(component) {
  180. return makeFlexible(component, true, true);
  181. }
  182. export function makeWidthFlexible(component) {
  183. return makeFlexible(component, true, false);
  184. }
  185. export var FlexibleWidthXYPlot = makeWidthFlexible(XYPlot);
  186. export var FlexibleHeightXYPlot = makeHeightFlexible(XYPlot);
  187. export var FlexibleXYPlot = makeVisFlexible(XYPlot);