Lots of action sheet work

This commit is contained in:
Max Lynch
2013-11-12 23:25:57 -06:00
parent 664fe55ff8
commit 3f89bca290
7 changed files with 261 additions and 65 deletions

View File

@ -3,7 +3,7 @@
angular.module('ionic.ui.actionSheet', [])
.directive('actionSheet', function() {
.directive('actionSheet', function($document) {
return {
restrict: 'E',
scope: true,
@ -12,17 +12,26 @@ angular.module('ionic.ui.actionSheet', [])
$scope.$on('$destroy', function() {
$element.remove();
});
$document.bind('keyup', function(e) {
if(e.which == 27) {
$scope.cancel();
$scope.$apply();
}
});
},
template: '<div class="action-sheet slide-in-up">' +
'<div class="action-sheet-group">' +
'<div class="action-sheet-title" ng-if="titleText">{{titleText}}</div>' +
'<button class="button" ng-click="buttonClicked($index)" ng-repeat="button in buttons">{{button.text}}</button>' +
'</div>' +
'<div class="action-sheet-group" ng-if="destructiveText">' +
'<button class="button destructive" ng-click="destructiveButtonClicked()">{{destructiveText}}</button>' +
'</div>' +
'<div class="action-sheet-group" ng-if="cancelText">' +
'<button class="button" ng-click="cancel()">{{cancelText}}</button>' +
template: '<div class="action-sheet-backdrop">' +
'<div class="action-sheet slide-in-up">' +
'<div class="action-sheet-group">' +
'<div class="action-sheet-title" ng-if="titleText">{{titleText}}</div>' +
'<button class="button" ng-click="buttonClicked($index)" ng-repeat="button in buttons">{{button.text}}</button>' +
'</div>' +
'<div class="action-sheet-group" ng-if="destructiveText">' +
'<button class="button destructive" ng-click="destructiveButtonClicked()">{{destructiveText}}</button>' +
'</div>' +
'<div class="action-sheet-group" ng-if="cancelText">' +
'<button class="button" ng-click="cancel()">{{cancelText}}</button>' +
'</div>' +
'</div>' +
'</div>'
};

View File

@ -1,6 +1,8 @@
angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ionic.ui.actionSheet'])
angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ionic.ui.actionSheet', 'ngAnimate'])
.factory('ActionSheet', ['$rootScope', '$document', '$compile', '$animate', 'TemplateLoader',
function($rootScope, $document, $compile, $animate, TemplateLoader) {
.factory('ActionSheet', ['$rootScope', '$document', '$compile', 'TemplateLoader', function($rootScope, $document, $compile, TemplateLoader) {
return {
/**
* Load an action sheet with the given template string.
@ -10,23 +12,38 @@ angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ioni
*
* @param {object} opts the options for this ActionSheet (see docs)
*/
show: function(opts, $scope) {
var scope = $scope && $scope.$new() || $rootScope.$new(true);
show: function(opts) {
var scope = $rootScope.$new(true);
angular.extend(scope, opts);
// Compile the template
var element = $compile('<action-sheet buttons="buttons"></action-sheet>')(scope);
// Grab the sheet element for animation
var sheetEl = angular.element(element[0].querySelector('.action-sheet'));
var hideSheet = function(didCancel) {
$animate.leave(sheetEl, function() {
if(didCancel) {
opts.cancel();
}
});
$animate.removeClass(element, 'active', function() {
scope.$destroy();
});
};
scope.cancel = function() {
scope.sheet.hide();
//scope.$destroy();
opts.cancel();
hideSheet(true);
};
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)) === true) {
scope.sheet.hide();
//scope.$destroy();
hideSheet(false);
}
};
@ -34,24 +51,21 @@ angular.module('ionic.service.actionSheet', ['ionic.service.templateLoad', 'ioni
// Check if the destructive button click event returned true, which means
// we can close the action sheet
if((opts.destructiveButtonClicked && opts.destructiveButtonClicked()) === true) {
scope.sheet.hide();
//scope.$destroy();
hideSheet(false);
}
};
// Compile the template
var element = $compile('<action-sheet buttons="buttons"></action-sheet>')(scope);
var s = element.scope();
$document[0].body.appendChild(element[0]);
var sheet = new ionic.views.ActionSheet({el: element[0] });
s.sheet = sheet;
scope.sheet = sheet;
sheet.show();
$animate.addClass(element, 'active');
$animate.enter(sheetEl, element, function() {
});
return sheet;
}
};
}]);

View File

@ -0,0 +1,47 @@
<html ng-app="actionTest">
<head>
<meta charset="utf-8">
<title>Action Sheet</title>
<!-- Sets initial viewport load and disables zooming -->
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="../../../../dist/css/ionic.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-touch.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular-animate.js"></script>
</head>
<body ng-controller="ActionCtrl">
<button ng-click="show()" class="button">Show</button>
<script src="../../../../dist/js/ionic.js"></script>
<script src="../../../../dist/js/ionic-angular.js"></script>
<script>
angular.module('actionTest', ['ionic'])
.controller('ActionCtrl', function($scope, ActionSheet) {
$scope.show = function() {
ActionSheet.show({
buttons: [
{ text: 'Option 1' },
{ text: 'Option 2' },
{ text: 'Option 3' },
],
destructiveText: 'Delete life',
titleText: 'Are you sure about life?',
cancelText: 'Cancel',
cancel: function() {
console.log('CANCELLED');
},
buttonClicked: function(index) {
console.log('BUTTON CLICKED', index);
return true;
},
destructiveButtonClicked: function() {
console.log('DESTRUCT');
return true;
}
});
};
});
</script>
</body>
</html>