OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // The scripts supported by the Font Settings Extension API. | 5 'use strict'; |
6 var scripts = [ | 6 |
7 { scriptCode: 'Zyyy', scriptName: 'Default'}, | 7 /** |
8 * @fileoverview The Advanced Font Settings Extension implementation. | |
9 */ | |
10 | |
11 function $(id) { | |
12 return document.getElementById(id); | |
13 } | |
14 | |
15 /** | |
16 * @namespace | |
17 */ | |
18 var advancedFonts = {}; | |
19 | |
20 /** | |
21 * The ICU script code for the Common, or global, script, which is used as the | |
22 * fallback when the script is undeclared. | |
23 * @const | |
24 */ | |
25 advancedFonts.COMMON_SCRIPT = 'Zyyy'; | |
26 | |
27 /** | |
28 * The scripts supported by the Font Settings Extension API. | |
29 * @const | |
30 */ | |
31 advancedFonts.scripts = [ | |
32 { scriptCode: advancedFonts.COMMON_SCRIPT, scriptName: 'Default'}, | |
8 { scriptCode: 'Afak', scriptName: 'Afaka'}, | 33 { scriptCode: 'Afak', scriptName: 'Afaka'}, |
9 { scriptCode: 'Arab', scriptName: 'Arabic'}, | 34 { scriptCode: 'Arab', scriptName: 'Arabic'}, |
10 { scriptCode: 'Armi', scriptName: 'Imperial Aramaic'}, | 35 { scriptCode: 'Armi', scriptName: 'Imperial Aramaic'}, |
11 { scriptCode: 'Armn', scriptName: 'Armenian'}, | 36 { scriptCode: 'Armn', scriptName: 'Armenian'}, |
12 { scriptCode: 'Avst', scriptName: 'Avestan'}, | 37 { scriptCode: 'Avst', scriptName: 'Avestan'}, |
13 { scriptCode: 'Bali', scriptName: 'Balinese'}, | 38 { scriptCode: 'Bali', scriptName: 'Balinese'}, |
14 { scriptCode: 'Bamu', scriptName: 'Bamum'}, | 39 { scriptCode: 'Bamu', scriptName: 'Bamum'}, |
15 { scriptCode: 'Bass', scriptName: 'Bassa Vah'}, | 40 { scriptCode: 'Bass', scriptName: 'Bassa Vah'}, |
16 { scriptCode: 'Batk', scriptName: 'Batak'}, | 41 { scriptCode: 'Batk', scriptName: 'Batak'}, |
17 { scriptCode: 'Beng', scriptName: 'Bengali'}, | 42 { scriptCode: 'Beng', scriptName: 'Bengali'}, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 { scriptCode: 'Visp', scriptName: 'Visible Speech'}, | 176 { scriptCode: 'Visp', scriptName: 'Visible Speech'}, |
152 { scriptCode: 'Wara', scriptName: 'Varang Kshiti'}, | 177 { scriptCode: 'Wara', scriptName: 'Varang Kshiti'}, |
153 { scriptCode: 'Wole', scriptName: 'Woleai'}, | 178 { scriptCode: 'Wole', scriptName: 'Woleai'}, |
154 { scriptCode: 'Xpeo', scriptName: 'Old Persian'}, | 179 { scriptCode: 'Xpeo', scriptName: 'Old Persian'}, |
155 { scriptCode: 'Xsux', scriptName: 'Sumero-Akkadian Cuneiform'}, | 180 { scriptCode: 'Xsux', scriptName: 'Sumero-Akkadian Cuneiform'}, |
156 { scriptCode: 'Yiii', scriptName: 'Yi'}, | 181 { scriptCode: 'Yiii', scriptName: 'Yi'}, |
157 { scriptCode: 'Zmth', scriptName: 'Mathematical Notation'}, | 182 { scriptCode: 'Zmth', scriptName: 'Mathematical Notation'}, |
158 { scriptCode: 'Zsym', scriptName: 'Symbols'} | 183 { scriptCode: 'Zsym', scriptName: 'Symbols'} |
159 ]; | 184 ]; |
160 | 185 |
161 // The generic font families supported by the Font Settings Extension API. | 186 /** |
162 var families = | 187 * The generic font families supported by the Font Settings Extension API. |
163 ["standard", "sansserif", "serif", "fixed", "cursive", "fantasy"]; | 188 * @const |
164 | 189 */ |
165 // Mapping between font list ids and the generic family setting they | 190 advancedFonts.FAMILIES = |
166 // represent. | 191 ['standard', 'sansserif', 'serif', 'fixed', 'cursive', 'fantasy']; |
167 var fontPickers = [ | 192 |
168 { fontList: 'standardFontList', name: 'standard' }, | 193 /** |
169 { fontList: 'serifFontList', name: 'serif' }, | 194 * Sample texts. |
170 { fontList: 'sansSerifFontList', name: 'sansserif' }, | 195 * @const |
171 { fontList: 'fixedFontList', name: 'fixed' } | 196 */ |
172 ]; | 197 advancedFonts.SAMPLE_TEXTS = { |
173 | |
174 // Ids of elements to contain the sample text. | |
175 var sampleTextDivIds = [ | |
176 'standardFontSample', | |
177 'serifFontSample', | |
178 'sansSerifFontSample', | |
179 'fixedFontSample', | |
180 'minFontSample' | |
181 ]; | |
182 | |
183 // Sample texts. | |
184 var defaultSampleText = 'The quick brown fox jumps over the lazy dog.'; | |
185 var scriptSpecificSampleText = { | |
186 // "Cyrllic script". | 198 // "Cyrllic script". |
187 'Cyrl': 'Кириллица', | 199 Cyrl: 'Кириллица', |
188 'Hang': '정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날.', | 200 Hang: '정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날.', |
189 'Hans': '床前明月光,疑是地上霜。举头望明月,低头思故乡。', | 201 Hans: '床前明月光,疑是地上霜。举头望明月,低头思故乡。', |
190 'Hant': '床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。', | 202 Hant: '床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。', |
191 'Jpan': '吾輩は猫である。名前はまだ無い。', | 203 Jpan: '吾輩は猫である。名前はまだ無い。', |
192 // "Khmer language". | 204 // "Khmer language". |
193 'Khmr': '\u1797\u17B6\u179F\u17B6\u1781\u17D2\u1798\u17C2\u179A', | 205 Khmr: '\u1797\u17B6\u179F\u17B6\u1781\u17D2\u1798\u17C2\u179A', |
194 }; | 206 Zyyy: 'The quick brown fox jumps over the lazy dog.' |
195 | 207 }; |
196 // Definition for ScriptList. | 208 |
197 cr.define('fontSettings.ui', function() { | 209 /** |
198 const List = cr.ui.List; | 210 * Controller of pending changes. |
199 const ListItem = cr.ui.ListItem; | 211 * @const |
200 const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | 212 */ |
201 | 213 advancedFonts.pendingChanges = new PendingChanges(); |
202 function ScriptListItem(info) { | 214 |
203 var el = cr.doc.createElement('li'); | 215 /** |
204 el.__proto__ = ScriptListItem.prototype; | 216 * Map from |genericFamily| to UI controls and data for its font setting. |
205 el.info_ = info; | 217 */ |
206 el.decorate(); | 218 advancedFonts.fontSettings = null; |
207 return el; | 219 |
220 /** | |
221 * Map from |fontSizeKey| to UI contols and data for its font size setting. | |
222 */ | |
223 advancedFonts.fontSizeSettings = null; | |
224 | |
225 /** | |
226 * Returns a bound function. | |
227 * @param {function(x, y, z)} callback The function to bind. | |
228 * @param {object} arg2 The argument to pass to the second parameter of | |
229 * |callback|. | |
230 * @param {object} arg3 The argument to pass to the third parameter of | |
231 * |callback|. | |
232 * @return {function(x, y, z)} A function of form function(x, y, z) which calls | |
233 * callback(x, y, z) where |y| and |z| are fixed to |arg2| and |arg3|, | |
234 * respectively. | |
235 */ | |
236 advancedFonts.getBoundCallback = function(callback, arg2, arg3) { | |
237 return function(arg1, ignoredArg2, ignoredArg3) { | |
238 callback(arg1, arg2, arg3) | |
208 }; | 239 }; |
209 | 240 }; |
210 ScriptListItem.prototype = { | 241 |
211 __proto__: ListItem.prototype, | 242 /** |
212 | 243 * Gets the font size used for |fontSizeKey|, including pending changes. Calls |
213 decorate: function() { | 244 * |callback| with the result. |
214 this.textContent = this.info_.scriptName; | 245 * |
215 if (this.info_.scriptCode == 'Zyyy') { | 246 * @param {string} fontSizeKey The font size setting key. See |
216 this.style.marginBottom = '1em'; | 247 * PendingChanges.getFontSize(). |
248 * @param {function(number, boolean)} callback The callback of form | |
249 * function(size, controllable). |size| is the effective setting, | |
250 * |controllable| is whether the setting can be set. | |
251 */ | |
252 advancedFonts.getEffectiveFontSize = function(fontSizeKey, callback) { | |
253 advancedFonts.fontSizeSettings[fontSizeKey].getter({}, function(details) { | |
254 var controllable = advancedFonts.isControllableLevel( | |
255 details.levelOfControl); | |
256 var size = details.pixelSize; | |
257 var pendingFontSize = advancedFonts.pendingChanges.getFontSize(fontSizeKey); | |
258 // If the setting is not controllable, we can have no pending change. | |
259 if (!controllable) { | |
260 if (pendingFontSize != null) { | |
261 advancedFonts.pendingChanges.setFontSize(fontSizeKey, null); | |
262 advancedFonts.refresh(); | |
263 pendingFontSize = null; | |
217 } | 264 } |
218 } | 265 } |
219 }; | 266 |
220 | 267 // If we have a pending change, it overrides the current setting. |
221 var ScriptList = cr.ui.define('list'); | 268 if (pendingFontSize != null) |
222 ScriptList.prototype = { | 269 size = pendingFontSize; |
223 __proto__: List.prototype, | 270 callback(size, controllable); |
224 | 271 }); |
225 decorate: function() { | 272 }; |
226 List.prototype.decorate.call(this); | 273 |
227 var sm = new ListSingleSelectionModel(); | 274 /** |
228 this.selectionModel = sm; | 275 * Gets the font used for |script| and |genericFamily|, including pending |
229 this.autoExpands = true; | 276 * changes. Calls |callback| with the result. |
230 this.dataModel = new cr.ui.ArrayDataModel(scripts); | 277 * |
231 this.style.height = '75vh'; | 278 * @param {string} script The script code. |
232 }, | 279 * @param {string} genericFamily The generic family. |
233 | 280 * @param {function(string, string, boolean)} callback The callback of form |
234 createItem: function(info) { | 281 * function(effectiveFont, font, controllable). |effectiveFont| is the font |
235 return new ScriptListItem(info); | 282 * used taking fallback into consideration, |font| is the actual setting |
236 } | 283 * (pending or not), |controllable| is whether the setting can be set. |
237 }; | 284 */ |
238 | 285 advancedFonts.getEffectiveFont = function(script, genericFamily, callback) { |
239 return { | 286 var pendingChanges = advancedFonts.pendingChanges; |
240 ScriptList: ScriptList, | 287 var details = { script: script, genericFamily: genericFamily }; |
241 ScriptListItem: ScriptListItem | 288 var pendingFont = |
242 }; | 289 pendingChanges.getFont(details.script, details.genericFamily); |
243 }); | 290 chrome.fontSettings.getFont(details, function(result) { |
244 | 291 var setting = {}; |
245 function getSelectedScript() { | 292 setting.font = result.fontId; |
246 var scriptList = document.getElementById('scriptList'); | 293 setting.controllable = |
247 return scriptList.selectedItem.scriptCode; | 294 advancedFonts.isControllableLevel(result.levelOfControl); |
248 } | 295 // If the setting is not controllable, we can have no pending change. |
249 | 296 if (!setting.controllable) { |
250 function getSelectedFont(fontList) { | 297 pendingFont = null; |
298 if (pendingChanges.getFont[script]) | |
299 pendingChanges.setFont([script][genericFamily], null); | |
300 refresh(); | |
301 } | |
302 | |
303 // If we have a pending change, it overrides the current setting. | |
304 if (pendingFont != null) | |
305 setting.font = pendingFont; | |
306 | |
307 // If we have a font, we're done. | |
308 if (setting.font) { | |
309 callback(setting.font, setting.font, setting.controllable); | |
310 return; | |
311 } | |
312 | |
313 // If we're still here, we have to fallback to common script, unless this | |
314 // already is common script. | |
315 if (script == advancedFonts.COMMON_SCRIPT) { | |
316 callback('', '', setting.controllable); | |
317 return; | |
318 } | |
319 advancedFonts.getEffectiveFont( | |
320 advancedFonts.COMMON_SCRIPT, | |
321 genericFamily, | |
322 advancedFonts.getBoundCallback( | |
323 callback, setting.font, setting.controllable)); | |
324 }); | |
325 }; | |
326 | |
327 /** | |
328 * Refreshes the UI controls related to a font setting. | |
329 * | |
330 * @param {{fontList: HTMLSelectElement, samples: Array.<HTMLElement>}} | |
331 * fontSetting The setting object (see advancedFonts.fontSettings). | |
332 * @param {string} effectiveFont The font used, including fallback to Common | |
333 * script. | |
334 * @param {string} font The value of the font setting. | |
335 * @param {boolean} controllable Whether the font setting can be controlled | |
336 * by this extension. | |
337 */ | |
338 advancedFonts.refreshFont = function( | |
339 fontSetting, effectiveFont, font, controllable) { | |
hirono
2013/08/30 11:01:35
Can we just use Function.bind instead of getBoundC
falken
2013/08/30 13:22:29
Done.
| |
340 for (var i = 0; i < fontSetting.samples.length; ++i) | |
341 fontSetting.samples[i].style.fontFamily = effectiveFont; | |
342 advancedFonts.setSelectedFont(fontSetting.fontList, font); | |
343 fontSetting.fontList.disabled = !controllable; | |
344 }; | |
345 | |
346 /** | |
347 * Refreshes the UI controls related to a font size setting. | |
348 * | |
349 * @param {{label: HTMLElement, slider: Slider, samples: Array.<HTMLElement>}} | |
350 * fontSizeSetting The setting object (see advancedFonts.fontSizeSettings). | |
351 * @param size The value of the font size setting. | |
352 * @param controllable Whether the setting can be controlled by this extension. | |
353 */ | |
354 advancedFonts.refreshFontSize = function(fontSizeSetting, size, controllable) { | |
355 fontSizeSetting.label.textContent = 'Size: ' + size + 'px'; | |
356 advancedFonts.setFontSizeSlider(fontSizeSetting.slider, size, controllable); | |
357 for (var i = 0; i < fontSizeSetting.samples.length; ++i) | |
358 fontSizeSetting.samples[i].style.fontSize = size + 'px'; | |
359 }; | |
360 | |
361 /** | |
362 * Refreshes all UI controls to reflect the current settings, including pending | |
363 * changes. | |
364 */ | |
365 advancedFonts.refresh = function() { | |
366 var script = advancedFonts.getSelectedScript(); | |
367 var sample; | |
368 if (advancedFonts.SAMPLE_TEXTS[script]) | |
369 sample = advancedFonts.SAMPLE_TEXTS[script]; | |
370 else | |
371 sample = advancedFonts.SAMPLE_TEXTS[advancedFonts.COMMON_SCRIPT]; | |
372 var sampleTexts = document.querySelectorAll('.sample-text-span'); | |
373 for (var i = 0; i < sampleTexts.length; i++) | |
374 sampleTexts[i].textContent = sample; | |
375 | |
376 var setting; | |
377 var callback; | |
378 for (var genericFamily in advancedFonts.fontSettings) { | |
379 setting = advancedFonts.fontSettings[genericFamily]; | |
380 callback = advancedFonts.refreshFont.bind(null, setting); | |
381 advancedFonts.getEffectiveFont(script, genericFamily, callback); | |
382 } | |
383 | |
384 for (var fontSizeKey in advancedFonts.fontSizeSettings) { | |
385 setting = advancedFonts.fontSizeSettings[fontSizeKey]; | |
386 callback = advancedFonts.refreshFontSize.bind(null, setting); | |
387 advancedFonts.getEffectiveFontSize(fontSizeKey, callback); | |
388 } | |
389 | |
390 $('apply-settings').disabled = advancedFonts.pendingChanges.isEmpty(); | |
391 }; | |
392 | |
393 /** | |
394 * @return {string} The currently selected script code. | |
395 */ | |
396 advancedFonts.getSelectedScript = function() { | |
397 var scriptList = $('scriptList'); | |
398 return scriptList.options[scriptList.selectedIndex].value; | |
399 }; | |
400 | |
401 /** | |
402 * @param {HTMLSelectElement} fontList The <select> containing a list of fonts. | |
403 * @return {string} The currently selected value of |fontList|. | |
404 */ | |
405 advancedFonts.getSelectedFont = function(fontList) { | |
251 return fontList.options[fontList.selectedIndex].value; | 406 return fontList.options[fontList.selectedIndex].value; |
252 } | 407 }; |
253 | 408 |
254 // Populates the font lists with the list of system fonts from |fonts|. | 409 /** |
255 function populateLists(fonts) { | 410 * Populates the font lists. |
256 for (var i = 0; i < fontPickers.length; i++) { | 411 * @param {Array.<{fontId: string, displayName: string>} fonts The list of |
257 var list = document.getElementById(fontPickers[i].fontList); | 412 * fonts on the system. |
258 | 413 */ |
259 // Add special item to indicate fallback to the non-per-script | 414 advancedFonts.populateFontLists = function(fonts) { |
415 for (var genericFamily in advancedFonts.fontSettings) { | |
416 var list = advancedFonts.fontSettings[genericFamily].fontList; | |
417 | |
418 // Add a special item to indicate fallback to the non-per-script | |
260 // font setting. The Font Settings API uses the empty string to indicate | 419 // font setting. The Font Settings API uses the empty string to indicate |
261 // fallback. | 420 // fallback. |
262 var defaultItem = document.createElement('option'); | 421 var defaultItem = document.createElement('option'); |
263 defaultItem.value = ''; | 422 defaultItem.value = ''; |
264 defaultItem.text = '(Use default)'; | 423 defaultItem.text = '(Use default)'; |
265 list.add(defaultItem); | 424 list.add(defaultItem); |
266 | 425 |
267 for (var j = 0; j < fonts.length; j++) { | 426 for (var i = 0; i < fonts.length; ++i) { |
268 var item = document.createElement('option'); | 427 var item = document.createElement('option'); |
269 item.value = fonts[j].fontId; | 428 item.value = fonts[i].fontId; |
270 item.text = fonts[j].displayName; | 429 item.text = fonts[i].displayName; |
271 list.add(item); | 430 list.add(item); |
272 } | 431 } |
273 } | 432 } |
433 advancedFonts.refresh(); | |
434 }; | |
274 | 435 |
275 updateFontListsForScript(); | 436 /** |
276 } | 437 * @param {HTMLSelectElement} fontList The <select> containing a list of fonts. |
438 * @param {string} genericFamily The generic family for the font setting. | |
439 * @return {function()} A function to be called when the user changes the | |
440 * selected font in |fontList|. The function updates the pending font | |
441 * change. | |
442 */ | |
443 advancedFonts.getFontChangeHandler = function(fontList, genericFamily) { | |
hirono
2013/08/30 11:01:35
Can we also use Function.bind for the function?
falken
2013/08/30 13:22:29
Done.
| |
444 return function() { | |
445 var script = advancedFonts.getSelectedScript(); | |
446 var font = advancedFonts.getSelectedFont(fontList); | |
277 | 447 |
278 // Returns a function that updates the font setting for |genericFamily| | 448 advancedFonts.pendingChanges.setFont(script, genericFamily, font); |
279 // to match the selected value in |fontList|. It can be used as an event | 449 advancedFonts.refresh(); |
280 // handler for selection changes in |fontList|. | 450 }; |
281 function getFontChangeHandler(fontList, genericFamily) { | 451 }; |
282 return function() { | |
283 var script = getSelectedScript(); | |
284 var font = getSelectedFont(fontList); | |
285 | 452 |
286 var details = {}; | 453 /** |
287 details.genericFamily = genericFamily; | 454 * Sets the selected value of |fontList| to |fontId|. |
288 details.fontId = font; | 455 * @param {HTMLSelectElement} fontList The <select> containing a list of fonts. |
289 details.script = script; | 456 * @param {string} fontId The font to set |fontList|'s selection to. |
290 | 457 */ |
291 chrome.fontSettings.setFont(details); | 458 advancedFonts.setSelectedFont = function(fontList, fontId) { |
292 }; | 459 var script = advancedFonts.getSelectedScript(); |
293 } | |
294 | |
295 // Sets the selected value of |fontList| to |fontId|. | |
296 function setSelectedFont(fontList, fontId) { | |
297 var script = getSelectedScript(); | |
298 var i; | 460 var i; |
299 for (i = 0; i < fontList.length; i++) { | 461 for (i = 0; i < fontList.length; i++) { |
300 if (fontId == fontList.options[i].value) { | 462 if (fontId == fontList.options[i].value) { |
301 fontList.selectedIndex = i; | 463 fontList.selectedIndex = i; |
302 break; | 464 break; |
303 } | 465 } |
304 } | 466 } |
305 if (i == fontList.length) { | 467 if (i == fontList.length) { |
306 console.warn("font '" + fontId + "' for " + fontList.id + ' for ' + | 468 console.warn("font '" + fontId + "' for " + fontList.id + ' for ' + |
307 script + ' is not on the system'); | 469 script + ' is not on the system'); |
308 } | 470 } |
309 } | 471 }; |
310 | 472 |
311 // Returns a callback function that sets the selected value of |list| to the | 473 /** |
312 // font returned from |chrome.fontSettings.getFont|. | 474 * Return a function that handles changes to the font size slider. |
313 function getFontHandler(list) { | 475 * @param {string} fontSizeKey The key for font size setting whose slider the |
314 return function(details) { | 476 * function should handle changed for. See PendingChanges.getFont. |
315 setSelectedFont(list, details.fontId); | 477 * @return {function(number)} A function to be called when the user changes the |
316 list.disabled = !isControllableLevel(details.levelOfControl); | 478 * font size slider for |fontSizeKey|. The function sets the pending font |
317 }; | 479 * size change. |
318 } | 480 */ |
319 | 481 advancedFonts.getFontSizeChangedFunc = function(fontSizeKey) { |
hirono
2013/08/30 11:01:35
ditto.
falken
2013/08/30 13:22:29
Done.
| |
320 // Called when the script list selection changes. Sets the selected value of | 482 return function(value) { |
321 // each font list to the current font setting, and updates the samples' lang | 483 var pixelSize = parseInt(value); |
322 // so that they are shown in the current font setting. | |
323 function updateFontListsForScript() { | |
324 var script = getSelectedScript(); | |
325 | |
326 for (var i = 0; i < fontPickers.length; i++) { | |
327 var list = document.getElementById(fontPickers[i].fontList); | |
328 var family = fontPickers[i].name; | |
329 | |
330 var details = {}; | |
331 details.genericFamily = family; | |
332 details.script = script; | |
333 chrome.fontSettings.getFont(details, getFontHandler(list)); | |
334 } | |
335 | |
336 if (typeof(scriptSpecificSampleText[script]) != 'undefined') | |
337 sample = scriptSpecificSampleText[script]; | |
338 else | |
339 sample = defaultSampleText; | |
340 for (var i = 0; i < sampleTextDivIds.length; i++) { | |
341 var sampleTextDiv = document.getElementById(sampleTextDivIds[i]); | |
342 // For font selection it's the script code that matters, not language, so | |
343 // just use en for lang. | |
344 sampleTextDiv.lang = 'en-' + script; | |
345 sampleTextDiv.innerText = sample; | |
346 } | |
347 } | |
348 | |
349 // Returns a function to be called when the user changes the font size | |
350 // input element |elem|. The function calls the Font Settings Extension API | |
351 // function |setter| to commit the change. | |
352 function getFontSizeChangedFunc(elem, setter) { | |
353 return function() { | |
354 var pixelSize = parseInt(elem.value); | |
355 if (!isNaN(pixelSize)) { | 484 if (!isNaN(pixelSize)) { |
356 setter({ pixelSize: pixelSize }); | 485 advancedFonts.pendingChanges.setFontSize(fontSizeKey, pixelSize); |
486 advancedFonts.refresh(); | |
357 } | 487 } |
358 } | 488 } |
359 } | 489 }; |
360 | 490 |
361 function isControllableLevel(levelOfControl) { | 491 /** |
492 * @param {string} levelOfControl The level of control string for a setting, | |
493 * as returned by the Font Settings Extension API. | |
494 * @return {boolean} True if |levelOfControl| signifies that the extension can | |
495 * control the setting; otherwise, returns false. | |
496 */ | |
497 advancedFonts.isControllableLevel = function(levelOfControl) { | |
362 return levelOfControl == 'controllable_by_this_extension' || | 498 return levelOfControl == 'controllable_by_this_extension' || |
363 levelOfControl == 'controlled_by_this_extension'; | 499 levelOfControl == 'controlled_by_this_extension'; |
364 } | 500 }; |
365 | 501 |
366 // Returns a function to be used as a listener for font size setting changed | 502 /** |
367 // events from the Font Settings Extension API. The function updates the input | 503 * Returns a function to be used as a listener for font size setting changed |
368 // element |elem| and the elements in |sampleTexts| to reflect the change. | 504 * events from the Font Settings Extension API. |
369 function getFontSizeChangedOnBrowserFunc(elem, sampleTexts) { | 505 * @param {Slider} slider The slider for the font size setting. |
506 * @param {HTMLElement} label The element that displays the slider's value. | |
507 * @param {Array.<HTMLElement>} sampleTexts Elements containing sample text | |
508 * which are rendered using this font size setting. | |
509 * @return {function(object)} A function that can listen for font size change | |
510 * events from the browser and update |slider|, |label|, and |sampleTexts| | |
511 * to reflect the change. | |
512 */ | |
513 advancedFonts.getFontSizeChangedOnBrowserFunc = | |
514 function(slider, label, sampleTexts) { | |
hirono
2013/08/30 11:01:35
ditto.
falken
2013/08/30 13:22:29
I just got rid of this. It was supposed to smartly
| |
370 return function(details) { | 515 return function(details) { |
371 var size = details.pixelSize.toString(); | 516 var size = details.pixelSize.toString(); |
372 elem.value = size; | 517 var controllable = |
373 elem.disabled = !isControllableLevel(details.levelOfControl); | 518 advancedFonts.isControllableLevel(details.levelOfControl); |
519 advancedFonts.setFontSizeSlider(slider, size, controllable); | |
520 label.disabled = !controllable; | |
374 for (var i = 0; i < sampleTexts.length; i++) | 521 for (var i = 0; i < sampleTexts.length; i++) |
375 document.getElementById(sampleTexts[i]).style.fontSize = size + 'px'; | 522 sampleTexts[i].style.fontSize = size + 'px'; |
523 label.innerText = 'Size: ' + size + 'px'; | |
376 } | 524 } |
377 } | 525 }; |
378 | 526 |
379 // Maps the HTML <input> element with |id| to the extension API accessor | 527 /* |
380 // functions |getter| and |setter| for a setting and onChange event |apiEvent| | 528 * Updates the specified font size slider's value and enabled property. |
381 // for the setting. Also, maps the element ids in |sampleTexts| to this setting. | 529 * @param {Slider} slider The slider for a font size setting. |
382 function initFontSizePref(id, sampleTexts, getter, setter, apiEvent) { | 530 * @param {number} size The value to set the slider to. |
383 var elem = document.getElementById(id); | 531 * @param {boolean} enabled Whether to enable or disable the slider. |
384 getter({}, function(details) { | 532 */ |
533 advancedFonts.setFontSizeSlider = function(slider, size, enabled) { | |
534 if (slider.getValue() != size) | |
535 slider.setValue(size); | |
536 var inputElement = slider.getInput(); | |
537 if (enabled) { | |
538 inputElement.parentNode.classList.remove('disabled'); | |
539 inputElement.disabled = false; | |
540 } else { | |
541 inputElement.parentNode.classList.add('disabled'); | |
542 inputElement.disabled = true; | |
543 } | |
544 }; | |
545 | |
546 /** | |
547 * Initializes the UI control elements related to the font size setting | |
548 * |fontSizeKey| and registers listeners for the user adjusting its slider and | |
549 * the setting changing on the browser-side. | |
550 * @param {string} fontSizeKey The key for font size setting. See | |
551 * PendingChanges.getFont(). | |
552 */ | |
553 advancedFonts.initFontSizeSetting = function(fontSizeKey) { | |
554 var fontSizeSettings = advancedFonts.fontSizeSettings; | |
555 var setting = fontSizeSettings[fontSizeKey]; | |
556 var label = setting.label; | |
557 var samples = setting.samples; | |
558 | |
559 setting.slider = new Slider( | |
560 setting.sliderContainer, | |
561 0, | |
562 setting.minValue, | |
563 setting.maxValue, | |
564 advancedFonts.getFontSizeChangedFunc(fontSizeKey) | |
565 ); | |
566 | |
567 var slider = setting.slider; | |
568 setting.getter({}, function(details) { | |
385 var size = details.pixelSize.toString(); | 569 var size = details.pixelSize.toString(); |
386 elem.value = size; | 570 var controllable = advancedFonts.isControllableLevel( |
387 elem.disabled = !isControllableLevel(details.levelOfControl); | 571 details.levelOfControl); |
388 for (var i = 0; i < sampleTexts.length; i++) | 572 advancedFonts.setFontSizeSlider(slider, size, controllable); |
389 document.getElementById(sampleTexts[i]).style.fontSize = size + 'px'; | 573 for (var i = 0; i < samples.length; i++) |
574 samples[i].style.fontSize = size + 'px'; | |
390 }); | 575 }); |
391 elem.addEventListener('change', getFontSizeChangedFunc(elem, setter)); | 576 var apiEvent = fontSizeSettings[fontSizeKey].onChanged; |
392 apiEvent.addListener(getFontSizeChangedOnBrowserFunc(elem, sampleTexts)); | 577 apiEvent.addListener( |
393 } | 578 advancedFonts.getFontSizeChangedOnBrowserFunc(slider, label, samples)); |
579 }; | |
394 | 580 |
395 function clearSettingsForScript(script) { | 581 /** |
396 for (var i = 0; i < families.length; i++) { | 582 * Clears the font settings for the specified script. |
583 * @param {string} script The script code. | |
584 */ | |
585 advancedFonts.clearSettingsForScript = function(script) { | |
586 advancedFonts.pendingChanges.clearOneScript(script); | |
587 for (var i = 0; i < advancedFonts.FAMILIES.length; i++) { | |
397 chrome.fontSettings.clearFont({ | 588 chrome.fontSettings.clearFont({ |
398 script: script, | 589 script: script, |
399 genericFamily: families[i] | 590 genericFamily: advancedFonts.FAMILIES[i] |
400 }); | 591 }); |
401 } | 592 } |
402 } | 593 }; |
403 | 594 |
404 function clearAllSettings() { | 595 /** |
405 for (var i = 0; i < scripts.length; i++) | 596 * Clears all font and font size settings. |
406 clearSettingsForScript(scripts[i].scriptCode); | 597 */ |
407 | 598 advancedFonts.clearAllSettings = function() { |
599 advancedFonts.pendingChanges.clear(); | |
600 for (var i = 0; i < advancedFonts.scripts.length; i++) | |
601 advancedFonts.clearSettingsForScript(advancedFonts.scripts[i].scriptCode); | |
408 chrome.fontSettings.clearDefaultFixedFontSize(); | 602 chrome.fontSettings.clearDefaultFixedFontSize(); |
409 chrome.fontSettings.clearDefaultFontSize(); | 603 chrome.fontSettings.clearDefaultFontSize(); |
410 chrome.fontSettings.clearMinimumFontSize(); | 604 chrome.fontSettings.clearMinimumFontSize(); |
411 } | 605 }; |
412 | 606 |
413 function closeOverlay() { | 607 /** |
608 * Closes the overlay. | |
609 */ | |
610 advancedFonts.closeOverlay = function() { | |
414 $('overlay-container').hidden = true; | 611 $('overlay-container').hidden = true; |
415 } | 612 }; |
416 | 613 |
417 function initResetButtons() { | 614 /** |
615 * Initializes apply and reset buttons. | |
616 */ | |
617 advancedFonts.initApplyAndResetButtons = function() { | |
618 var applyButton = $('apply-settings'); | |
619 applyButton.addEventListener('click', function() { | |
620 advancedFonts.pendingChanges.apply(); | |
621 advancedFonts.refresh(); | |
622 }); | |
623 | |
418 var overlay = $('overlay-container'); | 624 var overlay = $('overlay-container'); |
419 cr.ui.overlay.globalInitialization(); | 625 cr.ui.overlay.globalInitialization(); |
420 cr.ui.overlay.setupOverlay(overlay); | 626 cr.ui.overlay.setupOverlay(overlay); |
421 overlay.addEventListener('cancelOverlay', closeOverlay); | 627 overlay.addEventListener('cancelOverlay', advancedFonts.closeOverlay); |
422 | 628 |
423 $('reset-this-script-button').onclick = function(event) { | 629 $('reset-this-script-button').onclick = function(event) { |
424 var scriptName = $('scriptList').selectedItem.scriptName; | 630 var scriptList = $('scriptList'); |
631 var scriptName = scriptList.options[scriptList.selectedIndex].text; | |
425 $('reset-this-script-overlay-dialog-content').innerText = | 632 $('reset-this-script-overlay-dialog-content').innerText = |
426 'Are you sure you want to reset settings for ' + scriptName + | 633 'Are you sure you want to reset settings for ' + scriptName + |
427 ' script?'; | 634 ' script?'; |
428 | 635 |
429 $('overlay-container').hidden = false; | 636 $('overlay-container').hidden = false; |
430 $('reset-this-script-overlay-dialog').hidden = false; | 637 $('reset-this-script-overlay-dialog').hidden = false; |
431 $('reset-all-scripts-overlay-dialog').hidden = true; | 638 $('reset-all-scripts-overlay-dialog').hidden = true; |
432 } | 639 }; |
433 $('reset-this-script-ok').onclick = function(event) { | 640 $('reset-this-script-ok').onclick = function(event) { |
434 clearSettingsForScript(getSelectedScript()); | 641 advancedFonts.clearSettingsForScript(advancedFonts.getSelectedScript()); |
435 closeOverlay(); | 642 advancedFonts.closeOverlay(); |
643 advancedFonts.refresh(); | |
436 }; | 644 }; |
437 $('reset-this-script-cancel').onclick = closeOverlay; | 645 $('reset-this-script-cancel').onclick = advancedFonts.closeOverlay; |
438 | 646 |
439 $('reset-all-button').onclick = function(event) { | 647 $('reset-all-button').onclick = function(event) { |
440 $('overlay-container').hidden = false; | 648 $('overlay-container').hidden = false; |
441 $('reset-all-scripts-overlay-dialog').hidden = false; | 649 $('reset-all-scripts-overlay-dialog').hidden = false; |
442 $('reset-this-script-overlay-dialog').hidden = true; | 650 $('reset-this-script-overlay-dialog').hidden = true; |
651 }; | |
652 $('reset-all-ok').onclick = function(event) { | |
653 advancedFonts.clearAllSettings(); | |
654 advancedFonts.closeOverlay(); | |
655 advancedFonts.refresh(); | |
656 }; | |
657 $('reset-all-cancel').onclick = advancedFonts.closeOverlay; | |
658 }; | |
659 | |
660 /** | |
661 * Best guess for system fonts, taken from the IDS_WEB_FONT_FAMILY strings in | |
662 * Chrome. | |
663 * TODO: The font should be localized like Chrome does. | |
664 * @const | |
665 */ | |
666 advancedFonts.systemFonts = { | |
667 cros: 'Noto Sans UI, sans-serif', | |
668 linux: 'Ubuntu, sans-serif', | |
669 mac: 'Lucida Grande, sans-serif', | |
670 win: 'Segoe UI, Tahoma, sans-serif', | |
671 unknown: 'sans-serif' | |
672 }; | |
673 | |
674 /** | |
675 * @return {string} The platform this extension is running on. | |
676 */ | |
677 advancedFonts.getPlatform = function() { | |
678 var ua = window.navigator.appVersion; | |
679 if (ua.indexOf('Win') != -1) return 'win'; | |
680 if (ua.indexOf('Mac') != -1) return 'mac'; | |
681 if (ua.indexOf('Linux') != -1) return 'linux'; | |
682 if (ua.indexOf('CrOS') != -1) return 'cros'; | |
683 return 'unknown'; | |
684 }; | |
685 | |
686 /** | |
687 * Chrome settings tries to use the system font. So does this extension. | |
688 */ | |
689 advancedFonts.useSystemFont = function() { | |
690 document.body.style.fontFamily = | |
691 advancedFonts.systemFonts[advancedFonts.getPlatform()]; | |
692 }; | |
693 | |
694 /** | |
695 * Sorts the list of script codes by scriptName. Someday this extension will | |
696 * have localized script names, so the order will depend on locale. | |
697 */ | |
698 advancedFonts.sortScripts = function() { | |
699 var i; | |
700 var scripts = advancedFonts.scripts; | |
701 for (i = 0; i < scripts; ++i) { | |
702 if (scripts[i].scriptCode == advancedFonts.COMMON_SCRIPT) | |
703 break; | |
443 } | 704 } |
444 $('reset-all-ok').onclick = function(event) { | 705 var defaultScript = scripts.splice(i, 1)[0]; |
445 clearAllSettings(); | 706 |
446 closeOverlay(); | 707 scripts.sort(function(a, b) { |
708 if (a.scriptName > b.scriptName) | |
709 return 1; | |
710 if (a.scriptName < b.scriptName) | |
711 return -1; | |
712 return 0; | |
713 }); | |
714 | |
715 scripts.unshift(defaultScript); | |
716 }; | |
717 | |
718 /** | |
719 * Initializes UI controls for font settings. | |
720 */ | |
721 advancedFonts.initFontControls = function() { | |
722 advancedFonts.fontSettings = { | |
723 standard: { | |
724 fontList: $('standardFontList'), | |
725 samples: [$('standardFontSample'), $('minFontSample')] | |
726 }, | |
727 serif: { | |
728 fontList: $('serifFontList'), | |
729 samples: [$('serifFontSample')] | |
730 }, | |
731 sansserif: { | |
732 fontList: $('sansSerifFontList'), | |
733 samples: [$('sansSerifFontSample')] | |
734 }, | |
735 fixed: { | |
736 fontList: $('fixedFontList'), | |
737 samples: [$('fixedFontSample')] | |
738 } | |
739 }; | |
740 | |
741 for (var genericFamily in advancedFonts.fontSettings) { | |
742 var list = advancedFonts.fontSettings[genericFamily].fontList; | |
743 list.addEventListener('change', advancedFonts.getFontChangeHandler( | |
744 list, genericFamily)); | |
447 } | 745 } |
448 $('reset-all-cancel').onclick = closeOverlay; | 746 chrome.fontSettings.onFontChanged.addListener(advancedFonts.refresh); |
449 } | 747 chrome.fontSettings.getFontList(advancedFonts.populateFontLists); |
748 }; | |
450 | 749 |
451 function init() { | 750 /** |
452 var scriptList = document.getElementById('scriptList'); | 751 * Initializes UI controls for font size settings. |
453 fontSettings.ui.ScriptList.decorate(scriptList); | 752 */ |
454 scriptList.selectionModel.selectedIndex = 0; | 753 advancedFonts.initFontSizeControls = function() { |
455 scriptList.selectionModel.addEventListener('change', | 754 advancedFonts.fontSizeSettings = { |
456 updateFontListsForScript); | 755 defaultFontSize: { |
756 sliderContainer: $('defaultFontSizeSliderContainer'), | |
757 minValue: 6, | |
758 maxValue: 50, | |
759 samples: [ | |
760 $('standardFontSample'), $('serifFontSample'), $('sansSerifFontSample') | |
761 ], | |
762 label: $('defaultFontSizeLabel'), | |
763 getter: chrome.fontSettings.getDefaultFontSize, | |
764 onChanged: chrome.fontSettings.onDefaultFontSizeChanged | |
765 }, | |
766 defaultFixedFontSize: { | |
767 sliderContainer: $('defaultFixedFontSizeSliderContainer'), | |
768 minValue: 6, | |
769 maxValue: 50, | |
770 samples: [$('fixedFontSample')], | |
771 label: $('fixedFontSizeLabel'), | |
772 getter: chrome.fontSettings.getDefaultFixedFontSize, | |
773 onChanged: chrome.fontSettings.onDefaultFixedFontSizeChanged | |
774 }, | |
775 minFontSize: { | |
776 sliderContainer: $('minFontSizeSliderContainer'), | |
777 minValue: 6, | |
778 maxValue: 24, | |
779 samples: [$('minFontSample')], | |
780 label: $('minFontSizeLabel'), | |
781 getter: chrome.fontSettings.getMinimumFontSize, | |
782 onChanged: chrome.fontSettings.onMinimumFontSizeChanged | |
783 } | |
784 }; | |
457 | 785 |
458 // Populate the font lists. | 786 for (var fontSizeKey in advancedFonts.fontSizeSettings) |
459 chrome.fontSettings.getFontList(populateLists); | 787 advancedFonts.initFontSizeSetting(fontSizeKey); |
788 }; | |
460 | 789 |
461 // Add change handlers to the font lists. | 790 /** |
462 for (var i = 0; i < fontPickers.length; i++) { | 791 * Initializes the list of scripts. |
463 var list = document.getElementById(fontPickers[i].fontList); | 792 */ |
464 var handler = getFontChangeHandler(list, fontPickers[i].name); | 793 advancedFonts.initScriptList = function() { |
465 list.addEventListener('change', handler); | 794 var scriptList = $('scriptList'); |
795 advancedFonts.sortScripts(); | |
796 var scripts = advancedFonts.scripts; | |
797 for (var i = 0; i < scripts.length; i++) { | |
798 var script = document.createElement('option'); | |
799 script.value = scripts[i].scriptCode; | |
800 script.text = scripts[i].scriptName; | |
801 scriptList.add(script); | |
466 } | 802 } |
803 scriptList.selectedIndex = 0; | |
804 scriptList.addEventListener('change', advancedFonts.refresh); | |
805 }; | |
467 | 806 |
468 chrome.fontSettings.onFontChanged.addListener( | 807 /** |
469 updateFontListsForScript); | 808 * Initializes the extension. |
809 */ | |
810 advancedFonts.init = function() { | |
811 advancedFonts.useSystemFont(); | |
470 | 812 |
471 initFontSizePref( | 813 advancedFonts.initFontControls(); |
472 'defaultFontSizeRocker', | 814 advancedFonts.initFontSizeControls(); |
473 ['standardFontSample', 'serifFontSample', 'sansSerifFontSample'], | 815 advancedFonts.initScriptList(); |
474 chrome.fontSettings.getDefaultFontSize, | |
475 chrome.fontSettings.setDefaultFontSize, | |
476 chrome.fontSettings.onDefaultFontSizeChanged); | |
477 initFontSizePref( | |
478 'defaultFontSizeRange', | |
479 ['standardFontSample', 'serifFontSample', 'sansSerifFontSample'], | |
480 chrome.fontSettings.getDefaultFontSize, | |
481 chrome.fontSettings.setDefaultFontSize, | |
482 chrome.fontSettings.onDefaultFontSizeChanged); | |
483 initFontSizePref( | |
484 'defaultFixedFontSizeRocker', | |
485 ['fixedFontSample'], | |
486 chrome.fontSettings.getDefaultFixedFontSize, | |
487 chrome.fontSettings.setDefaultFixedFontSize, | |
488 chrome.fontSettings.onDefaultFixedFontSizeChanged); | |
489 initFontSizePref( | |
490 'defaultFixedFontSizeRange', | |
491 ['fixedFontSample'], | |
492 chrome.fontSettings.getDefaultFixedFontSize, | |
493 chrome.fontSettings.setDefaultFixedFontSize, | |
494 chrome.fontSettings.onDefaultFixedFontSizeChanged); | |
495 initFontSizePref( | |
496 'minFontSizeRocker', | |
497 ['minFontSample'], | |
498 chrome.fontSettings.getMinimumFontSize, | |
499 chrome.fontSettings.setMinimumFontSize, | |
500 chrome.fontSettings.onMinimumFontSizeChanged); | |
501 initFontSizePref( | |
502 'minFontSizeRange', | |
503 ['minFontSample'], | |
504 chrome.fontSettings.getMinimumFontSize, | |
505 chrome.fontSettings.setMinimumFontSize, | |
506 chrome.fontSettings.onMinimumFontSizeChanged); | |
507 | 816 |
508 initResetButtons(); | 817 advancedFonts.initApplyAndResetButtons(); |
509 } | 818 }; |
510 | 819 |
511 document.addEventListener('DOMContentLoaded', init); | 820 document.addEventListener('DOMContentLoaded', advancedFonts.init); |
OLD | NEW |