"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("./index.css");

var _base = require("../../utils/base.js");

var _throttle = _interopRequireDefault(require("lodash/throttle"));

var _dom = require("../../utils/dom");

var _icon = _interopRequireDefault(require("../icon"));

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

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _base.createComponentName)('loadmore'),
  components: {
    Icon: _icon.default
  },
  mixins: [_base.componentMixin],
  props: {
    /**
     * 距离底部多少开始加载
     */
    offset: {
      type: Number,
      default: 20
    },
    // 等待多久开始显示
    waitTime: {
      type: Number,
      default: 100
    },
    // 返回Promise<boolean> 当返回 false 的时候表示没有更多数据了, 不在展示
    loadMore: {
      type: Function,
      required: true
    }
  },

  data() {
    return {
      loading: false,
      error: false,
      hasMore: true
    };
  },

  computed: {
    scrollDom() {
      return (0, _dom.getElementScrollView)(this.$el);
    }

  },

  mounted() {
    this.addScrollListener();
    this.scrollListener();
  },

  beforeDestroy() {
    this.unbindEventListener();
  },

  methods: {
    addScrollListener() {
      var _this = this;

      var fn = (0, _throttle.default)(this.scrollListener, this.waitTime);

      this.unbindEventListener = function () {
        return (0, _dom.off)(_this.scrollDom, 'scroll', fn);
      };

      (0, _dom.on)(this.scrollDom, 'scroll', fn);
    },

    scrollListener() {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        var scrollDom, needLoadMore, scrollTop;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (!_this2.loading) {
                  _context.next = 2;
                  break;
                }

                return _context.abrupt("return");

              case 2:
                scrollDom = (0, _dom.getElementScrollView)(_this2.$el);
                needLoadMore = false;
                _this2.error = false;

                if (scrollDom === window) {
                  scrollTop = 0;
                  if (document.body) scrollTop = document.body.scrollTop;
                  scrollTop = Math.max(window.pageYOffset || 0, scrollTop);

                  if (document.documentElement && document.documentElement.clientHeight + scrollTop >= document.documentElement.scrollHeight - _this2.offset) {
                    needLoadMore = true;
                  }
                } else if (scrollDom.clientHeight + scrollDom.scrollTop >= scrollDom.scrollHeight - _this2.offset) {
                  needLoadMore = true;
                }

                if (!needLoadMore) {
                  _context.next = 28;
                  break;
                }

                _context.prev = 7;
                _this2.error = false;
                _this2.loading = true;
                _context.next = 12;
                return _this2.loadMore();

              case 12:
                _this2.hasMore = _context.sent;
                _context.next = 18;
                break;

              case 15:
                _context.prev = 15;
                _context.t0 = _context["catch"](7);
                _this2.error = true;

              case 18:
                _context.prev = 18;
                _this2.loading = false;
                return _context.finish(18);

              case 21:
                if (_this2.hasMore) {
                  _context.next = 25;
                  break;
                }

                _this2.unbindEventListener();

                _context.next = 28;
                break;

              case 25:
                if (_this2.error) {
                  _context.next = 28;
                  break;
                }

                _context.next = 28;
                return _this2.scrollListener();

              case 28:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[7, 15, 18, 21]]);
      }))();
    }

  }
};
/* template */

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

  var _h = _vm.$createElement;

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

  return _c("div", {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.hasMore,
      expression: "hasMore"
    }],
    class: _vm.bem()
  }, [!_vm.error ? _vm._t("loading", [_c("Icon", {
    attrs: {
      name: "loading",
      spin: ""
    }
  }), _vm._v(" "), _c("span", [_vm._v("玩命加载中...")])], {
    loading: _vm.loading
  }) : _c("div", {
    class: _vm.bem("error")
  }, [_vm._t("error", [_c("span", {
    on: {
      click: _vm.scrollListener
    }
  }, [_vm._v("加载失败点击重试")])], {
    refresh: _vm.scrollListener
  })], 2)], 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;