"use strict";

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

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

var _create = _interopRequireDefault(require("../utils/create"));

var _style = require("../utils/dom/style");

var _scroll = _interopRequireDefault(require("../utils/dom/scroll"));

var _bindEvent = require("../mixins/bind-event");

var _loading = _interopRequireDefault(require("../loading"));

// Mixins
// Components
var _default = (0, _create.default)({
  render: function render() {
    var _vm = this;

    var _h = _vm.$createElement;

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

    return _c('div', {
      class: _vm.b(),
      attrs: {
        "role": "feed",
        "aria-busy": _vm.innerLoading
      }
    }, [_vm.direction === 'down' ? [_vm._t("default"), _c('div', {
      key: "placeholder",
      ref: "placeholder",
      class: _vm.b('placeholder')
    })] : _vm._e(), _vm.innerLoading && !_vm.finished ? _c('div', {
      class: _vm.b('loading')
    }, [_vm._t("loading", [_c('Loading', {
      attrs: {
        "size": "16"
      }
    }, [_vm._v(_vm._s(_vm.loadingText || "加载中..."))])])], 2) : _vm._e(), _vm.finished ? _c('div', {
      class: _vm.b('finished-text')
    }, [_vm._t("finished", [_vm._v("\n      " + _vm._s(_vm.finishedText || "没有更多了") + "\n    ")])], 2) : _vm._e(), _vm.error ? _c('div', {
      class: _vm.b('error-text'),
      on: {
        "click": _vm.clickErrorText
      }
    }, [_vm._t("error", [_vm._v("\n      " + _vm._s(_vm.errorText || "加载失败，点击重新加载") + "\n    ")])], 2) : _vm._e(), _vm.direction === 'up' ? [_c('div', {
      key: "placeholder",
      ref: "placeholder",
      class: _vm.b('placeholder')
    }), _vm._t("default")] : _vm._e()], 2);
  },
  components: {
    Loading: _loading.default
  },
  name: "list",
  mixins: [(0, _bindEvent.BindEventMixin)(function (bind) {
    if (!this.scroller) {
      this.scroller = _scroll.default.getScrollEventTarget(this.$el);
    }

    bind(this.scroller, "scroll", this.check);
  })],
  model: {
    prop: "loading"
  },
  props: {
    error: Boolean,
    loading: Boolean,
    finished: Boolean,
    errorText: String,
    loadingText: String,
    finishedText: String,
    immediateCheck: {
      type: Boolean,
      default: true
    },
    offset: {
      type: [Number, String],
      default: 300
    },
    direction: {
      type: String,
      default: "down"
    }
  },
  data: function data() {
    return {
      // use sync innerLoading state to avoid repeated loading in some edge cases
      innerLoading: this.loading
    };
  },
  updated: function updated() {
    this.innerLoading = this.loading;
  },
  mounted: function mounted() {
    if (this.immediateCheck) {
      this.check();
    }
  },
  watch: {
    loading: "check",
    finished: "check"
  },
  methods: {
    // @exposed-api
    check: function check() {
      var _this = this;

      this.$nextTick(function () {
        if (_this.innerLoading || _this.finished || _this.error) {
          return;
        }

        var el = _this.$el,
            scroller = _this.scroller,
            offset = _this.offset,
            direction = _this.direction;
        var scrollerRect;

        if (scroller.getBoundingClientRect) {
          scrollerRect = scroller.getBoundingClientRect();
        } else {
          scrollerRect = {
            top: 0,
            bottom: scroller.innerHeight
          };
        }

        var scrollerHeight = scrollerRect.bottom - scrollerRect.top;
        /* istanbul ignore next */

        if (!scrollerHeight || (0, _style.isHidden)(el)) {
          return false;
        }

        var isReachEdge = false;

        var placeholderRect = _this.$refs.placeholder.getBoundingClientRect();

        if (direction === "up") {
          isReachEdge = scrollerRect.top - placeholderRect.top <= offset;
        } else {
          isReachEdge = placeholderRect.bottom - scrollerRect.bottom <= offset;
        }

        if (isReachEdge) {
          _this.innerLoading = true;

          _this.$emit("input", true);

          _this.$emit("load");
        }
      });
    },
    clickErrorText: function clickErrorText() {
      this.$emit("update:error", false);
      this.check();
    }
  }
});

exports.default = _default;