"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Notice = _interopRequireDefault(require("./../Notice"));

var _instance = _interopRequireDefault(require("./instance.js"));

var _ImgLazy = _interopRequireDefault(require("./../ImgLazy"));

var _instance2 = _interopRequireDefault(require("../Context/instance.js"));

var _excluded = ["threshold", "end", "endRefresh", "moveTimeout", "onTopRefresh", "onTopComplete", "onBottomRefresh", "onBottomComplete", "children", "hasMore", "showNotice", "noticeProps", "lazyLoad", "onScroll", "bottomLoadingCaption", "bottomNoDataCaption", "bottomErrorCaption", "onClickBottomError"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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

var Dragrefresh = /*#__PURE__*/function (_Component) {
  _inheritsLoose(Dragrefresh, _Component);

  function Dragrefresh(props, context) {
    var _this;

    _this = _Component.call(this, props, context) || this;

    _defineProperty(_assertThisInitialized(_this), "componentDidUpdate", function (prevProps) {
      if (prevProps.hasMore === _this.props.hasMore) return; // 刷新完成则设置刷新

      _this.setPagination(prevProps.hasMore, _this.props.hasMore);
    });

    _defineProperty(_assertThisInitialized(_this), "init", function () {
      // 全局配置
      var locale = _this.context.locale;
      if (!locale) locale = function locale(remark) {
        return remark || '';
      };
      var onScroll = _this.props.onScroll;
      var instance = new _instance["default"]({
        threshold: _this.props.threshold,
        end: _this.props.end,
        endRefresh: _this.props.endRefresh,
        moveTimeout: _this.props.moveTimeout,
        container: _this.$el,
        onScroll: onScroll,
        onTopRefresh: _this.onTopRefresh,
        // 头部刷新,加载第一页
        onBottomRefresh: _this.onBottomRefresh,
        // 底部刷新,加载下一页
        // 构建实体
        topContainer: _this.$elTopBox,
        // 实体交互
        duration: 150,
        onPull: function onPull(e) {
          var topContainer = e.topContainer;
          topContainer.style.height = e.touches.currentPosY + 'px';
          var topIcon = topContainer.querySelector('.df-pull-icon');
          var topCaption = topContainer.querySelector('.df-pull-caption');

          if (!e.isLoading) {
            if (e.touches.currentPosY >= e.params.threshold) {
              if (topIcon) topIcon.classList.add('df-pull-icon-down');
              if (topCaption) topCaption.innerHTML = locale('释放立即刷新', 'release');
            } else {
              if (topIcon) topIcon.classList.remove('df-pull-icon-down');
              if (topCaption) topCaption.innerHTML = locale('下拉可以刷新', 'pull_down');
            }
          }
        },
        onShowTop: function onShowTop(e) {
          var topContainer = e.topContainer;
          var topIcon = topContainer.querySelector('.df-pull-icon');
          var topCaption = topContainer.querySelector('.df-pull-caption');
          topContainer.style.height = e.params.threshold + 'px';
          if (topIcon) topIcon.classList.remove('df-pull-icon-down');
          if (topIcon) topIcon.classList.add('df-pull-icon-loading');
          if (topCaption) topCaption.innerHTML = locale('正在刷新...', 'refreshing');
        },
        onHideTop: function onHideTop(e) {
          var topContainer = e.topContainer;
          topContainer.style.height = '0';
        },
        onTopHid: function onTopHid(e) {
          var topContainer = e.topContainer;
          var topIcon = topContainer.querySelector('.df-pull-icon');
          if (topIcon) topIcon.classList.remove('df-pull-icon-down');
          if (topIcon) topIcon.classList.remove('df-pull-icon-loading');
        }
      });
      _this.instance = instance;
    });

    _defineProperty(_assertThisInitialized(_this), "onTopRefresh", function () {
      _this.props.onTopRefresh();
    });

    _defineProperty(_assertThisInitialized(_this), "onBottomRefresh", function () {
      if (!_this.props.onBottomRefresh) return;
      var hasMore = _this.props.hasMore;

      if (hasMore !== 0 && hasMore !== -1 && hasMore !== 404) {
        _this.props.onBottomRefresh();
      } else {
        if (_this.instance) _this.instance.isLoading = false; // 暂无数据时, 设置为可刷新
      }
    });

    _defineProperty(_assertThisInitialized(_this), "lazyLoad", function () {
      if (!_this.$el) return;
      if (_this.timeout) window.clearTimeout(_this.timeout);
      _this.timeout = setTimeout(function () {
        if (_this.lazy) {
          _this.lazy.load();
        } else {
          _this.lazy = new _ImgLazy["default"]({
            overflowContainer: _this.$el
          });

          _this.lazy.load();
        }
      }, 500);
    });

    _defineProperty(_assertThisInitialized(_this), "setPagination", function (prevHasMore, hasMore) {
      // 如果还没有初始化完成,则会再轮询调用一下
      if (!_this.instance) {
        setTimeout(function () {
          _this.setPagination(prevHasMore, hasMore);
        }, 100);
        return;
      }

      console.log("hasMore: \u7531" + prevHasMore + "\u53D8\u6210" + hasMore); // 无数据时不允许触发刷新

      if (_this.props.showNotice && hasMore === 404) {
        _this.instance.detach();
      } else if (_this.props.showNotice && prevHasMore === 404 && hasMore !== 404) {
        _this.instance.attach();
      } // 刷新完成, 需收起头


      if (hasMore !== -2) {
        _this.instance.hideTop();
      } // 设置为可刷新


      _this.instance.isLoading = false; // 刷新完成, 还有数据

      if (hasMore === 1) {
        // 如果还有数据，并且如果没有滚动条，则继续加载
        if (!_this.instance.hasScroll()) {
          console.log('还有数据,但没有滚动条,继续加载...');

          _this.instance.bottomRefresh();
        }
      } // 懒加载


      if (_this.props.lazyLoad) _this.lazyLoad();
    });

    return _this;
  }

  var _proto = Dragrefresh.prototype;

  _proto.componentDidMount = function componentDidMount() {
    this.init();
    this.setPagination(undefined, this.props.hasMore);
  };

  _proto.render = function render() {
    var _this2 = this;

    // 全局配置
    var locale = this.context.locale;
    if (!locale) locale = function locale(remark) {
      return remark || '';
    };

    var _this$props = this.props,
        threshold = _this$props.threshold,
        end = _this$props.end,
        endRefresh = _this$props.endRefresh,
        moveTimeout = _this$props.moveTimeout,
        onTopRefresh = _this$props.onTopRefresh,
        onTopComplete = _this$props.onTopComplete,
        onBottomRefresh = _this$props.onBottomRefresh,
        onBottomComplete = _this$props.onBottomComplete,
        children = _this$props.children,
        hasMore = _this$props.hasMore,
        showNotice = _this$props.showNotice,
        noticeProps = _this$props.noticeProps,
        lazyLoad = _this$props.lazyLoad,
        onScroll = _this$props.onScroll,
        _this$props$bottomLoa = _this$props.bottomLoadingCaption,
        bottomLoadingCaption = _this$props$bottomLoa === void 0 ? locale('正在加载...', 'loading') : _this$props$bottomLoa,
        _this$props$bottomNoD = _this$props.bottomNoDataCaption,
        bottomNoDataCaption = _this$props$bottomNoD === void 0 ? locale('没有更多数据了', 'no_more_data') : _this$props$bottomNoD,
        _this$props$bottomErr = _this$props.bottomErrorCaption,
        bottomErrorCaption = _this$props$bottomErr === void 0 ? locale('加载失败, 请稍后再试', 'refreshing_failed') : _this$props$bottomErr,
        onClickBottomError = _this$props.onClickBottomError,
        others = _objectWithoutPropertiesLoose(_this$props, _excluded);

    return /*#__PURE__*/_react["default"].createElement("div", _extends({
      ref: function ref(el) {
        _this2.$el = el;
      }
    }, others, {
      className: "container" + (others.className ? ' ' + others.className : '')
    }), onTopRefresh && /*#__PURE__*/_react["default"].createElement("div", {
      ref: function ref(el) {
        _this2.$elTopBox = el;
      },
      className: "SID-Dragrefresh-TopContainer df-pull"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-box"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-icon"
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-caption"
    }, locale('下拉可以刷新', 'pull_down')))), this.props.children, onBottomRefresh && (hasMore === 1 || hasMore === -2) && /*#__PURE__*/_react["default"].createElement("div", {
      className: "SID-Dragrefresh-BottomContainer df-pull",
      style: {
        height: '50px'
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-box"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-icon df-pull-icon-loading"
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-caption"
    }, bottomLoadingCaption))), onBottomRefresh && hasMore === 0 && /*#__PURE__*/_react["default"].createElement("div", {
      className: "SID-Dragrefresh-NoDataContainer df-pull",
      style: {
        height: '50px'
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-box"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-caption"
    }, bottomNoDataCaption))), onBottomRefresh && hasMore === -1 && /*#__PURE__*/_react["default"].createElement("div", {
      className: "SID-Dragrefresh-ErrorContainer df-pull",
      style: {
        height: '50px'
      },
      onClick: onClickBottomError
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-box"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "df-pull-caption"
    }, bottomErrorCaption))), hasMore === 404 && showNotice && /*#__PURE__*/_react["default"].createElement(_Notice["default"], _extends({
      caption: '暂无数据'
    }, noticeProps)));
  };

  return Dragrefresh;
}(_react.Component);

exports["default"] = Dragrefresh;

_defineProperty(Dragrefresh, "contextType", _instance2["default"]);

_defineProperty(Dragrefresh, "defaultProps", {
  showNotice: true
});

Dragrefresh.propTypes = process.env.NODE_ENV !== "production" ? {
  threshold: _propTypes["default"].number,
  // 头部下拉的触发位置
  end: _propTypes["default"].number,
  // 头部下拉的结束位置
  endRefresh: _propTypes["default"].bool,
  // 滑动到指位置后自动刷新
  moveTimeout: _propTypes["default"].number,
  // 滑动超时, 解决ios手指滑动到原生tabbar上, 不触发onTouchEnd
  onTopRefresh: _propTypes["default"].func,
  onTopComplete: _propTypes["default"].func,
  onBottomRefresh: _propTypes["default"].func,
  onBottomComplete: _propTypes["default"].func,
  children: _propTypes["default"].node,
  hasMore: _propTypes["default"].number,
  // hasMore: 0.无更多数据 1.数据加载完成 404.一条数据都没有 -1. 加载错误 -2. 重置状态,为了后面可以更新DOM
  showNotice: _propTypes["default"].bool,
  // 是否允许暂无数据
  noticeProps: _propTypes["default"].object,
  lazyLoad: _propTypes["default"].bool,
  onScroll: _propTypes["default"].func,
  // 滚动事件
  // 底部加载中
  bottomLoadingCaption: _propTypes["default"].string,
  // 底部加载完成
  bottomNoDataCaption: _propTypes["default"].string,
  // 底部加载错误
  bottomErrorCaption: _propTypes["default"].string,
  onClickBottomError: _propTypes["default"].func
} : {};
module.exports = exports.default;