Chromium Code Reviews| Index: chrome/browser/resources/settings/controls/settings_dropdown_menu.js |
| diff --git a/chrome/browser/resources/settings/controls/settings_dropdown_menu.js b/chrome/browser/resources/settings/controls/settings_dropdown_menu.js |
| index 28bddf25f5cd52809968372660c5846e359f8733..e98aef084b212845c05dc140577d1c0cdffa7f15 100644 |
| --- a/chrome/browser/resources/settings/controls/settings_dropdown_menu.js |
| +++ b/chrome/browser/resources/settings/controls/settings_dropdown_menu.js |
| @@ -3,6 +3,23 @@ |
| // found in the LICENSE file. |
| /** |
| + * This tuple is made up of a (value, name, attribute). The value and name are |
| + * used by the dropdown menu. The attribute is optional 'user data' that is |
| + * ignored by the dropdown menu. |
| + * @typedef {{ |
| + * 0: (number|string), |
| + * 1: string, |
| + * 2: (string|undefined) |
| + * }} |
| + */ |
| +var DropdownMenuOption; |
| + |
| +/** |
| + * @typedef {!Array<DropdownMenuOption>} |
| + */ |
| +var DropdownMenuOptionList; |
| + |
| +/** |
| * 'settings-dropdown-menu' is a control for displaying options |
| * in the settings. |
|
michaelpg
2015/11/12 23:33:05
add a sentence explaining the not-found item behav
|
| * |
| @@ -27,18 +44,15 @@ Polymer({ |
| /** |
| * List of options for the drop-down menu. |
| - * @type {!Array<{0: (Object|number|string), 1: string, |
| - * 2: (string|undefined)}>} |
| + * @type {!DropdownMenuOptionList} |
| */ |
| menuOptions: { |
| - notify: true, |
| type: Array, |
| value: function() { return []; }, |
| }, |
| /** |
| * A single Preference object being tracked. |
| - * @type {?PrefObject} |
| */ |
| pref: { |
| type: Object, |
| @@ -92,7 +106,7 @@ Polymer({ |
| }, |
| behaviors: [ |
| - I18nBehavior |
| + I18nBehavior, |
| ], |
| observers: [ |
| @@ -102,8 +116,7 @@ Polymer({ |
| /** |
| * Check to see if we have all the pieces needed to enable the control. |
| - * @param {!Array<{0: (Object|number|string), 1: string, |
| - * 2: (string|undefined)}>} menuOptions |
| + * @param {!DropdownMenuOptionList} menuOptions |
| * @param {string} selectedValue_ |
| * @private |
| */ |
| @@ -116,13 +129,13 @@ Polymer({ |
| // Create a map from index value [0] back to the index i. |
| var result = {}; |
| for (var i = 0; i < this.menuOptions.length; ++i) |
| - result[JSON.stringify(this.menuOptions[i][0])] = i.toString(); |
| + result[String(this.menuOptions[i][0])] = i.toString(); |
|
michaelpg
2015/11/12 23:33:05
btw, I think foo.toString() is more idiomatic than
|
| this.menuMap_ = result; |
| } |
| // We need the menuOptions and the selectedValue_. They may arrive |
| // at different times (each is asynchronous). |
| - this.selected_ = this.getItemIndex(this.selectedValue_); |
| + this.selected_ = this.itemIndex_(this.selectedValue_); |
| this.menuLabel_ = this.label; |
| this.$.dropdownMenu.disabled = false; |
|
michaelpg
2015/11/12 23:33:05
Also, what if I want a <settings-dropdown-menu dis
michaelpg
2015/11/12 23:33:05
I think disabling the control until the items are
|
| }, |
| @@ -132,7 +145,7 @@ Polymer({ |
| * @return {string} |
| * @private |
| */ |
| - getItemIndex: function(item) { |
| + itemIndex_: function(item) { |
| var result = this.menuMap_[item]; |
| if (result) |
| return result; |
| @@ -143,10 +156,10 @@ Polymer({ |
| /** |
| * @param {string} index An index into the menuOptions array. |
| - * @return {Object|number|string|undefined} |
| + * @return {number|string|undefined} |
| * @private |
| */ |
| - getItemValue: function(index) { |
| + itemValue_: function(index) { |
| if (this.menuOptions.length) { |
| var result = this.menuOptions[index]; |
|
michaelpg
2015/11/12 23:33:05
don't index into an array with a string
|
| if (result) |
| @@ -160,9 +173,9 @@ Polymer({ |
| * @private |
| */ |
| onSelectedChanged_: function() { |
| - var prefValue = this.getItemValue(this.selected_); |
| + var prefValue = this.itemValue_(this.selected_); |
| if (prefValue !== undefined) { |
| - this.selectedValue_ = JSON.stringify(prefValue); |
| + this.selectedValue_ = String(prefValue); |
|
michaelpg
2015/11/12 23:33:05
toString
|
| this.set('pref.value', prefValue); |
| } |
| }, |
| @@ -172,6 +185,7 @@ Polymer({ |
| * @private |
| */ |
| prefChanged_: function(value) { |
| - this.selectedValue_ = JSON.stringify(value); |
| + if (value !== undefined) |
| + this.selectedValue_ = String(value); |
|
michaelpg
2015/11/12 23:33:05
toString
|
| }, |
| }); |