Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Unified Diff: chrome/browser/resources/chromeos/quick_unlock/pin_keyboard.js

Issue 2444443002: cros: Allow pin keyboard to keep focus without popping up the pin keyboard. (Closed)
Patch Set: Fixed patch set 1 errors. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/chromeos/quick_unlock/pin_keyboard.js
diff --git a/chrome/browser/resources/chromeos/quick_unlock/pin_keyboard.js b/chrome/browser/resources/chromeos/quick_unlock/pin_keyboard.js
index d8b2762fb104855a57eb10cc72b4413c231f46ea..f0e16120a8c66406ab0aed8f42e54b00088bbc0d 100644
--- a/chrome/browser/resources/chromeos/quick_unlock/pin_keyboard.js
+++ b/chrome/browser/resources/chromeos/quick_unlock/pin_keyboard.js
@@ -12,9 +12,15 @@
* the PIN keyboard's value.
*
* Events:
- * pin-change: Fired when the PIN value has changed. The pin is available at
+ * pin-change: Fired when the PIN value has changed. The PIN is available at
* event.detail.pin.
- * submit: Fired when the PIN is submitted. The pin is available at
+ * pin-clear: Fired when the clear button is clicked. If the PIN keyboard
+ * has no input, this notifies the input which receives the PIN
+ * keyboard events that backspace has been pressed.
+ * pin-focus: Fired when the pin-input is focused. If the PIN keyboard has no
+ * input, this notifies the input which receives the PIN keyboard
+ * events to handle the input being focused.
+ * submit: Fired when the PIN is submitted. The PIN is available at
* event.detail.pin.
*
* Example:
@@ -40,6 +46,14 @@ var REPEAT_BACKSPACE_DELAY_MS = 150;
*/
var INITIAL_BACKSPACE_DELAY_MS = 500;
+/**
+ * The key codes of the keys allowed to be used on the pin input, in addition to
+ * number keys. Currently we allow backspace(8), tab(9), left(37) and right(39).
+ * @type {Array<number>}
+ * @const
+ */
+var PIN_INPUT_ALLOWED_NON_NUMBER_KEY_CODES = [8, 9, 37, 39];
+
Polymer({
is: 'pin-keyboard',
@@ -93,17 +107,6 @@ Polymer({
},
/**
- * @override
- */
- attached: function() {
- // Remove the space/enter key binds from the polymer
- // iron-a11y-keys-behavior.
- var digitButtons = Polymer.dom(this.root).querySelectorAll('.digit-button');
- for (var i = 0; i < digitButtons.length; ++i)
- digitButtons[i].keyEventTarget = null;
- },
-
- /**
* Gets the container holding the password field.
* @type {!HTMLInputElement}
*/
@@ -113,17 +116,27 @@ Polymer({
/** Transfers focus to the input element. */
focus: function() {
- this.$$('#pin-input').focus();
+ if (!this.hideInput)
+ this.$$('#pin-input').focus();
+ this.fire('pin-focus');
},
/**
* Called when a keypad number has been tapped.
- * @param {!{target: !PaperButtonElement}} event
+ * @param {Event} event The event object.
* @private
*/
- onNumberTap_: function(event, detail) {
+ onNumberTap_: function(event) {
var numberValue = event.target.getAttribute('value');
this.value += numberValue;
+
+ // If a number button is clicked, we do not want to switch focus to the
+ // button, therefore we transfer focus back to the input, but if a number
+ // button is tabbed into, it should keep focus, so users can use tab and
+ // spacebar/return to enter their PIN.
+ if (!event.target.receivedFocusFromKeyboard)
+ this.focus();
+ event.preventDefault();
},
/** Fires a submit event with the current PIN value. */
@@ -148,7 +161,17 @@ Polymer({
* @private
*/
onPinClear_: function() {
- this.value = this.value.substring(0, this.value.length - 1);
+ if (!this.hideInput) {
+ // If the input is shown check the location of the caret or the selected
+ // area to delete the correct character(s).
+ var selectionStart = this.$$('#pin-input').selectionStart;
+ var selectionEnd = this.$$('#pin-input').selectionEnd;
+ if (selectionStart == selectionEnd)
+ selectionStart--;
+ this.value = this.value.substring(0, selectionStart) +
+ this.value.substring(selectionEnd, this.value.length);
jdufault 2016/10/27 19:54:13 Needs indent
jdufault 2016/10/27 19:54:13 I believe you can change this.value.substring(sele
sammiequon 2016/10/27 22:00:20 Done.
sammiequon 2016/10/27 22:00:20 Done.
+ }
+ this.fire('pin-clear');
},
/**
@@ -184,6 +207,10 @@ Polymer({
*/
onBackspacePointerOut_: function(event) {
this.clearAndReset_();
+
+ if (!event.target.receivedFocusFromKeyboard)
+ this.focus();
+ event.preventDefault();
},
/**
@@ -198,6 +225,10 @@ Polymer({
if (!this.repeatBackspaceIntervalId_)
this.onPinClear_();
this.clearAndReset_();
+
+ if (!event.target.receivedFocusFromKeyboard)
+ this.focus();
+ event.preventDefault();
},
/** Called when a key event is pressed while the input element has focus. */
@@ -215,61 +246,18 @@ Polymer({
event.preventDefault();
return;
}
- },
- /**
- * Keypress does not handle backspace but handles the char codes nicely, so we
- * have a seperate event to process the backspaces.
- * @param {Event} event Keydown Event object.
- * @private
- */
- onKeyDown_: function(event) {
- // Backspace pressed.
- if (event.keyCode == 8) {
- this.onPinClear_();
- event.preventDefault();
- return;
- }
- },
-
- /**
- * Called when a key press event is fired while the number button is focused.
- * Ideally we would want to pass focus back to the input element, but the we
- * cannot or the virtual keyboard will keep poping up.
- * @param {Event} event Keypress Event object.
- * @private
- */
- onKeyPress_: function(event) {
- // If the active element is the input element, the input element itself will
- // handle the keypresses, so we do not handle them here.
- if (this.shadowRoot.activeElement == this.inputElement)
- return;
-
- var code = event.keyCode;
+ // Do not pass events that are not numbers or special keys we care about. We
+ // use this instead of input type number because there are several issues
+ // with input type number, such as no selectionStart/selectionEnd and
+ // entered non numbers causes the caret to jump to the left.
+ var isUsableKey = (event.keyCode >= 48 && event.keyCode <= 57) ||
+ PIN_INPUT_ALLOWED_NON_NUMBER_KEY_CODES.indexOf(event.keyCode) > -1;
- // Enter pressed.
- if (code == 13) {
- this.firePinSubmitEvent_();
+ if (!isUsableKey) {
event.preventDefault();
return;
}
-
- // Space pressed. We want the old polymer function of space activating the
- // button with focus.
- if (code == 32) {
- // Check if target was a number button.
- if (event.target.hasAttribute('value')) {
- this.value += event.target.getAttribute('value');
- return;
- }
- // Check if target was backspace button.
- if (event.target.classList.contains('backspace-button')) {
- this.onPinClear_();
- return;
- }
- }
-
- this.value += String.fromCharCode(code);
},
/**
@@ -282,16 +270,6 @@ Polymer({
},
/**
- * Computes whether the input type for the pin input should be password or
- * numerical.
- * @param {boolean} enablePassword
- * @private
- */
- getInputType_: function(enablePassword) {
- return enablePassword ? 'password' : 'number';
- },
-
- /**
* Computes the value of the pin input placeholder.
* @param {boolean} enablePassword
* @private

Powered by Google App Engine
This is Rietveld 408576698