"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));

require("core-js/modules/web.dom.iterable");

require("./index.css");

var _component = require("../_utils/component");

var _Icon = _interopRequireDefault(require("../Icon"));

var _NomoreTemplets = _interopRequireDefault(require("./NomoreTemplets"));

var _constant = require("../_utils/constant");

var _vueNormalizeComponent = _interopRequireDefault(require("../_utils/vue-normalize-component.js"));

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
if (!(0, _constant.isSSR)()) {
  require('intersection-observer');
}

var __vue_script__ = {
  name: (0, _component.createComponentName)('loadmore'),
  components: {
    Icon: _Icon.default
  },
  NomoreTemplets: _NomoreTemplets.default,
  mixins: [_component.componentMixin],
  props: {
    // 业务方请求
    loadmore: {
      type: Function,
      required: true
    },
    // 是否自动加载更多
    autoLoad: {
      type: Boolean,
      default: true
    },
    // 无数据状态
    isNoMore: {
      type: Boolean,
      default: false
    },
    // 加载失败状态
    isError: {
      type: Boolean,
      default: false
    },
    // 视图窗口展示的百分比 执行数据加载
    threshold: {
      type: Number,
      default: 0.5
    },
    // 距离底部加载的距离
    offset: {
      type: Number,
      default: 0
    }
  },

  data() {
    return {
      pending: false
    };
  },

  watch: {
    isNoMore(v) {
      if (!v) this.tryLoad();
    },

    isError(v) {
      if (!v) this.tryLoad();
    }

  },

  mounted() {
    var _this = this;

    // 传入fn进来 dom 渲染交给业务  返回promise 来操作页面loading状态
    var onIntersection = function onIntersection(observation) {
      observation.forEach(
      /*#__PURE__*/
      function () {
        var _ref = (0, _asyncToGenerator2.default)(
        /*#__PURE__*/
        _regenerator.default.mark(function _callee(change) {
          return _regenerator.default.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _this.intersectionRatio = change.intersectionRatio;

                  _this.tryLoad();

                case 2:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee, this);
        }));

        return function (_x) {
          return _ref.apply(this, arguments);
        };
      }());
    };

    this.observer = new IntersectionObserver(onIntersection, {
      threshold: [0, 0.25, 0.5, 0.75, 1],
      rootMargin: `0px 0px ${this.offset}px 0px`
    });
    var bottom = this.$refs.loadmore;
    this.observer.observe(bottom);
  },

  beforeDestroy() {
    if (this.$refs.loadmore && this.observer) {
      this.observer.unobserve(this.$refs.loadmore);
    }
  },

  methods: {
    tryLoad() {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee2() {
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                if (!(!_this2.pending && _this2.intersectionRatio > _this2.threshold)) {
                  _context2.next = 5;
                  break;
                }

                if (!(!_this2.autoLoad || _this2.isError || _this2.isNoMore)) {
                  _context2.next = 3;
                  break;
                }

                return _context2.abrupt("return");

              case 3:
                _context2.next = 5;
                return _this2.load();

              case 5:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }))();
    },

    reload() {
      var _this3 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee3() {
        return _regenerator.default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return _this3.load();

              case 2:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }))();
    },

    load() {
      var _this4 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee4() {
        return _regenerator.default.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                _this4.pending = true;
                _context4.prev = 1;
                _context4.next = 4;
                return _this4.loadmore();

              case 4:
                _this4.pending = false; // 处理加载更多后，组件仍处于视口，需要再重新重新加载下一页

                setTimeout(function () {
                  _this4.tryLoad();
                }, 0);
                _context4.next = 12;
                break;

              case 8:
                _context4.prev = 8;
                _context4.t0 = _context4["catch"](1);
                _this4.pending = false;
                throw _context4.t0;

              case 12:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this, [[1, 8]]);
      }))();
    }

  }
};
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("div", {
    ref: "loadmore",
    class: _vm.bem()
  }, [_vm.isError ? _c("div", {
    class: [_vm.bem("btn"), _vm.bem("error")],
    on: {
      click: _vm.reload
    }
  }, [_vm._t("error", [_vm._v("加载失败，请重试")])], 2) : _vm.isNoMore ? _c("div", [_c("div", {
    class: _vm.bem("nomore")
  }, [_vm._t("nomore", [_vm._v("\n                没有更多了\n            ")])], 2)]) : [!_vm.autoLoad ? _vm._t("more", [_c("div", {
    class: _vm.bem("btn"),
    on: {
      click: _vm.reload
    }
  }, [_vm.pending ? _c("Icon", {
    attrs: {
      name: "loading"
    }
  }) : _vm._e(), _vm._v("\n                加载更多\n            ")], 1)], {
    reload: _vm.reload,
    isLoading: _vm.pending
  }) : _c("div", {
    class: _vm.bem("loading")
  }, [_vm._t("loading", [_c("span", [_vm._v("玩命加载中...")])])], 2)], _vm._v(" "), _vm._t("footer")], 2);
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* component normalizer */

/* style inject */

/* style inject SSR */
var _default = (0, _vueNormalizeComponent.default)({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = _default;