/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var Vue = require('vue');
var isFunction = require('lodash/isFunction');
var config = require('../config.js');
var utils_dom = require('../utils/dom.js');
var affix_props = require('./props.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var name = "".concat(config.prefix, "-affix");
var _Affix = Vue__default["default"].extend({
  name: "TAffix",
  props: _objectSpread({}, affix_props["default"]),
  data: function data() {
    return {
      fixedTop: false,
      oldWidthHeight: {
        width: "0px",
        height: "0px"
      }
    };
  },
  watch: {
    offsetTop: function offsetTop() {
      this.calcInitValue();
    },
    offsetBottom: function offsetBottom() {
      this.calcInitValue();
    },
    fixedTop: function fixedTop(val) {
      this.$emit("fixedChange", val !== false, {
        top: val
      });
      if (isFunction__default["default"](this.onFixedChange)) this.onFixedChange(val !== false, {
        top: Number(val)
      });
    }
  },
  methods: {
    handleScroll: function handleScroll() {
      var _this = this;

      if (!this.ticking) {
        window.requestAnimationFrame(function () {
          var _this$$el$getBounding = _this.$el.getBoundingClientRect(),
              top = _this$$el$getBounding.top;

          var containerTop = 0;

          if (_this.scrollContainer instanceof HTMLElement) {
            containerTop = _this.scrollContainer.getBoundingClientRect().top;
          }

          var calcTop = top - containerTop;
          var calcBottom = containerTop + _this.containerHeight - _this.offsetBottom;

          if (_this.offsetTop !== void 0 && calcTop <= _this.offsetTop) {
            _this.fixedTop = containerTop + _this.offsetTop;
          } else if (_this.offsetBottom !== void 0 && top >= calcBottom) {
            _this.fixedTop = calcBottom;
          } else {
            _this.fixedTop = false;
          }

          _this.ticking = false;
        });
        this.ticking = true;
      }
    },
    calcInitValue: function calcInitValue() {
      var scrollContainer = this.scrollContainer;
      var containerHeight = scrollContainer[scrollContainer instanceof Window ? "innerHeight" : "clientHeight"];
      this.containerHeight = containerHeight - this.$el.clientHeight;

      var _ref = this.$el.querySelector(".".concat(name)) || this.$el,
          clientWidth = _ref.clientWidth,
          clientHeight = _ref.clientHeight;

      this.oldWidthHeight = {
        width: "".concat(clientWidth, "px"),
        height: "".concat(clientHeight, "px")
      };
      this.handleScroll();
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    return _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
      return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _this2.$nextTick();

            case 2:
              _this2.scrollContainer = utils_dom.getScrollContainer(_this2.container);

              _this2.calcInitValue();

              utils_dom.on(_this2.scrollContainer, "scroll", _this2.handleScroll);
              utils_dom.on(window, "resize", _this2.calcInitValue);
              if (!(_this2.scrollContainer instanceof Window)) utils_dom.on(window, "scroll", _this2.handleScroll);

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  },
  destroyed: function destroyed() {
    if (!this.scrollContainer) return;
    utils_dom.off(this.scrollContainer, "scroll", this.handleScroll);
    utils_dom.off(window, "resize", this.calcInitValue);
    if (!(this.scrollContainer instanceof Window)) utils_dom.off(window, "scroll", this.handleScroll);
  },
  render: function render() {
    var h = arguments[0];
    var children = this.$slots["default"],
        oldWidthHeight = this.oldWidthHeight,
        fixedTop = this.fixedTop,
        zIndex = this.zIndex;

    if (fixedTop !== false) {
      return h("div", [h("div", {
        "style": oldWidthHeight
      }), h("div", {
        "class": name,
        "style": {
          zIndex: zIndex,
          top: "".concat(fixedTop, "px"),
          width: oldWidthHeight.width
        }
      }, [children])]);
    }

    return h("div", [children]);
  }
});

exports["default"] = _Affix;
//# sourceMappingURL=affix.js.map
