OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 cr.define('options', function() { | |
6 | |
7 var OptionsPage = options.OptionsPage; | |
8 | |
9 /** | |
10 * FontSettings class | |
11 * Encapsulated handling of the 'Fonts and Encoding' page. | |
12 * @class | |
13 */ | |
14 function FontSettings() { | |
15 OptionsPage.call(this, | |
16 'fonts', | |
17 loadTimeData.getString('fontSettingsPageTabTitle'), | |
18 'font-settings'); | |
19 } | |
20 | |
21 cr.addSingletonGetter(FontSettings); | |
22 | |
23 FontSettings.prototype = { | |
24 __proto__: OptionsPage.prototype, | |
25 | |
26 /** | |
27 * Initialize the page. | |
28 */ | |
29 initializePage: function() { | |
30 OptionsPage.prototype.initializePage.call(this); | |
31 | |
32 var standardFontRange = $('standard-font-size'); | |
33 standardFontRange.valueMap = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, | |
34 22, 24, 26, 28, 30, 32, 34, 36, 40, 44, 48, 56, 64, 72]; | |
35 standardFontRange.continuous = false; | |
36 standardFontRange.notifyChange = this.standardRangeChanged_.bind(this); | |
37 standardFontRange.notifyPrefChange = | |
38 this.standardFontSizeChanged_.bind(this); | |
39 | |
40 var minimumFontRange = $('minimum-font-size'); | |
41 minimumFontRange.valueMap = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, | |
42 18, 20, 22, 24]; | |
43 minimumFontRange.continuous = false; | |
44 minimumFontRange.notifyChange = this.minimumRangeChanged_.bind(this); | |
45 minimumFontRange.notifyPrefChange = | |
46 this.minimumFontSizeChanged_.bind(this); | |
47 | |
48 var placeholder = loadTimeData.getString('fontSettingsPlaceholder'); | |
49 var elements = [$('standard-font-family'), $('serif-font-family'), | |
50 $('sans-serif-font-family'), $('fixed-font-family'), | |
51 $('font-encoding')]; | |
52 elements.forEach(function(el) { | |
53 el.appendChild(new Option(placeholder)); | |
54 el.setDisabled('noFontsAvailable', true); | |
55 }); | |
56 | |
57 $('font-settings-confirm').onclick = function() { | |
58 OptionsPage.closeOverlay(); | |
59 }; | |
60 }, | |
61 | |
62 /** | |
63 * Called by the options page when this page has been shown. | |
64 */ | |
65 didShowPage: function() { | |
66 // The fonts list may be large so we only load it when this page is | |
67 // loaded for the first time. This makes opening the options window | |
68 // faster and consume less memory if the user never opens the fonts | |
69 // dialog. | |
70 if (!this.hasShown) { | |
71 chrome.send('fetchFontsData'); | |
72 this.hasShown = true; | |
73 } | |
74 }, | |
75 | |
76 /** | |
77 * Called as the user changes the standard font size. This allows for | |
78 * reflecting the change in the UI before the preference has been changed. | |
79 * @param {Element} el The slider input element. | |
80 * @param {number} value The mapped value currently set by the slider. | |
81 * @private | |
82 */ | |
83 standardRangeChanged_: function(el, value) { | |
84 var fontSampleEl = $('standard-font-sample'); | |
85 this.setUpFontSample_(fontSampleEl, value, fontSampleEl.style.fontFamily, | |
86 true); | |
87 | |
88 fontSampleEl = $('serif-font-sample'); | |
89 this.setUpFontSample_(fontSampleEl, value, fontSampleEl.style.fontFamily, | |
90 true); | |
91 | |
92 fontSampleEl = $('sans-serif-font-sample'); | |
93 this.setUpFontSample_(fontSampleEl, value, fontSampleEl.style.fontFamily, | |
94 true); | |
95 | |
96 fontSampleEl = $('fixed-font-sample'); | |
97 this.setUpFontSample_(fontSampleEl, | |
98 value - OptionsPage.SIZE_DIFFERENCE_FIXED_STANDARD, | |
99 fontSampleEl.style.fontFamily, false); | |
100 }, | |
101 | |
102 /** | |
103 * Sets the 'default_fixed_font_size' preference when the standard font | |
104 * size has been changed by the user. | |
105 * @param {Element} el The slider input element. | |
106 * @param {number} value The mapped value that has been saved. | |
107 * @private | |
108 */ | |
109 standardFontSizeChanged_: function(el, value) { | |
110 Preferences.setIntegerPref( | |
111 'webkit.webprefs.default_fixed_font_size', | |
112 value - OptionsPage.SIZE_DIFFERENCE_FIXED_STANDARD, ''); | |
113 }, | |
114 | |
115 /** | |
116 * Called as the user changes the miniumum font size. This allows for | |
117 * reflecting the change in the UI before the preference has been changed. | |
118 * @param {Element} el The slider input element. | |
119 * @param {number} value The mapped value currently set by the slider. | |
120 * @private | |
121 */ | |
122 minimumRangeChanged_: function(el, value) { | |
123 var fontSampleEl = $('minimum-font-sample'); | |
124 this.setUpFontSample_(fontSampleEl, value, fontSampleEl.style.fontFamily, | |
125 true); | |
126 }, | |
127 | |
128 /** | |
129 * Sets the 'minimum_logical_font_size' preference when the minimum font | |
130 * size has been changed by the user. | |
131 * @param {Element} el The slider input element. | |
132 * @param {number} value The mapped value that has been saved. | |
133 * @private | |
134 */ | |
135 minimumFontSizeChanged_: function(el, value) { | |
136 Preferences.setIntegerPref( | |
137 'webkit.webprefs.minimum_logical_font_size', value, ''); | |
138 }, | |
139 | |
140 /** | |
141 * Sets the text, font size and font family of the sample text. | |
142 * @param {Element} el The div containing the sample text. | |
143 * @param {number} size The font size of the sample text. | |
144 * @param {string} font The font family of the sample text. | |
145 * @param {bool} showSize True if the font size should appear in the sample. | |
146 * @private | |
147 */ | |
148 setUpFontSample_: function(el, size, font, showSize) { | |
149 var prefix = showSize ? (size + ': ') : ''; | |
150 el.textContent = prefix + | |
151 loadTimeData.getString('fontSettingsLoremIpsum'); | |
152 el.style.fontSize = size + 'px'; | |
153 if (font) | |
154 el.style.fontFamily = font; | |
155 }, | |
156 | |
157 /** | |
158 * Populates a select list and selects the specified item. | |
159 * @param {Element} element The select element to populate. | |
160 * @param {Array} items The array of items from which to populate. | |
161 * @param {string} selectedValue The selected item. | |
162 * @private | |
163 */ | |
164 populateSelect_: function(element, items, selectedValue) { | |
165 // Remove any existing content. | |
166 element.textContent = ''; | |
167 | |
168 // Insert new child nodes into select element. | |
169 var value, text, selected, option; | |
170 for (var i = 0; i < items.length; i++) { | |
171 value = items[i][0]; | |
172 text = items[i][1]; | |
173 dir = items[i][2]; | |
174 if (text) { | |
175 selected = value == selectedValue; | |
176 var option = new Option(text, value, false, selected); | |
177 option.dir = dir; | |
178 element.appendChild(option); | |
179 } else { | |
180 element.appendChild(document.createElement('hr')); | |
181 } | |
182 } | |
183 | |
184 element.setDisabled('noFontsAvailable', false); | |
185 } | |
186 }; | |
187 | |
188 // Chrome callbacks | |
189 FontSettings.setFontsData = function(fonts, encodings, selectedValues) { | |
190 FontSettings.getInstance().populateSelect_($('standard-font-family'), fonts, | |
191 selectedValues[0]); | |
192 FontSettings.getInstance().populateSelect_($('serif-font-family'), fonts, | |
193 selectedValues[1]); | |
194 FontSettings.getInstance().populateSelect_($('sans-serif-font-family'), | |
195 fonts, selectedValues[2]); | |
196 FontSettings.getInstance().populateSelect_($('fixed-font-family'), fonts, | |
197 selectedValues[3]); | |
198 FontSettings.getInstance().populateSelect_($('font-encoding'), encodings, | |
199 selectedValues[4]); | |
200 }; | |
201 | |
202 FontSettings.setUpStandardFontSample = function(font, size) { | |
203 FontSettings.getInstance().setUpFontSample_($('standard-font-sample'), size, | |
204 font, true); | |
205 }; | |
206 | |
207 FontSettings.setUpSerifFontSample = function(font, size) { | |
208 FontSettings.getInstance().setUpFontSample_($('serif-font-sample'), size, | |
209 font, true); | |
210 }; | |
211 | |
212 FontSettings.setUpSansSerifFontSample = function(font, size) { | |
213 FontSettings.getInstance().setUpFontSample_($('sans-serif-font-sample'), | |
214 size, font, true); | |
215 }; | |
216 | |
217 FontSettings.setUpFixedFontSample = function(font, size) { | |
218 FontSettings.getInstance().setUpFontSample_($('fixed-font-sample'), | |
219 size, font, false); | |
220 }; | |
221 | |
222 FontSettings.setUpMinimumFontSample = function(size) { | |
223 // If size is less than 6, represent it as six in the sample to account | |
224 // for the minimum logical font size. | |
225 if (size < 6) | |
226 size = 6; | |
227 FontSettings.getInstance().setUpFontSample_($('minimum-font-sample'), size, | |
228 null, true); | |
229 }; | |
230 | |
231 // Export | |
232 return { | |
233 FontSettings: FontSettings | |
234 }; | |
235 }); | |
236 | |
OLD | NEW |