OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * 'pin-keyboard' is a keyboard that can be used to enter PINs or more generally | 7 * 'pin-keyboard' is a keyboard that can be used to enter PINs or more generally |
8 * numeric values. | 8 * numeric values. |
9 * | 9 * |
10 * Properties: | 10 * Properties: |
11 * value: The value of the PIN keyboard. Writing to this property will adjust | 11 * value: The value of the PIN keyboard. Writing to this property will adjust |
12 * the PIN keyboard's value. | 12 * the PIN keyboard's value. |
13 * | 13 * |
14 * Events: | 14 * Events: |
15 * pin-change: Fired when the PIN value has changed. The pin is available at | 15 * pin-change: Fired when the PIN value has changed. The pin is available at |
16 * event.detail.pin. | 16 * event.detail.pin. |
17 * submit: Fired when the PIN is submitted. The pin is available at | 17 * submit: Fired when the PIN is submitted. The pin is available at |
18 * event.detail.pin. | 18 * event.detail.pin. |
19 * | 19 * |
20 * Example: | 20 * Example: |
21 * <pin-keyboard on-pin-change="onPinChange" on-submit="onPinSubmit" | 21 * <pin-keyboard on-pin-change="onPinChange" on-submit="onPinSubmit" |
22 * value="{{pinValue}}"> | 22 * value="{{pinValue}}"> |
23 * </pin-keyboard> | 23 * </pin-keyboard> |
24 */ | 24 */ |
| 25 |
| 26 (function() { |
| 27 |
| 28 /** |
| 29 * Once auto backspace starts, the time between individual backspaces. |
| 30 * @type {number} |
| 31 * @const |
| 32 */ |
| 33 var REPEAT_BACKSPACE_DELAY_MS = 150; |
| 34 |
| 35 /** |
| 36 * How long the backspace button must be held down before auto backspace |
| 37 * starts. |
| 38 * @type {number} |
| 39 * @const |
| 40 */ |
| 41 var INITIAL_BACKSPACE_DELAY_MS = 500; |
| 42 |
25 Polymer({ | 43 Polymer({ |
26 is: 'pin-keyboard', | 44 is: 'pin-keyboard', |
27 | 45 |
28 behaviors: [ | 46 behaviors: [ |
29 I18nBehavior, | 47 I18nBehavior, |
30 ], | 48 ], |
31 | 49 |
32 properties: { | 50 properties: { |
33 /** | 51 /** |
34 * Whether or not the keyboard's input element should be numerical | 52 * Whether or not the keyboard's input element should be numerical |
(...skipping 11 matching lines...) Expand all Loading... |
46 type: Boolean, | 64 type: Boolean, |
47 value: false | 65 value: false |
48 }, | 66 }, |
49 | 67 |
50 /** The value stored in the keyboard's input element. */ | 68 /** The value stored in the keyboard's input element. */ |
51 value: { | 69 value: { |
52 type: String, | 70 type: String, |
53 notify: true, | 71 notify: true, |
54 value: '', | 72 value: '', |
55 observer: 'onPinValueChange_' | 73 observer: 'onPinValueChange_' |
| 74 }, |
| 75 |
| 76 /** |
| 77 * The intervalID used for the backspace button set/clear interval. |
| 78 * @private |
| 79 */ |
| 80 repeatBackspaceIntervalId_: { |
| 81 type: Number, |
| 82 value: 0 |
| 83 }, |
| 84 |
| 85 /** |
| 86 * The timeoutID used for the auto backspace. |
| 87 * @private |
| 88 */ |
| 89 startAutoBackspaceId_: { |
| 90 type: Number, |
| 91 value: 0 |
56 } | 92 } |
57 }, | 93 }, |
58 | 94 |
59 /** | 95 /** |
60 * Gets the container holding the password field. | 96 * Gets the container holding the password field. |
61 * @type {!HTMLInputElement} | 97 * @type {!HTMLInputElement} |
62 */ | 98 */ |
63 get inputElement() { | 99 get inputElement() { |
64 return this.$$('#pin-input'); | 100 return this.$$('#pin-input'); |
65 }, | 101 }, |
(...skipping 24 matching lines...) Expand all Loading... |
90 * @param {string} value | 126 * @param {string} value |
91 * @param {string} previous | 127 * @param {string} previous |
92 */ | 128 */ |
93 onPinValueChange_: function(value, previous) { | 129 onPinValueChange_: function(value, previous) { |
94 if (value != previous) | 130 if (value != previous) |
95 this.fire('pin-change', { pin: value }); | 131 this.fire('pin-change', { pin: value }); |
96 }, | 132 }, |
97 | 133 |
98 /** | 134 /** |
99 * Called when the user wants to erase the last character of the entered | 135 * Called when the user wants to erase the last character of the entered |
100 * PIN value. | 136 * PIN value. |
| 137 * @private |
101 */ | 138 */ |
102 onPinClear_: function() { | 139 onPinClear_: function() { |
103 this.value = this.value.substring(0, this.value.length - 1); | 140 this.value = this.value.substring(0, this.value.length - 1); |
104 }, | 141 }, |
105 | 142 |
| 143 /** |
| 144 * Called when the user presses or touches the backspace button. Starts a |
| 145 * timer which starts an interval to repeatedly backspace the pin value until |
| 146 * the interval is cleared. |
| 147 * @param {Event} event The event object. |
| 148 * @private |
| 149 */ |
| 150 onBackspacePointerDown_: function(event) { |
| 151 this.startAutoBackspaceId_ = setTimeout(function() { |
| 152 this.repeatBackspaceIntervalId_ = setInterval( |
| 153 this.onPinClear_.bind(this), REPEAT_BACKSPACE_DELAY_MS); |
| 154 }.bind(this), INITIAL_BACKSPACE_DELAY_MS); |
| 155 }, |
| 156 |
| 157 /** |
| 158 * Helper function which clears the timer / interval ids and resets them. |
| 159 * @private |
| 160 */ |
| 161 clearAndReset_: function() { |
| 162 clearInterval(this.repeatBackspaceIntervalId_); |
| 163 this.repeatBackspaceIntervalId_ = 0; |
| 164 clearTimeout(this.startAutoBackspaceId_); |
| 165 this.startAutoBackspaceId_ = 0; |
| 166 }, |
| 167 |
| 168 /** |
| 169 * Called when the user exits the backspace button. Stops the interval |
| 170 * callback. |
| 171 * @param {Event} event The event object. |
| 172 * @private |
| 173 */ |
| 174 onBackspacePointerOut_: function(event) { |
| 175 this.clearAndReset_(); |
| 176 }, |
| 177 |
| 178 /** |
| 179 * Called when the user unpresses or untouches the backspace button. Stops the |
| 180 * interval callback and fires a backspace event if there is no interval |
| 181 * running. |
| 182 * @param {Event} event The event object. |
| 183 * @private |
| 184 */ |
| 185 onBackspacePointerUp_: function(event) { |
| 186 // If an interval has started, do not fire event on pointer up. |
| 187 if (!this.repeatBackspaceIntervalId_) |
| 188 this.onPinClear_(); |
| 189 this.clearAndReset_(); |
| 190 }, |
| 191 |
106 /** Called when a key event is pressed while the input element has focus. */ | 192 /** Called when a key event is pressed while the input element has focus. */ |
107 onInputKeyDown_: function(event) { | 193 onInputKeyDown_: function(event) { |
108 // Up/down pressed, swallow the event to prevent the input value from | 194 // Up/down pressed, swallow the event to prevent the input value from |
109 // being incremented or decremented. | 195 // being incremented or decremented. |
110 if (event.keyCode == 38 || event.keyCode == 40) { | 196 if (event.keyCode == 38 || event.keyCode == 40) { |
111 event.preventDefault(); | 197 event.preventDefault(); |
112 return; | 198 return; |
113 } | 199 } |
114 | 200 |
115 // Enter pressed. | 201 // Enter pressed. |
(...skipping 20 matching lines...) Expand all Loading... |
136 }, | 222 }, |
137 | 223 |
138 /** | 224 /** |
139 * Called when a key press event is fired while the number button is focused. | 225 * Called when a key press event is fired while the number button is focused. |
140 * Ideally we would want to pass focus back to the input element, but the we | 226 * Ideally we would want to pass focus back to the input element, but the we |
141 * cannot or the virtual keyboard will keep poping up. | 227 * cannot or the virtual keyboard will keep poping up. |
142 * @param {Event} event Keypress Event object. | 228 * @param {Event} event Keypress Event object. |
143 * @private | 229 * @private |
144 */ | 230 */ |
145 onKeyPress_: function(event) { | 231 onKeyPress_: function(event) { |
| 232 // If the active element is the input element, the input element itself will |
| 233 // handle the keypresses, so we do not handle them here. |
| 234 if (this.shadowRoot.activeElement == this.inputElement) |
| 235 return; |
| 236 |
146 var code = event.keyCode; | 237 var code = event.keyCode; |
147 | 238 |
148 // Enter pressed. | 239 // Enter pressed. |
149 if (code == 13) { | 240 if (code == 13) { |
150 this.firePinSubmitEvent_(); | 241 this.firePinSubmitEvent_(); |
151 event.preventDefault(); | 242 event.preventDefault(); |
152 return; | 243 return; |
153 } | 244 } |
154 | 245 |
155 this.value += String.fromCharCode(code); | 246 this.value += String.fromCharCode(code); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 // possible. Number.isInteger will verify the value is not a NaN and that it | 285 // possible. Number.isInteger will verify the value is not a NaN and that it |
195 // does not contain decimals. | 286 // does not contain decimals. |
196 // This heuristic will fail for inputs like '1.0'. | 287 // This heuristic will fail for inputs like '1.0'. |
197 // | 288 // |
198 // Since we still support users entering their passwords through the PIN | 289 // Since we still support users entering their passwords through the PIN |
199 // keyboard, we swap the input box to rtl when we think it is a password | 290 // keyboard, we swap the input box to rtl when we think it is a password |
200 // (just numbers), if the document direction is rtl. | 291 // (just numbers), if the document direction is rtl. |
201 return (document.dir == 'rtl') && !Number.isInteger(+password); | 292 return (document.dir == 'rtl') && !Number.isInteger(+password); |
202 }, | 293 }, |
203 }); | 294 }); |
| 295 })(); |
OLD | NEW |