Skip to content

Add Enter/Escape keypress handling to $ionicPopup.prompt() $.alert() and $ionicPopup.confirm() #4739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
62 changes: 62 additions & 0 deletions js/angular/service/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
//DEPRECATED: notify the promise with an object with a close method
popup.responseDeferred.notify({ close: popup.responseDeferred.close });

// if any keypress handlers were requested, go ahead and assign them now
// options.keyPressHandlers is the user-generated assoc: keycode => function()
// popup.keyPressHandlers is a list of event-listener handles returned by addEventListener()
// and is used to unbind these event listeners when the popup is closing ("then" block below)
popup.element[0].keyPressHandlers = options.keyPressHandlers ? options.keyPressHandlers : {};
popup.keylistener = window.addEventListener('keypress', function (keyEvent) {
var userfunc = popup.element[0].keyPressHandlers[keyEvent.keyCode];
if (userfunc) userfunc(popup);
});

doShow();

return popup.responseDeferred.promise;
Expand All @@ -405,6 +415,8 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
popupStack.splice(index, 1);
}

window.removeEventListener('keypress', popup.keylistener);

popup.remove();

if (popupStack.length > 0) {
Expand Down Expand Up @@ -438,7 +450,23 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
}

function showAlert(opts) {
var keyPressHandlers = {
"13": function (popup) { // 13 = Enter = OK
var button = popup.scope.buttons[0];
var tapper = popup.scope.$buttonTapped;
var fclick = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true });
tapper(button, fclick);
},
"27": function (popup) { // 27 = Esc = Cancel
var button = popup.scope.buttons[0];
var tapper = popup.scope.$buttonTapped;
var fclick = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true });
tapper(button, fclick);
}
};

return showPopup(extend({
keyPressHandlers: keyPressHandlers,
buttons: [{
text: opts.okText || 'OK',
type: opts.okType || 'button-positive',
Expand All @@ -450,7 +478,23 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
}

function showConfirm(opts) {
var keyPressHandlers = {
"13": function (popup) { // 13 = Enter = OK
var button = popup.scope.buttons[1];
var tapper = popup.scope.$buttonTapped;
var fclick = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true });
tapper(button, fclick);
},
"27": function (popup) { // 27 = Esc = Cancel
var button = popup.scope.buttons[0];
var tapper = popup.scope.$buttonTapped;
var fclick = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true });
tapper(button, fclick);
}
};

return showPopup(extend({
keyPressHandlers: keyPressHandlers,
buttons: [{
text: opts.cancelText || 'Cancel',
type: opts.cancelType || 'button-default',
Expand All @@ -470,12 +514,30 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
scope.data.response = opts.defaultText ? opts.defaultText : '';
scope.data.placeholder = opts.inputPlaceholder ? opts.inputPlaceholder : '';
scope.data.maxlength = opts.maxLength ? parseInt(opts.maxLength) : '';

var text = '';
if (opts.template && /<[a-z][\s\S]*>/i.test(opts.template) === false) {
text = '<span>' + opts.template + '</span>';
delete opts.template;
}

var keyPressHandlers = {
"13": function (popup) { // 13 = Enter = OK
var button = popup.scope.buttons[1];
var tapper = popup.scope.$buttonTapped;
var fclick = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true });
tapper(button, fclick);
},
"27": function (popup) { // 27 = Esc = Cancel
var button = popup.scope.buttons[0];
var tapper = popup.scope.$buttonTapped;
var fclick = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true });
tapper(button, fclick);
}
};

return showPopup(extend({
keyPressHandlers: keyPressHandlers,
template: text + '<input ng-model="data.response" '
+ 'type="{{ data.fieldtype }}"'
+ 'maxlength="{{ data.maxlength }}"'
Expand Down