2016-11-08 15:20:30 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
let Spinner = Vue.extend(require('./spinner.vue'));
|
|
|
|
|
2016-07-27 06:15:02 +00:00
|
|
|
exports.install = Vue => {
|
2016-08-29 11:01:42 +00:00
|
|
|
let toggleLoading = (el, binding) => {
|
|
|
|
if (binding.value) {
|
|
|
|
Vue.nextTick(() => {
|
|
|
|
if (binding.modifiers.fullscreen) {
|
|
|
|
el.originalPosition = document.body.style.position;
|
|
|
|
el.originalOverflow = document.body.style.overflow;
|
|
|
|
|
|
|
|
['top', 'right', 'bottom', 'left'].forEach(property => {
|
|
|
|
el.maskStyle[property] = '0';
|
|
|
|
});
|
|
|
|
el.maskStyle.position = 'fixed';
|
|
|
|
el.spinnerStyle.position = 'fixed';
|
|
|
|
|
|
|
|
insertDom(document.body, el, binding);
|
|
|
|
} else {
|
|
|
|
if (binding.modifiers.body) {
|
|
|
|
el.originalPosition = document.body.style.position;
|
|
|
|
|
|
|
|
['top', 'left'].forEach(property => {
|
|
|
|
let scroll = property === 'top' ? 'scrollTop' : 'scrollLeft';
|
|
|
|
el.maskStyle[property] = el.getBoundingClientRect()[property] + document.body[scroll] + document.documentElement[scroll] + 'px';
|
|
|
|
});
|
|
|
|
['height', 'width'].forEach(property => {
|
|
|
|
el.maskStyle[property] = el.getBoundingClientRect()[property] + 'px';
|
|
|
|
});
|
|
|
|
|
|
|
|
insertDom(document.body, el, binding);
|
|
|
|
} else {
|
|
|
|
el.originalPosition = el.style.position;
|
|
|
|
|
|
|
|
['top', 'right', 'bottom', 'left'].forEach(property => {
|
|
|
|
el.maskStyle[property] = '0';
|
|
|
|
});
|
|
|
|
|
|
|
|
insertDom(el, el, binding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (el.domVisible) {
|
|
|
|
el.mask.style.display = 'none';
|
|
|
|
el.spinner.style.display = 'none';
|
|
|
|
el.domVisible = false;
|
|
|
|
|
2016-11-13 03:46:20 +00:00
|
|
|
if (binding.modifiers.fullscreen && el.originalOverflow !== 'hidden') {
|
2016-08-29 11:01:42 +00:00
|
|
|
document.body.style.overflow = el.originalOverflow;
|
|
|
|
}
|
|
|
|
if (binding.modifiers.fullscreen || binding.modifiers.body) {
|
|
|
|
document.body.style.position = el.originalPosition;
|
|
|
|
} else {
|
|
|
|
el.style.position = el.originalPosition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-08-12 07:49:47 +00:00
|
|
|
let insertDom = (parent, directive, binding) => {
|
2016-07-27 06:15:02 +00:00
|
|
|
if (!directive.domVisible) {
|
|
|
|
Object.keys(directive.maskStyle).forEach(property => {
|
|
|
|
directive.mask.style[property] = directive.maskStyle[property];
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(directive.spinnerStyle).forEach(property => {
|
|
|
|
directive.spinner.style[property] = directive.spinnerStyle[property];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (directive.originalPosition !== 'absolute') {
|
|
|
|
parent.style.position = 'relative';
|
|
|
|
}
|
2016-10-13 12:05:42 +00:00
|
|
|
if (binding.modifiers.fullscreen && binding.modifiers.lock) {
|
2016-07-27 06:15:02 +00:00
|
|
|
parent.style.overflow = 'hidden';
|
|
|
|
}
|
|
|
|
directive.mask.style.display = 'block';
|
|
|
|
directive.spinner.style.display = 'inline-block';
|
|
|
|
directive.domVisible = true;
|
|
|
|
|
|
|
|
parent.appendChild(directive.mask);
|
|
|
|
directive.mask.appendChild(directive.spinner);
|
|
|
|
directive.domInserted = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Vue.directive('loading', {
|
2016-08-29 11:01:42 +00:00
|
|
|
bind: function(el, binding) {
|
2016-08-12 07:49:47 +00:00
|
|
|
el.mask = document.createElement('div');
|
|
|
|
el.mask.className = 'el-loading-mask';
|
|
|
|
el.maskStyle = {
|
2016-07-27 06:15:02 +00:00
|
|
|
position: 'absolute',
|
|
|
|
zIndex: '10000',
|
2016-11-08 15:20:30 +00:00
|
|
|
backgroundColor: 'rgba(255, 255, 255, .9)',
|
2016-07-27 06:15:02 +00:00
|
|
|
margin: '0'
|
|
|
|
};
|
|
|
|
|
2016-11-08 15:20:30 +00:00
|
|
|
let spinner = new Spinner({
|
|
|
|
data: {
|
|
|
|
text: el.getAttribute('element-loading-text'),
|
|
|
|
fullScreen: !!binding.modifiers.fullscreen
|
|
|
|
}
|
|
|
|
});
|
|
|
|
spinner.$mount(el.mask);
|
|
|
|
el.spinner = spinner.$el;
|
2016-08-12 07:49:47 +00:00
|
|
|
el.spinnerStyle = {
|
2016-08-25 10:37:03 +00:00
|
|
|
position: 'absolute'
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
2016-08-29 11:01:42 +00:00
|
|
|
toggleLoading(el, binding);
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
2016-08-12 07:49:47 +00:00
|
|
|
update: function(el, binding) {
|
2016-10-18 04:16:15 +00:00
|
|
|
if (binding.oldValue !== binding.value) {
|
|
|
|
toggleLoading(el, binding);
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
|
2016-08-12 07:49:47 +00:00
|
|
|
unbind: function(el, binding) {
|
|
|
|
if (el.domInserted) {
|
|
|
|
if (binding.modifiers.fullscreen || binding.modifiers.body) {
|
|
|
|
document.body.removeChild(el.mask);
|
|
|
|
el.mask.removeChild(el.spinner);
|
2016-07-27 06:15:02 +00:00
|
|
|
} else {
|
2016-11-03 09:07:50 +00:00
|
|
|
el.mask &&
|
|
|
|
el.mask.parentNode &&
|
|
|
|
el.mask.parentNode.removeChild(el.mask);
|
|
|
|
el.spinner &&
|
|
|
|
el.spinner.parentNode &&
|
|
|
|
el.spinner.parentNode.removeChild(el.spinner);
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|