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

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

Issue 11227018: Font Settings API: UI overhaul of example extension. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update version and description string Created 8 years, 2 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
« no previous file with comments | « chrome/common/extensions/docs/examples/api/fontSettings/options.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.
6 var scripts = [
7 { scriptCode: 'Zyyy', scriptName: 'Default' },
8 { scriptCode: 'Arab', scriptName: 'Arabic' },
9 { scriptCode: 'Armn', scriptName: 'Armenian' },
10 { scriptCode: "Beng", scriptName: 'Bengali' },
11 { scriptCode: "Cher", scriptName: 'Cherokee' },
12 { scriptCode: "Cyrl", scriptName: 'Cyrillic' },
13 { scriptCode: "Deva", scriptName: 'Devanagari' },
14 { scriptCode: "Ethi", scriptName: 'Ethiopic' },
15 { scriptCode: "Geor", scriptName: 'Georgian' },
16 { scriptCode: "Grek", scriptName: 'Greek' },
17 { scriptCode: "Gujr", scriptName: 'Gujarati' },
18 { scriptCode: "Guru", scriptName: 'Gurmukhi' },
19 { scriptCode: "Hebr", scriptName: 'Hebrew' },
20 { scriptCode: "Jpan", scriptName: 'Japanese' },
21 { scriptCode: "Knda", scriptName: 'Kannada' },
22 { scriptCode: "Khmr", scriptName: 'Khmer' },
23 { scriptCode: "Hang", scriptName: 'Korean' },
24 { scriptCode: "Laoo", scriptName: 'Lao' },
25 { scriptCode: "Mlym", scriptName: 'Malayalam' },
26 { scriptCode: "Mong", scriptName: 'Mongolian' },
27 { scriptCode: "Mymr", scriptName: 'Myanmar' },
28 { scriptCode: "Orya", scriptName: 'Oriya' },
29 { scriptCode: "Hans", scriptName: 'Simplified Chinese' },
30 { scriptCode: "Sinh", scriptName: 'Sinhala' },
31 { scriptCode: "Taml", scriptName: 'Tamil' },
32 { scriptCode: "Telu", scriptName: 'Telugu' },
33 { scriptCode: "Thaa", scriptName: 'Thaana' },
34 { scriptCode: "Thai", scriptName: 'Thai' },
35 { scriptCode: "Tibt", scriptName: 'Tibetan' },
36 { scriptCode: "Hant", scriptName: 'Traditional Chinese' },
37 { scriptCode: "Cans", scriptName: 'Unified Canadian Aboriginal Syllabics' },
38 { scriptCode: "Yiii", scriptName: 'Yi' }
39 ];
40
41 // The generic font families supported by the Font Settings Extension API.
42 var families =
43 ["standard", "sansserif", "serif", "fixed", "cursive", "fantasy"];
44
5 // Mapping between font list ids and the generic family setting they 45 // Mapping between font list ids and the generic family setting they
6 // represent. 46 // represent.
7 var genericFamilies = [ 47 var fontPickers = [
8 { fontList: 'standardFontList', name: 'standard' }, 48 { fontList: 'standardFontList', name: 'standard' },
9 { fontList: 'serifFontList', name: 'serif' }, 49 { fontList: 'serifFontList', name: 'serif' },
10 { fontList: 'sansSerifFontList', name: 'sansserif' }, 50 { fontList: 'sansSerifFontList', name: 'sansserif' },
11 { fontList: 'fixedFontList', name: 'fixed' } 51 { fontList: 'fixedFontList', name: 'fixed' }
12 ]; 52 ];
13 53
14 // Ids of elements to contain the "Lorem ipsum" sample text. 54 // Ids of elements to contain the sample text.
15 var sampleTextDivIds = [ 55 var sampleTextDivIds = [
16 'standardFontSample', 56 'standardFontSample',
17 'serifFontSample', 57 'serifFontSample',
18 'sansSerifFontSample', 58 'sansSerifFontSample',
19 'fixedFontSample', 59 'fixedFontSample',
20 'minFontSample' 60 'minFontSample'
21 ]; 61 ];
22 62
23 var defaultSampleText = 'Lorem ipsum dolor sit amat.'; 63 // Sample texts.
64 var defaultSampleText = 'The quick brown fox jumps over the lazy dog.';
24 var scriptSpecificSampleText = { 65 var scriptSpecificSampleText = {
25 // "Cyrllic script". 66 // "Cyrllic script".
26 'Cyrl': 'Кириллица', 67 'Cyrl': 'Кириллица',
27 'Hang': '정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날.', 68 'Hang': '정 참판 양반댁 규수 큰 교자 타고 혼례 치른 날.',
28 'Hans': '床前明月光,疑是地上霜。举头望明月,低头思故乡。', 69 'Hans': '床前明月光,疑是地上霜。举头望明月,低头思故乡。',
29 'Hant': '床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。', 70 'Hant': '床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。',
30 'Jpan': '吾輩は猫である。名前はまだ無い。', 71 'Jpan': '吾輩は猫である。名前はまだ無い。',
31 // "Khmer language". 72 // "Khmer language".
32 'Khmr': '\u1797\u17B6\u179F\u17B6\u1781\u17D2\u1798\u17C2\u179A', 73 'Khmr': '\u1797\u17B6\u179F\u17B6\u1781\u17D2\u1798\u17C2\u179A',
33 }; 74 };
34 75
76 // Definition for ScriptList.
77 cr.define('fontSettings.ui', function() {
78 const List = cr.ui.List;
79 const ListItem = cr.ui.ListItem;
80 const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel;
81
82 function ScriptListItem(info) {
83 var el = cr.doc.createElement('li');
84 el.__proto__ = ScriptListItem.prototype;
85 el.info_ = info;
86 el.decorate();
87 return el;
88 };
89
90 ScriptListItem.prototype = {
91 __proto__: ListItem.prototype,
92
93 decorate: function() {
94 this.textContent = this.info_.scriptName;
95 if (this.info_.scriptCode == 'Zyyy') {
96 this.style.marginBottom = '1em';
97 }
98 }
99 };
100
101 var ScriptList = cr.ui.define('list');
102 ScriptList.prototype = {
103 __proto__: List.prototype,
104
105 decorate: function() {
106 List.prototype.decorate.call(this);
107 var sm = new ListSingleSelectionModel();
108 this.selectionModel = sm;
109 this.autoExpands = true;
110 this.dataModel = new cr.ui.ArrayDataModel(scripts);
111
112 // The list auto expands but is still just barely short enough to require
113 // a scroll bar. This is a hack to resize it to not require the scroll
114 // bar.
115 this.style.height = (this.clientHeight + 4) + 'px';
116 },
117
118 createItem: function(info) {
119 return new ScriptListItem(info);
120 }
121 };
122
123 return {
124 ScriptList: ScriptList,
125 ScriptListItem: ScriptListItem
126 };
127 });
128
35 function getSelectedScript() { 129 function getSelectedScript() {
36 var scriptList = document.getElementById('scriptList'); 130 var scriptList = document.getElementById('scriptList');
37 return scriptList.options[scriptList.selectedIndex].value; 131 return scriptList.selectedItem.scriptCode;
38 } 132 }
39 133
40 function getSelectedFont(fontList) { 134 function getSelectedFont(fontList) {
41 return fontList.options[fontList.selectedIndex].value; 135 return fontList.options[fontList.selectedIndex].value;
42 } 136 }
43 137
44 // Populates the font lists with the list of system fonts from |fonts|. 138 // Populates the font lists with the list of system fonts from |fonts|.
45 function populateLists(fonts) { 139 function populateLists(fonts) {
46 for (var i = 0; i < genericFamilies.length; i++) { 140 for (var i = 0; i < fontPickers.length; i++) {
47 var list = document.getElementById(genericFamilies[i].fontList); 141 var list = document.getElementById(fontPickers[i].fontList);
48 142
49 // Add special "(none)" item to indicate fallback to the non-per-script 143 // Add special item to indicate fallback to the non-per-script
50 // font setting. The Font Settings API uses the empty string to indicate 144 // font setting. The Font Settings API uses the empty string to indicate
51 // fallback. 145 // fallback.
52 var noneItem = document.createElement('option'); 146 var defaultItem = document.createElement('option');
53 noneItem.value = ''; 147 defaultItem.value = '';
54 noneItem.text = '(none)'; 148 defaultItem.text = '(Use default)';
55 list.add(noneItem); 149 list.add(defaultItem);
56 150
57 for (var j = 0; j < fonts.length; j++) { 151 for (var j = 0; j < fonts.length; j++) {
58 var item = document.createElement('option'); 152 var item = document.createElement('option');
59 item.value = fonts[j].fontId; 153 item.value = fonts[j].fontId;
60 item.text = fonts[j].displayName; 154 item.text = fonts[j].displayName;
61 list.add(item); 155 list.add(item);
62 } 156 }
63 } 157 }
64 158
65 updateFontListsForScript(); 159 updateFontListsForScript();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 // Returns a callback function that sets the selected value of |list| to the 195 // Returns a callback function that sets the selected value of |list| to the
102 // font returned from |chrome.fontSettings.getFont|. 196 // font returned from |chrome.fontSettings.getFont|.
103 function getFontHandler(list) { 197 function getFontHandler(list) {
104 return function(details) { 198 return function(details) {
105 setSelectedFont(list, details.fontId); 199 setSelectedFont(list, details.fontId);
106 list.disabled = !isControllableLevel(details.levelOfControl); 200 list.disabled = !isControllableLevel(details.levelOfControl);
107 }; 201 };
108 } 202 }
109 203
110 // Called when the script list selection changes. Sets the selected value of 204 // Called when the script list selection changes. Sets the selected value of
111 // each font list to the current font setting, and updates the document's lang 205 // each font list to the current font setting, and updates the samples' lang
112 // so that the samples are shown in the current font setting. 206 // so that they are shown in the current font setting.
113 function updateFontListsForScript() { 207 function updateFontListsForScript() {
114 var script = getSelectedScript(); 208 var script = getSelectedScript();
115 209
116 for (var i = 0; i < genericFamilies.length; i++) { 210 for (var i = 0; i < fontPickers.length; i++) {
117 var list = document.getElementById(genericFamilies[i].fontList); 211 var list = document.getElementById(fontPickers[i].fontList);
118 var family = genericFamilies[i].name; 212 var family = fontPickers[i].name;
119 213
120 var details = {}; 214 var details = {};
121 details.genericFamily = family; 215 details.genericFamily = family;
122 details.script = script; 216 details.script = script;
123 // For font selection it's the script code that matters, not language, so
124 // just use en for lang.
125 document.body.lang = 'en-' + script;
126
127 chrome.fontSettings.getFont(details, getFontHandler(list)); 217 chrome.fontSettings.getFont(details, getFontHandler(list));
128 } 218 }
129 219
130 if (typeof(scriptSpecificSampleText[script]) != 'undefined') 220 if (typeof(scriptSpecificSampleText[script]) != 'undefined')
131 sample = scriptSpecificSampleText[script]; 221 sample = scriptSpecificSampleText[script];
132 else 222 else
133 sample = defaultSampleText; 223 sample = defaultSampleText;
134 for (var i = 0; i < sampleTextDivIds.length; i++) { 224 for (var i = 0; i < sampleTextDivIds.length; i++) {
135 document.getElementById(sampleTextDivIds[i]).innerText = sample; 225 var sampleTextDiv = document.getElementById(sampleTextDivIds[i]);
226 // For font selection it's the script code that matters, not language, so
227 // just use en for lang.
228 sampleTextDiv.lang = 'en-' + script;
229 sampleTextDiv.innerText = sample;
136 } 230 }
137 } 231 }
138 232
139 // Returns a function to be called when the user changes the font size 233 // Returns a function to be called when the user changes the font size
140 // input element |elem|. The function calls the Font Settings Extension API 234 // input element |elem|. The function calls the Font Settings Extension API
141 // function |setter| to commit the change. 235 // function |setter| to commit the change.
142 function getFontSizeChangedFunc(elem, setter) { 236 function getFontSizeChangedFunc(elem, setter) {
143 return function() { 237 return function() {
144 var pixelSize = parseInt(elem.value); 238 var pixelSize = parseInt(elem.value);
145 if (!isNaN(pixelSize)) { 239 if (!isNaN(pixelSize)) {
(...skipping 29 matching lines...) Expand all
175 var size = details.pixelSize.toString(); 269 var size = details.pixelSize.toString();
176 elem.value = size; 270 elem.value = size;
177 elem.disabled = !isControllableLevel(details.levelOfControl); 271 elem.disabled = !isControllableLevel(details.levelOfControl);
178 for (var i = 0; i < sampleTexts.length; i++) 272 for (var i = 0; i < sampleTexts.length; i++)
179 document.getElementById(sampleTexts[i]).style.fontSize = size + 'px'; 273 document.getElementById(sampleTexts[i]).style.fontSize = size + 'px';
180 }); 274 });
181 elem.addEventListener('change', getFontSizeChangedFunc(elem, setter)); 275 elem.addEventListener('change', getFontSizeChangedFunc(elem, setter));
182 apiEvent.addListener(getFontSizeChangedOnBrowserFunc(elem, sampleTexts)); 276 apiEvent.addListener(getFontSizeChangedOnBrowserFunc(elem, sampleTexts));
183 } 277 }
184 278
279 function clearSettingsForScript(script) {
280 for (var i = 0; i < families.length; i++) {
281 chrome.fontSettings.clearFont({
282 script: script,
283 genericFamily: families[i]
284 });
285 }
286 }
287
185 function clearAllSettings() { 288 function clearAllSettings() {
186 var scripts = 289 for (var i = 0; i < scripts.length; i++)
187 ["Afak", "Arab", "Armi", "Armn", "Avst", "Bali", "Bamu", "Bass", "Batk", 290 clearSettingsForScript(scripts[i].scriptCode);
188 "Beng", "Blis", "Bopo", "Brah", "Brai", "Bugi", "Buhd", "Cakm", "Cans",
189 "Cari", "Cham", "Cher", "Cirt", "Copt", "Cprt", "Cyrl", "Cyrs", "Deva",
190 "Dsrt", "Dupl", "Egyd", "Egyh", "Egyp", "Elba", "Ethi", "Geor", "Geok",
191 "Glag", "Goth", "Gran", "Grek", "Gujr", "Guru", "Hang", "Hani", "Hano",
192 "Hans", "Hant", "Hebr", "Hluw", "Hmng", "Hung", "Inds", "Ital", "Java",
193 "Jpan", "Jurc", "Kali", "Khar", "Khmr", "Khoj", "Knda", "Kpel", "Kthi",
194 "Lana", "Laoo", "Latf", "Latg", "Latn", "Lepc", "Limb", "Lina", "Linb",
195 "Lisu", "Loma", "Lyci", "Lydi", "Mand", "Mani", "Maya", "Mend", "Merc",
196 "Mero", "Mlym", "Moon", "Mong", "Mroo", "Mtei", "Mymr", "Narb", "Nbat",
197 "Nkgb", "Nkoo", "Nshu", "Ogam", "Olck", "Orkh", "Orya", "Osma", "Palm",
198 "Perm", "Phag", "Phli", "Phlp", "Phlv", "Phnx", "Plrd", "Prti", "Rjng",
199 "Roro", "Runr", "Samr", "Sara", "Sarb", "Saur", "Sgnw", "Shaw", "Shrd",
200 "Sind", "Sinh", "Sora", "Sund", "Sylo", "Syrc", "Syre", "Syrj", "Syrn",
201 "Tagb", "Takr", "Tale", "Talu", "Taml", "Tang", "Tavt", "Telu", "Teng",
202 "Tfng", "Tglg", "Thaa", "Thai", "Tibt", "Tirh", "Ugar", "Vaii", "Visp",
203 "Wara", "Wole", "Xpeo", "Xsux", "Yiii", "Zmth", "Zsym", "Zyyy"];
204 var families =
205 ["standard", "sansserif", "serif", "fixed", "cursive", "fantasy"];
206 for (var i = 0; i < scripts.length; i++) {
207 for (var j = 0; j < families.length; j++) {
208 chrome.fontSettings.clearFont({
209 script: scripts[i],
210 genericFamily: families[j]
211 });
212 }
213 }
214 291
215 chrome.fontSettings.clearDefaultFixedFontSize(); 292 chrome.fontSettings.clearDefaultFixedFontSize();
216 chrome.fontSettings.clearDefaultFontSize(); 293 chrome.fontSettings.clearDefaultFontSize();
217 chrome.fontSettings.clearMinimumFontSize(); 294 chrome.fontSettings.clearMinimumFontSize();
218 } 295 }
219 296
297 function closeOverlay() {
298 $('overlay-container').hidden = true;
299 }
300
301 function initResetButtons() {
302 var overlay = $('overlay-container');
303 cr.ui.overlay.globalInitialization();
304 cr.ui.overlay.setupOverlay(overlay);
305 overlay.addEventListener('cancelOverlay', closeOverlay);
306
307 $('reset-this-script-button').onclick = function(event) {
308 var scriptName = $('scriptList').selectedItem.scriptName;
309 $('reset-this-script-overlay-dialog-content').innerText =
310 'Are you sure you want to reset settings for ' + scriptName +
311 ' script?';
312
313 $('overlay-container').hidden = false;
314 $('reset-this-script-overlay-dialog').hidden = false;
315 $('reset-all-scripts-overlay-dialog').hidden = true;
316 }
317 $('reset-this-script-ok').onclick = function(event) {
318 clearSettingsForScript(getSelectedScript());
319 closeOverlay();
320 };
321 $('reset-this-script-cancel').onclick = closeOverlay;
322
323 $('reset-all-button').onclick = function(event) {
324 $('overlay-container').hidden = false;
325 $('reset-all-scripts-overlay-dialog').hidden = false;
326 $('reset-this-script-overlay-dialog').hidden = true;
327 }
328 $('reset-all-ok').onclick = function(event) {
329 clearAllSettings();
330 closeOverlay();
331 }
332 $('reset-all-cancel').onclick = closeOverlay;
333 }
334
220 function init() { 335 function init() {
221 scriptList = document.getElementById('scriptList'); 336 var scriptList = document.getElementById('scriptList');
222 scriptList.addEventListener('change', 337 fontSettings.ui.ScriptList.decorate(scriptList);
223 updateFontListsForScript); 338 scriptList.selectionModel.selectedIndex = 0;
339 scriptList.selectionModel.addEventListener('change',
340 updateFontListsForScript);
224 341
225 // Populate the font lists. 342 // Populate the font lists.
226 chrome.fontSettings.getFontList(populateLists); 343 chrome.fontSettings.getFontList(populateLists);
227 344
228 // Add change handlers to the font lists. 345 // Add change handlers to the font lists.
229 for (var i = 0; i < genericFamilies.length; i++) { 346 for (var i = 0; i < fontPickers.length; i++) {
230 var list = document.getElementById(genericFamilies[i].fontList); 347 var list = document.getElementById(fontPickers[i].fontList);
231 var handler = getFontChangeHandler(list, genericFamilies[i].name); 348 var handler = getFontChangeHandler(list, fontPickers[i].name);
232 list.addEventListener('change', handler); 349 list.addEventListener('change', handler);
233 } 350 }
234 351
235 chrome.fontSettings.onFontChanged.addListener( 352 chrome.fontSettings.onFontChanged.addListener(
236 updateFontListsForScript); 353 updateFontListsForScript);
237 354
238 initFontSizePref( 355 initFontSizePref(
239 'defaultFontSize', 356 'defaultFontSizeRocker',
240 ['standardFontSample', 'serifFontSample', 'sansSerifFontSample'], 357 ['standardFontSample', 'serifFontSample', 'sansSerifFontSample'],
241 chrome.fontSettings.getDefaultFontSize, 358 chrome.fontSettings.getDefaultFontSize,
242 chrome.fontSettings.setDefaultFontSize, 359 chrome.fontSettings.setDefaultFontSize,
243 chrome.fontSettings.onDefaultFontSizeChanged); 360 chrome.fontSettings.onDefaultFontSizeChanged);
244 initFontSizePref( 361 initFontSizePref(
245 'defaultFixedFontSize', 362 'defaultFontSizeRange',
363 ['standardFontSample', 'serifFontSample', 'sansSerifFontSample'],
364 chrome.fontSettings.getDefaultFontSize,
365 chrome.fontSettings.setDefaultFontSize,
366 chrome.fontSettings.onDefaultFontSizeChanged);
367 initFontSizePref(
368 'defaultFixedFontSizeRocker',
246 ['fixedFontSample'], 369 ['fixedFontSample'],
247 chrome.fontSettings.getDefaultFixedFontSize, 370 chrome.fontSettings.getDefaultFixedFontSize,
248 chrome.fontSettings.setDefaultFixedFontSize, 371 chrome.fontSettings.setDefaultFixedFontSize,
249 chrome.fontSettings.onDefaultFixedFontSizeChanged); 372 chrome.fontSettings.onDefaultFixedFontSizeChanged);
250 initFontSizePref( 373 initFontSizePref(
251 'minFontSize', 374 'defaultFixedFontSizeRange',
375 ['fixedFontSample'],
376 chrome.fontSettings.getDefaultFixedFontSize,
377 chrome.fontSettings.setDefaultFixedFontSize,
378 chrome.fontSettings.onDefaultFixedFontSizeChanged);
379 initFontSizePref(
380 'minFontSizeRocker',
381 ['minFontSample'],
382 chrome.fontSettings.getMinimumFontSize,
383 chrome.fontSettings.setMinimumFontSize,
384 chrome.fontSettings.onMinimumFontSizeChanged);
385 initFontSizePref(
386 'minFontSizeRange',
252 ['minFontSample'], 387 ['minFontSample'],
253 chrome.fontSettings.getMinimumFontSize, 388 chrome.fontSettings.getMinimumFontSize,
254 chrome.fontSettings.setMinimumFontSize, 389 chrome.fontSettings.setMinimumFontSize,
255 chrome.fontSettings.onMinimumFontSizeChanged); 390 chrome.fontSettings.onMinimumFontSizeChanged);
256 391
257 var clearButton = document.getElementById('clearButton'); 392 initResetButtons();
258 clearButton.addEventListener('click', clearAllSettings);
259 } 393 }
260 394
261 document.addEventListener('DOMContentLoaded', init); 395 document.addEventListener('DOMContentLoaded', init);
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/examples/api/fontSettings/options.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698