From 813de473548c2e661a5dd910a8b77956a67bc0a8 Mon Sep 17 00:00:00 2001 From: Leopoldthecoder Date: Fri, 13 Jan 2017 15:31:05 +0800 Subject: [PATCH] Loading: add fading animations --- packages/loading/src/directive.js | 46 +++++++++++++++----------- packages/loading/src/index.js | 17 +++++++--- packages/loading/src/loading.vue | 20 +++++++---- packages/theme-default/src/loading.css | 6 ++++ test/unit/specs/loading.spec.js | 28 +++++++++++----- 5 files changed, 78 insertions(+), 39 deletions(-) diff --git a/packages/loading/src/directive.js b/packages/loading/src/directive.js index 11943f29f..ffda96472 100644 --- a/packages/loading/src/directive.js +++ b/packages/loading/src/directive.js @@ -36,37 +36,42 @@ exports.install = Vue => { }); } else { if (el.domVisible) { - el.mask.style.display = 'none'; - el.domVisible = false; - - if (binding.modifiers.fullscreen && el.originalOverflow !== 'hidden') { - 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; - } + const destroyElement = function() { + el.mask.removeEventListener('transitionend', destroyElement); + el.domVisible = false; + if (binding.modifiers.fullscreen && el.originalOverflow !== 'hidden') { + 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; + } + }; + el.mask.addEventListener('transitionend', destroyElement); + el.instance.visible = false; } } }; - let insertDom = (parent, directive, binding) => { - if (!directive.domVisible) { - Object.keys(directive.maskStyle).forEach(property => { - directive.mask.style[property] = directive.maskStyle[property]; + let insertDom = (parent, el, binding) => { + if (!el.domVisible) { + Object.keys(el.maskStyle).forEach(property => { + el.mask.style[property] = el.maskStyle[property]; }); - if (directive.originalPosition !== 'absolute') { + if (el.originalPosition !== 'absolute') { parent.style.position = 'relative'; } if (binding.modifiers.fullscreen && binding.modifiers.lock) { parent.style.overflow = 'hidden'; } - directive.mask.style.display = 'block'; - directive.domVisible = true; + el.domVisible = true; - parent.appendChild(directive.mask); - directive.domInserted = true; + parent.appendChild(el.mask); + Vue.nextTick(() => { + el.instance.visible = true; + }); + el.domInserted = true; } }; @@ -79,6 +84,7 @@ exports.install = Vue => { fullscreen: !!binding.modifiers.fullscreen } }); + el.instance = mask; el.mask = mask.$el; el.maskStyle = {}; diff --git a/packages/loading/src/index.js b/packages/loading/src/index.js index 24c06032f..c5182fb71 100644 --- a/packages/loading/src/index.js +++ b/packages/loading/src/index.js @@ -17,6 +17,14 @@ let fullscreenLoading; LoadingConstructor.prototype.originalPosition = ''; LoadingConstructor.prototype.originalOverflow = ''; +const destroyElement = function() { + this.$el.removeEventListener('transitionend', destroyElement); + this.$el && + this.$el.parentNode && + this.$el.parentNode.removeChild(this.$el); + this.$destroy(); +}; + LoadingConstructor.prototype.close = function() { if (this.fullscreen && this.originalOverflow !== 'hidden') { document.body.style.overflow = this.originalOverflow; @@ -29,10 +37,8 @@ LoadingConstructor.prototype.close = function() { if (this.fullscreen) { fullscreenLoading = undefined; } - this.$el && - this.$el.parentNode && - this.$el.parentNode.removeChild(this.$el); - this.$destroy(); + this.$el.addEventListener('transitionend', destroyElement.bind(this)); + this.visible = false; }; const addStyle = (options, parent, instance) => { @@ -90,6 +96,9 @@ const Loading = (options = {}) => { parent.style.overflow = 'hidden'; } parent.appendChild(instance.$el); + Vue.nextTick(() => { + instance.visible = true; + }); if (options.fullscreen) { fullscreenLoading = instance; } diff --git a/packages/loading/src/loading.vue b/packages/loading/src/loading.vue index 30a295642..9a78483ad 100644 --- a/packages/loading/src/loading.vue +++ b/packages/loading/src/loading.vue @@ -1,12 +1,17 @@