"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
    },
    autoLoadingTimes: {
      type: Number,
      default: 2
    },
    showNoMore: {
      type: Boolean,
      default: true
    },
    threshold: {
      type: Number,
      default: 0.5
    },
    offset: {
      type: Number,
      default: 0
    }
  },

  data() {
    return {
      status: '',
      times: 0
    };
  },

  mounted() {
    var _this = this;

    // 传入fn进来 dom 渲染交给业务  返回promise 来操作页面loading状态
    var onIntersection = function onIntersection(observation) {
      observation.forEach(function (change) {
        if (_this.status !== 'loading' && change.intersectionRatio > _this.threshold) {
          if (_this.times === _this.autoLoadingTimes) {
            return;
          }

          _this.load();
        }
      });
    };

    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: {
    resetData() {
      // 初始化子组件数据
      this.status = '';
      this.times = 0;
    },

    reload() {
      var _this2 = this;

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

              case 2:
                if (_this2.status !== 'error') {
                  _this2.times = 0;
                }

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

    load() {
      var _this3 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee2() {
        var status;
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _this3.status = 'loading';
                _context2.prev = 1;
                _context2.next = 4;
                return _this3.loadmore();

              case 4:
                status = _context2.sent;

                if (status === false) {
                  _this3.status = 'nomore';
                } else {
                  _this3.status = '';
                }

                _this3.times++;
                _context2.next = 12;
                break;

              case 9:
                _context2.prev = 9;
                _context2.t0 = _context2["catch"](1);
                _this3.status = 'error';

              case 12:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this, [[1, 9]]);
      }))();
    }

  }
};
/* template */

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

  var _h = _vm.$createElement;

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

  return _c("div", {
    class: _vm.bem()
  }, [_vm._t("default"), _vm._v(" "), _c("div", {
    ref: "loadmore",
    staticStyle: {
      height: "1px"
    }
  }), _vm._v(" "), _vm.status === "nomore" && _vm.showNoMore ? _c("div", [_c("div", {
    class: _vm.bem("nomore")
  }, [_vm._t("nomore", [_vm._v("\n                没有更多了\n            ")])], 2)]) : _vm.status === "error" ? _c("div", {
    class: [_vm.bem("btn"), _vm.bem("error")],
    on: {
      click: _vm.reload
    }
  }, [_vm._t("error", [_vm._v("加载失败，请重试")])], 2) : _vm.times === _vm.autoLoadingTimes ? _vm._t("more", [_c("div", {
    class: _vm.bem("btn"),
    on: {
      click: _vm.reload
    }
  }, [_vm.status === "loading" ? _c("Icon", {
    attrs: {
      name: "loading"
    }
  }) : _vm._e(), _vm._v("\n            加载更多\n        ")], 1)], {
    reload: _vm.reload,
    isLoading: _vm.status === "loading"
  }) : _vm.status === "loading" ? _c("div", {
    class: _vm.bem("loading")
  }, [_vm._t("loading", [_c("span", [_vm._v("玩命加载中...")])])], 2) : _c("div", {
    class: _vm.bem("min-height")
  }), _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;