"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 _component = require("../_utils/component");

var _Icon = _interopRequireDefault(require("../Icon"));

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

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

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _component.createComponentName)('modal'),
  components: {
    Icon: _Icon.default
  },
  mixins: [_component.componentMixin],
  props: {
    // 是否可见
    visible: {
      type: Boolean,
      default: false
    },
    // 显示遮罩层
    mask: {
      type: Boolean,
      default: true
    },
    transitionName: {
      type: String,
      default: 'gd-modal-fade'
    },
    transitionOn: {
      type: Object
    },
    transitionDuration: {
      type: Object
    },
    // 是否支持键盘esc关闭
    keyborad: {
      type: Boolean,
      default: true
    },
    // 标题
    title: {
      type: String
    },
    appendBody: {
      type: Boolean,
      default: true
    },
    // modal 宽度
    width: {
      type: String,
      default: '350px'
    },
    top: {
      type: String,
      default: '15%'
    },
    theme: {
      type: String,
      default: 'light' // light or dark

    },
    bottom: {
      type: String,
      default: 'auto'
    },
    // 显示关闭按钮
    showClose: {
      type: Boolean,
      default: true
    },
    // 自定义关闭按钮文笔
    closeText: {
      type: String
    },
    // 确认按钮文字
    okText: {
      type: String
    },
    // 取消按钮文字
    cancelText: {
      type: String
    },
    // 点击遮罩层是否允许关闭
    maskClosable: {
      type: Boolean,
      default: true
    },
    onCancel: {
      type: Function
    },
    afterClose: {
      type: Function
    },
    // 居中
    centered: {
      type: Boolean,
      default: false
    },
    onOk: {
      type: Function
    }
  },

  data() {
    return {
      zIndex: _popupManager.default.getZindex(),
      mounted: false
    };
  },

  computed: {
    dialogStyle() {
      return {
        marginTop: this.top,
        width: this.width
      };
    },

    hasFooter() {
      return this.okText || this.noText || this.$slots.footer;
    }

  },
  watch: {
    visible(value) {
      if (value) {
        _popupManager.default.pushStack(this);
      } else if (!value) {
        _popupManager.default.popStack(this);
      }
    }

  },

  mounted() {
    this.mounted = true;
    if (this.isSSR()) return;
    if (this.visible) _popupManager.default.pushStack(this);
  },

  beforeDestroy() {
    if (this.appendBody && this.$el.parentElement) {
      this.$el.parentElement.removeChild(this.$el);
    }

    _popupManager.default.popStack(this);
  },

  methods: {
    close(e) {
      if (this.maskClosable && e.target === this.$refs.wrapper) {
        this.cancel();
      }
    },

    confirm() {
      var _this = 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:
                _this.$emit('update:visible', false);

                if (!_this.onOk) {
                  _context.next = 4;
                  break;
                }

                _context.next = 4;
                return _this.onOk();

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

    cancel() {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee2() {
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                // visible.sync
                _this2.$emit('update:visible', false);

                if (!_this2.onCancel) {
                  _context2.next = 4;
                  break;
                }

                _context2.next = 4;
                return _this2.onCancel();

              case 4:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }))();
    },

    onClickOverlay() {
      if (this.showClose) {
        this.cancel();
      }
    },

    afterLeave() {
      if (this.afterClose) this.afterClose();
    }

  }
};
/* template */

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

  var _h = _vm.$createElement;

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

  return _c("transition", _vm._g({
    attrs: {
      name: _vm.transitionName,
      duration: _vm.transitionDuration
    },
    on: {
      "after-leave": _vm.afterLeave
    }
  }, _vm.transitionOn), [_vm.mounted && _vm.visible ? _c("div", {
    ref: "wrapper",
    class: _vm.bem("wrapper", {
      centered: _vm.centered
    }),
    style: {
      zIndex: _vm.zIndex,
      position: _vm.appendBody ? "fixed" : "absolute"
    },
    on: {
      click: _vm.close
    }
  }, [_c("div", {
    class: _vm.bem([_vm.theme]),
    style: {
      width: _vm.width,
      top: _vm.centered ? "0" : _vm.top,
      bottom: _vm.bottom
    }
  }, [_c("div", {
    class: _vm.bem("header")
  }, [_vm._t("header", [_vm.title ? _c("span", {
    class: _vm.bem("title")
  }, [_vm._v(_vm._s(_vm.title))]) : _vm._e()]), _vm._v(" "), _vm.showClose && !_vm.closeText ? _c("icon", {
    class: _vm.bem("close"),
    attrs: {
      name: "close",
      color: "#AAA"
    },
    nativeOn: {
      click: function click($event) {
        _vm.onClickOverlay(false);
      }
    }
  }) : _vm._e(), _vm._v(" "), _vm.showClose && _vm.closeText ? _c("span", {
    class: _vm.bem("close"),
    on: {
      click: function click($event) {
        _vm.onClickOverlay(false);
      }
    }
  }, [_vm._v(_vm._s(_vm.closeText))]) : _vm._e()], 2), _vm._v(" "), _c("div", {
    class: _vm.bem("body")
  }, [_vm._t("default")], 2), _vm._v(" "), _vm.hasFooter ? _c("div", {
    class: _vm.bem("footer")
  }, [_vm._t("footer", [_c("div", {
    class: _vm.bem("buttons")
  }, [_vm.okText ? _c("gd-button", {
    attrs: {
      rect: "",
      type: "primary"
    },
    on: {
      click: _vm.confirm
    }
  }, [_vm._v("\n                            " + _vm._s(_vm.okText) + "\n                        ")]) : _vm._e(), _vm._v(" "), _vm.cancelText ? _c("gd-button", {
    attrs: {
      rect: ""
    },
    on: {
      click: _vm.cancel
    }
  }, [_vm._v("\n                            " + _vm._s(_vm.cancelText) + "\n                        ")]) : _vm._e()], 1)])], 2) : _vm._e()])]) : _vm._e()]);
};

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;