Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 * `settings-input` is a single-line text field for user input associated | 7 * `settings-input` is a single-line text field for user input associated |
| 8 * with a pref value. | 8 * with a pref value. |
| 9 */ | 9 */ |
| 10 Polymer({ | 10 Polymer({ |
| 11 is: 'settings-input', | 11 is: 'settings-input', |
| 12 | 12 |
| 13 behaviors: [CrPolicyPrefBehavior, PrefControlBehavior], | 13 behaviors: [CrPolicyPrefBehavior, PrefControlBehavior], |
| 14 | 14 |
| 15 properties: { | 15 properties: { |
| 16 /** | 16 /** |
| 17 * The preference object to control. | 17 * The preference object to control. |
| 18 * @type {!chrome.settingsPrivate.PrefObject|undefined} | 18 * @type {!chrome.settingsPrivate.PrefObject|undefined} |
| 19 * @override | 19 * @override |
| 20 */ | 20 */ |
| 21 pref: { | 21 pref: {observer: 'prefChanged_'}, |
| 22 observer: 'prefChanged_' | |
| 23 }, | |
| 24 | 22 |
| 25 /* The current value of the input, reflected to/from |pref|. */ | 23 /* The current value of the input, reflected to/from |pref|. */ |
| 26 value: { | 24 value: { |
| 27 type: String, | 25 type: String, |
| 28 value: '', | 26 value: '', |
| 29 notify: true, | 27 notify: true, |
| 30 }, | 28 }, |
| 31 | 29 |
| 32 /* Set to true to disable editing the input. */ | 30 /* Set to true to disable editing the input. */ |
| 33 disabled: { | 31 disabled: {type: Boolean, value: false, reflectToAttribute: true}, |
| 34 type: Boolean, | |
| 35 value: false, | |
| 36 reflectToAttribute: true | |
| 37 }, | |
| 38 | 32 |
| 39 canTab: Boolean, | 33 canTab: Boolean, |
| 40 | 34 |
| 35 invalid: { | |
| 36 type: Boolean, | |
| 37 value: false, | |
| 38 notify: true, | |
|
dpapad
2017/03/24 01:39:31
Is this needed? From the docs, this is only applic
scottchen
2017/03/24 20:48:54
Based on your comment regarding using data-binding
| |
| 39 }, | |
| 40 | |
| 41 /* Properties for paper-input. This is not strictly necessary. | 41 /* Properties for paper-input. This is not strictly necessary. |
| 42 * Though it does define the types for the closure compiler. */ | 42 * Though it does define the types for the closure compiler. */ |
| 43 errorMessage: { type: String }, | 43 errorMessage: {type: String}, |
| 44 label: { type: String }, | 44 label: {type: String}, |
| 45 noLabelFloat: { type: Boolean, value: false }, | 45 noLabelFloat: {type: Boolean, value: false}, |
| 46 pattern: { type: String }, | 46 pattern: {type: String}, |
| 47 readonly: { type: Boolean, value: false }, | 47 readonly: {type: Boolean, value: false}, |
| 48 required: { type: Boolean, value: false }, | 48 required: {type: Boolean, value: false}, |
| 49 stopKeyboardEventPropagation: { type: Boolean, value: false }, | 49 stopKeyboardEventPropagation: {type: Boolean, value: false}, |
| 50 type: { type: String }, | 50 type: {type: String}, |
| 51 }, | 51 }, |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Focuses the 'input' element. | 54 * Focuses the 'input' element. |
| 55 */ | 55 */ |
| 56 focus: function() { | 56 focus: function() { |
| 57 this.$.input.focus(); | 57 this.$.input.focus(); |
| 58 }, | 58 }, |
| 59 | 59 |
| 60 /** | 60 /** |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 | 96 |
| 97 /** | 97 /** |
| 98 * Change event handler for paper-input. Updates the pref value. | 98 * Change event handler for paper-input. Updates the pref value. |
| 99 * settings-input uses the change event because it is fired by the Enter key. | 99 * settings-input uses the change event because it is fired by the Enter key. |
| 100 * @private | 100 * @private |
| 101 */ | 101 */ |
| 102 onChange_: function() { | 102 onChange_: function() { |
| 103 if (!this.pref) | 103 if (!this.pref) |
| 104 return; | 104 return; |
| 105 | 105 |
| 106 if (this.invalid) { | |
| 107 this.resetValue_(); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 106 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { | 111 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { |
| 107 if (!this.value) { | 112 if (!this.value) { |
| 108 // Ignore empty input field and restore value. | 113 this.resetValue_(); |
| 109 this.value = this.pref.value.toString(); | |
| 110 return; | 114 return; |
| 111 } | 115 } |
| 112 var n = parseInt(this.value, 10); | 116 var n = parseInt(this.value, 10); |
| 113 if (isNaN(n)) { | 117 if (isNaN(n)) { |
| 114 console.error('Bad value for numerical pref: ' + this.value); | 118 console.error('Bad value for numerical pref: ' + this.value); |
| 115 return; | 119 return; |
| 116 } | 120 } |
| 117 this.set('pref.value', n); | 121 this.set('pref.value', n); |
| 118 } else { | 122 } else { |
| 119 assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING || | 123 assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING || |
| 120 this.pref.type == chrome.settingsPrivate.PrefType.URL); | 124 this.pref.type == chrome.settingsPrivate.PrefType.URL); |
| 121 this.set('pref.value', this.value); | 125 this.set('pref.value', this.value); |
| 122 } | 126 } |
| 123 }, | 127 }, |
| 124 | 128 |
| 129 /** @private */ | |
| 130 resetValue_: function() { | |
| 131 this.invalid = false; | |
| 132 this.setInputValueFromPref_(); | |
| 133 this.$.input.blur(); | |
| 134 }, | |
| 135 | |
| 125 /** | 136 /** |
| 126 * Handler for profile name keydowns. | 137 * Handler for profile name keydowns. |
|
dpapad
2017/03/24 01:39:31
Is this comment obsolete?
scottchen
2017/03/24 20:48:54
Acknowledged.
| |
| 127 * @param {!Event} event | 138 * @param {!Event} event |
| 128 * @private | 139 * @private |
| 129 */ | 140 */ |
| 130 onKeydown_: function(event) { | 141 onKeydown_: function(event) { |
| 142 /* If pressed enter when input is invalid, do not trigger on-change */ | |
|
dpapad
2017/03/24 01:39:31
End full sentence comment with a period.
scottchen
2017/03/24 20:48:54
Done.
| |
| 143 if (event.key == 'Enter' && this.invalid) { | |
| 144 event.preventDefault(); | |
| 145 return; | |
| 146 } | |
| 147 | |
| 131 if (event.key != 'Escape') | 148 if (event.key != 'Escape') |
| 132 return; | 149 return; |
| 133 | 150 |
| 134 this.setInputValueFromPref_(); | 151 this.resetValue_(); |
| 135 this.$.input.blur(); | |
| 136 }, | 152 }, |
| 137 | 153 |
| 138 /** | 154 /** |
| 139 * @param {boolean} disabled | 155 * @param {boolean} disabled |
| 140 * @return {boolean} Whether the element should be disabled. | 156 * @return {boolean} Whether the element should be disabled. |
| 141 * @private | 157 * @private |
| 142 */ | 158 */ |
| 143 isDisabled_: function(disabled) { | 159 isDisabled_: function(disabled) { |
| 144 return disabled || this.isPrefEnforced(); | 160 return disabled || this.isPrefEnforced(); |
| 145 }, | 161 }, |
| 146 }); | 162 }); |
| OLD | NEW |