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