UNPKG

kitten-components

Version:
180 lines (146 loc) 6.77 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.SliderWithTooltipAndPower = undefined; 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; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _sliderBar = require('kitten/components/sliders/slider-bar'); var _sliderKeyDownHandler = require('kitten/handlers/sliders/slider-key-down-handler'); var _sliderTooltip = require('kitten/components/sliders/slider-tooltip'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } 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; } var SliderWithTooltipAndPower = exports.SliderWithTooltipAndPower = function (_React$Component) { _inherits(SliderWithTooltipAndPower, _React$Component); function SliderWithTooltipAndPower(props) { _classCallCheck(this, SliderWithTooltipAndPower); var _this = _possibleConstructorReturn(this, (SliderWithTooltipAndPower.__proto__ || Object.getPrototypeOf(SliderWithTooltipAndPower)).call(this, props)); _this.handleMove = _this.handleMove.bind(_this); _this.handleKeyDown = _sliderKeyDownHandler.sliderKeyDownHandler.bind(_this); return _this; } // Allow other components to focus _createClass(SliderWithTooltipAndPower, [{ key: 'focus', value: function focus() { this.refs.focus(); } }, { key: 'handleMove', value: function handleMove(ratio) { var _props = this.props, min = _props.min, max = _props.max; var powerRatio = this.computePowerRatio(ratio); var value = Math.round(powerRatio * (max - min) + min); this.move(value); } }, { key: 'ratio', value: function ratio() { return this.ratioForValue(this.props.value); } }, { key: 'move', value: function move(to) { var value = this.valueInBounds(to); var ratio = this.ratioForValue(value); this.setState({ ratio: ratio }); this.props.onChange(value, ratio); } // Turns a normal ratio (between 0 and 1) into a ratio with a different // distribution based on the power. // // Example: // computePowerRatio(0) # => 0 // computePowerRatio(0.5) # => 0.76534543 // computePowerRatio(1) # => 1 }, { key: 'computePowerRatio', value: function computePowerRatio(ratio) { return ratio < 0 ? 0 : Math.pow(ratio, this.props.power); } // Inverse of computePowerRatio. Turns a powered ratio (between 0 and 1) into // the ratio where we should place the slider. // // Example: // computeRatio(0) # => 0 // computeRatio(0.76534543) # => 0.5 // computeRatio(1) # => 1 }, { key: 'computeRatio', value: function computeRatio(powerRatio) { return Math.pow(powerRatio, 1 / this.props.power); } }, { key: 'ratioForValue', value: function ratioForValue(value) { var _props2 = this.props, min = _props2.min, max = _props2.max; if (value === null || isNaN(value)) return 0; var powerRatio = (value - min) / (max - min); return this.ratioInBounds(this.computeRatio(powerRatio)); } }, { key: 'ratioInBounds', value: function ratioInBounds(ratio) { return ratio > 1 ? 1 : ratio < 0 ? 0 : ratio; } }, { key: 'valueInBounds', value: function valueInBounds(value) { var _props3 = this.props, min = _props3.min, max = _props3.max, step = _props3.step; if (value === null) return min < max ? min : max; if (min < max) { if (value < min) return min;else if (value > max) return max; } else { if (value > min) return min;else if (value < max) return max; } return Math.round(value / step) * step; } }, { key: 'render', value: function render() { var _React$createElement; return _react2.default.createElement( 'div', null, _react2.default.createElement( _sliderTooltip.SliderTooltip, { className: this.props.tooltipClass, percentage: this.ratio() * 100 + '%' }, this.props.tooltipText ), _react2.default.createElement(_sliderBar.SliderBar, (_React$createElement = { onAction: this.props.onAction, onMove: this.props.onMove, name: this.props.name, value: this.props.value, min: this.props.min, max: this.props.max, ratio: this.ratio() }, _defineProperty(_React$createElement, 'onMove', this.handleMove), _defineProperty(_React$createElement, 'onStart', this.handleStart), _defineProperty(_React$createElement, 'onClick', this.handleClick), _defineProperty(_React$createElement, 'onAction', this.props.onAction), _defineProperty(_React$createElement, 'onKeyDown', this.handleKeyDown), _React$createElement)) ); } }]); return SliderWithTooltipAndPower; }(_react2.default.Component); SliderWithTooltipAndPower.defaultProps = { value: null, min: 0, max: 100, step: 1, power: 1, onChange: function onChange() {}, onChangeEnd: function onChangeEnd() {} };