'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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 _BaseComponent2 = require('../BaseComponent');

var _BaseComponent3 = _interopRequireDefault(_BaseComponent2);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames9 = require('classnames');

var _classnames10 = _interopRequireDefault(_classnames9);

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 Drawer = function (_BaseComponent) {
  _inherits(Drawer, _BaseComponent);

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

    var _this = _possibleConstructorReturn(this, (Drawer.__proto__ || Object.getPrototypeOf(Drawer)).call(this, props, { name: 'Drawer' }));

    _this.state = {
      open: props.open || false,
      docked: props.docked || false
    };
    return _this;
  }

  _createClass(Drawer, [{
    key: 'isOpen',
    value: function isOpen() {
      return this.state.open;
    }
  }, {
    key: 'toggle',
    value: function toggle() {
      if (this.isOpen()) {
        this._close();
      } else {
        this._open();
      }
    }
  }, {
    key: '_open',
    value: function _open() {
      this._log('open', this.state);
      this.setState({ open: true });
    }
  }, {
    key: '_close',
    value: function _close() {
      this._log('close', this.state);
      this.setState({ open: false });
      this.sideNavContent.style.transform = '';
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      this.hasUnprefixedTransform = 'transform' in document.documentElement.style;
      if (this.hasUnprefixedTransform) {
        this._setupTouchHandlers();
      }
      if (this.sideNavContent) {
        this.sideNavContent.addEventListener('click', function (e) {
          if (!_this2.state.docked) {
            _this2._close();
          }
        });
      }
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.setState(nextProps);
    }
  }, {
    key: '_setupTouchHandlers',
    value: function _setupTouchHandlers() {
      this.touchStartX = null;
      this.sideNavTransform = null;
      if (this.sideNavContent) {
        this.sideNavContent.addEventListener('touchstart', this.onSideNavTouchStart.bind(this));
        this.sideNavContent.addEventListener('touchmove', this.onSideNavTouchMove.bind(this));
        this.sideNavContent.addEventListener('touchend', this.onSideNavTouchEnd.bind(this));
      }
    }
  }, {
    key: 'onSideNavTouchStart',
    value: function onSideNavTouchStart(e) {
      this.touchStartX = e.touches[0].pageX;
    }
  }, {
    key: 'onSideNavTouchMove',
    value: function onSideNavTouchMove(e) {
      e.preventDefault();
      var newTouchX = e.touches[0].pageX;
      this.sideNavTransform = Math.min(0, newTouchX - this.touchStartX);
      this.sideNavContent.style.transform = 'translateX(' + this.sideNavTransform + 'px)';
    }
  }, {
    key: 'onSideNavTouchEnd',
    value: function onSideNavTouchEnd(e) {
      var TOUCH_SLOP = 8 * window.devicePixelRatio * 3;
      if (this.sideNavTransform < -TOUCH_SLOP) {
        this._close();
        return;
      }
      this._open();
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;

      var _props = this.props,
          style = _props.style,
          fixed = _props.fixed,
          _props$className = _props.className,
          className = _props$className === undefined ? 'drawer' : _props$className,
          containerClassName = _props.containerClassName,
          overlay = _props.overlay,
          _props$overlayClassNa = _props.overlayClassName,
          overlayClassName = _props$overlayClassNa === undefined ? 'drawer__overlay' : _props$overlayClassNa,
          overlayStyle = _props.overlayStyle,
          onOverlayClick = _props.onOverlayClick,
          _props$type = _props.type,
          type = _props$type === undefined ? 'persistent' : _props$type,
          _props$align = _props.align,
          align = _props$align === undefined ? 'left' : _props$align,
          children = _props.children;
      var _state = this.state,
          open = _state.open,
          docked = _state.docked;


      var baseClassName = 'px-drawer';
      var baseClasses = (0, _classnames10.default)(baseClassName, _defineProperty({}, baseClassName + '--is-docked', docked), _defineProperty({}, baseClassName + '--is-open', open));

      var overlayClasses = (0, _classnames10.default)(overlayClassName, _defineProperty({}, overlayClassName + '--is-open', open));

      var drawerClasses = (0, _classnames10.default)(className, _defineProperty({}, className + '--' + type, true), _defineProperty({}, className + '--' + align, true), _defineProperty({}, className + '--is-fixed', fixed), _defineProperty({}, className + '--is-open', open), _defineProperty({}, className + '--is-docked', docked));

      return _react2.default.createElement(
        'div',
        { className: baseClasses },
        !docked && _react2.default.createElement('div', { id: 'overlay', style: overlayStyle, onClick: onOverlayClick, className: overlayClasses }),
        _react2.default.createElement(
          'div',
          { id: 'drawer', className: drawerClasses, style: style, ref: function ref(e) {
              _this3.sideNavContent = e;
            } },
          _react2.default.createElement(
            'div',
            { id: 'drawerContent', className: 'drawer__content' },
            _react2.default.createElement(
              'div',
              null,
              children
            )
          )
        )
      );
    }
  }]);

  return Drawer;
}(_BaseComponent3.default);

exports.default = Drawer;


Drawer.propTypes = {
  open: _propTypes2.default.bool,
  docked: _propTypes2.default.bool
};

Drawer.defaultProps = {
  opened: false,
  docked: true,
  align: 'left'
};