Chromium Code Reviews| 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 Polymer({ | 5 Polymer({ |
| 6 is: 'pin-keyboard', | 6 is: 'pin-keyboard', |
| 7 | 7 |
| 8 // Called when a keypad number has been tapped. | 8 properties: { |
| 9 onNumberTap_: function(event, detail) { | 9 /** The value stored in the keyboard's input element. */ |
| 10 var target = event.target; | 10 value: { |
| 11 var value = target.getAttribute('value'); | 11 type: String, |
| 12 | 12 notify: true, |
| 13 var input = this.$$('#pin-input'); | 13 value: '', |
| 14 input.value += value; | 14 } |
| 15 }, | 15 }, |
| 16 | 16 |
| 17 // Called when the user wants to submit the PIN. | 17 /** |
| 18 onPinSubmit_: function() { | 18 * Transfers focus to the input element. |
| 19 var pin = this.$$('#pin-input').value; | 19 */ |
| 20 this.fire('submit', { pin: pin }); | 20 focus: function() { |
| 21 this.$$('#pin-input').focus(); | |
| 21 }, | 22 }, |
| 22 | 23 |
| 23 // Called when a key event is pressed while the input element has focus. | 24 /** |
| 25 * Called when a keypad number has been tapped. | |
| 26 */ | |
| 27 onNumberTap_: function(event, detail) { | |
| 28 var numberValue = event.target.getAttribute('value'); | |
| 29 this.value += numberValue; | |
| 30 | |
| 31 this.firePinChangeEvent_(); | |
|
xiyuan
2016/06/21 20:16:24
I wonder whether we can add an "observer" for |val
jdufault
2016/06/22 22:58:53
Done. I think polymer is raising a 'change' event
| |
| 32 }, | |
| 33 | |
| 34 /** | |
| 35 * Fires a submit event with the current PIN value. | |
| 36 */ | |
| 37 firePinSubmitEvent_: function() { | |
| 38 this.fire('submit', { pin: this.value }); | |
| 39 }, | |
| 40 | |
| 41 /** | |
| 42 * Fires an update event with the current PIN value. | |
| 43 * @param {string} opt_previousValue Optional previous value. Update event | |
| 44 * will not be fired if the current value is equal to the previous value. | |
| 45 */ | |
| 46 firePinChangeEvent_: function(opt_previousValue) { | |
| 47 if (this.value != opt_previousValue) { | |
| 48 this.fire('change', { pin: this.value }); | |
| 49 } | |
| 50 }, | |
| 51 | |
| 52 /** | |
| 53 * Called when a key event is pressed while the input element has focus. | |
| 54 */ | |
| 24 onInputKeyDown_: function(event) { | 55 onInputKeyDown_: function(event) { |
| 25 // Up/down pressed, swallow the event to prevent the input value from | 56 // Up/down pressed, swallow the event to prevent the input value from |
| 26 // being incremented or decremented. | 57 // being incremented or decremented. |
| 27 if (event.keyCode == 38 || event.keyCode == 40) | 58 if (event.keyCode == 38 || event.keyCode == 40) { |
| 28 event.preventDefault(); | 59 event.preventDefault(); |
| 60 return; | |
| 61 } | |
| 29 | 62 |
| 30 // Enter pressed. | 63 // Enter pressed. |
| 31 if (event.keyCode == 13) { | 64 if (event.keyCode == 13) { |
| 32 this.onPinSubmit_(); | 65 this.firePinSubmitEvent_(); |
| 33 event.preventDefault(); | 66 event.preventDefault(); |
| 67 return; | |
| 34 } | 68 } |
| 69 | |
| 70 // The PIN was very likely updated. Fire an update event after the event has | |
| 71 // finished processing. | |
| 72 setTimeout(this.firePinChangeEvent_.bind(this, this.value)); | |
| 35 } | 73 } |
| 36 }); | 74 }); |
| OLD | NEW |