Skip to content
This repository was archived by the owner on May 4, 2022. It is now read-only.

add Enter keypress to ionic popups #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions js/angular/service/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,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 || {};
popup.keylistener = popup.element[0].addEventListener('keypress', function (keyEvent) {
var userfunc = this.keyPressHandlers[keyEvent.keyCode];
if (userfunc) userfunc(popup);
});

doShow();

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

popup.element[0].removeEventListener('keypress', popup.keylistener);

popup.remove();

if (popupStack.length > 0) {
Expand Down Expand Up @@ -443,7 +455,17 @@ 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);
}
};

return showPopup(extend({
keyPressHandlers: keyPressHandlers,
buttons: [{
text: opts.okText || 'OK',
type: opts.okType || 'button-positive',
Expand All @@ -455,7 +477,17 @@ 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);
}
};

return showPopup(extend({
keyPressHandlers: keyPressHandlers,
buttons: [{
text: opts.cancelText || 'Cancel',
type: opts.cancelType || 'button-default',
Expand All @@ -480,7 +512,18 @@ function($ionicTemplateLoader, $ionicBackdrop, $q, $timeout, $rootScope, $ionicB
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);
}
};

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