From cb3c5f2ad08ed725d0c4294e4a456de772900cac Mon Sep 17 00:00:00 2001 From: Brian Soumakian Date: Thu, 11 Aug 2016 14:53:42 -0700 Subject: [PATCH] add destroy-on-close support for modals https://github.com/base-apps/angular-base-apps/issues/16 --- docs/partials/examples-modal.html | 7 +++++ docs/templates/modal.html | 22 +++++++++++++++ js/angular/components/modal/modal.js | 42 ++++++++++++++++++---------- 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/docs/partials/examples-modal.html b/docs/partials/examples-modal.html index e1ec24d..71464f0 100644 --- a/docs/partials/examples-modal.html +++ b/docs/partials/examples-modal.html @@ -43,3 +43,10 @@

Yo, do you really want to do this?

Swipe up on this modal to close it.

+ + +
+ × +

One Hit Wonder

+

Close me and you'll never see me again!

+
diff --git a/docs/templates/modal.html b/docs/templates/modal.html index ac341f1..215d00e 100644 --- a/docs/templates/modal.html +++ b/docs/templates/modal.html @@ -119,6 +119,26 @@

Yo, do you really want to do this?


+

One-time Modals

+ +

A modal can be configured to only display once and never again using the destroy-on-close attribute. The modal will be completely removed from the DOM once closed.

+ +
+ Auto Destroy Modal +
+ + +Auto Destroy Modal + +
+ × +

One Hit Wonder

+

Close me and you'll never see me again!

+
+
+ +
+

Programmatic Modals

Modals can be created on the fly using the ModalFactory. Clicking the button will execute the code shown below.

@@ -136,6 +156,8 @@

Programmatic Modals

'overlay': true, // Set if the modal can be closed by clicking on the overlay 'overlayClose': false, + // Set if the modal should be destroyed once closed + 'destroyOnClose': false, // Define a template to use for the modal 'templateUrl': 'partials/examples-dynamic-modal.html', // Allows you to pass in properties to the scope of the modal diff --git a/js/angular/components/modal/modal.js b/js/angular/components/modal/modal.js index d680f94..dd354b3 100644 --- a/js/angular/components/modal/modal.js +++ b/js/angular/components/modal/modal.js @@ -75,6 +75,7 @@ scope.active = false; scope.overlay = attrs.overlay === 'false' ? false : true; + scope.destroyOnClose = attrs.destroyOnClose === 'true' ? true : false; scope.overlayClose = attrs.overlayClose === 'false' ? false : true; var animationIn = attrs.animationIn || 'fadeIn'; @@ -93,7 +94,22 @@ if (scope.active) { scope.active = false; adviseActiveChanged(); - animate(); + + if (scope.destroyOnClose) { + animate().then(function() { + element.remove(); + scope.$destroy(); + }); + } else { + animate(); + } + } else { + if (scope.destroyOnClose) { + $timeout(function() { + element.remove(); + scope.$destroy(); + }, 0, false); + } } return; }; @@ -159,7 +175,7 @@ element.addClass('is-active'); } - animateFn(dialog, scope.active, animationIn, animationOut); + return animateFn(dialog, scope.active, animationIn, animationOut); } } } @@ -189,6 +205,7 @@ 'animationOut', 'overlay', 'overlayClose', + 'destroyOnClose', 'ignoreAllClose', 'class' ]; @@ -305,7 +322,12 @@ element.attr('animation-out', config[prop]); break; case 'overlayClose': - element.attr('overlay-close', (config[prop] === 'false' || config[prop] === false) ? 'false' : 'true'); // must be string, see postLink() above + // must be string, see postLink() above + element.attr('overlay-close', (config[prop] === 'false' || config[prop] === false) ? 'false' : 'true'); + break; + case 'destroyOnClose': + // must be string, see postLink() above + element.attr('destroy-on-close', (config[prop] === 'true' || config[prop] === true) ? 'true' : 'false'); break; case 'ignoreAllClose': element.attr('zf-ignore-all-close', 'zf-ignore-all-close'); @@ -362,6 +384,7 @@ 'class': 'tiny dialog confirm-modal', 'overlay': true, 'overlayClose': false, + 'destroyOnClose': true, 'templateUrl': 'components/modal/modal-confirm.html', 'contentScope': { title: config.title, @@ -374,18 +397,12 @@ config.enterCallback(); } modal.deactivate(); - $timeout(function() { - modal.destroy(); - }, 1000); }, cancel: function() { if (config.cancelCallback) { config.cancelCallback(); } modal.deactivate(); - $timeout(function() { - modal.destroy(); - }, 1000); } } }); @@ -409,6 +426,7 @@ 'class': 'tiny dialog prompt-modal', 'overlay': true, 'overlayClose': false, + 'destroyOnClose': true, 'templateUrl': 'components/modal/modal-prompt.html', 'contentScope': { title: config.title, @@ -424,9 +442,6 @@ config.enterCallback(data.value); } modal.deactivate(); - $timeout(function() { - modal.destroy(); - }, 1000); } }, cancel: function() { @@ -434,9 +449,6 @@ config.cancelCallback(); } modal.deactivate(); - $timeout(function() { - modal.destroy(); - }, 1000); } } });