OLD | NEW |
---|---|
(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 * 'settings-appearance-font-menu' is a control for displaying font options | |
7 * in the appearance settings. | |
8 * | |
9 * Example: | |
10 * | |
11 * <settings-appearance-font-menu pref="{{prefs.foo}}"> | |
12 * </settings-appearance-font-menu> | |
13 * | |
14 * @group Chrome Settings Elements | |
15 * @element settings-appearance-font-menu | |
16 */ | |
17 Polymer({ | |
18 is: 'settings-appearance-font-menu', | |
19 | |
20 properties: { | |
21 /** | |
22 * A text label for the drop-down menu. | |
23 */ | |
24 label: { | |
25 type: String, | |
26 }, | |
27 | |
28 /** | |
29 * List of options for the drop-down menu. | |
30 * @type {!Array<{0: (Object|number|string), 1: string, | |
31 * 2: (string|undefined)}>} | |
32 */ | |
33 menuOptions: { | |
34 notify: true, | |
35 type: Array, | |
36 value: function() { return []; }, | |
37 }, | |
38 | |
39 /** | |
40 * Preference state. | |
stevenjb
2015/10/29 19:56:02
This should be more descriptive.
dschuyler
2015/11/04 22:10:56
Done.
| |
41 * @type {!Object} | |
stevenjb
2015/10/29 19:56:02
'Object' doesn't add any type info. Isn't this alw
dschuyler
2015/11/04 22:10:56
I've tested it with number, string and dictionary.
| |
42 */ | |
43 pref: { | |
44 type: Object, | |
45 notify: true, | |
46 }, | |
47 | |
48 /** | |
49 * Either loading text or the label for the drop-down menu. | |
50 * @private | |
51 */ | |
52 menuLabel_: { | |
53 type: String, | |
54 value: function() { return loadTimeData.getString('loading'); }, | |
55 }, | |
56 | |
57 /** | |
58 * A reverse lookup from the menu value back to the index in the | |
59 * menuOptions array. | |
60 * @private {!Object<string, string>} | |
61 */ | |
62 menuMap_: { | |
63 type: Object, | |
64 value: function() { return {}; }, | |
65 }, | |
66 | |
67 /** | |
68 * The current selected item (an index number as a string). | |
69 * @private | |
70 */ | |
71 selected_: { | |
72 notify: true, | |
73 observer: 'onSelectedChanged_', | |
74 type: String, | |
75 }, | |
76 | |
77 /** | |
78 * The current selected pref value. | |
79 * @private | |
80 */ | |
81 selectedValue_: { | |
82 type: String, | |
83 }, | |
84 | |
85 /** | |
86 * Whether to show the custom font size menu item. | |
87 * @private | |
88 */ | |
89 showNotFoundValue_: { | |
90 type: Boolean, | |
91 }, | |
92 }, | |
93 | |
94 behaviors: [ | |
95 I18nBehavior | |
96 ], | |
97 | |
98 observers: [ | |
99 'checkSetup_(menuOptions, selectedValue_)', | |
100 'prefChanged_(pref.value)', | |
101 ], | |
102 | |
103 /** | |
104 * Check to see if we have all the pieces needed to enable the control. | |
105 * @param {!Array<{0: (Object|number|string), 1: string, | |
106 * 2: (string|undefined)}>} menuOptions | |
107 * @param {string} selectedValue_ | |
108 * @private | |
109 */ | |
110 checkSetup_: function(menuOptions, selectedValue_) { | |
111 if (!this.menuOptions.length) { | |
112 return; | |
113 } | |
114 | |
115 if (!Object.keys(this.menuMap_).length) { | |
116 // Create a map from index value [0] back to the index i. | |
117 var result = {}; | |
118 for (var i = 0; i < this.menuOptions.length; ++i) | |
119 result[JSON.stringify(this.menuOptions[i][0])] = i.toString(); | |
120 this.menuMap_ = result; | |
121 } | |
122 | |
123 // We need the menuOptions and the selectedValue_. They may arrive | |
124 // at different times (each is asynchronous). | |
125 this.selected_ = this.getItemIndex(this.selectedValue_); | |
126 this.menuLabel_ = this.label; | |
127 this.$.dropdownMenu.disabled = false; | |
128 }, | |
129 | |
130 /** | |
131 * @param {string} item A value from the menuOptions array. | |
132 * @return {string} | |
133 * @private | |
134 */ | |
135 getItemIndex: function(item) { | |
136 var result = this.menuMap_[item]; | |
137 if (result) | |
138 return result; | |
139 this.showNotFoundValue_ = true; | |
140 // The 'not found' item is added as the last of the options. | |
141 return (this.menuOptions.length).toString(); | |
142 }, | |
143 | |
144 /** | |
145 * @param {string} index An index into the menuOptions array. | |
146 * @return {Object|number|string|undefined} | |
147 * @private | |
148 */ | |
149 getItemValue: function(index) { | |
150 if (this.menuOptions.length) { | |
151 var result = this.menuOptions[index]; | |
152 if (result) | |
153 return result[0]; | |
154 } | |
155 return undefined; | |
156 }, | |
157 | |
158 /** | |
159 * Pass the selection change to the pref value. | |
160 * @private | |
161 */ | |
162 onSelectedChanged_: function() { | |
163 var prefValue = this.getItemValue(this.selected_); | |
164 if (prefValue !== undefined) { | |
165 this.selectedValue_ = JSON.stringify(prefValue); | |
166 this.set('pref.value', prefValue); | |
167 } | |
168 }, | |
169 | |
170 /** | |
171 * @param {number|string} value A value from the menuOptions array. | |
172 * @private | |
173 */ | |
174 prefChanged_: function(value) { | |
175 this.selectedValue_ = JSON.stringify(value); | |
176 }, | |
177 }); | |
OLD | NEW |