| 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 /** |
| 6 * @fileoverview |
| 7 * 'pin-keyboard' is a keyboard that can be used to enter PINs or more generally |
| 8 * numeric values. |
| 9 * |
| 10 * Properties: |
| 11 * value: The value of the PIN keyboard. Writing to this property will adjust |
| 12 * the PIN keyboard's value. |
| 13 * |
| 14 * Events: |
| 15 * pin-change: Fired when the PIN value has changed. The pin is available at |
| 16 * event.detail.pin. |
| 17 * submit: Fired when the PIN is submitted. The pin is available at |
| 18 * event.detail.pin. |
| 19 * |
| 20 * Example: |
| 21 * <pin-keyboard on-pin-change="onPinChange" on-submit="onPinSubmit" |
| 22 * value="{{pinValue}}"> |
| 23 * </pin-keyboard> |
| 24 */ |
| 5 Polymer({ | 25 Polymer({ |
| 6 is: 'pin-keyboard', | 26 is: 'pin-keyboard', |
| 7 | 27 |
| 8 // Called when a keypad number has been tapped. | 28 properties: { |
| 9 onNumberTap_: function(event, detail) { | 29 /** The value stored in the keyboard's input element. */ |
| 10 var target = event.target; | 30 value: { |
| 11 var value = target.getAttribute('value'); | 31 type: String, |
| 12 | 32 notify: true, |
| 13 var input = this.$$('#pin-input'); | 33 value: '', |
| 14 input.value += value; | 34 observer: 'onPinValueChange_' |
| 35 } |
| 15 }, | 36 }, |
| 16 | 37 |
| 17 // Called when the user wants to submit the PIN. | 38 /** Transfers focus to the input element. */ |
| 18 onPinSubmit_: function() { | 39 focus: function() { |
| 19 var pin = this.$$('#pin-input').value; | 40 this.$$('#pin-input').focus(); |
| 20 this.fire('submit', { pin: pin }); | |
| 21 }, | 41 }, |
| 22 | 42 |
| 23 // Called when a key event is pressed while the input element has focus. | 43 /** Called when a keypad number has been tapped. */ |
| 44 onNumberTap_: function(event, detail) { |
| 45 var numberValue = event.target.getAttribute('value'); |
| 46 this.value += numberValue; |
| 47 }, |
| 48 |
| 49 /** Fires a submit event with the current PIN value. */ |
| 50 firePinSubmitEvent_: function() { |
| 51 this.fire('submit', { pin: this.value }); |
| 52 }, |
| 53 |
| 54 /** |
| 55 * Fires an update event with the current PIN value. The event will only be |
| 56 * fired if the PIN value has actually changed. |
| 57 * @param {string} value |
| 58 * @param {string} previous |
| 59 */ |
| 60 onPinValueChange_: function(value, previous) { |
| 61 if (value != previous) |
| 62 this.fire('pin-change', { pin: value }); |
| 63 }, |
| 64 |
| 65 /** Called when a key event is pressed while the input element has focus. */ |
| 24 onInputKeyDown_: function(event) { | 66 onInputKeyDown_: function(event) { |
| 25 // Up/down pressed, swallow the event to prevent the input value from | 67 // Up/down pressed, swallow the event to prevent the input value from |
| 26 // being incremented or decremented. | 68 // being incremented or decremented. |
| 27 if (event.keyCode == 38 || event.keyCode == 40) | 69 if (event.keyCode == 38 || event.keyCode == 40) { |
| 28 event.preventDefault(); | 70 event.preventDefault(); |
| 71 return; |
| 72 } |
| 29 | 73 |
| 30 // Enter pressed. | 74 // Enter pressed. |
| 31 if (event.keyCode == 13) { | 75 if (event.keyCode == 13) { |
| 32 this.onPinSubmit_(); | 76 this.firePinSubmitEvent_(); |
| 33 event.preventDefault(); | 77 event.preventDefault(); |
| 78 return; |
| 34 } | 79 } |
| 35 } | 80 } |
| 36 }); | 81 }); |
| OLD | NEW |