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 12 matching lines...) Expand all Loading... | |
23 * | 23 * |
24 * Example: | 24 * Example: |
25 * | 25 * |
26 * <settings-dropdown-menu pref="{{prefs.foo}}"> | 26 * <settings-dropdown-menu pref="{{prefs.foo}}"> |
27 * </settings-dropdown-menu> | 27 * </settings-dropdown-menu> |
28 */ | 28 */ |
29 Polymer({ | 29 Polymer({ |
30 is: 'settings-dropdown-menu', | 30 is: 'settings-dropdown-menu', |
31 | 31 |
32 properties: { | 32 properties: { |
33 /** A text label for the drop-down menu. */ | |
34 label: String, | |
35 | |
36 /** | 33 /** |
37 * List of options for the drop-down menu. | 34 * List of options for the drop-down menu. |
38 * @type {!DropdownMenuOptionList} | 35 * @type {!DropdownMenuOptionList} |
Dan Beam
2016/10/04 01:59:45
{?
dpapad
2016/10/04 20:38:12
Done.
| |
39 */ | 36 */ |
40 menuOptions: { | 37 menuOptions: { |
41 type: Array, | 38 type: Array, |
42 value: function() { return []; }, | 39 value: null, |
43 }, | 40 }, |
44 | 41 |
45 /** Whether the dropdown menu should be disabled. */ | 42 /** Whether the dropdown menu should be disabled. */ |
46 disabled: { | 43 disabled: { |
47 type: Boolean, | 44 type: Boolean, |
48 reflectToAttribute: true, | 45 reflectToAttribute: true, |
49 value: false, | 46 value: false, |
50 }, | 47 }, |
51 | 48 |
52 /** | 49 /** |
53 * Either loading text or the label for the drop-down menu. | |
54 * @private | |
55 */ | |
56 menuLabel_: { | |
57 type: String, | |
58 value: function() { return loadTimeData.getString('loading'); }, | |
59 }, | |
60 | |
61 /** | |
62 * The current selected value, as a string. | 50 * The current selected value, as a string. |
63 * @private | 51 * @private |
64 */ | 52 */ |
65 selected_: String, | 53 selected_: String, |
66 | 54 |
67 /** | 55 /** |
68 * The value of the 'custom' item. | 56 * The value of the 'custom' item. |
69 * @private | 57 * @private |
70 */ | 58 */ |
71 notFoundValue_: { | 59 notFoundValue_: { |
72 type: String, | 60 type: String, |
73 value: 'SETTINGS_DROPDOWN_NOT_FOUND_ITEM', | 61 value: 'SETTINGS_DROPDOWN_NOT_FOUND_ITEM', |
74 }, | 62 }, |
75 }, | 63 }, |
76 | 64 |
77 behaviors: [ | 65 behaviors: [ |
78 PrefControlBehavior, | 66 PrefControlBehavior, |
79 ], | 67 ], |
80 | 68 |
81 observers: [ | 69 observers: [ |
82 'checkSetup_(menuOptions)', | 70 'updateSelected_(menuOptions, pref.value)', |
83 'updateSelected_(pref.value)', | |
84 ], | 71 ], |
85 | 72 |
86 ready: function() { | |
87 this.checkSetup_(this.menuOptions); | |
88 }, | |
89 | |
90 /** | |
91 * Check to see if we have all the pieces needed to enable the control. | |
92 * @param {DropdownMenuOptionList} menuOptions | |
93 * @private | |
94 */ | |
95 checkSetup_: function(menuOptions) { | |
96 if (!this.menuOptions.length) | |
97 return; | |
98 | |
99 // Do not set |menuLabel_| to a falsy value: http://goo.gl/OnKYko | |
100 // (paper-dropdown-menu#181). | |
101 this.menuLabel_ = this.label || ' '; | |
102 this.updateSelected_(); | |
103 }, | |
104 | |
105 /** | 73 /** |
106 * Pass the selection change to the pref value. | 74 * Pass the selection change to the pref value. |
107 * @private | 75 * @private |
108 */ | 76 */ |
109 onSelect_: function() { | 77 onChange_: function() { |
110 if (!this.pref || this.selected_ == undefined || | 78 this.selected_ = this.$.dropdownMenu.value; |
111 this.selected_ == this.notFoundValue_) { | 79 |
80 if (this.selected_ == this.notFoundValue_) | |
112 return; | 81 return; |
113 } | 82 |
114 var prefValue = Settings.PrefUtil.stringToPrefValue( | 83 var prefValue = Settings.PrefUtil.stringToPrefValue( |
115 this.selected_, this.pref); | 84 this.selected_, assert(this.pref)); |
116 if (prefValue !== undefined) | 85 if (prefValue !== undefined) |
117 this.set('pref.value', prefValue); | 86 this.set('pref.value', prefValue); |
118 }, | 87 }, |
119 | 88 |
120 /** | 89 /** |
121 * Updates the selected item when the pref or menuOptions change. | 90 * Updates the selected item when the pref or menuOptions change. |
122 * @private | 91 * @private |
123 */ | 92 */ |
124 updateSelected_: function() { | 93 updateSelected_: function() { |
125 if (!this.pref || !this.menuOptions.length) | 94 if (this.menuOptions === null || !this.menuOptions.length) |
126 return; | 95 return; |
96 | |
127 var prefValue = this.pref.value; | 97 var prefValue = this.pref.value; |
128 var option = this.menuOptions.find(function(menuItem) { | 98 var option = this.menuOptions.find(function(menuItem) { |
129 return menuItem.value == prefValue; | 99 return menuItem.value == prefValue; |
130 }); | 100 }); |
131 if (option == undefined) | 101 |
132 this.selected_ = this.notFoundValue_; | 102 // Need to wait for the dom-repeat to render, before assigning a value to |
133 else | 103 // |selected_|, otherwise select#value is not populated correctly. |
134 this.selected_ = Settings.PrefUtil.prefToString(this.pref); | 104 this.async(function() { |
105 this.selected_ = option == undefined ? | |
106 this.notFoundValue_ : | |
107 Settings.PrefUtil.prefToString(assert(this.pref)); | |
108 }.bind(this)); | |
135 }, | 109 }, |
136 | 110 |
137 /** | 111 /** |
138 * @param {string} selected | 112 * @param {string} selected |
139 * @return {boolean} | 113 * @return {boolean} |
140 * @private | 114 * @private |
141 */ | 115 */ |
142 isSelectedNotFound_: function(selected) { | 116 isSelectedNotFound_: function(selected) { |
143 return this.menuOptions.length > 0 && selected == this.notFoundValue_; | 117 return this.menuOptions.length > 0 && selected == this.notFoundValue_; |
144 }, | 118 }, |
145 | 119 |
146 /** | 120 /** |
147 * @return {boolean} | 121 * @return {boolean} |
148 * @private | 122 * @private |
149 */ | 123 */ |
150 shouldDisableMenu_: function() { | 124 shouldDisableMenu_: function() { |
151 return this.disabled || !this.menuOptions.length; | 125 return this.disabled || this.menuOptions === null || |
126 this.menuOptions.length == 0; | |
152 }, | 127 }, |
153 }); | 128 }); |
OLD | NEW |