2017-06-30 14:49:20 +00:00
|
|
|
import Vue from 'vue';
|
2017-10-11 10:00:58 +00:00
|
|
|
import Loading from './loading.vue';
|
2017-06-30 14:49:20 +00:00
|
|
|
import { addClass, removeClass, getStyle } from 'element-ui/src/utils/dom';
|
2017-10-11 10:00:58 +00:00
|
|
|
const Mask = Vue.extend(Loading);
|
2016-11-08 15:20:30 +00:00
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
exports.install = Vue => {
|
2017-06-30 14:49:20 +00:00
|
|
|
if (Vue.prototype.$isServer) return;
|
2017-09-21 11:14:06 +00:00
|
|
|
const toggleLoading = (el, binding) => {
|
2016-08-29 11:01:42 +00:00
|
|
|
if (binding.value) {
|
|
|
|
Vue.nextTick(() => {
|
|
|
|
if (binding.modifiers.fullscreen) {
|
2017-08-18 08:58:18 +00:00
|
|
|
el.originalPosition = getStyle(document.body, 'position');
|
|
|
|
el.originalOverflow = getStyle(document.body, 'overflow');
|
2016-08-29 11:01:42 +00:00
|
|
|
|
2017-06-30 14:49:20 +00:00
|
|
|
addClass(el.mask, 'is-fullscreen');
|
|
|
|
insertDom(document.body, el, binding);
|
2016-08-29 11:01:42 +00:00
|
|
|
} else {
|
2017-06-30 14:49:20 +00:00
|
|
|
removeClass(el.mask, 'is-fullscreen');
|
2016-11-13 13:12:04 +00:00
|
|
|
|
2016-08-29 11:01:42 +00:00
|
|
|
if (binding.modifiers.body) {
|
2017-08-18 08:58:18 +00:00
|
|
|
el.originalPosition = getStyle(document.body, 'position');
|
2016-08-29 11:01:42 +00:00
|
|
|
|
|
|
|
['top', 'left'].forEach(property => {
|
2017-09-21 11:14:06 +00:00
|
|
|
const scroll = property === 'top' ? 'scrollTop' : 'scrollLeft';
|
2017-06-30 14:49:20 +00:00
|
|
|
el.maskStyle[property] = el.getBoundingClientRect()[property] + document.body[scroll] + document.documentElement[scroll] + 'px';
|
2016-08-29 11:01:42 +00:00
|
|
|
});
|
|
|
|
['height', 'width'].forEach(property => {
|
2017-06-30 14:49:20 +00:00
|
|
|
el.maskStyle[property] = el.getBoundingClientRect()[property] + 'px';
|
|
|
|
});
|
2016-08-29 11:01:42 +00:00
|
|
|
|
2017-06-30 14:49:20 +00:00
|
|
|
insertDom(document.body, el, binding);
|
2016-08-29 11:01:42 +00:00
|
|
|
} else {
|
2017-08-18 08:58:18 +00:00
|
|
|
el.originalPosition = getStyle(el, 'position');
|
2017-06-30 14:49:20 +00:00
|
|
|
insertDom(el, el, binding);
|
2016-08-29 11:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 14:49:20 +00:00
|
|
|
});
|
2016-08-29 11:01:42 +00:00
|
|
|
} else {
|
|
|
|
if (el.domVisible) {
|
2017-01-13 11:52:26 +00:00
|
|
|
el.instance.$on('after-leave', _ => {
|
2017-06-30 14:49:20 +00:00
|
|
|
el.domVisible = false;
|
2017-01-13 07:31:05 +00:00
|
|
|
if (binding.modifiers.fullscreen && el.originalOverflow !== 'hidden') {
|
2017-06-30 14:49:20 +00:00
|
|
|
document.body.style.overflow = el.originalOverflow;
|
2017-01-13 07:31:05 +00:00
|
|
|
}
|
|
|
|
if (binding.modifiers.fullscreen || binding.modifiers.body) {
|
2017-06-30 14:49:20 +00:00
|
|
|
document.body.style.position = el.originalPosition;
|
2017-01-13 07:31:05 +00:00
|
|
|
} else {
|
2017-06-30 14:49:20 +00:00
|
|
|
el.style.position = el.originalPosition;
|
2017-01-13 07:31:05 +00:00
|
|
|
}
|
2017-06-30 14:49:20 +00:00
|
|
|
});
|
|
|
|
el.instance.visible = false;
|
2016-08-29 11:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 14:49:20 +00:00
|
|
|
};
|
2017-09-21 11:14:06 +00:00
|
|
|
const insertDom = (parent, el, binding) => {
|
2017-06-30 14:25:16 +00:00
|
|
|
if (!el.domVisible && getStyle(el, 'display') !== 'none' && getStyle(el, 'visibility') !== 'hidden') {
|
2017-01-13 07:31:05 +00:00
|
|
|
Object.keys(el.maskStyle).forEach(property => {
|
2017-06-30 14:49:20 +00:00
|
|
|
el.mask.style[property] = el.maskStyle[property];
|
|
|
|
});
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2017-08-18 08:58:18 +00:00
|
|
|
if (el.originalPosition !== 'absolute' && el.originalPosition !== 'fixed') {
|
2017-06-30 14:49:20 +00:00
|
|
|
parent.style.position = 'relative';
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2016-10-13 12:05:42 +00:00
|
|
|
if (binding.modifiers.fullscreen && binding.modifiers.lock) {
|
2017-06-30 14:49:20 +00:00
|
|
|
parent.style.overflow = 'hidden';
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2017-06-30 14:49:20 +00:00
|
|
|
el.domVisible = true;
|
2016-07-27 06:15:02 +00:00
|
|
|
|
2017-06-30 14:49:20 +00:00
|
|
|
parent.appendChild(el.mask);
|
2017-01-13 07:31:05 +00:00
|
|
|
Vue.nextTick(() => {
|
2017-06-30 14:49:20 +00:00
|
|
|
el.instance.visible = true;
|
|
|
|
});
|
|
|
|
el.domInserted = true;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2017-06-30 14:49:20 +00:00
|
|
|
};
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
Vue.directive('loading', {
|
2017-10-09 09:34:29 +00:00
|
|
|
bind: function(el, binding, vnode) {
|
|
|
|
const textExr = el.getAttribute('element-loading-text');
|
|
|
|
const spinnerExr = el.getAttribute('element-loading-spinner');
|
|
|
|
const backgroundExr = el.getAttribute('element-loading-background');
|
|
|
|
const vm = vnode.context;
|
2017-09-21 11:14:06 +00:00
|
|
|
const mask = new Mask({
|
2016-11-15 12:29:33 +00:00
|
|
|
el: document.createElement('div'),
|
2016-11-08 15:20:30 +00:00
|
|
|
data: {
|
2017-10-09 09:34:29 +00:00
|
|
|
text: vm && vm[textExr] || textExr,
|
|
|
|
spinner: vm && vm[spinnerExr] || spinnerExr,
|
|
|
|
background: vm && vm[backgroundExr] || backgroundExr,
|
2016-11-15 12:29:33 +00:00
|
|
|
fullscreen: !!binding.modifiers.fullscreen
|
2016-11-08 15:20:30 +00:00
|
|
|
}
|
2017-06-30 14:49:20 +00:00
|
|
|
});
|
|
|
|
el.instance = mask;
|
|
|
|
el.mask = mask.$el;
|
|
|
|
el.maskStyle = {};
|
2016-11-15 12:29:33 +00:00
|
|
|
|
2017-06-30 14:49:20 +00:00
|
|
|
toggleLoading(el, binding);
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
2017-06-30 14:49:20 +00:00
|
|
|
update: function(el, binding) {
|
|
|
|
el.instance.setText(el.getAttribute('element-loading-text'));
|
2016-10-18 04:16:15 +00:00
|
|
|
if (binding.oldValue !== binding.value) {
|
2017-06-30 14:49:20 +00:00
|
|
|
toggleLoading(el, binding);
|
2016-10-18 04:16:15 +00:00
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
2017-06-30 14:49:20 +00:00
|
|
|
unbind: function(el, binding) {
|
2016-08-12 07:49:47 +00:00
|
|
|
if (el.domInserted) {
|
2017-10-16 06:07:24 +00:00
|
|
|
el.mask &&
|
|
|
|
el.mask.parentNode &&
|
|
|
|
el.mask.parentNode.removeChild(el.mask);
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 14:49:20 +00:00
|
|
|
});
|
|
|
|
};
|