Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 * `cr-settings-input` is a single-line text field for user input associated | |
| 8 * with a pref value. | |
| 9 * | |
| 10 * Example: | |
| 11 * | |
| 12 * <cr-settings-input></cr-settings-input> | |
| 
 
michaelpg
2015/09/24 18:59:31
exclude useless example IMO
 
stevenjb
2015/09/24 19:28:53
Done.
 
 | |
| 13 * | |
| 14 * @element cr-settings-input | |
| 15 */ | |
| 16 Polymer({ | |
| 17 is: 'cr-settings-input', | |
| 18 | |
| 19 behaviors: [PolicyControllable], | |
| 20 | |
| 21 properties: { | |
| 22 /** | |
| 23 * The preference object to control. | |
| 24 * @type {chrome.settingsPrivate.PrefObject|undefined} | |
| 25 */ | |
| 26 pref: { | |
| 27 type: Object, | |
| 28 notify: true, | |
| 29 observer: 'prefChanged_' | |
| 30 }, | |
| 31 | |
| 32 /** | |
| 33 * The current value of the input, reflected to/from |pref|. | |
| 34 */ | |
| 35 value: { | |
| 36 type: String, | |
| 37 value: '', | |
| 38 notify: true, | |
| 39 }, | |
| 40 | |
| 41 /** | |
| 42 * Set to true to disable editing the input. | |
| 43 */ | |
| 44 disabled: { | |
| 45 type: Boolean, | |
| 46 value: false, | |
| 47 reflectToAttribute: true | |
| 48 }, | |
| 49 | |
| 50 /** Propagate the label property. */ | |
| 51 label: { type: String }, | |
| 52 | |
| 53 /** Propagate the no-label-float property. */ | |
| 54 noLabelFloat: { type: Boolean, value: false }, | |
| 55 | |
| 56 /** Propagate the required property. */ | |
| 57 required: { type: Boolean, value: false }, | |
| 58 | |
| 59 /** Propagate the pattern property. */ | |
| 60 pattern: { type: String }, | |
| 61 | |
| 62 /** Propagate the type property. */ | |
| 63 type: { type: String }, | |
| 64 | |
| 65 /** Propagate the errorMessage property. */ | |
| 66 errorMessage: { type: String }, | |
| 67 }, | |
| 68 | |
| 69 /** | |
| 70 * Focuses the 'input' element. | |
| 71 */ | |
| 72 focus: function() { | |
| 
 
michaelpg
2015/09/24 18:59:31
opt nit: put ready() first
 
stevenjb
2015/09/24 19:28:52
Done.
 
 | |
| 73 this.$.input.inputElement.focus(); | |
| 74 }, | |
| 75 | |
| 76 /** @override */ | |
| 77 ready: function() { | |
| 78 this.$.events.forward(this.$.input, ['change']); | |
| 79 }, | |
| 80 | |
| 81 /** | |
| 82 * Polymer changed observer for |pref|. | |
| 83 * @private | |
| 84 */ | |
| 85 prefChanged_: function() { | |
| 86 if (!this.pref) | |
| 87 return; | |
| 88 | |
| 89 // Ignore updates while the input is focused so that user input is not | |
| 90 // overwritten. | |
| 91 if (this.$.input.focused) | |
| 92 return; | |
| 93 | |
| 94 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER || | |
| 95 this.pref.type == chrome.settingsPrivate.PrefType.BOOLEAN) { | |
| 96 this.value = this.pref.value.toString(); | |
| 97 } else { | |
| 98 assert(this.pref.type != chrome.settingsPrivate.PrefType.LIST); | |
| 
 
michaelpg
2015/09/24 18:59:31
nit... i'd rather assert it's a string or url type
 
stevenjb
2015/09/24 19:28:53
Done.
 
 | |
| 99 this.value = /** @type {string} */(this.pref.value); | |
| 100 } | |
| 101 }, | |
| 102 | |
| 103 /** | |
| 104 * Blur method for paper-input. Only update the pref value on a blur event. | |
| 105 * @private | |
| 106 */ | |
| 107 onBlur_: function() { | |
| 108 if (!this.pref) | |
| 109 return; | |
| 110 | |
| 111 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { | |
| 112 var n = parseInt(this.value, 10); | |
| 113 if (isNaN(n)) { | |
| 114 console.error('Bad value for numerical pref: ' + this.value); | |
| 
 
michaelpg
2015/09/24 18:59:30
should we error when this.value == ''?
 
stevenjb
2015/09/24 19:28:53
Hmm, maybe not. I'll go ahead and special case tha
 
stevenjb
2015/09/24 19:28:53
Hmm, maybe not. I'll go ahead and special case tha
 
 | |
| 115 return; | |
| 116 } | |
| 117 this.set('pref.value', n); | |
| 118 } else if (this.pref.type == chrome.settingsPrivate.PrefType.BOOLEAN) { | |
| 119 this.set('pref.value', this.value == 'true'); | |
| 
 
michaelpg
2015/09/24 18:59:31
This doesn't seem actually useful.
 
stevenjb
2015/09/24 19:28:53
Fair. I'll assert instead.
 
stevenjb
2015/09/24 19:28:53
Fair. I'll assert instead.
 
michaelpg
2015/09/24 19:42:14
fyi, you've been double-saving comments lately...
 
stevenjb
2015/09/24 19:59:59
Yeah, I don't know WTF is up with that. Thought ma
 
 | |
| 120 } else { | |
| 121 assert(this.pref.type != chrome.settingsPrivate.PrefType.LIST); | |
| 
 
michaelpg
2015/09/24 18:59:31
same assert nit
 
stevenjb
2015/09/24 19:28:53
Done.
 
 | |
| 122 this.set('pref.value', this.value); | |
| 123 } | |
| 124 }, | |
| 125 | |
| 126 /** | |
| 127 * @param {boolean} disabled | |
| 128 * @param {?chrome.settingsPrivate.PrefObject} pref | |
| 129 * @return {boolean} Whether the element should be disabled. | |
| 130 * @private | |
| 131 */ | |
| 132 isDisabled_: function(disabled, pref) { | |
| 133 return disabled || this.isPolicyControlled(pref); | |
| 134 }, | |
| 135 }); | |
| OLD | NEW |