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.
 
 
 
 

92 lines
3.4 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. // Copyright (c) 2016 - 2017 Uber Technologies, Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. import PropTypes from 'prop-types';
  22. /**
  23. * Get the dimensions of the component for the future use.
  24. * @param {Object} props Props.
  25. * @param {Object} defaultMargins Object with default margins.
  26. * @returns {Object} Dimensions of the component.
  27. */
  28. export function getInnerDimensions(props, defaultMargins) {
  29. var margin = props.margin,
  30. width = props.width,
  31. height = props.height;
  32. var marginProps = _extends({}, defaultMargins, typeof margin === 'number' ? {
  33. left: margin,
  34. right: margin,
  35. top: margin,
  36. bottom: margin
  37. } : margin);
  38. var _marginProps$left = marginProps.left,
  39. marginLeft = _marginProps$left === undefined ? 0 : _marginProps$left,
  40. _marginProps$top = marginProps.top,
  41. marginTop = _marginProps$top === undefined ? 0 : _marginProps$top,
  42. _marginProps$right = marginProps.right,
  43. marginRight = _marginProps$right === undefined ? 0 : _marginProps$right,
  44. _marginProps$bottom = marginProps.bottom,
  45. marginBottom = _marginProps$bottom === undefined ? 0 : _marginProps$bottom;
  46. return {
  47. marginLeft: marginLeft,
  48. marginTop: marginTop,
  49. marginRight: marginRight,
  50. marginBottom: marginBottom,
  51. innerHeight: height - marginBottom - marginTop,
  52. innerWidth: width - marginLeft - marginRight
  53. };
  54. }
  55. /**
  56. * Calculate the margin of the sunburst,
  57. * so it can be at the center of the container
  58. * @param {Number} width - the width of the container
  59. * @param {Number} height - the height of the container
  60. * @param {Number} radius - the max radius of the sunburst
  61. * @return {Object} an object includes {bottom, left, right, top}
  62. */
  63. export function getRadialLayoutMargin(width, height, radius) {
  64. var marginX = width / 2 - radius;
  65. var marginY = height / 2 - radius;
  66. return {
  67. bottom: marginY,
  68. left: marginX,
  69. right: marginX,
  70. top: marginY
  71. };
  72. }
  73. export var MarginPropType = PropTypes.oneOfType([PropTypes.shape({
  74. left: PropTypes.number,
  75. top: PropTypes.number,
  76. right: PropTypes.number,
  77. bottom: PropTypes.number
  78. }), PropTypes.number]);
  79. export var DEFAULT_MARGINS = {
  80. left: 40,
  81. right: 10,
  82. top: 10,
  83. bottom: 40
  84. };