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.
5 cr.define('options', function() {
7 var OptionsPage = options.OptionsPage;
8 var Page = cr.ui.pageManager.Page;
9 var PageManager = cr.ui.pageManager.PageManager;
13 * Encapsulated handling of the 'Fonts and Encoding' page.
16 function FontSettings() {
17 Page.call(this, 'fonts',
18 loadTimeData.getString('fontSettingsPageTabTitle'),
22 cr.addSingletonGetter(FontSettings);
24 FontSettings.prototype = {
25 __proto__: Page.prototype,
28 initializePage: function() {
29 Page.prototype.initializePage.call(this);
31 var standardFontRange = $('standard-font-size');
32 standardFontRange.valueMap = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20,
33 22, 24, 26, 28, 30, 32, 34, 36, 40, 44, 48, 56, 64, 72];
34 standardFontRange.addEventListener(
35 'change', this.standardRangeChanged_.bind(this, standardFontRange));
36 standardFontRange.addEventListener(
37 'input', this.standardRangeChanged_.bind(this, standardFontRange));
38 standardFontRange.customChangeHandler =
39 this.standardFontSizeChanged_.bind(standardFontRange);
41 var minimumFontRange = $('minimum-font-size');
42 minimumFontRange.valueMap = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
44 minimumFontRange.addEventListener(
45 'change', this.minimumRangeChanged_.bind(this, minimumFontRange));
46 minimumFontRange.addEventListener(
47 'input', this.minimumRangeChanged_.bind(this, minimumFontRange));
48 minimumFontRange.customChangeHandler =
49 this.minimumFontSizeChanged_.bind(minimumFontRange);
51 var placeholder = loadTimeData.getString('fontSettingsPlaceholder');
52 var elements = [$('standard-font-family'), $('serif-font-family'),
53 $('sans-serif-font-family'), $('fixed-font-family'),
55 elements.forEach(function(el) {
56 el.appendChild(new Option(placeholder));
57 el.setDisabled('noFontsAvailable', true);
60 $('font-settings-confirm').onclick = function() {
61 PageManager.closeOverlay();
64 $('advanced-font-settings-options').onclick = function() {
65 chrome.send('openAdvancedFontSettingsOptions');
70 didShowPage: function() {
71 // The fonts list may be large so we only load it when this page is
72 // loaded for the first time. This makes opening the options window
73 // faster and consume less memory if the user never opens the fonts
76 chrome.send('fetchFontsData');
82 * Handler that is called when the user changes the position of the standard
83 * font size slider. This allows the UI to show a preview of the change
84 * before the slider has been released and the associated prefs updated.
85 * @param {Element} el The slider input element.
86 * @param {Event} event Change event.
89 standardRangeChanged_: function(el, event) {
90 var size = el.mapPositionToPref(el.value);
91 var fontSampleEl = $('standard-font-sample');
92 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
95 fontSampleEl = $('serif-font-sample');
96 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
99 fontSampleEl = $('sans-serif-font-sample');
100 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
103 fontSampleEl = $('fixed-font-sample');
104 this.setUpFontSample_(fontSampleEl,
105 size - OptionsPage.SIZE_DIFFERENCE_FIXED_STANDARD,
106 fontSampleEl.style.fontFamily, false);
110 * Sets the 'default_fixed_font_size' preference when the user changes the
111 * standard font size.
112 * @param {Event} event Change event.
115 standardFontSizeChanged_: function(event) {
116 var size = this.mapPositionToPref(this.value);
117 Preferences.setIntegerPref(
118 'webkit.webprefs.default_fixed_font_size',
119 size - OptionsPage.SIZE_DIFFERENCE_FIXED_STANDARD, true);
124 * Handler that is called when the user changes the position of the minimum
125 * font size slider. This allows the UI to show a preview of the change
126 * before the slider has been released and the associated prefs updated.
127 * @param {Element} el The slider input element.
128 * @param {Event} event Change event.
131 minimumRangeChanged_: function(el, event) {
132 var size = el.mapPositionToPref(el.value);
133 var fontSampleEl = $('minimum-font-sample');
134 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
139 * Sets the 'minimum_logical_font_size' preference when the user changes the
141 * @param {Event} event Change event.
144 minimumFontSizeChanged_: function(event) {
145 var size = this.mapPositionToPref(this.value);
146 Preferences.setIntegerPref(
147 'webkit.webprefs.minimum_logical_font_size', size, true);
152 * Sets the text, font size and font family of the sample text.
153 * @param {Element} el The div containing the sample text.
154 * @param {number} size The font size of the sample text.
155 * @param {string} font The font family of the sample text.
156 * @param {boolean} showSize True if the font size should appear in the
160 setUpFontSample_: function(el, size, font, showSize) {
161 var prefix = showSize ? (size + ': ') : '';
162 el.textContent = prefix +
163 loadTimeData.getString('fontSettingsLoremIpsum');
164 el.style.fontSize = size + 'px';
166 el.style.fontFamily = font;
170 * Populates a select list and selects the specified item.
171 * @param {Element} element The select element to populate.
172 * @param {Array} items The array of items from which to populate.
173 * @param {string} selectedValue The selected item.
176 populateSelect_: function(element, items, selectedValue) {
177 // Remove any existing content.
178 element.textContent = '';
180 // Insert new child nodes into select element.
181 for (var i = 0; i < items.length; i++) {
182 var value = items[i][0];
183 var text = items[i][1];
184 var dir = items[i][2];
186 var selected = value == selectedValue;
187 var option = new Option(text, value, false, selected);
189 element.appendChild(option);
191 element.appendChild(document.createElement('hr'));
195 element.setDisabled('noFontsAvailable', false);
200 FontSettings.setFontsData = function(fonts, encodings, selectedValues) {
201 FontSettings.getInstance().populateSelect_($('standard-font-family'), fonts,
203 FontSettings.getInstance().populateSelect_($('serif-font-family'), fonts,
205 FontSettings.getInstance().populateSelect_($('sans-serif-font-family'),
206 fonts, selectedValues[2]);
207 FontSettings.getInstance().populateSelect_($('fixed-font-family'), fonts,
209 FontSettings.getInstance().populateSelect_($('font-encoding'), encodings,
213 FontSettings.setUpStandardFontSample = function(font, size) {
214 FontSettings.getInstance().setUpFontSample_($('standard-font-sample'), size,
218 FontSettings.setUpSerifFontSample = function(font, size) {
219 FontSettings.getInstance().setUpFontSample_($('serif-font-sample'), size,
223 FontSettings.setUpSansSerifFontSample = function(font, size) {
224 FontSettings.getInstance().setUpFontSample_($('sans-serif-font-sample'),
228 FontSettings.setUpFixedFontSample = function(font, size) {
229 FontSettings.getInstance().setUpFontSample_($('fixed-font-sample'),
233 FontSettings.setUpMinimumFontSample = function(size) {
234 // If size is less than 6, represent it as six in the sample to account
235 // for the minimum logical font size.
238 FontSettings.getInstance().setUpFontSample_($('minimum-font-sample'), size,
243 * @param {boolean} available Whether the Advanced Font Settings Extension
244 * is installed and enabled.
246 FontSettings.notifyAdvancedFontSettingsAvailability = function(available) {
247 $('advanced-font-settings-install').hidden = available;
248 $('advanced-font-settings-options').hidden = !available;
253 FontSettings: FontSettings