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

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

Issue 2766093002: MD Settings: validate home button url input (Closed)
Patch Set: Merge branch 'master' into home-button 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
35 invalid: {
36 type: Boolean,
37 value: false,
38 notify: true,
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 },
46 pattern: { type: String },
47 readonly: { type: Boolean, value: false },
48 required: { type: Boolean, value: false },
49 stopKeyboardEventPropagation: { type: Boolean, value: false },
50 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 /**
61 * Polymer changed observer for |pref|. 55 * Polymer changed observer for |pref|.
62 * @private 56 * @private
63 */ 57 */
64 prefChanged_: function() { 58 prefChanged_: function() {
65 if (!this.pref) 59 if (!this.pref)
66 return; 60 return;
67 61
68 // Ignore updates while the input is focused so that user input is not 62 // Ignore updates while the input is focused so that user input is not
69 // overwritten. 63 // overwritten.
70 if (this.$.input.focused) 64 if (this.$.input.focused)
71 return; 65 return;
72 66
73 this.setInputValueFromPref_(); 67 this.setInputValueFromPref_();
74 }, 68 },
75 69
76 /** @private */ 70 /** @private */
77 setInputValueFromPref_: function() { 71 setInputValueFromPref_: function() {
78 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { 72 assert(this.pref.type == chrome.settingsPrivate.PrefType.URL);
79 this.value = this.pref.value.toString(); 73 this.value = /** @type {string} */ (this.pref.value);
80 } else {
81 assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING ||
82 this.pref.type == chrome.settingsPrivate.PrefType.URL);
83 this.value = /** @type {string} */(this.pref.value);
84 }
85 }, 74 },
86 75
87 /** 76 /**
88 * Gets a tab index for this control if it can be tabbed to. 77 * Gets a tab index for this control if it can be tabbed to.
89 * @param {boolean} canTab 78 * @param {boolean} canTab
90 * @return {number} 79 * @return {number}
91 * @private 80 * @private
92 */ 81 */
93 getTabindex_: function(canTab) { 82 getTabindex_: function(canTab) {
94 return canTab ? 0 : -1; 83 return canTab ? 0 : -1;
95 }, 84 },
96 85
97 /** 86 /**
98 * Change event handler for paper-input. Updates the pref value. 87 * 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. 88 * settings-input uses the change event because it is fired by the Enter key.
100 * @private 89 * @private
101 */ 90 */
102 onChange_: function() { 91 onChange_: function() {
103 if (!this.pref) 92 if (!this.pref)
104 return; 93 return;
105 94
106 if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { 95 if (this.invalid) {
107 if (!this.value) { 96 this.resetValue_();
108 // Ignore empty input field and restore value. 97 return;
109 this.value = this.pref.value.toString();
110 return;
111 }
112 var n = parseInt(this.value, 10);
113 if (isNaN(n)) {
114 console.error('Bad value for numerical pref: ' + this.value);
115 return;
116 }
117 this.set('pref.value', n);
118 } else {
119 assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING ||
120 this.pref.type == chrome.settingsPrivate.PrefType.URL);
121 this.set('pref.value', this.value);
122 } 98 }
99
100 assert(this.pref.type == chrome.settingsPrivate.PrefType.URL);
101 this.set('pref.value', this.value);
123 }, 102 },
124 103
125 /** 104 /** @private */
126 * Handler for profile name keydowns. 105 resetValue_: function() {
127 * @param {!Event} event 106 this.invalid = false;
128 * @private
129 */
130 onKeydown_: function(event) {
131 if (event.key != 'Escape')
132 return;
133
134 this.setInputValueFromPref_(); 107 this.setInputValueFromPref_();
135 this.$.input.blur(); 108 this.$.input.blur();
136 }, 109 },
137 110
138 /** 111 /**
112 * Keydown handler to specify enter-key and escape-key interactions.
113 * @param {!Event} event
114 * @private
115 */
116 onKeydown_: function(event) {
117 // If pressed enter when input is invalid, do not trigger on-change.
118 if (event.key == 'Enter' && this.invalid) {
119 event.preventDefault();
120 return;
121 }
122
123 if (event.key != 'Escape')
dpapad 2017/03/24 21:47:43 Nit(optional): Maybe shorten as follows. if (even
scottchen 2017/03/24 22:56:31 Acknowledged. Will do in the refactor CL.
124 return;
125
126 this.resetValue_();
127 },
128
129 /**
139 * @param {boolean} disabled 130 * @param {boolean} disabled
140 * @return {boolean} Whether the element should be disabled. 131 * @return {boolean} Whether the element should be disabled.
141 * @private 132 * @private
142 */ 133 */
143 isDisabled_: function(disabled) { 134 isDisabled_: function(disabled) {
144 return disabled || this.isPrefEnforced(); 135 return disabled || this.isPrefEnforced();
145 }, 136 },
146 }); 137 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698