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.
 
 
 
 

271 lines
8.2 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  8. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  9. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  10. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  11. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  12. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  13. var _createClass2 = require('babel-runtime/helpers/createClass');
  14. var _createClass3 = _interopRequireDefault(_createClass2);
  15. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  16. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  17. var _inherits2 = require('babel-runtime/helpers/inherits');
  18. var _inherits3 = _interopRequireDefault(_inherits2);
  19. var _simpleAssign = require('simple-assign');
  20. var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
  21. var _react = require('react');
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require('prop-types');
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _autoPrefix = require('../utils/autoPrefix');
  26. var _autoPrefix2 = _interopRequireDefault(_autoPrefix);
  27. var _transitions = require('../styles/transitions');
  28. var _transitions2 = _interopRequireDefault(_transitions);
  29. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  30. function getRelativeValue(value, min, max) {
  31. var clampedValue = Math.min(Math.max(min, value), max);
  32. return clampedValue / (max - min);
  33. }
  34. function getArcLength(fraction, props) {
  35. return fraction * Math.PI * (props.size - props.thickness);
  36. }
  37. function getStyles(props, context) {
  38. var max = props.max,
  39. min = props.min,
  40. size = props.size,
  41. value = props.value;
  42. var palette = context.muiTheme.baseTheme.palette;
  43. var styles = {
  44. root: {
  45. position: 'relative',
  46. display: 'inline-block',
  47. width: size,
  48. height: size
  49. },
  50. wrapper: {
  51. width: size,
  52. height: size,
  53. display: 'inline-block',
  54. transition: _transitions2.default.create('transform', '20s', null, 'linear'),
  55. transitionTimingFunction: 'linear'
  56. },
  57. svg: {
  58. width: size,
  59. height: size,
  60. position: 'relative'
  61. },
  62. path: {
  63. stroke: props.color || palette.primary1Color,
  64. strokeLinecap: 'round',
  65. transition: _transitions2.default.create('all', '1.5s', null, 'ease-in-out')
  66. }
  67. };
  68. if (props.mode === 'determinate') {
  69. var relVal = getRelativeValue(value, min, max);
  70. styles.path.transition = _transitions2.default.create('all', '0.3s', null, 'linear');
  71. styles.path.strokeDasharray = getArcLength(relVal, props) + ', ' + getArcLength(1, props);
  72. }
  73. return styles;
  74. }
  75. var CircularProgress = function (_Component) {
  76. (0, _inherits3.default)(CircularProgress, _Component);
  77. function CircularProgress() {
  78. (0, _classCallCheck3.default)(this, CircularProgress);
  79. return (0, _possibleConstructorReturn3.default)(this, (CircularProgress.__proto__ || (0, _getPrototypeOf2.default)(CircularProgress)).apply(this, arguments));
  80. }
  81. (0, _createClass3.default)(CircularProgress, [{
  82. key: 'componentDidMount',
  83. value: function componentDidMount() {
  84. this.scalePath(this.refs.path);
  85. this.rotateWrapper(this.refs.wrapper);
  86. }
  87. }, {
  88. key: 'componentWillUnmount',
  89. value: function componentWillUnmount() {
  90. clearTimeout(this.scalePathTimer);
  91. clearTimeout(this.rotateWrapperTimer);
  92. }
  93. }, {
  94. key: 'scalePath',
  95. value: function scalePath(path) {
  96. var _this2 = this;
  97. var step = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  98. if (this.props.mode !== 'indeterminate') return;
  99. step %= 3;
  100. if (step === 0) {
  101. path.style.strokeDasharray = getArcLength(0, this.props) + ', ' + getArcLength(1, this.props);
  102. path.style.strokeDashoffset = 0;
  103. path.style.transitionDuration = '0ms';
  104. } else if (step === 1) {
  105. path.style.strokeDasharray = getArcLength(0.7, this.props) + ', ' + getArcLength(1, this.props);
  106. path.style.strokeDashoffset = getArcLength(-0.3, this.props);
  107. path.style.transitionDuration = '750ms';
  108. } else {
  109. path.style.strokeDasharray = getArcLength(0.7, this.props) + ', ' + getArcLength(1, this.props);
  110. path.style.strokeDashoffset = getArcLength(-1, this.props);
  111. path.style.transitionDuration = '850ms';
  112. }
  113. this.scalePathTimer = setTimeout(function () {
  114. return _this2.scalePath(path, step + 1);
  115. }, step ? 750 : 250);
  116. }
  117. }, {
  118. key: 'rotateWrapper',
  119. value: function rotateWrapper(wrapper) {
  120. var _this3 = this;
  121. if (this.props.mode !== 'indeterminate') return;
  122. _autoPrefix2.default.set(wrapper.style, 'transform', 'rotate(0deg)');
  123. _autoPrefix2.default.set(wrapper.style, 'transitionDuration', '0ms');
  124. setTimeout(function () {
  125. _autoPrefix2.default.set(wrapper.style, 'transform', 'rotate(1800deg)');
  126. _autoPrefix2.default.set(wrapper.style, 'transitionDuration', '10s');
  127. _autoPrefix2.default.set(wrapper.style, 'transitionTimingFunction', 'linear');
  128. }, 50);
  129. this.rotateWrapperTimer = setTimeout(function () {
  130. return _this3.rotateWrapper(wrapper);
  131. }, 10050);
  132. }
  133. }, {
  134. key: 'render',
  135. value: function render() {
  136. var _props = this.props,
  137. style = _props.style,
  138. innerStyle = _props.innerStyle,
  139. size = _props.size,
  140. thickness = _props.thickness,
  141. other = (0, _objectWithoutProperties3.default)(_props, ['style', 'innerStyle', 'size', 'thickness']);
  142. var prepareStyles = this.context.muiTheme.prepareStyles;
  143. var styles = getStyles(this.props, this.context);
  144. return _react2.default.createElement(
  145. 'div',
  146. (0, _extends3.default)({}, other, { style: prepareStyles((0, _simpleAssign2.default)(styles.root, style)) }),
  147. _react2.default.createElement(
  148. 'div',
  149. { ref: 'wrapper', style: prepareStyles((0, _simpleAssign2.default)(styles.wrapper, innerStyle)) },
  150. _react2.default.createElement(
  151. 'svg',
  152. {
  153. viewBox: '0 0 ' + size + ' ' + size,
  154. style: prepareStyles(styles.svg)
  155. },
  156. _react2.default.createElement('circle', {
  157. ref: 'path',
  158. style: prepareStyles(styles.path),
  159. cx: size / 2,
  160. cy: size / 2,
  161. r: (size - thickness) / 2,
  162. fill: 'none',
  163. strokeWidth: thickness,
  164. strokeMiterlimit: '20'
  165. })
  166. )
  167. )
  168. );
  169. }
  170. }]);
  171. return CircularProgress;
  172. }(_react.Component);
  173. CircularProgress.defaultProps = {
  174. mode: 'indeterminate',
  175. value: 0,
  176. min: 0,
  177. max: 100,
  178. size: 40,
  179. thickness: 3.5
  180. };
  181. CircularProgress.contextTypes = {
  182. muiTheme: _propTypes2.default.object.isRequired
  183. };
  184. CircularProgress.propTypes = process.env.NODE_ENV !== "production" ? {
  185. /**
  186. * Override the progress's color.
  187. */
  188. color: _propTypes2.default.string,
  189. /**
  190. * Style for inner wrapper div.
  191. */
  192. innerStyle: _propTypes2.default.object,
  193. /**
  194. * The max value of progress, only works in determinate mode.
  195. */
  196. max: _propTypes2.default.number,
  197. /**
  198. * The min value of progress, only works in determinate mode.
  199. */
  200. min: _propTypes2.default.number,
  201. /**
  202. * The mode of show your progress, indeterminate
  203. * for when there is no value for progress.
  204. */
  205. mode: _propTypes2.default.oneOf(['determinate', 'indeterminate']),
  206. /**
  207. * The diameter of the progress in pixels.
  208. */
  209. size: _propTypes2.default.number,
  210. /**
  211. * Override the inline-styles of the root element.
  212. */
  213. style: _propTypes2.default.object,
  214. /**
  215. * Stroke width in pixels.
  216. */
  217. thickness: _propTypes2.default.number,
  218. /**
  219. * The value of progress, only works in determinate mode.
  220. */
  221. value: _propTypes2.default.number
  222. } : {};
  223. exports.default = CircularProgress;