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

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

Issue 2446003002: settings-dropdown-menu must set <select>#value after its options change (Closed)
Patch Set: Hide custom option via CSS Created 4 years, 1 month 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 * The |name| is shown in the gui. The |value| us use to set or compare with 6 * The |name| is shown in the gui. The |value| us use to set or compare with
7 * the preference value. 7 * the preference value.
8 * @typedef {{ 8 * @typedef {{
9 * name: string, 9 * name: string,
10 * value: (number|string) 10 * value: (number|string)
(...skipping 29 matching lines...) Expand all
40 }, 40 },
41 41
42 /** Whether the dropdown menu should be disabled. */ 42 /** Whether the dropdown menu should be disabled. */
43 disabled: { 43 disabled: {
44 type: Boolean, 44 type: Boolean,
45 reflectToAttribute: true, 45 reflectToAttribute: true,
46 value: false, 46 value: false,
47 }, 47 },
48 48
49 /** 49 /**
50 * The current selected value, as a string. 50 * The value of the "custom" item.
51 * @private
52 */
53 selected_: String,
54
55 /**
56 * The value of the 'custom' item.
57 * @private 51 * @private
58 */ 52 */
59 notFoundValue_: { 53 notFoundValue_: {
60 type: String, 54 type: String,
61 value: 'SETTINGS_DROPDOWN_NOT_FOUND_ITEM', 55 value: 'SETTINGS_DROPDOWN_NOT_FOUND_ITEM',
56 readOnly: true,
62 }, 57 },
63 }, 58 },
64 59
65 behaviors: [ 60 behaviors: [
66 PrefControlBehavior, 61 PrefControlBehavior,
67 ], 62 ],
68 63
69 observers: [ 64 observers: [
70 'updateSelected_(menuOptions, pref.value)', 65 'updateSelected_(menuOptions, pref.value)',
71 ], 66 ],
72 67
73 /** 68 /**
74 * Pass the selection change to the pref value. 69 * Pass the selection change to the pref value.
75 * @private 70 * @private
76 */ 71 */
77 onChange_: function() { 72 onChange_: function() {
78 this.selected_ = this.$.dropdownMenu.value; 73 var selected = this.$.dropdownMenu.value;
79 74
80 if (this.selected_ == this.notFoundValue_) 75 if (selected == this.notFoundValue_)
81 return; 76 return;
82 77
83 var prefValue = Settings.PrefUtil.stringToPrefValue( 78 var prefValue = Settings.PrefUtil.stringToPrefValue(
84 this.selected_, assert(this.pref)); 79 selected, assert(this.pref));
85 if (prefValue !== undefined) 80 if (prefValue !== undefined)
86 this.set('pref.value', prefValue); 81 this.set('pref.value', prefValue);
87 }, 82 },
88 83
89 /** 84 /**
90 * Updates the selected item when the pref or menuOptions change. 85 * Updates the selected item when the pref or menuOptions change.
91 * @private 86 * @private
92 */ 87 */
93 updateSelected_: function() { 88 updateSelected_: function() {
94 if (this.menuOptions === null || !this.menuOptions.length) 89 if (this.menuOptions === null || !this.menuOptions.length)
95 return; 90 return;
96 91
97 var prefValue = this.pref.value; 92 var prefValue = this.pref.value;
98 var option = this.menuOptions.find(function(menuItem) { 93 var option = this.menuOptions.find(function(menuItem) {
99 return menuItem.value == prefValue; 94 return menuItem.value == prefValue;
100 }); 95 });
101 96
102 // Need to wait for the dom-repeat to render, before assigning a value to 97 // Wait for the dom-repeat to populate the <select> before setting
103 // |selected_|, otherwise select#value is not populated correctly. 98 // <select>#value so the correct option gets selected.
104 this.async(function() { 99 this.async(function() {
105 this.selected_ = option == undefined ? 100 this.$.dropdownMenu.value = option == undefined ?
106 this.notFoundValue_ : 101 this.notFoundValue_ :
107 Settings.PrefUtil.prefToString(assert(this.pref)); 102 Settings.PrefUtil.prefToString(assert(this.pref));
108 }.bind(this)); 103 }.bind(this));
109 }, 104 },
110 105
111 /** 106 /**
112 * @param {string} selected 107 * @param {?DropdownMenuOptionList} menuOptions
108 * @param {string} prefValue
113 * @return {boolean} 109 * @return {boolean}
114 * @private 110 * @private
115 */ 111 */
116 isSelectedNotFound_: function(selected) { 112 showNotFoundValue_: function(menuOptions, prefValue) {
117 return this.menuOptions.length > 0 && selected == this.notFoundValue_; 113 // Don't show "Custom" before the options load.
114 if (!menuOptions || !menuOptions.length)
115 return false;
116
117 var option = menuOptions.find(function(menuItem) {
118 return menuItem.value == prefValue;
119 });
120 return !option;
118 }, 121 },
119 122
120 /** 123 /**
121 * @return {boolean} 124 * @return {boolean}
122 * @private 125 * @private
123 */ 126 */
124 shouldDisableMenu_: function() { 127 shouldDisableMenu_: function() {
125 return this.disabled || this.menuOptions === null || 128 return this.disabled || this.menuOptions === null ||
126 this.menuOptions.length == 0; 129 this.menuOptions.length == 0;
127 }, 130 },
128 }); 131 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698