Skip to content

Commit

Permalink
fix($ionicActionSheet): fix problems with cancel() not being called
Browse files Browse the repository at this point in the history
Closes #1013, #1576.
  • Loading branch information
hallucynogenyc authored and ajoslin committed Jun 6, 2014
1 parent dfdf084 commit 323e2ce
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions js/angular/service/actionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
* - `{string}` `titleText` The title to show on the action sheet.
* - `{string=}` `cancelText` The text for a 'cancel' button on the action sheet.
* - `{string=}` `destructiveText` The text for a 'danger' on the action sheet.
* - `{function=}` `cancel` Called if the cancel button is pressed or the backdrop is tapped.
* - `{function=}` `cancel` Called if the cancel button is pressed, the backdrop is tapped or
* the hardware back button is pressed.
* - `{function=}` `buttonClicked` Called when one of the non-destructive buttons is clicked,
* with the index of the button that was clicked and the button object. Return true to close
* the action sheet, or false to keep it opened.
Expand All @@ -77,26 +78,17 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
show: function(opts) {
var scope = $rootScope.$new(true);

extend(scope, {
cancel: angular.noop,
buttonClicked: angular.noop,
destructiveButtonClicked: angular.noop,
buttons: []
}, opts);
angular.extend(scope, opts);

// Compile the template
var element = $compile('<ion-action-sheet buttons="buttons"></ion-action-sheet>')(scope);

// Grab the sheet element for animation
var sheetEl = jqLite(element[0].querySelector('.action-sheet-wrapper'));

var hideSheet = function(didCancel) {
// removes the actionSheet from the screen
var hideSheet = function(h) {
sheetEl.removeClass('action-sheet-up');
if(didCancel) {
$timeout(function(){
opts.cancel();
}, 200);
}

$animate.removeClass(element, 'active', function() {
scope.$destroy();
Expand All @@ -107,31 +99,36 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
scope.$deregisterBackButton && scope.$deregisterBackButton();
};

// Support Android back button to close
// registerBackButtonAction returns a callback to deregister the action
scope.$deregisterBackButton = $ionicPlatform.registerBackButtonAction(
function(){
hideSheet();
scope.cancel(); //
},
PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET
);


// called when the user presses the cancel button
scope.cancel = function() {
hideSheet(true);
hideSheet();
$timeout(function(){
// after the animation is out, call the cancel callback
opts.cancel && opts.cancel();
},200)
};

scope.buttonClicked = function(index) {
// Check if the button click event returned true, which means
// we can close the action sheet
if((opts.buttonClicked && opts.buttonClicked(index, opts.buttons[index])) === true) {
hideSheet(false);
hideSheet();
}
};

scope.destructiveButtonClicked = function() {
// Check if the destructive button click event returned true, which means
// we can close the action sheet
if((opts.destructiveButtonClicked && opts.destructiveButtonClicked()) === true) {
hideSheet(false);
hideSheet();
}
};

Expand Down

0 comments on commit 323e2ce

Please sign in to comment.