mindoc/static/element-ui/lib/utils/popup/index.js
2017-04-19 18:19:27 +08:00

296 lines
7.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
exports.__esModule = true;
exports.PopupManager = undefined;
var _vue = require('vue');
var _vue2 = _interopRequireDefault(_vue);
var _merge = require('element-ui/lib/utils/merge');
var _merge2 = _interopRequireDefault(_merge);
var _popupManager = require('element-ui/lib/utils/popup/popup-manager');
var _popupManager2 = _interopRequireDefault(_popupManager);
var _scrollbarWidth = require('../scrollbar-width');
var _scrollbarWidth2 = _interopRequireDefault(_scrollbarWidth);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var idSeed = 1;
var transitions = [];
var hookTransition = function hookTransition(transition) {
if (transitions.indexOf(transition) !== -1) return;
var getVueInstance = function getVueInstance(element) {
var instance = element.__vue__;
if (!instance) {
var textNode = element.previousSibling;
if (textNode.__vue__) {
instance = textNode.__vue__;
}
}
return instance;
};
_vue2.default.transition(transition, {
afterEnter: function afterEnter(el) {
var instance = getVueInstance(el);
if (instance) {
instance.doAfterOpen && instance.doAfterOpen();
}
},
afterLeave: function afterLeave(el) {
var instance = getVueInstance(el);
if (instance) {
instance.doAfterClose && instance.doAfterClose();
}
}
});
};
var scrollBarWidth = void 0;
var getDOM = function getDOM(dom) {
if (dom.nodeType === 3) {
dom = dom.nextElementSibling || dom.nextSibling;
getDOM(dom);
}
return dom;
};
exports.default = {
props: {
value: {
type: Boolean,
default: false
},
transition: {
type: String,
default: ''
},
openDelay: {},
closeDelay: {},
zIndex: {},
modal: {
type: Boolean,
default: false
},
modalFade: {
type: Boolean,
default: true
},
modalClass: {},
modalAppendToBody: {
type: Boolean,
default: false
},
lockScroll: {
type: Boolean,
default: true
},
closeOnPressEscape: {
type: Boolean,
default: false
},
closeOnClickModal: {
type: Boolean,
default: false
}
},
created: function created() {
if (this.transition) {
hookTransition(this.transition);
}
},
beforeMount: function beforeMount() {
this._popupId = 'popup-' + idSeed++;
_popupManager2.default.register(this._popupId, this);
},
beforeDestroy: function beforeDestroy() {
_popupManager2.default.deregister(this._popupId);
_popupManager2.default.closeModal(this._popupId);
if (this.modal && this.bodyOverflow !== null && this.bodyOverflow !== 'hidden') {
document.body.style.overflow = this.bodyOverflow;
document.body.style.paddingRight = this.bodyPaddingRight;
}
this.bodyOverflow = null;
this.bodyPaddingRight = null;
},
data: function data() {
return {
opened: false,
bodyOverflow: null,
bodyPaddingRight: null,
rendered: false
};
},
watch: {
value: function value(val) {
var _this = this;
if (val) {
if (this._opening) return;
if (!this.rendered) {
this.rendered = true;
_vue2.default.nextTick(function () {
_this.open();
});
} else {
this.open();
}
} else {
this.close();
}
}
},
methods: {
open: function open(options) {
var _this2 = this;
if (!this.rendered) {
this.rendered = true;
this.$emit('input', true);
}
var props = (0, _merge2.default)({}, this.$props || this, options);
if (this._closeTimer) {
clearTimeout(this._closeTimer);
this._closeTimer = null;
}
clearTimeout(this._openTimer);
var openDelay = Number(props.openDelay);
if (openDelay > 0) {
this._openTimer = setTimeout(function () {
_this2._openTimer = null;
_this2.doOpen(props);
}, openDelay);
} else {
this.doOpen(props);
}
},
doOpen: function doOpen(props) {
if (this.$isServer) return;
if (this.willOpen && !this.willOpen()) return;
if (this.opened) return;
this._opening = true;
// 使用 vue-popup 的组件,如果需要和父组件通信显示的状态,应该使用 value它是一个 prop
// 这样在父组件中用 v-model 即可;否则可以使用 visible它是一个 data
this.visible = true;
this.$emit('input', true);
var dom = getDOM(this.$el);
var modal = props.modal;
var zIndex = props.zIndex;
if (zIndex) {
_popupManager2.default.zIndex = zIndex;
}
if (modal) {
if (this._closing) {
_popupManager2.default.closeModal(this._popupId);
this._closing = false;
}
_popupManager2.default.openModal(this._popupId, _popupManager2.default.nextZIndex(), this.modalAppendToBody ? undefined : dom, props.modalClass, props.modalFade);
if (props.lockScroll) {
if (!this.bodyOverflow) {
this.bodyPaddingRight = document.body.style.paddingRight;
this.bodyOverflow = document.body.style.overflow;
}
scrollBarWidth = (0, _scrollbarWidth2.default)();
var bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
if (scrollBarWidth > 0 && bodyHasOverflow) {
document.body.style.paddingRight = scrollBarWidth + 'px';
}
document.body.style.overflow = 'hidden';
}
}
if (getComputedStyle(dom).position === 'static') {
dom.style.position = 'absolute';
}
dom.style.zIndex = _popupManager2.default.nextZIndex();
this.opened = true;
this.onOpen && this.onOpen();
if (!this.transition) {
this.doAfterOpen();
}
},
doAfterOpen: function doAfterOpen() {
this._opening = false;
},
close: function close() {
var _this3 = this;
if (this.willClose && !this.willClose()) return;
if (this._openTimer !== null) {
clearTimeout(this._openTimer);
this._openTimer = null;
}
clearTimeout(this._closeTimer);
var closeDelay = Number(this.closeDelay);
if (closeDelay > 0) {
this._closeTimer = setTimeout(function () {
_this3._closeTimer = null;
_this3.doClose();
}, closeDelay);
} else {
this.doClose();
}
},
doClose: function doClose() {
var _this4 = this;
this.visible = false;
this.$emit('input', false);
this._closing = true;
this.onClose && this.onClose();
if (this.lockScroll) {
setTimeout(function () {
if (_this4.modal && _this4.bodyOverflow !== 'hidden') {
document.body.style.overflow = _this4.bodyOverflow;
document.body.style.paddingRight = _this4.bodyPaddingRight;
}
_this4.bodyOverflow = null;
_this4.bodyPaddingRight = null;
}, 200);
}
this.opened = false;
if (!this.transition) {
this.doAfterClose();
}
},
doAfterClose: function doAfterClose() {
_popupManager2.default.closeModal(this._popupId);
this._closing = false;
}
}
};
exports.PopupManager = _popupManager2.default;