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;
11 * Encapsulated handling of the 'Fonts and Encoding' page.
14 function FontSettings() {
15 OptionsPage.call(this,
17 loadTimeData.getString('fontSettingsPageTabTitle'),
21 cr.addSingletonGetter(FontSettings);
23 FontSettings.prototype = {
24 __proto__: OptionsPage.prototype,
27 * Initialize the page.
29 initializePage: function() {
30 OptionsPage.prototype.initializePage.call(this);
32 var standardFontRange = $('standard-font-size');
33 standardFontRange.valueMap = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20,
34 22, 24, 26, 28, 30, 32, 34, 36, 40, 44, 48, 56, 64, 72];
35 standardFontRange.addEventListener(
36 'change', this.standardRangeChanged_.bind(this, standardFontRange));
37 standardFontRange.customChangeHandler =
38 this.standardFontSizeChanged_.bind(standardFontRange);
40 var minimumFontRange = $('minimum-font-size');
41 minimumFontRange.valueMap = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
43 minimumFontRange.addEventListener(
44 'change', this.minimumRangeChanged_.bind(this, minimumFontRange));
45 minimumFontRange.customChangeHandler =
46 this.minimumFontSizeChanged_.bind(minimumFontRange);
48 var placeholder = loadTimeData.getString('fontSettingsPlaceholder');
49 var elements = [$('standard-font-family'), $('serif-font-family'),
50 $('sans-serif-font-family'), $('fixed-font-family'),
52 elements.forEach(function(el) {
53 el.appendChild(new Option(placeholder));
54 el.setDisabled('noFontsAvailable', true);
57 $('font-settings-confirm').onclick = function() {
58 OptionsPage.closeOverlay();
61 $('advanced-font-settings-options').onclick = function() {
62 chrome.send('openAdvancedFontSettingsOptions');
67 * Called by the options page when this page has been shown.
69 didShowPage: function() {
70 // The fonts list may be large so we only load it when this page is
71 // loaded for the first time. This makes opening the options window
72 // faster and consume less memory if the user never opens the fonts
75 chrome.send('fetchFontsData');
81 * Handler that is called when the user changes the position of the standard
82 * font size slider. This allows the UI to show a preview of the change
83 * before the slider has been released and the associated prefs updated.
84 * @param {Element} el The slider input element.
85 * @param {Event} event Change event.
88 standardRangeChanged_: function(el, event) {
89 var size = el.mapPositionToPref(el.value);
90 var fontSampleEl = $('standard-font-sample');
91 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
94 fontSampleEl = $('serif-font-sample');
95 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
98 fontSampleEl = $('sans-serif-font-sample');
99 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
102 fontSampleEl = $('fixed-font-sample');
103 this.setUpFontSample_(fontSampleEl,
104 size - OptionsPage.SIZE_DIFFERENCE_FIXED_STANDARD,
105 fontSampleEl.style.fontFamily, false);
109 * Sets the 'default_fixed_font_size' preference when the user changes the
110 * standard font size.
111 * @param {Event} event Change event.
114 standardFontSizeChanged_: function(event) {
115 var size = this.mapPositionToPref(this.value);
116 Preferences.setIntegerPref(
117 'webkit.webprefs.default_fixed_font_size',
118 size - OptionsPage.SIZE_DIFFERENCE_FIXED_STANDARD, true);
123 * Handler that is called when the user changes the position of the minimum
124 * font size slider. This allows the UI to show a preview of the change
125 * before the slider has been released and the associated prefs updated.
126 * @param {Element} el The slider input element.
127 * @param {Event} event Change event.
130 minimumRangeChanged_: function(el, event) {
131 var size = el.mapPositionToPref(el.value);
132 var fontSampleEl = $('minimum-font-sample');
133 this.setUpFontSample_(fontSampleEl, size, fontSampleEl.style.fontFamily,
138 * Sets the 'minimum_logical_font_size' preference when the user changes the
140 * @param {Event} event Change event.
143 minimumFontSizeChanged_: function(event) {
144 var size = this.mapPositionToPref(this.value);
145 Preferences.setIntegerPref(
146 'webkit.webprefs.minimum_logical_font_size', size, true);
151 * Sets the text, font size and font family of the sample text.
152 * @param {Element} el The div containing the sample text.
153 * @param {number} size The font size of the sample text.
154 * @param {string} font The font family of the sample text.
155 * @param {bool} showSize True if the font size should appear in the sample.
158 setUpFontSample_: function(el, size, font, showSize) {
159 var prefix = showSize ? (size + ': ') : '';
160 el.textContent = prefix +
161 loadTimeData.getString('fontSettingsLoremIpsum');
162 el.style.fontSize = size + 'px';
164 el.style.fontFamily = font;
168 * Populates a select list and selects the specified item.
169 * @param {Element} element The select element to populate.
170 * @param {Array} items The array of items from which to populate.
171 * @param {string} selectedValue The selected item.
174 populateSelect_: function(element, items, selectedValue) {
175 // Remove any existing content.
176 element.textContent = '';
178 // Insert new child nodes into select element.
179 var value, text, selected, option;
180 for (var i = 0; i < items.length; i++) {
185 selected = value == selectedValue;
186 var option = new Option(text, value, false, selected);
188 element.appendChild(option);
190 element.appendChild(document.createElement('hr'));
194 element.setDisabled('noFontsAvailable', false);
199 FontSettings.setFontsData = function(fonts, encodings, selectedValues) {
200 FontSettings.getInstance().populateSelect_($('standard-font-family'), fonts,
202 FontSettings.getInstance().populateSelect_($('serif-font-family'), fonts,
204 FontSettings.getInstance().populateSelect_($('sans-serif-font-family'),
205 fonts, selectedValues[2]);
206 FontSettings.getInstance().populateSelect_($('fixed-font-family'), fonts,
208 FontSettings.getInstance().populateSelect_($('font-encoding'), encodings,
212 FontSettings.setUpStandardFontSample = function(font, size) {
213 FontSettings.getInstance().setUpFontSample_($('standard-font-sample'), size,
217 FontSettings.setUpSerifFontSample = function(font, size) {
218 FontSettings.getInstance().setUpFontSample_($('serif-font-sample'), size,
222 FontSettings.setUpSansSerifFontSample = function(font, size) {
223 FontSettings.getInstance().setUpFontSample_($('sans-serif-font-sample'),
227 FontSettings.setUpFixedFontSample = function(font, size) {
228 FontSettings.getInstance().setUpFontSample_($('fixed-font-sample'),
232 FontSettings.setUpMinimumFontSample = function(size) {
233 // If size is less than 6, represent it as six in the sample to account
234 // for the minimum logical font size.
237 FontSettings.getInstance().setUpFontSample_($('minimum-font-sample'), size,
242 * @param {boolean} available Whether the Advanced Font Settings Extension
243 * is installed and enabled.
245 FontSettings.notifyAdvancedFontSettingsAvailability = function(available) {
246 $('advanced-font-settings-install').hidden = available;
247 $('advanced-font-settings-options').hidden = !available;
252 FontSettings: FontSettings