Skip to content

Commit

Permalink
fix(popup): close highest overlay
Browse files Browse the repository at this point in the history
Remember a stack of overlays to ensure the highest one is closed when
clicking the backdrop. Closes #2157
  • Loading branch information
adamdbradley committed Jun 8, 2015
1 parent 031f66f commit bcfe210
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
25 changes: 23 additions & 2 deletions js/angular/service/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
// on iOS, clicks will sometimes bleed through/ghost click on underlying
// elements
$ionicClickBlock.show(600);
stack.add(self);

var modalEl = jqLite(self.modalEl);

Expand Down Expand Up @@ -185,7 +186,7 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
if (!self._isShown) return;
//After animating in, allow hide on backdrop click
self.$el.on('click', function(e) {
if (self.backdropClickToClose && e.target === self.el) {
if (self.backdropClickToClose && e.target === self.el && stack.isHighest(self)) {
self.hide();
}
});
Expand All @@ -205,6 +206,7 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
// on iOS, clicks will sometimes bleed through/ghost click on underlying
// elements
$ionicClickBlock.show(600);
stack.remove(self);

self.el.classList.remove('active');
modalEl.addClass('ng-leave');
Expand Down Expand Up @@ -293,6 +295,23 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
return modal;
};

var modalStack = [];
var stack = {
add: function(modal) {
modalStack.push(modal);
},
remove: function(modal) {
var index = modalStack.indexOf(modal);
if (index > -1 && index < modalStack.length) {
modalStack.splice(index, 1);
}
},
isHighest: function(modal) {
var index = modalStack.indexOf(modal);
return (index > -1 && index === modalStack.length - 1);
}
};

return {
/**
* @ngdoc method
Expand Down Expand Up @@ -328,6 +347,8 @@ function($rootScope, $ionicBody, $compile, $timeout, $ionicPlatform, $ionicTempl
cb && cb(modal);
return modal;
});
}
},

stack: stack
};
}]);
7 changes: 5 additions & 2 deletions js/angular/service/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ IonicModule
'$ionicBody',
'$compile',
'$ionicPlatform',
'$ionicModal',
'IONIC_BACK_PRIORITY',
function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicBody, $compile, $ionicPlatform, IONIC_BACK_PRIORITY) {
function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicBody, $compile, $ionicPlatform, $ionicModal, IONIC_BACK_PRIORITY) {
//TODO allow this to be configured
var config = {
stackPushDelay: 75
Expand Down Expand Up @@ -321,6 +322,7 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
self.show = function() {
if (self.isShown || self.removed) return;

$ionicModal.stack.add(self);
self.isShown = true;
ionic.requestAnimationFrame(function() {
//if hidden while waiting for raf, don't show
Expand All @@ -336,14 +338,15 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
callback = callback || noop;
if (!self.isShown) return callback();

$ionicModal.stack.remove(self);
self.isShown = false;
self.element.removeClass('active');
self.element.addClass('popup-hidden');
$timeout(callback, 250, false);
};

self.remove = function() {
if (self.removed) return;
if (self.removed || !$ionicModal.stack.isHighest(self)) return;

self.hide(function() {
self.element.remove();
Expand Down
1 change: 1 addition & 0 deletions test/unit/angular/service/popup.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ describe('$ionicPopup service', function() {
spyOn(popup, 'hide').andCallFake(function(cb) {
cb();
});
popup.show();
spyOn(popup.element, 'remove');
spyOn(popup.scope, '$destroy');
popup.remove();
Expand Down

1 comment on commit bcfe210

@bhagwans
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to solve same issue with ionic2 ?

Please sign in to comment.