Skip to content

Commit

Permalink
feat($ionicPopup): on android, make back button close popup
Browse files Browse the repository at this point in the history
Fixes #1222
  • Loading branch information
ajoslin committed Apr 29, 2014
1 parent 5e56c2d commit b87bcb3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
9 changes: 6 additions & 3 deletions js/angular/service/actionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
};

// Support Android back button to close
scope.$deregisterBackButton = $ionicPlatform.registerBackButtonAction(function(){
hideSheet();
}, 300);
scope.$deregisterBackButton = $ionicPlatform.registerBackButtonAction(
function(){
hideSheet();
},
PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET
);

scope.cancel = function() {
hideSheet(true);
Expand Down
3 changes: 3 additions & 0 deletions js/angular/service/platform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var PLATFORM_BACK_BUTTON_PRIORITY_VIEW = 100;
var PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET = 300;
var PLATFORM_BACK_BUTTON_PRIORITY_POPUP = 500;
/**
* @ngdoc service
* @name $ionicPlatform
Expand Down
14 changes: 11 additions & 3 deletions js/angular/service/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,15 @@ var POPUP_TPL =
*/
IonicModule
.factory('$ionicPopup', [
'$animate',
'$ionicTemplateLoader',
'$ionicBackdrop',
'$log',
'$q',
'$timeout',
'$rootScope',
'$document',
'$compile',
function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $rootScope, $document, $compile) {
'$ionicPlatform',
function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $document, $compile, $ionicPlatform) {
//TODO allow this to be configured
var config = {
stackPushDelay: 50
Expand Down Expand Up @@ -348,6 +347,10 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
});
}

function onHardwareBackButton(e) {
popupStack[0] && popupStack[0].responseDeferred.resolve();
}

function showPopup(options) {
var popupPromise = $ionicPopup._createPopup(options);
var previousPopup = popupStack[0];
Expand All @@ -363,6 +366,10 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
//Add popup-open & backdrop if this is first popup
document.body.classList.add('popup-open');
$ionicBackdrop.retain();
$ionicPopup._backButtonActionDone = $ionicPlatform.registerBackButtonAction(
onHardwareBackButton,
PLATFORM_BACK_BUTTON_PRIORITY_POPUP
);
}
popupStack.unshift(popup);
popup.show();
Expand All @@ -386,6 +393,7 @@ function($animate, $ionicTemplateLoader, $ionicBackdrop, $log, $q, $timeout, $ro
//Remove popup-open & backdrop if this is last popup
document.body.classList.remove('popup-open');
$ionicBackdrop.release();
($ionicPopup._backButtonActionDone || angular.noop)();
}

return result;
Expand Down
5 changes: 4 additions & 1 deletion js/angular/service/viewService.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ function($rootScope, $state, $location, $document, $animate, $ionicPlatform, $io
e.preventDefault();
return false;
}
$ionicPlatform.registerBackButtonAction(onHardwareBackButton, 100);
$ionicPlatform.registerBackButtonAction(
onHardwareBackButton,
PLATFORM_BACK_BUTTON_PRIORITY_VIEW
);

}])

Expand Down
16 changes: 13 additions & 3 deletions test/unit/angular/service/popup.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,21 @@ describe('$ionicPopup service', function() {
afterEach(function() {
document.body.classList.remove('popup-open');
});
it('should add popup-open and retain backdrop if no previous popup', inject(function($ionicBackdrop, $timeout) {

it('should add popup-open and retain backdrop and register back button action if no previous popup', inject(function($ionicBackdrop, $timeout, $ionicPlatform) {
spyOn($ionicPlatform, 'registerBackButtonAction').andReturn('actionReturn');
spyOn($ionicBackdrop, 'retain');
$ionicPopup.show();
$timeout.flush();
expect(angular.element(document.body).hasClass('popup-open')).toBe(true);
expect($ionicBackdrop.retain).toHaveBeenCalled();
expect($ionicPlatform.registerBackButtonAction).toHaveBeenCalledWith(
jasmine.any(Function),
PLATFORM_BACK_BUTTON_PRIORITY_POPUP
);
expect($ionicPopup._backButtonActionDone).toBe('actionReturn');
}));

it('should hide previous popup if exists and not popup-open & backdrop', inject(function($ionicBackdrop, $timeout) {
var previousPopup = { hide: jasmine.createSpy('hide') };
spyOn($ionicBackdrop, 'retain');
Expand Down Expand Up @@ -231,19 +239,21 @@ describe('$ionicPopup service', function() {
expect(previousPopup.show).toHaveBeenCalled();
}));

it('should release backdrop and remove popup-open if no previous', inject(function($q, $timeout, $ionicBackdrop) {
it('should release backdrop and remove popup-open and deregister back if no previous', inject(function($q, $timeout, $ionicBackdrop, $ionicPlatform) {
var fakePopup = {
show: jasmine.createSpy('show'),
remove: jasmine.createSpy('remove'),
responseDeferred: $q.defer()
};
document.body.classList.add('popup-open');
var backDoneSpy = jasmine.createSpy('backDone');
spyOn($ionicPlatform, 'registerBackButtonAction').andReturn(backDoneSpy);
spyOn($ionicBackdrop, 'release');
spyOn($ionicPopup, '_createPopup').andReturn($q.when(fakePopup));
$ionicPopup.show();
fakePopup.responseDeferred.resolve();
$timeout.flush();
expect($ionicBackdrop.release).toHaveBeenCalled();
expect(backDoneSpy).toHaveBeenCalled();
expect(document.body.classList.contains('popup-open')).toBe(false);
}));
});
Expand Down

0 comments on commit b87bcb3

Please sign in to comment.