Skip to content

Commit

Permalink
add destroy-on-close support for modals
Browse files Browse the repository at this point in the history
  • Loading branch information
soumak77 committed Aug 11, 2016
1 parent ee6c203 commit cb3c5f2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
7 changes: 7 additions & 0 deletions docs/partials/examples-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ <h4>Yo, do you <em>really</em> want to do this?</h4>
<div zf-modal zf-swipe-close="up" animation-out="slideOutUp" id="example-touch-modal" class="tiny dialog">
<p class="text-center">Swipe up on this modal to close it.</p>
</div>

<!-- Auto destroy modal -->
<div zf-modal id="autoDestroyModal" destroy-on-close="true">
<a zf-close class="close-button">&times;</a>
<h3>One Hit Wonder</h3>
<p>Close me and you'll never see me again!</p>
</div>
22 changes: 22 additions & 0 deletions docs/templates/modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ <h4>Yo, do you <em>really</em> want to do this?</h4>

<hr>

<h3>One-time Modals</h3>

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

<div class="text-center">
<a class="button" zf-open="autoDestroyModal">Auto Destroy Modal</a>
</div>

<hljs language="html">
<a zf-open="autoDestroyModal" class="button">Auto Destroy Modal</a>

<div zf-modal id="autoDestroyModal" destroy-on-close="true">
<a zf-close class="close-button">&times;</a>
<h3>One Hit Wonder</h3>
<p>Close me and you'll never see me again!</p>
</div>
</hljs>

<hr>

<h3>Programmatic Modals</h3>

<p>Modals can be created on the fly using the <code>ModalFactory</code>. Clicking the button will execute the code shown below.</p>
Expand All @@ -136,6 +156,8 @@ <h3>Programmatic Modals</h3>
'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
Expand Down
42 changes: 27 additions & 15 deletions js/angular/components/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
};
Expand Down Expand Up @@ -159,7 +175,7 @@
element.addClass('is-active');
}

animateFn(dialog, scope.active, animationIn, animationOut);
return animateFn(dialog, scope.active, animationIn, animationOut);
}
}
}
Expand Down Expand Up @@ -189,6 +205,7 @@
'animationOut',
'overlay',
'overlayClose',
'destroyOnClose',
'ignoreAllClose',
'class'
];
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}
});
Expand All @@ -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,
Expand All @@ -424,19 +442,13 @@
config.enterCallback(data.value);
}
modal.deactivate();
$timeout(function() {
modal.destroy();
}, 1000);
}
},
cancel: function() {
if (config.cancelCallback) {
config.cancelCallback();
}
modal.deactivate();
$timeout(function() {
modal.destroy();
}, 1000);
}
}
});
Expand Down

0 comments on commit cb3c5f2

Please sign in to comment.