Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(545)

Side by Side Diff: chrome/browser/resources/settings/controls/settings_input.js

Issue 2766093002: MD Settings: validate home button url input (Closed)
Patch Set: adjust blur/enter behaviors Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
41 /* Properties for paper-input. This is not strictly necessary. 35 /* Properties for paper-input. This is not strictly necessary.
42 * Though it does define the types for the closure compiler. */ 36 * Though it does define the types for the closure compiler. */
43 errorMessage: { type: String }, 37 errorMessage: {type: String},
44 label: { type: String }, 38 label: {type: String},
45 noLabelFloat: { type: Boolean, value: false }, 39 noLabelFloat: {type: Boolean, value: false},
46 pattern: { type: String }, 40 pattern: {type: String},
47 readonly: { type: Boolean, value: false }, 41 readonly: {type: Boolean, value: false},
48 required: { type: Boolean, value: false }, 42 required: {type: Boolean, value: false},
49 stopKeyboardEventPropagation: { type: Boolean, value: false }, 43 stopKeyboardEventPropagation: {type: Boolean, value: false},
50 type: { type: String }, 44 type: {type: String},
51 }, 45 },
52 46
53 /** 47 /**
54 * Focuses the 'input' element. 48 * Focuses the 'input' element.
55 */ 49 */
56 focus: function() { 50 focus: function() {
57 this.$.input.focus(); 51 this.$.input.focus();
58 }, 52 },
59 53
60 /** 54 /**
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 90
97 /** 91 /**
98 * Change event handler for paper-input. Updates the pref value. 92 * 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. 93 * settings-input uses the change event because it is fired by the Enter key.
100 * @private 94 * @private
101 */ 95 */
102 onChange_: function() { 96 onChange_: function() {
103 if (!this.pref) 97 if (!this.pref)
104 return; 98 return;
105 99
100 if (this.$.input.inputElement.invalid) {
101 this.resetValue_();
102 return;
103 }
104
106 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { 105 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) {
107 if (!this.value) { 106 if (!this.value) {
108 // Ignore empty input field and restore value. 107 this.resetValue_();
109 this.value = this.pref.value.toString();
110 return; 108 return;
111 } 109 }
112 var n = parseInt(this.value, 10); 110 var n = parseInt(this.value, 10);
113 if (isNaN(n)) { 111 if (isNaN(n)) {
114 console.error('Bad value for numerical pref: ' + this.value); 112 console.error('Bad value for numerical pref: ' + this.value);
115 return; 113 return;
116 } 114 }
117 this.set('pref.value', n); 115 this.set('pref.value', n);
118 } else { 116 } else {
119 assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING || 117 assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING ||
120 this.pref.type == chrome.settingsPrivate.PrefType.URL); 118 this.pref.type == chrome.settingsPrivate.PrefType.URL);
121 this.set('pref.value', this.value); 119 this.set('pref.value', this.value);
122 } 120 }
123 }, 121 },
124 122
123 /** @private */
124 resetValue_: function() {
125 this.$.input.inputElement.invalid = false;
126 this.setInputValueFromPref_();
127 this.$.input.blur();
128 },
129
125 /** 130 /**
126 * Handler for profile name keydowns. 131 * Handler for profile name keydowns.
127 * @param {!Event} event 132 * @param {!Event} event
128 * @private 133 * @private
129 */ 134 */
130 onKeydown_: function(event) { 135 onKeydown_: function(event) {
136 /* If pressed enter when input is invalid, do not trigger on-change */
137 if (event.key == 'Enter' && this.$.input.inputElement.invalid) {
138 event.preventDefault();
139 return;
140 }
141
131 if (event.key != 'Escape') 142 if (event.key != 'Escape')
132 return; 143 return;
133 144
134 this.setInputValueFromPref_(); 145 this.resetValue_();
135 this.$.input.blur();
136 }, 146 },
137 147
138 /** 148 /**
139 * @param {boolean} disabled 149 * @param {boolean} disabled
140 * @return {boolean} Whether the element should be disabled. 150 * @return {boolean} Whether the element should be disabled.
141 * @private 151 * @private
142 */ 152 */
143 isDisabled_: function(disabled) { 153 isDisabled_: function(disabled) {
144 return disabled || this.isPrefEnforced(); 154 return disabled || this.isPrefEnforced();
145 }, 155 },
146 }); 156 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698