Skip to content

Commit

Permalink
refactor(modal): use ngAnimate for animation
Browse files Browse the repository at this point in the history
- Fix scope.$apply call not working previously because scope was already
  destroyed. Use $rootScope instead.
- Move $destroy call nearer to the dom removal.
- Remove fallback timer (emulateTime param)

Fixes angular-ui#2585
Fixes angular-ui#2674
Fixes angular-ui#2536
Fixes angular-ui#2790
Fixes angular-ui#3182
Closes angular-ui#2724
  • Loading branch information
chrisirhc authored and ayakovlevgh committed Mar 24, 2015
1 parent e14c717 commit 17b526b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 35 deletions.
26 changes: 10 additions & 16 deletions src/modal/modal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
angular.module('ui.bootstrap.modal', [])

/**
* A helper, internal data structure that acts as a map but also allows getting / removing
Expand Down Expand Up @@ -155,8 +155,8 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
};
})

.factory('$modalStack', ['$transition', '$timeout', '$document', '$compile', '$rootScope', '$$stackedMap',
function ($transition, $timeout, $document, $compile, $rootScope, $$stackedMap) {
.factory('$modalStack', ['$animate', '$timeout', '$document', '$compile', '$rootScope', '$$stackedMap',
function ($animate, $timeout, $document, $compile, $rootScope, $$stackedMap) {

var OPENED_MODAL_CLASS = 'modal-open';

Expand Down Expand Up @@ -190,8 +190,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
openedWindows.remove(modalInstance);

//remove window DOM element
removeAfterAnimate(modalWindow.modalDomEl, modalWindow.modalScope, 300, function() {
modalWindow.modalScope.$destroy();
removeAfterAnimate(modalWindow.modalDomEl, modalWindow.modalScope, function() {
body.toggleClass(OPENED_MODAL_CLASS, openedWindows.length() > 0);
checkRemoveBackdrop();
});
Expand All @@ -201,28 +200,22 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
//remove backdrop if no longer needed
if (backdropDomEl && backdropIndex() == -1) {
var backdropScopeRef = backdropScope;
removeAfterAnimate(backdropDomEl, backdropScope, 150, function () {
backdropScopeRef.$destroy();
removeAfterAnimate(backdropDomEl, backdropScope, function () {
backdropScopeRef = null;
});
backdropDomEl = undefined;
backdropScope = undefined;
}
}

function removeAfterAnimate(domEl, scope, emulateTime, done) {
function removeAfterAnimate(domEl, scope, done) {
// Closing animation
scope.animate = false;

var transitionEndEventName = $transition.transitionEndEventName;
if (transitionEndEventName) {
if ($animate.enabled()) {
// transition out
var timeout = $timeout(afterAnimating, emulateTime);

domEl.bind(transitionEndEventName, function () {
$timeout.cancel(timeout);
afterAnimating();
scope.$apply();
domEl.one('$animate:close', function closeFn() {
$rootScope.$evalAsync(afterAnimating);
});
} else {
// Ensure this call is async
Expand All @@ -236,6 +229,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
afterAnimating.done = true;

domEl.remove();
scope.$destroy();
if (done) {
done();
}
Expand Down
29 changes: 10 additions & 19 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ describe('$modal', function () {
element.trigger(e);
};

var waitForBackdropAnimation = function () {
inject(function ($transition) {
if ($transition.transitionEndEventName) {
$timeout.flush();
}
});
};

beforeEach(module('ui.bootstrap.modal'));
beforeEach(module('template/modal/backdrop.html'));
beforeEach(module('template/modal/window.html'));
Expand Down Expand Up @@ -106,16 +98,20 @@ describe('$modal', function () {
return modal;
}

function close(modal, result) {
function close(modal, result, noFlush) {
var closed = modal.close(result);
$timeout.flush();
if (!noFlush) {
$timeout.flush();
}
$rootScope.$digest();
return closed;
}

function dismiss(modal, reason) {
function dismiss(modal, reason, noFlush) {
var closed = modal.dismiss(reason);
$timeout.flush();
if (!noFlush) {
$timeout.flush();
}
$rootScope.$digest();
return closed;
}
Expand All @@ -134,7 +130,6 @@ describe('$modal', function () {

expect($document).toHaveModalsOpen(0);

waitForBackdropAnimation();
expect($document).not.toHaveBackdrop();
});

Expand All @@ -150,7 +145,7 @@ describe('$modal', function () {

expect($document).toHaveModalsOpen(0);

dismiss(modal, 'closing in test');
dismiss(modal, 'closing in test', true);
});

it('should not throw an exception on a second close', function () {
Expand All @@ -165,7 +160,7 @@ describe('$modal', function () {

expect($document).toHaveModalsOpen(0);

close(modal, 'closing in test');
close(modal, 'closing in test', true);
});

it('should open a modal from templateUrl', function () {
Expand All @@ -181,7 +176,6 @@ describe('$modal', function () {

expect($document).toHaveModalsOpen(0);

waitForBackdropAnimation();
expect($document).not.toHaveBackdrop();
});

Expand Down Expand Up @@ -244,8 +238,6 @@ describe('$modal', function () {
function openAndCloseModalWithAutofocusElement() {
var modal = open({template: '<div><input type="text" id="auto-focus-element" autofocus></div>'});

waitForBackdropAnimation();

expect(angular.element('#auto-focus-element')).toHaveFocus();

close(modal, 'closed ok');
Expand Down Expand Up @@ -505,7 +497,6 @@ describe('$modal', function () {
expect(backdropEl).toHaveClass('in');

dismiss(modal);
waitForBackdropAnimation();

modal = open({ template: '<div>With backdrop</div>' });
backdropEl = $document.find('body > div.modal-backdrop');
Expand Down

0 comments on commit 17b526b

Please sign in to comment.