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: |
(...skipping 22 matching lines...) Expand all Loading... | |
33 /** | 33 /** |
34 * Whether or not the keyboard's input element should be numerical | 34 * Whether or not the keyboard's input element should be numerical |
35 * or password. | 35 * or password. |
36 */ | 36 */ |
37 enablePassword: { | 37 enablePassword: { |
38 type: Boolean, | 38 type: Boolean, |
39 value: false | 39 value: false |
40 }, | 40 }, |
41 | 41 |
42 /** | 42 /** |
43 * Whether or not the keyboard's submit button should be shown. | 43 * Whether or not the keyboard's input should be shown. |
44 */ | 44 */ |
45 enableSubmitButton: { | 45 enableInputShown: { |
46 type: Boolean, | 46 type: Boolean, |
47 value: false | 47 notify: true, |
48 value: false, | |
49 observer: 'showInput_' | |
48 }, | 50 }, |
49 | 51 |
50 /** The value stored in the keyboard's input element. */ | 52 /** The value stored in the keyboard's input element. */ |
51 value: { | 53 value: { |
52 type: String, | 54 type: String, |
53 notify: true, | 55 notify: true, |
54 value: '', | 56 value: '', |
55 observer: 'onPinValueChange_' | 57 observer: 'onPinValueChange_' |
56 } | 58 } |
57 }, | 59 }, |
58 | 60 |
59 /** | 61 /** |
60 * Gets the container holding the password field. | 62 * Gets the container holding the password field. |
61 * @type {!HTMLInputElement} | 63 * @type {!HTMLInputElement} |
62 */ | 64 */ |
63 get inputElement() { | 65 get inputElement() { |
64 return this.$$('#pin-input'); | 66 return this.$$('#pin-input'); |
65 }, | 67 }, |
66 | 68 |
69 /** | |
70 * Shows or hides the input element. | |
71 * @param {boolean} show | |
72 * @private | |
73 */ | |
74 showInput_: function(show) { | |
jdufault
2016/08/24 01:35:10
Use a property binding
sammiequon
2016/08/25 18:12:33
Done.
| |
75 this.$$('#pin-input').hidden = !show; | |
76 }, | |
77 | |
67 /** Transfers focus to the input element. */ | 78 /** Transfers focus to the input element. */ |
68 focus: function() { | 79 focus: function() { |
69 this.$$('#pin-input').focus(); | 80 this.$$('#pin-input').focus(); |
70 }, | 81 }, |
71 | 82 |
72 /** | 83 /** |
73 * Called when a keypad number has been tapped. | 84 * Called when a keypad number has been tapped. |
74 * @param {!{target: !PaperButtonElement}} event | 85 * @param {!{target: !PaperButtonElement}} event |
75 * @private | 86 * @private |
76 */ | 87 */ |
77 onNumberTap_: function(event, detail) { | 88 onNumberTap_: function(event, detail) { |
78 var numberValue = event.target.getAttribute('value'); | 89 var numberValue = event.target.getAttribute('value'); |
79 this.value += numberValue; | 90 this.value += numberValue; |
80 | |
81 // If a number button is clicked, we do not want to switch focus to the | |
82 // button, therefore we transfer focus back to the input, but if a number | |
83 // button is tabbed into, it should keep focus, so users can use tab and | |
84 // spacebar/return to enter their PIN. | |
85 if (!event.target.receivedFocusFromKeyboard) | |
86 this.focus(); | |
87 }, | 91 }, |
88 | 92 |
89 /** Fires a submit event with the current PIN value. */ | 93 /** Fires a submit event with the current PIN value. */ |
90 firePinSubmitEvent_: function() { | 94 firePinSubmitEvent_: function() { |
91 this.fire('submit', { pin: this.value }); | 95 this.fire('submit', { pin: this.value }); |
92 }, | 96 }, |
93 | 97 |
94 /** | 98 /** |
95 * Fires an update event with the current PIN value. The event will only be | 99 * Fires an update event with the current PIN value. The event will only be |
96 * fired if the PIN value has actually changed. | 100 * fired if the PIN value has actually changed. |
97 * @param {string} value | 101 * @param {string} value |
98 * @param {string} previous | 102 * @param {string} previous |
99 */ | 103 */ |
100 onPinValueChange_: function(value, previous) { | 104 onPinValueChange_: function(value, previous) { |
101 if (value != previous) | 105 if (value != previous) |
102 this.fire('pin-change', { pin: value }); | 106 this.fire('pin-change', { pin: value }); |
103 }, | 107 }, |
104 | 108 |
105 /** Called when the user wants to erase the last character of the entered | 109 /** |
110 * Called when the user wants to erase the last character of the entered | |
106 * PIN value. | 111 * PIN value. |
107 */ | 112 */ |
108 onPinClear_: function() { | 113 onPinClear_: function() { |
109 this.value = this.value.substring(0, this.value.length - 1); | 114 this.value = this.value.substring(0, this.value.length - 1); |
110 }, | 115 }, |
111 | 116 |
112 /** Called when a key event is pressed while the input element has focus. */ | 117 /** Called when a key event is pressed while the input element has focus. */ |
113 onInputKeyDown_: function(event) { | 118 onInputKeyDown_: function(event) { |
114 // Up/down pressed, swallow the event to prevent the input value from | 119 // Up/down pressed, swallow the event to prevent the input value from |
115 // being incremented or decremented. | 120 // being incremented or decremented. |
116 if (event.keyCode == 38 || event.keyCode == 40) { | 121 if (event.keyCode == 38 || event.keyCode == 40) { |
117 event.preventDefault(); | 122 event.preventDefault(); |
118 return; | 123 return; |
119 } | 124 } |
120 | 125 |
121 // Enter pressed. | 126 // Enter pressed. |
122 if (event.keyCode == 13) { | 127 if (event.keyCode == 13) { |
123 this.firePinSubmitEvent_(); | 128 this.firePinSubmitEvent_(); |
124 event.preventDefault(); | 129 event.preventDefault(); |
125 return; | 130 return; |
126 } | 131 } |
127 }, | 132 }, |
128 | 133 |
129 /** | 134 /** |
135 * Keypress does not handle backspace but handles the char codes nicely, so we | |
136 * have a seperate event to process the backspaces. | |
137 * @param {Event} e Keypress Event object. | |
138 * @private | |
139 */ | |
140 onKeyDown_: function(event) { | |
141 // Backspace pressed. | |
142 if (event.keyCode == 8) { | |
143 this.onPinClear_(); | |
144 event.preventDefault(); | |
145 return; | |
146 } | |
147 }, | |
148 | |
149 /** | |
150 * Called when a key press event is fired while the number button is focused. | |
151 * Ideally we would want to pass focus back to the input element, but the we | |
152 * cannot or the virtual keyboard will keep poping up. | |
153 * @param {Event} e Keypress Event object. | |
154 * @private | |
155 */ | |
156 onKeyPress_: function(event) { | |
157 var code = event.keyCode; | |
158 | |
159 // Enter pressed. | |
160 if (code == 13) { | |
161 this.firePinSubmitEvent_(); | |
162 event.preventDefault(); | |
163 return; | |
164 } | |
165 | |
166 this.value += String.fromCharCode(code); | |
167 }, | |
168 | |
169 /** | |
130 * Changes the color of the submit button if PIN is ready. | 170 * Changes the color of the submit button if PIN is ready. |
131 * @param {string} value | 171 * @param {string} value |
132 * @private | 172 * @private |
133 */ | 173 */ |
134 getSubmitClass_: function(value) { | 174 getSubmitClass_: function(value) { |
135 return value.length > 0 ? 'ready-background' : ''; | 175 return value.length > 0 ? 'ready-background' : ''; |
136 }, | 176 }, |
137 | 177 |
138 /** | 178 /** |
139 * Computes whether the input type for the pin input should be password or | 179 * Computes whether the input type for the pin input should be password or |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 | 215 |
176 /** | 216 /** |
177 * Computes if the submit button is visible. | 217 * Computes if the submit button is visible. |
178 * @param {boolean} submitEnabled | 218 * @param {boolean} submitEnabled |
179 * @private | 219 * @private |
180 */ | 220 */ |
181 getSubmitHiddenClass_: function(submitEnabled) { | 221 getSubmitHiddenClass_: function(submitEnabled) { |
182 return submitEnabled ? '' : 'submit-button-hidden'; | 222 return submitEnabled ? '' : 'submit-button-hidden'; |
183 } | 223 } |
184 }); | 224 }); |
OLD | NEW |