'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; // require (PrototypeArray.js equals)

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

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

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

    _this.componentDidUpdate = function (prevProps) {
      if (_this.instance && _this.instance.activeIndex !== _this.props.activeIndex) {
        _this.instance.slideTo(_this.props.activeIndex, _this.props.speed, _this.props.enableOnChange);
      }
      if (_this.props.stopPropagation !== prevProps.stopPropagation) {
        _this.instance.updateParams({ stopPropagation: _this.props.stopPropagation });
      }
      if (!_this.props.list.equals(prevProps.list)) {
        _this.update();
      }
    };

    _this.componentDidMount = function () {
      _this.instance();
      // 轮播图片, 自适应的情况下, 高度需要计算
      if (!(_this.props.style && _this.props.style.height) && _this.props.list.length && _this.props.delay) {
        setTimeout(function () {
          _this.instance.updateContainerSize();
        }, _this.props.delay);
      }
    };

    _this.instance = function () {
      _this.instance = new _instance2.default(_this.$el, {
        height: _this.props.style && _this.props.style.height ? _this.props.style.height : null,
        width: _this.props.style && _this.props.style.width ? _this.props.style.width : null,
        stopPropagation: _this.props.stopPropagation,
        autoplay: _this.props.autoplay,
        slidesPerView: _this.props.slidesPerView,
        loop: _this.props.loop,
        onClick: _this.onClick,
        onSlideChangeEnd: _this.props.onChange ? _this.props.onChange : null
      });
      if (_this.props.activeIndex) _this.instance.slideTo(_this.props.activeIndex || 0);
    };

    _this.onClick = function (s, e) {
      var index = s.activeIndex;
      if (_this.props.onClick) _this.props.onClick(_this.props.list[index], index, s, e);
    };

    _this.getCarrouselClassName = function () {
      var _this$props = _this.props,
          className = _this$props.className,
          list = _this$props.list;

      if (className) {
        if (className.indexOf('carrousel-container') !== -1 || className.indexOf('carrousel-page') !== -1) {
          return className;
        }
      }
      return (list.length > 0 ? 'carrousel-container' : 'carrousel-page') + (className ? ' ' + className : '');
    };

    _this.getSlideStyle = function (item) {
      var slideAttribute = _this.props.slideAttribute;

      if (item.bg) {
        return Object.assign({ backgroundImage: 'url(' + _this.props.defaultSrc + ')' }, slideAttribute.style);
      }
      return slideAttribute.style;
    };

    _this.update = function () {
      // 更新为默认图片
      var imgs = _this.$el.querySelectorAll('.carrousel-lazy');
      for (var i = 0; i < imgs.length; i++) {
        var imgTarget = imgs[i];
        if (!imgTarget) continue;
        if (imgTarget.tagName === 'IMG') {
          imgTarget.src = _this.props.defaultSrc;
        } else {
          imgTarget.style.backgroundImage = 'url(' + _this.props.defaultSrc + ')';
        }
      }
      // 更新Carrousel
      if (_this.instance) _this.instance.update();
    };

    return _this;
  }

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

    var _props = this.props,
        className = _props.className,
        style = _props.style,
        _props$slideAttribute = _props.slideAttribute,
        slideAttribute = _props$slideAttribute === undefined ? {} : _props$slideAttribute,
        pagination = _props.pagination,
        _props$paginationAttr = _props.paginationAttribute,
        paginationAttribute = _props$paginationAttr === undefined ? {} : _props$paginationAttr,
        _props$prevAttribute = _props.prevAttribute,
        prevAttribute = _props$prevAttribute === undefined ? {} : _props$prevAttribute,
        _props$nextAttribute = _props.nextAttribute,
        nextAttribute = _props$nextAttribute === undefined ? {} : _props$nextAttribute,
        stopPropagation = _props.stopPropagation,
        activeIndex = _props.activeIndex,
        loop = _props.loop,
        autoplay = _props.autoplay,
        slidesPerView = _props.slidesPerView,
        defaultSrc = _props.defaultSrc,
        list = _props.list,
        enableOnChange = _props.enableOnChange,
        speed = _props.speed,
        onClick = _props.onClick,
        onChange = _props.onChange,
        delay = _props.delay,
        children = _props.children,
        others = _objectWithoutProperties(_props, ['className', 'style', 'slideAttribute', 'pagination', 'paginationAttribute', 'prevAttribute', 'nextAttribute', 'stopPropagation', 'activeIndex', 'loop', 'autoplay', 'slidesPerView', 'defaultSrc', 'list', 'enableOnChange', 'speed', 'onClick', 'onChange', 'delay', 'children']);

    var childrenArr = _react2.default.Children.toArray(children);
    return _react2.default.createElement(
      'div',
      _extends({ ref: function ref(el) {
          _this2.$el = el;
        }, className: this.getCarrouselClassName(), style: style }, others),
      _react2.default.createElement(
        'div',
        { className: 'carrousel-wrapper' },
        list.length > 0 && list.map(function (item, index) {
          return _react2.default.createElement(
            'div',
            _extends({}, slideAttribute, { className: 'carrousel-slide' + (slideAttribute.className ? ' ' + slideAttribute.className : '') + (item.bg ? ' carrousel-lazy' : ''), style: _this2.getSlideStyle(item), key: index, 'data-load-src': item.bg }),
            item.img && _react2.default.createElement('img', { className: 'carrousel-slide-img carrousel-lazy', alt: '', src: defaultSrc, 'data-load-src': item.img }),
            item.caption && _react2.default.createElement(
              'div',
              { className: 'carrousel-summary' },
              item.iconAttribute && _react2.default.createElement('i', _extends({}, item.iconAttribute, { className: 'icon carrousel-summary-icon' + (item.iconAttribute.className ? ' ' + item.iconAttribute.className : '') })),
              _react2.default.createElement(
                'span',
                { className: 'nowrap carrousel-summary-caption', style: { marginRight: '20px' } },
                item.caption
              )
            )
          );
        }),
        list.length === 0 && childrenArr && childrenArr.map(function (item, index) {
          return _react2.default.createElement(
            'div',
            { className: 'carrousel-slide', key: index },
            item
          );
        })
      ),
      pagination === true && _react2.default.createElement('div', _extends({}, paginationAttribute, { className: 'carrousel-pagination' + (paginationAttribute.className ? ' ' + paginationAttribute.className : '') })),
      pagination && pagination !== true && pagination,
      list.length > 1 && _react2.default.createElement('div', _extends({}, prevAttribute, { className: 'carrousel-prev' + (prevAttribute.className ? ' ' + prevAttribute.className : '') })),
      list.length > 1 && _react2.default.createElement('div', _extends({}, nextAttribute, { className: 'carrousel-next' + (nextAttribute.className ? ' ' + nextAttribute.className : '') }))
    );
  };

  return Carrousel;
}(_react.Component), _class.defaultProps = {
  slideAttribute: {},
  paginationAttribute: {},
  prevAttribute: {},
  nextAttribute: {},
  stopPropagation: false, // 设置为false解决与FastClick插件touch事件冲突的问题
  activeIndex: 0,
  page: 0,
  loop: false,
  pagination: false,
  autoplay: 0,
  slidesPerView: 1,
  list: [],
  defaultSrc: '//res.waiqin365.com/d/seedsui/carrousel/default.png',
  enableOnChange: true,
  speed: 300,
  delay: 500
}, _temp);
exports.default = Carrousel;
Carrousel.propTypes = process.env.NODE_ENV !== "production" ? {
  style: _propTypes2.default.object, // 设置容器Style
  className: _propTypes2.default.string, // 设置容器className

  slideAttribute: _propTypes2.default.object,

  pagination: _propTypes2.default.oneOfType([// 是否显示小点点
  _propTypes2.default.bool, _propTypes2.default.node]),
  paginationAttribute: _propTypes2.default.object,

  prevAttribute: _propTypes2.default.object,
  nextAttribute: _propTypes2.default.object,

  stopPropagation: _propTypes2.default.bool, // 是否阻止点击事件的传播, 设置为false解决与FastClick插件touch事件冲突的问题
  activeIndex: _propTypes2.default.number, // 默认选中第几块

  loop: _propTypes2.default.bool, // 是否循环显示
  autoplay: _propTypes2.default.number, // 是否自动播放
  slidesPerView: _propTypes2.default.number, // 一屏显示几块,默认1块
  defaultSrc: _propTypes2.default.string, // 默认图片
  list: _propTypes2.default.array, // [{bg: 'xx', img: 'xx', iconAttribute: {}, caption: 'xx'}]
  enableOnChange: _propTypes2.default.bool, // 手动调用slideTo方法是否触发onChange事件回调
  speed: _propTypes2.default.number, // 动画过渡的速度
  onClick: _propTypes2.default.func, // func(s, e)
  onChange: _propTypes2.default.func,
  delay: _propTypes2.default.number, // 延迟初始化秒数

  children: _propTypes2.default.node // 轮播页,例<Carrousel><div>第1页</div></Carrousel>
} : {};
module.exports = exports['default'];