Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: chrome/common/extensions/docs/examples/api/fontSettings/popup.js

Issue 9616038: Add example extension using Font Settings API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 var scriptList;
6
7 // Mapping between font list ids and the generic family setting they
8 // represent.
9 var genericFamilies = [
10 { fontList: 'standardFontList', name: 'standard' },
11 { fontList: 'serifFontList', name: 'serif' },
12 { fontList: 'sansSerifFontList', name: 'sansserif' },
13 { fontList: 'fixedFontList', name: 'fixed' }
14 ];
15
16 function getSelectedScript() {
17 return scriptList.options[scriptList.selectedIndex].value;
18 }
19
20 function getSelectedFont(fontList) {
21 return fontList.options[fontList.selectedIndex].value;
22 }
23
24 // Populates the font lists with the list of system fonts from |fonts|.
25 function populateLists(fonts) {
26 for (var i = 0; i < genericFamilies.length; i++) {
27 var list = document.getElementById(genericFamilies[i].fontList);
28
29 // Add special "(none)" item to indicate fallback to the non-per-script
30 // font setting. The Font Settings API uses the empty string to indicate
31 // fallback.
32 var noneItem = document.createElement('option');
33 noneItem.value = '';
34 noneItem.text = '(none)';
35 list.add(noneItem);
36
37 for (var j = 0; j < fonts.length; j++) {
38 var item = document.createElement('option');
39 item.value = fonts[j].fontName;
40 item.text = fonts[j].localizedName;
41 list.add(item);
42 }
43 }
44
45 updateListSelections();
46 }
47
48 // Returns a function that updates the font setting for |genericFamily|
49 // to match the selected value in |fontList|. It can be used as an event
50 // handler for selection changes in |fontList|.
51 function getFontChangeHandler(fontList, genericFamily) {
52 return function() {
53 var script = getSelectedScript();
54 var font = getSelectedFont(fontList);
55
56 chrome.experimental.fontSettings.setFontName({
57 script: script,
58 genericFamily: genericFamily,
59 fontName: font
60 });
61 };
62 }
63
64 // Sets the selected value of |fontList| to |fontName|.
65 function setSelectedFont(fontList, fontName) {
66 var script = getSelectedScript();
67
68 for (var i = 0; i < fontList.length; i++) {
69 if (fontName == fontList.options[i].value) {
70 fontList.selectedIndex = i;
71 break;
72 }
73 }
74 if (i == fontList.length) {
75 console.warn("font '" + fontName + "' for " + fontList.id + ' for ' +
76 script + ' is not on the system');
77 }
78 }
79
80 // Returns a callback function that sets the selected value of |list| to the
81 // font returned from |chrome.experimental.fontSettings.getFontName|.
82 function getFontNameHandler(list) {
83 return function(details) {
84 setSelectedFont(list, details.fontName);
85 };
86 }
87
88 // Sets the selected value of each font list to the current font setting.
89 function updateListSelections() {
90 var script = getSelectedScript();
91
92 for (var i = 0; i < genericFamilies.length; i++) {
93 var list = document.getElementById(genericFamilies[i].fontList);
94 var family = genericFamilies[i].name;
95
96 chrome.experimental.fontSettings.getFontName({
97 genericFamily: family,
98 script: script
99 }, getFontNameHandler(list));
100 }
101 }
102
103 function init() {
104 scriptList = document.getElementById('scriptList');
105 scriptList.addEventListener('change', updateListSelections);
106
107 // Populate the font lists.
108 chrome.experimental.fontSettings.getFontList(populateLists);
109
110 // Add change handlers to the font lists.
111 for (var i = 0; i < genericFamilies.length; i++) {
112 var list = document.getElementById(genericFamilies[i].fontList);
113 var handler = getFontChangeHandler(list, genericFamilies[i].name);
114 list.addEventListener('change', handler);
115 }
116 }
117
118 document.addEventListener('DOMContentLoaded', init);
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/examples/api/fontSettings/popup.html ('k') | chrome/common/extensions/docs/samples.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698