mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-10-25 02:44:52 +08:00
*
This commit is contained in:
296
static/element-ui/lib/utils/popup/index.js
Normal file
296
static/element-ui/lib/utils/popup/index.js
Normal file
@@ -0,0 +1,296 @@
|
||||
'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;
|
||||
179
static/element-ui/lib/utils/popup/popup-manager.js
Normal file
179
static/element-ui/lib/utils/popup/popup-manager.js
Normal file
@@ -0,0 +1,179 @@
|
||||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
var _vue = require('vue');
|
||||
|
||||
var _vue2 = _interopRequireDefault(_vue);
|
||||
|
||||
var _dom = require('element-ui/lib/utils/dom');
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
var hasModal = false;
|
||||
|
||||
var getModal = function getModal() {
|
||||
if (_vue2.default.prototype.$isServer) return;
|
||||
var modalDom = PopupManager.modalDom;
|
||||
if (modalDom) {
|
||||
hasModal = true;
|
||||
} else {
|
||||
hasModal = false;
|
||||
modalDom = document.createElement('div');
|
||||
PopupManager.modalDom = modalDom;
|
||||
|
||||
modalDom.addEventListener('touchmove', function (event) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
});
|
||||
|
||||
modalDom.addEventListener('click', function () {
|
||||
PopupManager.doOnModalClick && PopupManager.doOnModalClick();
|
||||
});
|
||||
}
|
||||
|
||||
return modalDom;
|
||||
};
|
||||
|
||||
var instances = {};
|
||||
|
||||
var PopupManager = {
|
||||
zIndex: 2000,
|
||||
|
||||
modalFade: true,
|
||||
|
||||
getInstance: function getInstance(id) {
|
||||
return instances[id];
|
||||
},
|
||||
|
||||
register: function register(id, instance) {
|
||||
if (id && instance) {
|
||||
instances[id] = instance;
|
||||
}
|
||||
},
|
||||
|
||||
deregister: function deregister(id) {
|
||||
if (id) {
|
||||
instances[id] = null;
|
||||
delete instances[id];
|
||||
}
|
||||
},
|
||||
|
||||
nextZIndex: function nextZIndex() {
|
||||
return PopupManager.zIndex++;
|
||||
},
|
||||
|
||||
modalStack: [],
|
||||
|
||||
doOnModalClick: function doOnModalClick() {
|
||||
var topItem = PopupManager.modalStack[PopupManager.modalStack.length - 1];
|
||||
if (!topItem) return;
|
||||
|
||||
var instance = PopupManager.getInstance(topItem.id);
|
||||
if (instance && instance.closeOnClickModal) {
|
||||
instance.close();
|
||||
}
|
||||
},
|
||||
|
||||
openModal: function openModal(id, zIndex, dom, modalClass, modalFade) {
|
||||
if (_vue2.default.prototype.$isServer) return;
|
||||
if (!id || zIndex === undefined) return;
|
||||
this.modalFade = modalFade;
|
||||
|
||||
var modalStack = this.modalStack;
|
||||
|
||||
for (var i = 0, j = modalStack.length; i < j; i++) {
|
||||
var item = modalStack[i];
|
||||
if (item.id === id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var modalDom = getModal();
|
||||
|
||||
(0, _dom.addClass)(modalDom, 'v-modal');
|
||||
if (this.modalFade && !hasModal) {
|
||||
(0, _dom.addClass)(modalDom, 'v-modal-enter');
|
||||
}
|
||||
if (modalClass) {
|
||||
var classArr = modalClass.trim().split(/\s+/);
|
||||
classArr.forEach(function (item) {
|
||||
return (0, _dom.addClass)(modalDom, item);
|
||||
});
|
||||
}
|
||||
setTimeout(function () {
|
||||
(0, _dom.removeClass)(modalDom, 'v-modal-enter');
|
||||
}, 200);
|
||||
|
||||
if (dom && dom.parentNode && dom.parentNode.nodeType !== 11) {
|
||||
dom.parentNode.appendChild(modalDom);
|
||||
} else {
|
||||
document.body.appendChild(modalDom);
|
||||
}
|
||||
|
||||
if (zIndex) {
|
||||
modalDom.style.zIndex = zIndex;
|
||||
}
|
||||
modalDom.style.display = '';
|
||||
|
||||
this.modalStack.push({ id: id, zIndex: zIndex, modalClass: modalClass });
|
||||
},
|
||||
|
||||
closeModal: function closeModal(id) {
|
||||
var modalStack = this.modalStack;
|
||||
var modalDom = getModal();
|
||||
|
||||
if (modalStack.length > 0) {
|
||||
var topItem = modalStack[modalStack.length - 1];
|
||||
if (topItem.id === id) {
|
||||
if (topItem.modalClass) {
|
||||
var classArr = topItem.modalClass.trim().split(/\s+/);
|
||||
classArr.forEach(function (item) {
|
||||
return (0, _dom.removeClass)(modalDom, item);
|
||||
});
|
||||
}
|
||||
|
||||
modalStack.pop();
|
||||
if (modalStack.length > 0) {
|
||||
modalDom.style.zIndex = modalStack[modalStack.length - 1].zIndex;
|
||||
}
|
||||
} else {
|
||||
for (var i = modalStack.length - 1; i >= 0; i--) {
|
||||
if (modalStack[i].id === id) {
|
||||
modalStack.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (modalStack.length === 0) {
|
||||
if (this.modalFade) {
|
||||
(0, _dom.addClass)(modalDom, 'v-modal-leave');
|
||||
}
|
||||
setTimeout(function () {
|
||||
if (modalStack.length === 0) {
|
||||
if (modalDom.parentNode) modalDom.parentNode.removeChild(modalDom);
|
||||
modalDom.style.display = 'none';
|
||||
PopupManager.modalDom = undefined;
|
||||
}
|
||||
(0, _dom.removeClass)(modalDom, 'v-modal-leave');
|
||||
}, 200);
|
||||
}
|
||||
}
|
||||
};
|
||||
!_vue2.default.prototype.$isServer && window.addEventListener('keydown', function (event) {
|
||||
if (event.keyCode === 27) {
|
||||
// ESC
|
||||
if (PopupManager.modalStack.length > 0) {
|
||||
var topItem = PopupManager.modalStack[PopupManager.modalStack.length - 1];
|
||||
if (!topItem) return;
|
||||
var instance = PopupManager.getInstance(topItem.id);
|
||||
if (instance.closeOnPressEscape) {
|
||||
instance.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
exports.default = PopupManager;
|
||||
Reference in New Issue
Block a user