'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SliderBar = 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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _elementHelper = require('kitten/helpers/dom/element-helper');

var _grabberIcon = require('kitten/components/icons/grabber-icon');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; }

// DOM for the Slider component. See Slider for state and
var SliderBar = exports.SliderBar = function (_React$Component) {
  _inherits(SliderBar, _React$Component);

  function SliderBar(props) {
    _classCallCheck(this, SliderBar);

    var _this = _possibleConstructorReturn(this, (SliderBar.__proto__ || Object.getPrototypeOf(SliderBar)).call(this, props));

    _this.state = {
      grabbing: false
    };

    _this.handleMove = _this.handleMove.bind(_this);
    _this.handleStart = _this.handleStart.bind(_this);
    _this.handleEnd = _this.handleEnd.bind(_this);
    _this.handleClick = _this.handleClick.bind(_this);
    return _this;
  }

  // Allow other components to focus


  _createClass(SliderBar, [{
    key: 'focus',
    value: function focus() {
      this.refs.thumb.focus();
    }
  }, {
    key: 'handleClick',
    value: function handleClick(e) {
      this.props.onAction(e);
      this.setState({ grabbing: false });
      this.handleMove(e);
    }
  }, {
    key: 'handleStart',
    value: function handleStart(e) {
      this.props.onAction(e);
      e.stopPropagation();
      e.preventDefault();

      if (_elementHelper.domElementHelper.canUseDom()) {
        document.addEventListener('mousemove', this.handleMove);
        document.addEventListener('touchmove', this.handleMove);
        document.addEventListener('mouseup', this.handleEnd);
        document.addEventListener('touchend', this.handleEnd);
      }

      this.setState({ grabbing: true });
    }
  }, {
    key: 'handleEnd',
    value: function handleEnd() {
      if (_elementHelper.domElementHelper.canUseDom()) {
        document.removeEventListener('mousemove', this.handleMove);
        document.removeEventListener('touchmove', this.handleMove);
        document.removeEventListener('mouseup', this.handleEnd);
        document.removeEventListener('touchend', this.handleEnd);
      }

      this.setState({ grabbing: false });
    }
  }, {
    key: 'handleMove',
    value: function handleMove(e) {
      e.stopPropagation();
      e.preventDefault();

      var coordinate = e.touches ? e.touches[0].clientX : e.clientX;
      var trackPosition = this.refs.track.getBoundingClientRect();
      var ratio = (coordinate - trackPosition.left) / trackPosition.width;

      this.props.onMove(ratio);
    }
  }, {
    key: 'renderInput',
    value: function renderInput() {
      if (this.props.name) return _react2.default.createElement('input', {
        type: 'hidden',
        name: this.props.name,
        value: this.props.value || ''
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var percentage = this.props.ratio * 100 + '%';
      var trackStyles = { width: percentage };
      var thumbStyles = { left: percentage };
      var grabbingClass = this.state.grabbing ? 'is-grabbing' : null;

      return _react2.default.createElement(
        'div',
        {
          className: (0, _classnames2.default)('k-Slider', grabbingClass),
          onClick: this.handleClick
        },
        _react2.default.createElement(
          'div',
          { className: 'k-Slider__ramp', ref: 'track' },
          _react2.default.createElement('div', { className: 'k-Slider__progress', style: trackStyles }),
          _react2.default.createElement(
            'div',
            {
              className: 'k-Slider__handle',
              ref: 'thumb',
              style: thumbStyles,
              role: 'slider',
              tabIndex: '0',
              'aria-valuemin': this.props.min,
              'aria-valuemax': this.props.max,
              'aria-valuenow': this.props.value,
              onKeyDown: this.props.onKeyDown,
              onMouseDown: this.handleStart,
              onTouchStart: this.handleStart,
              onClick: this.handleClick,
              onFocus: this.props.onAction
            },
            _react2.default.createElement(_grabberIcon.GrabberIcon, {
              className: 'k-Slider__handleIcon',
              width: '32',
              height: '32'
            })
          )
        ),
        this.renderInput()
      );
    }
  }]);

  return SliderBar;
}(_react2.default.Component);