"use strict";

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

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

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

var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));

var _symbol = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol"));

var _portal = _interopRequireDefault(require("../portal"));

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

var _popper = _interopRequireDefault(require("popper.js"));

var _popupManager = _interopRequireDefault(require("../../utils/popup-manager"));

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

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

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
var forEach = function forEach(l, fn) {
  for (var i = 0; i < l.length; i++) {
    fn && fn(l[i], i);
  }
};

var popperJs = (0, _symbol.default)('popperJs');
var __vue_script__ = {
  name: (0, _base.createComponentName)('popover'),
  components: {
    Portal: _portal.default
  },
  mixins: [_base.componentMixin],
  props: {
    tag: {
      type: [String, Object],
      default: 'span'
    },
    popoverClass: {
      type: [String, Array],
      default: ''
    },
    visible: {
      type: Boolean
    },
    defaultVisible: {
      type: Boolean,
      default: false
    },
    transitionName: {
      type: String
    },
    transitionOn: {
      type: Object
    },
    lazy: {
      type: Boolean,
      default: true
    },
    trigger: {
      type: String,
      default: 'hover',
      validator: function validator(value) {
        return ['click', 'hover', null].indexOf(value) > -1;
      }
    },
    placement: {
      type: String,
      default: 'bottom'
    },
    // 延迟多久显示
    delayOver: {
      type: Number,
      default: 100
    },
    modifiers: {
      type: Object
    }
  },

  data() {
    var visible = false;
    if (this.$hasProp('visible')) visible = this.visible;else if (this.$hasProp('defaultVisible')) visible = this.defaultVisible;
    return {
      isShowPopper: visible,
      zIndex: 0,
      hasShow: visible,
      mounted: false
    };
  },

  computed: {
    componentProps() {
      var _this = this;

      var keys = (0, _keys.default)(this.$attrs);
      var attrs = {};
      keys.forEach(function (key) {
        if (!(key in _this.$options.props)) {
          attrs[key] = _this.$attrs[key];
        }
      });
      return attrs;
    }

  },
  watch: {
    isShowPopper(value) {
      if (value) this.hasShow = true;

      if (value) {
        this.$emit('show');

        _popupManager.default.pushStack(this);

        this.updatePopper();
      } else {
        this.$emit('hide');

        _popupManager.default.popStack(this);
      }
    },

    visible(value) {
      this.isShowPopper = value;
    }

  },

  update() {
    this.updatePopper();
  },

  mounted() {
    this.removeOnDestroy = true;
    this.eventsEnabled = true;
    if (this.$isServer) return;

    if (this.isShowPopper) {
      _popupManager.default.pushStack(this);
    }

    this.createPopper();
  },

  beforeDestroy() {
    this.offTrigger();

    if (this[popperJs]) {
      this[popperJs].destroy();
      this[popperJs] = null;
    }
  },

  methods: {
    initTrigger() {
      this.offTrigger();

      switch (this.trigger) {
        case 'click':
          (0, _dom.on)(this.target, 'click', this.doToggle);
          (0, _dom.on)(document, 'click', this.handleDocumentClick);
          break;

        case 'hover':
          (0, _dom.on)(this.target, 'mouseover', this.onMouseOver);
          (0, _dom.on)(this.target, 'focus', this.onMouseOver);
          (0, _dom.on)(this.popup, 'mouseover', this.onMouseOver);
          (0, _dom.on)(this.popup, 'focus', this.onMouseOver);
          (0, _dom.on)(this.target, 'mouseout', this.onMouseOut);
          (0, _dom.on)(this.target, 'blur', this.onMouseOut);
          (0, _dom.on)(this.popup, 'mouseout', this.onMouseOut);
          (0, _dom.on)(this.popup, 'blur', this.onMouseOut);
          break;
      }
    },

    changeVisible(value) {
      if (this.$hasProp('visible')) {
        this.$emit('update:visible', value);
      } else {
        this.isShowPopper = value;
      }
    },

    doToggle() {
      if (!this.forceShow) {
        this.changeVisible(!this.isShowPopper);
      }
    },

    onMouseOver() {
      var _this2 = this;

      clearTimeout(this._timer);
      this._timer = setTimeout(function () {
        _this2.changeVisible(true);
      }, this.delayOver);
    },

    onMouseOut() {
      var _this3 = this;

      clearTimeout(this._timer);
      this._timer = setTimeout(function () {
        _this3.changeVisible(false);
      }, this.delayOver);
    },

    handleDocumentClick(e) {
      if (!this.$el || !this.target || this.elementContains(this.$el, e.target) || this.elementContains(this.target, e.target) || !this.popup || this.elementContains(this.popup, e.target)) {
        return;
      }

      this.$emit('documentClick', this);
      this.changeVisible(false);
    },

    elementContains(elm, otherElm) {
      if (typeof elm.contains === 'function') {
        return elm.contains(otherElm);
      }

      return false;
    },

    getPopperConfig() {
      var _this4 = this;

      var self = this;
      var tmpMap = {
        onCreate(data) {
          data.instance.popper.style.transform = '';
          data.instance.popper.style.left = data.popper.left + 'px';
          data.instance.popper.style.top = data.popper.top + 'px';
          data.instance.popper.style.zIndex = self.zIndex;
          data.instance.popper.style.willChange = '';
          self.$emit('create', data);
        },

        onUpdate(data) {
          if (self.isShowPopper) {
            data.instance.popper.style.transform = '';
            data.instance.popper.style.left = data.popper.left + 'px';
            data.instance.popper.style.top = data.popper.top + 'px';
            data.instance.popper.style.zIndex = self.zIndex;
            data.instance.popper.style.willChange = '';
          }
        }

      }; // 通过 props 属性 设置 Plupload 配置

      forEach(['placement', 'eventsEnabled', 'removeOnDestroy', 'modifiers'], function (key) {
        _this4[key] && (tmpMap[key] = _this4[key]);
      });
      return tmpMap;
    },

    createPopper() {
      if (this.$isServer) return;

      if (!this.$refs.target) {
        throw new Error('没有设置 target 或者 popper');
      }

      this.target = this.$refs.target.$el || this.$refs.target;

      if (this.$refs.popup) {
        this.popup = this.$refs.popup.$el || this.$refs.popup;
        this[popperJs] = new _popper.default(this.target, this.popup, this.getPopperConfig());
      }

      this.initTrigger();
    },

    updatePopper() {
      var _this5 = this;

      if (this[popperJs]) {
        this[popperJs].scheduleUpdate();
      } else {
        this.$nextTick(function () {
          _this5.createPopper();
        });
      }
    },

    offTrigger() {
      (0, _dom.off)(this.target, 'click', this.doToggle);
      (0, _dom.off)(this.target, 'mouseup', this.doClose);
      (0, _dom.off)(this.target, 'mousedown', this.doShow);
      (0, _dom.off)(this.target, 'focus', this.doShow);
      (0, _dom.off)(this.target, 'blur', this.doClose);
      (0, _dom.off)(this.target, 'mouseout', this.onMouseOut);
      (0, _dom.off)(this.target, 'mouseover', this.onMouseOver);
      (0, _dom.off)(document, 'click', this.handleDocumentClick);
    }

  }
};
/* template */

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

  var _h = _vm.$createElement;

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

  return _c(_vm.tag, _vm._g(_vm._b({
    ref: "target",
    tag: "span"
  }, "span", _vm.componentProps, false), _vm.$listeners), [_vm._t("default"), _vm._v(" "), (_vm.lazy ? _vm.hasShow : true) ? _c("portal", [_c("transition", _vm._g({
    attrs: {
      name: _vm.transitionName
    }
  }, _vm.transitionOn), [_c("span", {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.isShowPopper,
      expression: "isShowPopper"
    }],
    ref: "popup",
    class: _vm.popoverClass,
    style: {
      zIndex: _vm.zIndex
    }
  }, [_vm._t("popup")], 2)])], 1) : _vm._e()], 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;