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

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

Issue 2773233002: MD Settings: change settings-input to home-url-input due to reduced usage. (Closed)
Patch Set: reduce code 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
(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 * `settings-input` is a single-line text field for user input associated
8 * with a pref value.
9 */
10 Polymer({
11 is: 'settings-input',
12
13 behaviors: [CrPolicyPrefBehavior, PrefControlBehavior],
14
15 properties: {
16 /**
17 * The preference object to control.
18 * @type {!chrome.settingsPrivate.PrefObject|undefined}
19 * @override
20 */
21 pref: {observer: 'prefChanged_'},
22
23 /* The current value of the input, reflected to/from |pref|. */
24 value: {
25 type: String,
26 value: '',
27 notify: true,
28 },
29
30 /* Set to true to disable editing the input. */
31 disabled: {type: Boolean, value: false, reflectToAttribute: true},
32
33 canTab: Boolean,
34
35 invalid: {
36 type: Boolean,
37 value: false,
38 notify: true,
39 },
40
41 /* Properties for paper-input. This is not strictly necessary.
42 * Though it does define the types for the closure compiler. */
43 errorMessage: {type: String},
44 label: {type: String},
45 },
46
47 /**
48 * Focuses the 'input' element.
49 */
50 focus: function() {
51 this.$.input.focus();
52 },
53
54 /**
55 * Polymer changed observer for |pref|.
56 * @private
57 */
58 prefChanged_: function() {
59 if (!this.pref)
60 return;
61
62 // Ignore updates while the input is focused so that user input is not
63 // overwritten.
64 if (this.$.input.focused)
65 return;
66
67 this.setInputValueFromPref_();
68 },
69
70 /** @private */
71 setInputValueFromPref_: function() {
72 assert(this.pref.type == chrome.settingsPrivate.PrefType.URL);
73 this.value = /** @type {string} */ (this.pref.value);
74 },
75
76 /**
77 * Gets a tab index for this control if it can be tabbed to.
78 * @param {boolean} canTab
79 * @return {number}
80 * @private
81 */
82 getTabindex_: function(canTab) {
83 return canTab ? 0 : -1;
84 },
85
86 /**
87 * Change event handler for paper-input. Updates the pref value.
88 * settings-input uses the change event because it is fired by the Enter key.
89 * @private
90 */
91 onChange_: function() {
92 if (!this.pref)
93 return;
94
95 if (this.invalid) {
96 this.resetValue_();
97 return;
98 }
99
100 assert(this.pref.type == chrome.settingsPrivate.PrefType.URL);
101 this.set('pref.value', this.value);
102 },
103
104 /** @private */
105 resetValue_: function() {
106 this.invalid = false;
107 this.setInputValueFromPref_();
108 this.$.input.blur();
109 },
110
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')
124 return;
125
126 this.resetValue_();
127 },
128
129 /**
130 * @param {boolean} disabled
131 * @return {boolean} Whether the element should be disabled.
132 * @private
133 */
134 isDisabled_: function(disabled) {
135 return disabled || this.isPrefEnforced();
136 },
137 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698