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 repeatBackspaceIntervalMs = 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 startAutoBackspaceMs = 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() { | |
jdufault
2016/09/06 14:46:37
setTimeout and this.async do the same/similar thin
sammiequon
2016/09/06 17:08:07
Done.
| |
152 this.async(function() { | |
153 this.repeatBackspaceIntervalId_ = setInterval( | |
154 this.onPinClear_.bind(this), repeatBackspaceIntervalMs); | |
155 }.bind(this), 1); | |
156 }.bind(this), startAutoBackspaceMs); | |
157 }, | |
158 | |
159 /** | |
160 * Helper function which clears the timer / interval ids and resets them. | |
161 * @private | |
162 */ | |
163 clearAndReset_: function() { | |
164 clearInterval(this.repeatBackspaceIntervalId_); | |
jdufault
2016/09/06 14:46:37
Do we need separate interval id variables? Can we
sammiequon
2016/09/06 17:08:07
The reason I use two variables is because I change
| |
165 this.repeatBackspaceIntervalId_ = 0; | |
166 clearTimeout(this.startAutoBackspaceId_); | |
167 this.startAutoBackspaceId_ = 0; | |
168 }, | |
169 | |
170 /** | |
171 * Called when the user exits the backspace button. Stops the interval | |
172 * callback. | |
173 * @param {Event} event The event object. | |
174 * @private | |
175 */ | |
176 onBackspacePointerOut_: function(event) { | |
177 this.clearAndReset_(); | |
178 }, | |
179 | |
180 /** | |
181 * Called when the user unpresses or untouches the backspace button. Stops the | |
182 * interval callback and fires a backspace event if there is no interval | |
183 * running. | |
184 * @param {Event} event The event object. | |
185 * @private | |
186 */ | |
187 onBackspacePointerUp_: function(event) { | |
188 // If an interval has started, do not fire event on pointer up. | |
189 if (!this.repeatBackspaceIntervalId_) | |
190 this.onPinClear_(); | |
191 this.clearAndReset_(); | |
192 }, | |
193 | |
106 /** Called when a key event is pressed while the input element has focus. */ | 194 /** Called when a key event is pressed while the input element has focus. */ |
107 onInputKeyDown_: function(event) { | 195 onInputKeyDown_: function(event) { |
108 // Up/down pressed, swallow the event to prevent the input value from | 196 // Up/down pressed, swallow the event to prevent the input value from |
109 // being incremented or decremented. | 197 // being incremented or decremented. |
110 if (event.keyCode == 38 || event.keyCode == 40) { | 198 if (event.keyCode == 38 || event.keyCode == 40) { |
111 event.preventDefault(); | 199 event.preventDefault(); |
112 return; | 200 return; |
113 } | 201 } |
114 | 202 |
115 // Enter pressed. | 203 // Enter pressed. |
(...skipping 20 matching lines...) Expand all Loading... | |
136 }, | 224 }, |
137 | 225 |
138 /** | 226 /** |
139 * Called when a key press event is fired while the number button is focused. | 227 * 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 | 228 * 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. | 229 * cannot or the virtual keyboard will keep poping up. |
142 * @param {Event} event Keypress Event object. | 230 * @param {Event} event Keypress Event object. |
143 * @private | 231 * @private |
144 */ | 232 */ |
145 onKeyPress_: function(event) { | 233 onKeyPress_: function(event) { |
234 // If the active element is the input element, the input element itself will | |
235 // handle the keypresses, so we do not handle them here. | |
236 if (this.shadowRoot.activeElement == this.inputElement) | |
237 return; | |
238 | |
146 var code = event.keyCode; | 239 var code = event.keyCode; |
147 | 240 |
148 // Enter pressed. | 241 // Enter pressed. |
149 if (code == 13) { | 242 if (code == 13) { |
150 this.firePinSubmitEvent_(); | 243 this.firePinSubmitEvent_(); |
151 event.preventDefault(); | 244 event.preventDefault(); |
152 return; | 245 return; |
153 } | 246 } |
154 | 247 |
155 this.value += String.fromCharCode(code); | 248 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 | 287 // possible. Number.isInteger will verify the value is not a NaN and that it |
195 // does not contain decimals. | 288 // does not contain decimals. |
196 // This heuristic will fail for inputs like '1.0'. | 289 // This heuristic will fail for inputs like '1.0'. |
197 // | 290 // |
198 // Since we still support users entering their passwords through the PIN | 291 // 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 | 292 // keyboard, we swap the input box to rtl when we think it is a password |
200 // (just numbers), if the document direction is rtl. | 293 // (just numbers), if the document direction is rtl. |
201 return (document.dir == 'rtl') && !Number.isInteger(+password); | 294 return (document.dir == 'rtl') && !Number.isInteger(+password); |
202 }, | 295 }, |
203 }); | 296 }); |
297 })(); | |
OLD | NEW |