Chromium Code Reviews| 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 |
|
hirono
2013/08/28 08:47:30
Could you add a line 'use strict'; ?
It lets the J
falken
2013/08/29 09:35:44
Done.
| |
| 5 var COMMON_SCRIPT = 'Zyyy'; | |
| 6 | |
| 5 // The scripts supported by the Font Settings Extension API. | 7 // The scripts supported by the Font Settings Extension API. |
| 6 var scripts = [ | 8 var scripts = [ |
| 7 { scriptCode: 'Zyyy', scriptName: 'Default'}, | 9 { scriptCode: COMMON_SCRIPT, scriptName: 'Default'}, |
| 8 { scriptCode: 'Afak', scriptName: 'Afaka'}, | 10 { scriptCode: 'Afak', scriptName: 'Afaka'}, |
| 9 { scriptCode: 'Arab', scriptName: 'Arabic'}, | 11 { scriptCode: 'Arab', scriptName: 'Arabic'}, |
| 10 { scriptCode: 'Armi', scriptName: 'Imperial Aramaic'}, | 12 { scriptCode: 'Armi', scriptName: 'Imperial Aramaic'}, |
| 11 { scriptCode: 'Armn', scriptName: 'Armenian'}, | 13 { scriptCode: 'Armn', scriptName: 'Armenian'}, |
| 12 { scriptCode: 'Avst', scriptName: 'Avestan'}, | 14 { scriptCode: 'Avst', scriptName: 'Avestan'}, |
| 13 { scriptCode: 'Bali', scriptName: 'Balinese'}, | 15 { scriptCode: 'Bali', scriptName: 'Balinese'}, |
| 14 { scriptCode: 'Bamu', scriptName: 'Bamum'}, | 16 { scriptCode: 'Bamu', scriptName: 'Bamum'}, |
| 15 { scriptCode: 'Bass', scriptName: 'Bassa Vah'}, | 17 { scriptCode: 'Bass', scriptName: 'Bassa Vah'}, |
| 16 { scriptCode: 'Batk', scriptName: 'Batak'}, | 18 { scriptCode: 'Batk', scriptName: 'Batak'}, |
| 17 { scriptCode: 'Beng', scriptName: 'Bengali'}, | 19 { scriptCode: 'Beng', scriptName: 'Bengali'}, |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 { scriptCode: 'Wara', scriptName: 'Varang Kshiti'}, | 154 { scriptCode: 'Wara', scriptName: 'Varang Kshiti'}, |
| 153 { scriptCode: 'Wole', scriptName: 'Woleai'}, | 155 { scriptCode: 'Wole', scriptName: 'Woleai'}, |
| 154 { scriptCode: 'Xpeo', scriptName: 'Old Persian'}, | 156 { scriptCode: 'Xpeo', scriptName: 'Old Persian'}, |
| 155 { scriptCode: 'Xsux', scriptName: 'Sumero-Akkadian Cuneiform'}, | 157 { scriptCode: 'Xsux', scriptName: 'Sumero-Akkadian Cuneiform'}, |
| 156 { scriptCode: 'Yiii', scriptName: 'Yi'}, | 158 { scriptCode: 'Yiii', scriptName: 'Yi'}, |
| 157 { scriptCode: 'Zmth', scriptName: 'Mathematical Notation'}, | 159 { scriptCode: 'Zmth', scriptName: 'Mathematical Notation'}, |
| 158 { scriptCode: 'Zsym', scriptName: 'Symbols'} | 160 { scriptCode: 'Zsym', scriptName: 'Symbols'} |
| 159 ]; | 161 ]; |
| 160 | 162 |
| 161 // The generic font families supported by the Font Settings Extension API. | 163 // The generic font families supported by the Font Settings Extension API. |
| 162 var families = | 164 var FAMILIES = |
| 163 ["standard", "sansserif", "serif", "fixed", "cursive", "fantasy"]; | 165 ['standard', 'sansserif', 'serif', 'fixed', 'cursive', 'fantasy']; |
| 164 | |
| 165 // Mapping between font list ids and the generic family setting they | |
| 166 // represent. | |
| 167 var fontPickers = [ | |
| 168 { fontList: 'standardFontList', name: 'standard' }, | |
| 169 { fontList: 'serifFontList', name: 'serif' }, | |
| 170 { fontList: 'sansSerifFontList', name: 'sansserif' }, | |
| 171 { fontList: 'fixedFontList', name: 'fixed' } | |
| 172 ]; | |
| 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 | 166 |
| 183 // Sample texts. | 167 // Sample texts. |
| 184 var defaultSampleText = 'The quick brown fox jumps over the lazy dog.'; | 168 var SAMPLE_TEXTS = { |
| 185 var scriptSpecificSampleText = { | |
| 186 // "Cyrllic script". | 169 // "Cyrllic script". |
| 187 'Cyrl': 'Кириллица', | 170 'Cyrl': 'Кириллица', |
| 188 'Hang': '정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날.', | 171 'Hang': '정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날.', |
| 189 'Hans': '床前明月光,疑是地上霜。举头望明月,低头思故乡。', | 172 'Hans': '床前明月光,疑是地上霜。举头望明月,低头思故乡。', |
| 190 'Hant': '床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。', | 173 'Hant': '床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。', |
| 191 'Jpan': '吾輩は猫である。名前はまだ無い。', | 174 'Jpan': '吾輩は猫である。名前はまだ無い。', |
| 192 // "Khmer language". | 175 // "Khmer language". |
| 193 'Khmr': '\u1797\u17B6\u179F\u17B6\u1781\u17D2\u1798\u17C2\u179A', | 176 'Khmr': '\u1797\u17B6\u179F\u17B6\u1781\u17D2\u1798\u17C2\u179A', |
| 177 'Zyyy': 'The quick brown fox jumps over the lazy dog.' | |
| 194 }; | 178 }; |
| 195 | 179 |
| 196 // Definition for ScriptList. | 180 // Controller of pending changes. |
| 197 cr.define('fontSettings.ui', function() { | 181 var pendingChanges = new PendingChanges(); |
| 198 const List = cr.ui.List; | |
| 199 const ListItem = cr.ui.ListItem; | |
| 200 const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | |
| 201 | 182 |
| 202 function ScriptListItem(info) { | 183 // Map of |genericFamily| to UI controls and data for its font setting. |
| 203 var el = cr.doc.createElement('li'); | 184 var fontSettings = null; |
| 204 el.__proto__ = ScriptListItem.prototype; | 185 |
| 205 el.info_ = info; | 186 // Map of |fontSizeKey| to UI contols and data for its font size setting. |
| 206 el.decorate(); | 187 var fontSizeSettings = null; |
| 207 return el; | 188 |
| 189 function $(id) { | |
| 190 return document.getElementById(id); | |
| 191 } | |
| 192 | |
| 193 // Returns a function of form function(x, y, z) which calls callback(x, y, z) | |
| 194 // where |y| and |z| are fixed to |font| and |controllable|, respectively. | |
| 195 function getBoundCallback(callback, font, controllable) { | |
| 196 return function(effectiveFont) { | |
| 197 callback(effectiveFont, font, controllable) | |
| 208 }; | 198 }; |
| 199 } | |
| 209 | 200 |
| 210 ScriptListItem.prototype = { | 201 // Gets the font size used for |fontSizeKey|, including pending changes. Calls |
| 211 __proto__: ListItem.prototype, | 202 // |callback| which must be of the form func(size, controllable). |size| is the |
| 212 | 203 // effective setting, |controllable| is whether the setting can be set. |
| 213 decorate: function() { | 204 function getEffectiveFontSize(fontSizeKey, callback) { |
| 214 this.textContent = this.info_.scriptName; | 205 fontSizeSettings[fontSizeKey].getter({}, function(details) { |
| 215 if (this.info_.scriptCode == 'Zyyy') { | 206 var controllable = isControllableLevel(details.levelOfControl); |
| 216 this.style.marginBottom = '1em'; | 207 var size = details.pixelSize; |
| 208 var pendingFontSize = pendingChanges.getFontSize(fontSizeKey); | |
| 209 // If the setting is not controllable, we can have no pending change. | |
| 210 if (!controllable) { | |
| 211 if (pendingFontSize != null) { | |
| 212 pendingChanges.setFontSize(fontSizeKey, null); | |
| 213 refresh(); | |
| 214 pendingFontSize = null; | |
| 217 } | 215 } |
| 218 } | 216 } |
| 219 }; | |
| 220 | 217 |
| 221 var ScriptList = cr.ui.define('list'); | 218 // If we have a pending change, it overrides the current setting. |
| 222 ScriptList.prototype = { | 219 if (pendingFontSize != null) |
| 223 __proto__: List.prototype, | 220 size = pendingFontSize; |
| 224 | 221 callback(size, controllable); |
| 225 decorate: function() { | 222 }); |
| 226 List.prototype.decorate.call(this); | |
| 227 var sm = new ListSingleSelectionModel(); | |
| 228 this.selectionModel = sm; | |
| 229 this.autoExpands = true; | |
| 230 this.dataModel = new cr.ui.ArrayDataModel(scripts); | |
| 231 this.style.height = '75vh'; | |
| 232 }, | |
| 233 | |
| 234 createItem: function(info) { | |
| 235 return new ScriptListItem(info); | |
| 236 } | |
| 237 }; | |
| 238 | |
| 239 return { | |
| 240 ScriptList: ScriptList, | |
| 241 ScriptListItem: ScriptListItem | |
| 242 }; | |
| 243 }); | |
| 244 | |
| 245 function getSelectedScript() { | |
| 246 var scriptList = document.getElementById('scriptList'); | |
| 247 return scriptList.selectedItem.scriptCode; | |
| 248 } | 223 } |
| 249 | 224 |
| 225 // Gets the font used for |script| and |genericFamily|, including pending | |
| 226 // changes. Calls |callback| which must be of the form func(effectiveFont, | |
| 227 // font, controllable). |effectiveFont| is the font used taking fallback into | |
| 228 // consideration, |font| is the actual setting (pending or not), |controllable| | |
| 229 // is whether the setting can be set. | |
| 230 function getEffectiveFont(script, genericFamily, callback) { | |
| 231 var details = { script: script, genericFamily: genericFamily }; | |
| 232 var pendingFont = | |
| 233 pendingChanges.getFont(details.script, details.genericFamily); | |
| 234 chrome.fontSettings.getFont(details, function(result) { | |
| 235 var setting = {}; | |
| 236 setting.font = result.fontId; | |
| 237 setting.controllable = isControllableLevel(result.levelOfControl); | |
| 238 // If the setting is not controllable, we can have no pending change. | |
| 239 if (!setting.controllable) { | |
| 240 pendingFont = null; | |
| 241 if (pendingChanges.getFont[script]) | |
| 242 pendingChanges.setFont([script][genericFamily], null); | |
| 243 refresh(); | |
| 244 } | |
| 245 | |
| 246 // If we have a pending change, it overrides the current setting. | |
| 247 if (pendingFont != null) | |
| 248 setting.font = pendingFont; | |
| 249 | |
| 250 // If we have a font, we're done. | |
| 251 if (setting.font) { | |
| 252 callback(setting.font, setting.font, setting.controllable); | |
| 253 return; | |
| 254 } | |
| 255 | |
| 256 // If we're still here, we have to fallback to common script, unless this | |
| 257 // already is common script. | |
| 258 if (script == COMMON_SCRIPT) { | |
| 259 callback('', '', setting.controllable); | |
| 260 return; | |
| 261 } | |
| 262 getEffectiveFont( | |
| 263 COMMON_SCRIPT, | |
| 264 genericFamily, | |
| 265 getBoundCallback(callback, setting.font, setting.controllable)); | |
| 266 }); | |
| 267 } | |
| 268 | |
| 269 // Returns a function of form function(effectiveFont, font, controllable) that | |
| 270 // refreshes the UI controls related to a font setting. |effectiveFont| is the | |
| 271 // font used including fallback, |font| is the value of the font setting | |
| 272 // (including pending changes), |controllable| is whether the setting can be | |
| 273 // controlled. | |
| 274 function getRefreshFontFunction(list, samples) { | |
| 275 return function(effectiveFont, font, controllable) { | |
| 276 for (var i = 0; i < samples.length; ++i) | |
| 277 samples[i].style.fontFamily = effectiveFont; | |
| 278 setSelectedFont(list, font); | |
| 279 list.disabled = !controllable; | |
| 280 } | |
| 281 } | |
| 282 | |
| 283 // Returns a function of form function(size, controllable) that refreshes the | |
| 284 // UI controls related to a font size setting. |size| is the value of the font | |
| 285 // setting (including pending changes), |controllable| is whether the setting | |
| 286 // can be controlled. | |
| 287 function getRefreshFontSizeFunction(fontSizeKey) { | |
| 288 var fontSizeSetting = fontSizeSettings[fontSizeKey]; | |
| 289 return function(size, controllable) { | |
| 290 fontSizeSetting.label.innerText = 'Size: ' + size + 'px'; | |
| 291 setFontSizeSlider(fontSizeSetting.slider, size, controllable); | |
| 292 for (var i = 0; i < fontSizeSetting.samples.length; ++i) | |
| 293 fontSizeSetting.samples[i].style.fontSize = size + 'px'; | |
| 294 }; | |
| 295 } | |
| 296 | |
| 297 // Refreshes all UI controls to reflect the current settings, including pending | |
| 298 // changes. | |
| 299 function refresh() { | |
| 300 var script = getSelectedScript(); | |
| 301 var sample; | |
| 302 if (SAMPLE_TEXTS[script]) | |
| 303 sample = SAMPLE_TEXTS[script]; | |
| 304 else | |
| 305 sample = SAMPLE_TEXTS[COMMON_SCRIPT]; | |
| 306 var sampleTexts = document.querySelectorAll('.sample-text-span'); | |
| 307 for (var i = 0; i < sampleTexts.length; i++) | |
| 308 sampleTexts[i].textContent = sample; | |
| 309 | |
| 310 for (var genericFamily in fontSettings) { | |
| 311 var setting = fontSettings[genericFamily]; | |
| 312 getEffectiveFont(script, genericFamily, | |
| 313 getRefreshFontFunction(setting.fontList, setting.samples)); | |
| 314 } | |
| 315 | |
| 316 for (var fontSizeKey in fontSizeSettings) | |
| 317 getEffectiveFontSize(fontSizeKey, getRefreshFontSizeFunction(fontSizeKey)); | |
| 318 | |
| 319 $('apply-settings').disabled = pendingChanges.isEmpty(); | |
| 320 } | |
| 321 | |
| 322 // Returns the currently selected script code. | |
| 323 function getSelectedScript() { | |
| 324 return $('scriptList').options[scriptList.selectedIndex].value; | |
| 325 } | |
| 326 | |
| 327 // Returns the currently selected value of |fontList|. | |
| 250 function getSelectedFont(fontList) { | 328 function getSelectedFont(fontList) { |
| 251 return fontList.options[fontList.selectedIndex].value; | 329 return fontList.options[fontList.selectedIndex].value; |
| 252 } | 330 } |
| 253 | 331 |
| 254 // Populates the font lists with the list of system fonts from |fonts|. | 332 // Populates the font lists. |fonts| is the list of system fonts. |
| 255 function populateLists(fonts) { | 333 function populateFontLists(fonts) { |
| 256 for (var i = 0; i < fontPickers.length; i++) { | 334 for (var genericFamily in fontSettings) { |
| 257 var list = document.getElementById(fontPickers[i].fontList); | 335 var list = fontSettings[genericFamily].fontList; |
| 258 | 336 |
| 259 // Add special item to indicate fallback to the non-per-script | 337 // 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 | 338 // font setting. The Font Settings API uses the empty string to indicate |
| 261 // fallback. | 339 // fallback. |
| 262 var defaultItem = document.createElement('option'); | 340 var defaultItem = document.createElement('option'); |
| 263 defaultItem.value = ''; | 341 defaultItem.value = ''; |
| 264 defaultItem.text = '(Use default)'; | 342 defaultItem.text = '(Use default)'; |
| 265 list.add(defaultItem); | 343 list.add(defaultItem); |
| 266 | 344 |
| 267 for (var j = 0; j < fonts.length; j++) { | 345 for (var i = 0; i < fonts.length; ++i) { |
| 268 var item = document.createElement('option'); | 346 var item = document.createElement('option'); |
| 269 item.value = fonts[j].fontId; | 347 item.value = fonts[i].fontId; |
| 270 item.text = fonts[j].displayName; | 348 item.text = fonts[i].displayName; |
| 271 list.add(item); | 349 list.add(item); |
| 272 } | 350 } |
| 273 } | 351 } |
| 274 | 352 refresh(); |
| 275 updateFontListsForScript(); | |
| 276 } | 353 } |
| 277 | 354 |
| 278 // Returns a function that updates the font setting for |genericFamily| | 355 // Returns a function to be called when the user changes the selected font |
| 279 // to match the selected value in |fontList|. It can be used as an event | 356 // in |fontList|. The function updates the pending font change. |
| 280 // handler for selection changes in |fontList|. | |
| 281 function getFontChangeHandler(fontList, genericFamily) { | 357 function getFontChangeHandler(fontList, genericFamily) { |
| 282 return function() { | 358 return function() { |
| 283 var script = getSelectedScript(); | 359 var script = getSelectedScript(); |
| 284 var font = getSelectedFont(fontList); | 360 var font = getSelectedFont(fontList); |
| 285 | 361 |
| 286 var details = {}; | 362 pendingChanges.setFont(script, genericFamily, font); |
| 287 details.genericFamily = genericFamily; | 363 refresh(); |
| 288 details.fontId = font; | |
| 289 details.script = script; | |
| 290 | |
| 291 chrome.fontSettings.setFont(details); | |
| 292 }; | 364 }; |
| 293 } | 365 } |
| 294 | 366 |
| 295 // Sets the selected value of |fontList| to |fontId|. | 367 // Sets the selected value of |fontList| to |fontId|. |
| 296 function setSelectedFont(fontList, fontId) { | 368 function setSelectedFont(fontList, fontId) { |
| 297 var script = getSelectedScript(); | 369 var script = getSelectedScript(); |
| 298 var i; | 370 var i; |
| 299 for (i = 0; i < fontList.length; i++) { | 371 for (i = 0; i < fontList.length; i++) { |
| 300 if (fontId == fontList.options[i].value) { | 372 if (fontId == fontList.options[i].value) { |
| 301 fontList.selectedIndex = i; | 373 fontList.selectedIndex = i; |
| 302 break; | 374 break; |
| 303 } | 375 } |
| 304 } | 376 } |
| 305 if (i == fontList.length) { | 377 if (i == fontList.length) { |
| 306 console.warn("font '" + fontId + "' for " + fontList.id + ' for ' + | 378 console.warn("font '" + fontId + "' for " + fontList.id + ' for ' + |
| 307 script + ' is not on the system'); | 379 script + ' is not on the system'); |
| 308 } | 380 } |
| 309 } | 381 } |
| 310 | 382 |
| 311 // Returns a callback function that sets the selected value of |list| to the | 383 // Returns a function to be called when the user changes the font size slider |
| 312 // font returned from |chrome.fontSettings.getFont|. | 384 // |slider|. The function sets the pending font size change. |
| 313 function getFontHandler(list) { | 385 function getFontSizeChangedFunc(label, fontSizeKey) { |
| 314 return function(details) { | 386 return function(value) { |
| 315 setSelectedFont(list, details.fontId); | 387 var pixelSize = parseInt(value); |
| 316 list.disabled = !isControllableLevel(details.levelOfControl); | |
| 317 }; | |
| 318 } | |
| 319 | |
| 320 // Called when the script list selection changes. Sets the selected value of | |
| 321 // each font list to the current font setting, and updates the samples' lang | |
| 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)) { | 388 if (!isNaN(pixelSize)) { |
| 356 setter({ pixelSize: pixelSize }); | 389 pendingChanges.setFontSize(fontSizeKey, pixelSize); |
| 390 refresh(); | |
| 357 } | 391 } |
| 358 } | 392 } |
| 359 } | 393 } |
| 360 | 394 |
| 395 // Returns true if |levelOfControl| signifies that the extension can | |
| 396 // control the setting; otherwise, returns false. | |
| 361 function isControllableLevel(levelOfControl) { | 397 function isControllableLevel(levelOfControl) { |
| 362 return levelOfControl == 'controllable_by_this_extension' || | 398 return levelOfControl == 'controllable_by_this_extension' || |
| 363 levelOfControl == 'controlled_by_this_extension'; | 399 levelOfControl == 'controlled_by_this_extension'; |
| 364 } | 400 } |
| 365 | 401 |
| 366 // Returns a function to be used as a listener for font size setting changed | 402 // Returns a function to be used as a listener for font size setting changed |
| 367 // events from the Font Settings Extension API. The function updates the input | 403 // events from the Font Settings Extension API. The function updates |slider|, |
| 368 // element |elem| and the elements in |sampleTexts| to reflect the change. | 404 // |label|, and |sampleTexts| related to the font size setting to reflect the |
| 369 function getFontSizeChangedOnBrowserFunc(elem, sampleTexts) { | 405 // change. |
| 406 function getFontSizeChangedOnBrowserFunc(slider, label, sampleTexts) { | |
| 370 return function(details) { | 407 return function(details) { |
| 371 var size = details.pixelSize.toString(); | 408 var size = details.pixelSize.toString(); |
| 372 elem.value = size; | 409 var controllable = isControllableLevel(details.levelOfControl); |
| 373 elem.disabled = !isControllableLevel(details.levelOfControl); | 410 setFontSizeSlider(slider, size, controllable); |
| 411 label.disabled = !controllable; | |
| 374 for (var i = 0; i < sampleTexts.length; i++) | 412 for (var i = 0; i < sampleTexts.length; i++) |
| 375 document.getElementById(sampleTexts[i]).style.fontSize = size + 'px'; | 413 sampleTexts[i].style.fontSize = size + 'px'; |
| 414 label.innerText = 'Size: ' + size + 'px'; | |
| 376 } | 415 } |
| 377 } | 416 } |
| 378 | 417 |
| 379 // Maps the HTML <input> element with |id| to the extension API accessor | 418 // Updates the font size slider to have value |size| and to be enabled iff |
| 380 // functions |getter| and |setter| for a setting and onChange event |apiEvent| | 419 // |enabled|. |
| 381 // for the setting. Also, maps the element ids in |sampleTexts| to this setting. | 420 function setFontSizeSlider(slider, size, enabled) { |
| 382 function initFontSizePref(id, sampleTexts, getter, setter, apiEvent) { | 421 if (slider.getValue() != size) |
| 383 var elem = document.getElementById(id); | 422 slider.setValue(size); |
| 384 getter({}, function(details) { | 423 var inputElement = slider.getInput(); |
| 385 var size = details.pixelSize.toString(); | 424 if (enabled) { |
| 386 elem.value = size; | 425 inputElement.parentNode.classList.remove('disabled'); |
| 387 elem.disabled = !isControllableLevel(details.levelOfControl); | 426 inputElement.disabled = false; |
| 388 for (var i = 0; i < sampleTexts.length; i++) | 427 } else { |
| 389 document.getElementById(sampleTexts[i]).style.fontSize = size + 'px'; | 428 inputElement.parentNode.classList.add('disabled'); |
| 390 }); | 429 inputElement.disabled = true; |
| 391 elem.addEventListener('change', getFontSizeChangedFunc(elem, setter)); | 430 } |
| 392 apiEvent.addListener(getFontSizeChangedOnBrowserFunc(elem, sampleTexts)); | |
| 393 } | 431 } |
| 394 | 432 |
| 433 // Initializes the UI control elements related to the font size setting | |
| 434 // |fontSizeKey| and registers listeners for the user adjusting its slider and | |
| 435 // the setting changing on the browser-side. | |
| 436 function initFontSizeSetting(fontSizeKey) { | |
| 437 var setting = fontSizeSettings[fontSizeKey]; | |
| 438 var label = setting.label; | |
| 439 var samples = setting.samples; | |
| 440 | |
| 441 setting.slider = new Slider( | |
| 442 setting.sliderContainer, | |
| 443 0, | |
| 444 setting.minValue, | |
| 445 setting.maxValue, | |
| 446 getFontSizeChangedFunc(label, fontSizeKey) | |
| 447 ); | |
| 448 | |
| 449 var slider = setting.slider; | |
| 450 setting.getter({}, function(details) { | |
| 451 var size = details.pixelSize.toString(); | |
| 452 setFontSizeSlider(slider, size, | |
| 453 isControllableLevel(details.levelOfControl)); | |
| 454 for (var i = 0; i < samples.length; i++) | |
| 455 samples[i].style.fontSize = size + 'px'; | |
| 456 }); | |
| 457 var apiEvent = fontSizeSettings[fontSizeKey].onChanged; | |
| 458 apiEvent.addListener(getFontSizeChangedOnBrowserFunc(slider, label, samples)); | |
| 459 } | |
| 460 | |
| 461 // Clears the font settings for script code |script|. | |
| 395 function clearSettingsForScript(script) { | 462 function clearSettingsForScript(script) { |
| 396 for (var i = 0; i < families.length; i++) { | 463 pendingChanges.clearOneScript(script); |
| 464 for (var i = 0; i < FAMILIES.length; i++) { | |
| 397 chrome.fontSettings.clearFont({ | 465 chrome.fontSettings.clearFont({ |
| 398 script: script, | 466 script: script, |
| 399 genericFamily: families[i] | 467 genericFamily: FAMILIES[i] |
| 400 }); | 468 }); |
| 401 } | 469 } |
| 402 } | 470 } |
| 403 | 471 |
| 472 // Clears all font and font size settings. | |
| 404 function clearAllSettings() { | 473 function clearAllSettings() { |
| 474 pendingChanges.clear(); | |
| 405 for (var i = 0; i < scripts.length; i++) | 475 for (var i = 0; i < scripts.length; i++) |
| 406 clearSettingsForScript(scripts[i].scriptCode); | 476 clearSettingsForScript(scripts[i].scriptCode); |
| 407 | |
| 408 chrome.fontSettings.clearDefaultFixedFontSize(); | 477 chrome.fontSettings.clearDefaultFixedFontSize(); |
| 409 chrome.fontSettings.clearDefaultFontSize(); | 478 chrome.fontSettings.clearDefaultFontSize(); |
| 410 chrome.fontSettings.clearMinimumFontSize(); | 479 chrome.fontSettings.clearMinimumFontSize(); |
| 411 } | 480 } |
| 412 | 481 |
| 413 function closeOverlay() { | 482 function closeOverlay() { |
| 414 $('overlay-container').hidden = true; | 483 $('overlay-container').hidden = true; |
| 415 } | 484 } |
| 416 | 485 |
| 417 function initResetButtons() { | 486 // Initializes apply and reset buttons. |
| 487 function initApplyAndResetButtons() { | |
| 488 var applyButton = $('apply-settings'); | |
| 489 applyButton.addEventListener('click', function() { | |
| 490 pendingChanges.apply(); | |
| 491 refresh(); | |
| 492 }); | |
| 493 | |
| 418 var overlay = $('overlay-container'); | 494 var overlay = $('overlay-container'); |
| 419 cr.ui.overlay.globalInitialization(); | 495 cr.ui.overlay.globalInitialization(); |
| 420 cr.ui.overlay.setupOverlay(overlay); | 496 cr.ui.overlay.setupOverlay(overlay); |
| 421 overlay.addEventListener('cancelOverlay', closeOverlay); | 497 overlay.addEventListener('cancelOverlay', closeOverlay); |
| 422 | 498 |
| 423 $('reset-this-script-button').onclick = function(event) { | 499 $('reset-this-script-button').onclick = function(event) { |
| 424 var scriptName = $('scriptList').selectedItem.scriptName; | 500 var scriptList = $('scriptList'); |
| 501 var scriptName = scriptList.options[scriptList.selectedIndex].text; | |
| 425 $('reset-this-script-overlay-dialog-content').innerText = | 502 $('reset-this-script-overlay-dialog-content').innerText = |
| 426 'Are you sure you want to reset settings for ' + scriptName + | 503 'Are you sure you want to reset settings for ' + scriptName + |
| 427 ' script?'; | 504 ' script?'; |
| 428 | 505 |
| 429 $('overlay-container').hidden = false; | 506 $('overlay-container').hidden = false; |
| 430 $('reset-this-script-overlay-dialog').hidden = false; | 507 $('reset-this-script-overlay-dialog').hidden = false; |
| 431 $('reset-all-scripts-overlay-dialog').hidden = true; | 508 $('reset-all-scripts-overlay-dialog').hidden = true; |
| 432 } | 509 }; |
| 433 $('reset-this-script-ok').onclick = function(event) { | 510 $('reset-this-script-ok').onclick = function(event) { |
| 434 clearSettingsForScript(getSelectedScript()); | 511 clearSettingsForScript(getSelectedScript()); |
| 435 closeOverlay(); | 512 closeOverlay(); |
| 513 refresh(); | |
| 436 }; | 514 }; |
| 437 $('reset-this-script-cancel').onclick = closeOverlay; | 515 $('reset-this-script-cancel').onclick = closeOverlay; |
| 438 | 516 |
| 439 $('reset-all-button').onclick = function(event) { | 517 $('reset-all-button').onclick = function(event) { |
| 440 $('overlay-container').hidden = false; | 518 $('overlay-container').hidden = false; |
| 441 $('reset-all-scripts-overlay-dialog').hidden = false; | 519 $('reset-all-scripts-overlay-dialog').hidden = false; |
| 442 $('reset-this-script-overlay-dialog').hidden = true; | 520 $('reset-this-script-overlay-dialog').hidden = true; |
| 443 } | 521 }; |
| 444 $('reset-all-ok').onclick = function(event) { | 522 $('reset-all-ok').onclick = function(event) { |
| 445 clearAllSettings(); | 523 clearAllSettings(); |
| 446 closeOverlay(); | 524 closeOverlay(); |
| 447 } | 525 refresh(); |
| 526 }; | |
| 448 $('reset-all-cancel').onclick = closeOverlay; | 527 $('reset-all-cancel').onclick = closeOverlay; |
| 449 } | 528 } |
| 450 | 529 |
| 530 // Taken from the IDS_WEB_FONT_FAMILY strings in Chrome. | |
| 531 // TODO: The font should be localized like Chrome does. | |
| 532 var systemFonts = { | |
| 533 cros: 'Noto Sans UI, sans-serif', | |
| 534 linux: 'Ubuntu, sans-serif', | |
| 535 mac: 'Lucida Grande, sans-serif', | |
| 536 win: 'Segoe UI, Tahoma, sans-serif', | |
| 537 unknown: 'sans-serif' | |
| 538 } | |
| 539 | |
| 540 function getPlatform() { | |
| 541 var ua = window.navigator.appVersion; | |
| 542 if (ua.indexOf('Win') != -1) return 'win'; | |
| 543 if (ua.indexOf('Mac') != -1) return 'mac'; | |
| 544 if (ua.indexOf('Linux') != -1) return 'linux'; | |
| 545 if (ua.indexOf('CrOS') != -1) return 'cros'; | |
| 546 return 'unknown'; | |
| 547 } | |
| 548 | |
| 549 // Chrome settings tries to use the system font. So does this extension. | |
| 550 function useSystemFont() { | |
| 551 document.body.style.fontFamily = systemFonts[getPlatform()]; | |
| 552 } | |
| 553 | |
| 554 function sortScripts() { | |
| 555 var i; | |
| 556 for (i = 0; i < scripts; ++i) { | |
| 557 if (scripts[i].scriptCode == COMMON_SCRIPT) | |
| 558 break; | |
| 559 } | |
| 560 var defaultScript = scripts.splice(i, 1)[0]; | |
| 561 | |
| 562 scripts.sort(function(a, b) { | |
| 563 if (a.scriptName > b.scriptName) | |
| 564 return 1; | |
| 565 if (a.scriptName < b.scriptName) | |
| 566 return -1; | |
| 567 return 0; | |
| 568 }); | |
| 569 | |
| 570 scripts.unshift(defaultScript); | |
| 571 } | |
| 572 | |
| 573 // Initializes UI controls for font settings. | |
| 574 function initFontControls() { | |
| 575 fontSettings = { | |
| 576 'standard': { | |
| 577 fontList: $('standardFontList'), | |
| 578 samples: [$('standardFontSample'), $('minFontSample')] | |
| 579 }, | |
| 580 'serif': { | |
| 581 fontList: $('serifFontList'), | |
| 582 samples: [$('serifFontSample')] | |
| 583 }, | |
| 584 'sansserif': { | |
| 585 fontList: $('sansSerifFontList'), | |
| 586 samples: [$('sansSerifFontSample')] | |
| 587 }, | |
| 588 'fixed': { | |
| 589 fontList: $('fixedFontList'), | |
| 590 samples: [$('fixedFontSample')] | |
| 591 } | |
| 592 }; | |
| 593 | |
| 594 for (var genericFamily in fontSettings) { | |
| 595 var list = fontSettings[genericFamily].fontList; | |
| 596 list.addEventListener('change', getFontChangeHandler(list, genericFamily)); | |
| 597 } | |
| 598 chrome.fontSettings.onFontChanged.addListener(refresh); | |
| 599 chrome.fontSettings.getFontList(populateFontLists); | |
| 600 } | |
| 601 | |
| 602 // Initializes UI controls for font size settings. | |
| 603 function initFontSizeControls() { | |
| 604 fontSizeSettings = { | |
| 605 'defaultFontSize': { | |
| 606 sliderContainer: $('defaultFontSizeSliderContainer'), | |
| 607 minValue: 6, | |
| 608 maxValue: 50, | |
| 609 samples: [ | |
| 610 $('standardFontSample'), $('serifFontSample'), $('sansSerifFontSample') | |
| 611 ], | |
| 612 label: $('defaultFontSizeLabel'), | |
| 613 getter: chrome.fontSettings.getDefaultFontSize, | |
| 614 onChanged: chrome.fontSettings.onDefaultFontSizeChanged | |
| 615 }, | |
| 616 'defaultFixedFontSize': { | |
| 617 sliderContainer: $('defaultFixedFontSizeSliderContainer'), | |
| 618 minValue: 6, | |
| 619 maxValue: 50, | |
| 620 samples: [$('fixedFontSample')], | |
| 621 label: $('fixedFontSizeLabel'), | |
| 622 getter: chrome.fontSettings.getDefaultFixedFontSize, | |
| 623 onChanged: chrome.fontSettings.onDefaultFixedFontSizeChanged | |
| 624 }, | |
| 625 'minFontSize': { | |
| 626 sliderContainer: $('minFontSizeSliderContainer'), | |
| 627 minValue: 6, | |
| 628 maxValue: 24, | |
| 629 samples: [$('minFontSample')], | |
| 630 label: $('minFontSizeLabel'), | |
| 631 getter: chrome.fontSettings.getMinimumFontSize, | |
| 632 onChanged: chrome.fontSettings.onMinimumFontSizeChanged | |
| 633 } | |
| 634 }; | |
| 635 | |
| 636 for (var fontSizeKey in fontSizeSettings) | |
| 637 initFontSizeSetting(fontSizeKey); | |
| 638 } | |
| 639 | |
| 640 // Initializes the list of scripts. | |
| 641 function initScriptList() { | |
| 642 var scriptList = $('scriptList'); | |
| 643 sortScripts(); | |
| 644 for (var i = 0; i < scripts.length; i++) { | |
| 645 var script = document.createElement('option'); | |
| 646 script.value = scripts[i].scriptCode; | |
| 647 script.text = scripts[i].scriptName; | |
| 648 scriptList.add(script); | |
| 649 } | |
| 650 scriptList.selectedIndex = 0; | |
| 651 scriptList.addEventListener('change', refresh); | |
| 652 } | |
| 653 | |
| 451 function init() { | 654 function init() { |
| 452 var scriptList = document.getElementById('scriptList'); | 655 useSystemFont(); |
| 453 fontSettings.ui.ScriptList.decorate(scriptList); | |
| 454 scriptList.selectionModel.selectedIndex = 0; | |
| 455 scriptList.selectionModel.addEventListener('change', | |
| 456 updateFontListsForScript); | |
| 457 | 656 |
| 458 // Populate the font lists. | 657 initFontControls(); |
| 459 chrome.fontSettings.getFontList(populateLists); | 658 initFontSizeControls(); |
| 659 initScriptList(); | |
| 460 | 660 |
| 461 // Add change handlers to the font lists. | 661 initApplyAndResetButtons(); |
| 462 for (var i = 0; i < fontPickers.length; i++) { | |
| 463 var list = document.getElementById(fontPickers[i].fontList); | |
| 464 var handler = getFontChangeHandler(list, fontPickers[i].name); | |
| 465 list.addEventListener('change', handler); | |
| 466 } | |
| 467 | |
| 468 chrome.fontSettings.onFontChanged.addListener( | |
| 469 updateFontListsForScript); | |
| 470 | |
| 471 initFontSizePref( | |
| 472 'defaultFontSizeRocker', | |
| 473 ['standardFontSample', 'serifFontSample', 'sansSerifFontSample'], | |
| 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 | |
| 508 initResetButtons(); | |
| 509 } | 662 } |
| 510 | 663 |
| 511 document.addEventListener('DOMContentLoaded', init); | 664 document.addEventListener('DOMContentLoaded', init); |
| OLD | NEW |