Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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. | |
| 51 * @private | |
| 52 */ | |
| 53 selected_: String, | |
| 54 | |
| 55 /** | |
| 56 * The value of the 'custom' item. | 50 * 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', |
| 62 }, | 56 }, |
| 63 }, | 57 }, |
| 64 | 58 |
| 65 behaviors: [ | 59 behaviors: [ |
| 66 PrefControlBehavior, | 60 PrefControlBehavior, |
| 67 ], | 61 ], |
| 68 | 62 |
| 69 observers: [ | 63 observers: [ |
| 70 'updateSelected_(menuOptions, pref.value)', | 64 'updateSelected_(menuOptions, pref.value)', |
| 71 ], | 65 ], |
| 72 | 66 |
| 73 /** | 67 /** |
| 74 * Pass the selection change to the pref value. | 68 * Pass the selection change to the pref value. |
| 75 * @private | 69 * @private |
| 76 */ | 70 */ |
| 77 onChange_: function() { | 71 onChange_: function() { |
| 78 this.selected_ = this.$.dropdownMenu.value; | 72 var selected = this.$.dropdownMenu.value; |
| 79 | 73 |
| 80 if (this.selected_ == this.notFoundValue_) | 74 if (selected == this.notFoundValue_) |
| 81 return; | 75 return; |
| 82 | 76 |
| 83 var prefValue = Settings.PrefUtil.stringToPrefValue( | 77 var prefValue = Settings.PrefUtil.stringToPrefValue( |
| 84 this.selected_, assert(this.pref)); | 78 selected, assert(this.pref)); |
| 85 if (prefValue !== undefined) | 79 if (prefValue !== undefined) |
| 86 this.set('pref.value', prefValue); | 80 this.set('pref.value', prefValue); |
| 87 }, | 81 }, |
| 88 | 82 |
| 89 /** | 83 /** |
| 90 * Updates the selected item when the pref or menuOptions change. | 84 * Updates the selected item when the pref or menuOptions change. |
| 91 * @private | 85 * @private |
| 92 */ | 86 */ |
| 93 updateSelected_: function() { | 87 updateSelected_: function() { |
| 94 if (this.menuOptions === null || !this.menuOptions.length) | 88 if (this.menuOptions === null || !this.menuOptions.length) |
| 95 return; | 89 return; |
| 96 | 90 |
| 97 var prefValue = this.pref.value; | 91 var prefValue = this.pref.value; |
| 98 var option = this.menuOptions.find(function(menuItem) { | 92 var option = this.menuOptions.find(function(menuItem) { |
| 99 return menuItem.value == prefValue; | 93 return menuItem.value == prefValue; |
| 100 }); | 94 }); |
| 101 | 95 |
| 102 // Need to wait for the dom-repeat to render, before assigning a value to | 96 // Wait for the dom-repeat to populate the <select> before setting |
| 103 // |selected_|, otherwise select#value is not populated correctly. | 97 // <select>#value so the correct option gets selected. |
| 104 this.async(function() { | 98 this.async(function() { |
| 105 this.selected_ = option == undefined ? | 99 this.$.dropdownMenu.value = option == undefined ? |
|
dpapad
2016/10/25 01:02:38
Instead of removing the 1-way binding and directly
michaelpg
2016/10/25 01:14:37
We could, but I'm not sure why we would. Either wa
dpapad
2016/10/25 01:30:48
Ok, that's a fair point.
| |
| 106 this.notFoundValue_ : | 100 this.notFoundValue_ : |
| 107 Settings.PrefUtil.prefToString(assert(this.pref)); | 101 Settings.PrefUtil.prefToString(assert(this.pref)); |
| 108 }.bind(this)); | 102 }.bind(this)); |
| 109 }, | 103 }, |
| 110 | 104 |
| 111 /** | 105 /** |
| 112 * @param {string} selected | 106 * @param {?DropdownMenuOptionList} menuOptions |
| 107 * @param {string} prefValue | |
| 113 * @return {boolean} | 108 * @return {boolean} |
| 114 * @private | 109 * @private |
| 115 */ | 110 */ |
| 116 isSelectedNotFound_: function(selected) { | 111 showNotFoundValue_: function(menuOptions, prefValue) { |
| 117 return this.menuOptions.length > 0 && selected == this.notFoundValue_; | 112 // Don't show "Custom" before the options load. |
| 113 if (!menuOptions || !menuOptions.length) | |
| 114 return false; | |
| 115 | |
| 116 var option = menuOptions.find(function(menuItem) { | |
| 117 return menuItem.value == prefValue; | |
| 118 }); | |
| 119 return !option; | |
| 118 }, | 120 }, |
| 119 | 121 |
| 120 /** | 122 /** |
| 121 * @return {boolean} | 123 * @return {boolean} |
| 122 * @private | 124 * @private |
| 123 */ | 125 */ |
| 124 shouldDisableMenu_: function() { | 126 shouldDisableMenu_: function() { |
| 125 return this.disabled || this.menuOptions === null || | 127 return this.disabled || this.menuOptions === null || |
| 126 this.menuOptions.length == 0; | 128 this.menuOptions.length == 0; |
| 127 }, | 129 }, |
| 128 }); | 130 }); |
| OLD | NEW |