'use strict';

exports.__esModule = true;
exports.default = undefined;

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; };

var _class, _temp, _initialiseProps;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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 Marquee = (_temp = _class = function (_Component) {
  _inherits(Marquee, _Component);

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

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _initialiseProps.call(_this);

    return _this;
  }
  // 过滤已经回调的属性


  Marquee.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        list = _props.list,
        _props$contentAttribu = _props.contentAttribute,
        contentAttribute = _props$contentAttribu === undefined ? {} : _props$contentAttribu,
        step = _props.step,
        duration = _props.duration,
        autoplay = _props.autoplay,
        direction = _props.direction,
        loop = _props.loop,
        _onClick = _props.onClick,
        others = _objectWithoutProperties(_props, ['list', 'contentAttribute', 'step', 'duration', 'autoplay', 'direction', 'loop', 'onClick']);

    // 剔除掉onClick事件, 因为在容器onClick已经回调了


    contentAttribute = this.filterProps(contentAttribute);

    return _react2.default.createElement(
      'ul',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        } }, others, { className: 'marquee' + (others.className ? ' ' + others.className : '') }),
      list && list.map(function (item, index) {
        return _react2.default.createElement(
          'li',
          _extends({ key: index }, contentAttribute, { className: 'marquee-li' + (contentAttribute.className ? ' ' + contentAttribute.className : ''), style: Object.assign({ height: step + 'px' }, contentAttribute.style || {}), onClick: function onClick(e) {
              if (_onClick) _onClick(e, item, index);
            } }),
          item.value
        );
      })
    );
  };

  return Marquee;
}(_react.Component), _class.defaultProps = {
  step: 50,
  duration: 300,
  direction: 'top',
  loop: true
}, _initialiseProps = function _initialiseProps() {
  var _this3 = this;

  this.componentDidUpdate = function (prevProps) {
    if (!_this3.props.list.equals(prevProps.list)) {
      _this3.update();
    }
  };

  this.componentDidMount = function () {
    if (_this3.instance || _this3.props.list.length === 0) return;
    _this3.init();
  };

  this.update = function () {
    _this3.instance.setStart(0);
    _this3.instance.setEnd(_this3.props.step * (_this3.props.list.length - 1));
    _this3.instance.update();
    if (_this3.props.autoplay) {
      _this3.instance.play();
    }
  };

  this.init = function () {
    var _props2 = _this3.props,
        list = _props2.list,
        step = _props2.step,
        duration = _props2.duration,
        autoplay = _props2.autoplay,
        direction = _props2.direction,
        loop = _props2.loop;

    _this3.instance = new _instance2.default(_this3.$el, {
      start: 0,
      end: step * (list.length - 1),
      step: step,
      duration: duration,
      delay: isNaN(autoplay) ? 2000 : Number(autoplay || 0),
      direction: direction,
      loop: loop
    });
    if (autoplay || isNaN(autoplay)) {
      _this3.instance.play();
    }
  };

  this.filterProps = function (props) {
    if (!props) return props;
    var propsed = {};
    for (var n in props) {
      if (n !== 'onClick') {
        propsed[n] = props[n];
      }
    }
    return propsed;
  };
}, _temp);
exports.default = Marquee;
Marquee.propTypes = process.env.NODE_ENV !== "production" ? {
  list: _propTypes2.default.array, // [{key: 'xx', value: ''}]
  contentAttribute: _propTypes2.default.object,
  step: _propTypes2.default.number,
  duration: _propTypes2.default.number,
  autoplay: _propTypes2.default.number, // 是否自动播放, 播放间隔毫秒数
  direction: _propTypes2.default.string, // top | bottom | left | right
  loop: _propTypes2.default.bool,
  onClick: _propTypes2.default.func
} : {};
module.exports = exports['default'];