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 * Called by the options page when this page has been shown.
72 didShowPage: function() {
73 // The fonts list may be large so we only load it when this page is
74 // loaded for the first time. This makes opening the options window
75 // faster and consume less memory if the user never opens the fonts
78 chrome
.send('fetchFontsData');
84 * Handler that is called when the user changes the position of the standard
85 * font size slider. This allows the UI to show a preview of the change
86 * before the slider has been released and the associated prefs updated.
87 * @param {Element} el The slider input element.
88 * @param {Event} event Change event.
91 standardRangeChanged_: function(el
, event
) {
92 var size
= el
.mapPositionToPref(el
.value
);
93 var fontSampleEl
= $('standard-font-sample');
94 this.setUpFontSample_(fontSampleEl
, size
, fontSampleEl
.style
.fontFamily
,
97 fontSampleEl
= $('serif-font-sample');
98 this.setUpFontSample_(fontSampleEl
, size
, fontSampleEl
.style
.fontFamily
,
101 fontSampleEl
= $('sans-serif-font-sample');
102 this.setUpFontSample_(fontSampleEl
, size
, fontSampleEl
.style
.fontFamily
,
105 fontSampleEl
= $('fixed-font-sample');
106 this.setUpFontSample_(fontSampleEl
,
107 size
- OptionsPage
.SIZE_DIFFERENCE_FIXED_STANDARD
,
108 fontSampleEl
.style
.fontFamily
, false);
112 * Sets the 'default_fixed_font_size' preference when the user changes the
113 * standard font size.
114 * @param {Event} event Change event.
117 standardFontSizeChanged_: function(event
) {
118 var size
= this.mapPositionToPref(this.value
);
119 Preferences
.setIntegerPref(
120 'webkit.webprefs.default_fixed_font_size',
121 size
- OptionsPage
.SIZE_DIFFERENCE_FIXED_STANDARD
, true);
126 * Handler that is called when the user changes the position of the minimum
127 * font size slider. This allows the UI to show a preview of the change
128 * before the slider has been released and the associated prefs updated.
129 * @param {Element} el The slider input element.
130 * @param {Event} event Change event.
133 minimumRangeChanged_: function(el
, event
) {
134 var size
= el
.mapPositionToPref(el
.value
);
135 var fontSampleEl
= $('minimum-font-sample');
136 this.setUpFontSample_(fontSampleEl
, size
, fontSampleEl
.style
.fontFamily
,
141 * Sets the 'minimum_logical_font_size' preference when the user changes the
143 * @param {Event} event Change event.
146 minimumFontSizeChanged_: function(event
) {
147 var size
= this.mapPositionToPref(this.value
);
148 Preferences
.setIntegerPref(
149 'webkit.webprefs.minimum_logical_font_size', size
, true);
154 * Sets the text, font size and font family of the sample text.
155 * @param {Element} el The div containing the sample text.
156 * @param {number} size The font size of the sample text.
157 * @param {string} font The font family of the sample text.
158 * @param {boolean} showSize True if the font size should appear in the
162 setUpFontSample_: function(el
, size
, font
, showSize
) {
163 var prefix
= showSize
? (size
+ ': ') : '';
164 el
.textContent
= prefix
+
165 loadTimeData
.getString('fontSettingsLoremIpsum');
166 el
.style
.fontSize
= size
+ 'px';
168 el
.style
.fontFamily
= font
;
172 * Populates a select list and selects the specified item.
173 * @param {Element} element The select element to populate.
174 * @param {Array} items The array of items from which to populate.
175 * @param {string} selectedValue The selected item.
178 populateSelect_: function(element
, items
, selectedValue
) {
179 // Remove any existing content.
180 element
.textContent
= '';
182 // Insert new child nodes into select element.
183 for (var i
= 0; i
< items
.length
; i
++) {
184 var value
= items
[i
][0];
185 var text
= items
[i
][1];
186 var dir
= items
[i
][2];
188 var selected
= value
== selectedValue
;
189 var option
= new Option(text
, value
, false, selected
);
191 element
.appendChild(option
);
193 element
.appendChild(document
.createElement('hr'));
197 element
.setDisabled('noFontsAvailable', false);
202 FontSettings
.setFontsData = function(fonts
, encodings
, selectedValues
) {
203 FontSettings
.getInstance().populateSelect_($('standard-font-family'), fonts
,
205 FontSettings
.getInstance().populateSelect_($('serif-font-family'), fonts
,
207 FontSettings
.getInstance().populateSelect_($('sans-serif-font-family'),
208 fonts
, selectedValues
[2]);
209 FontSettings
.getInstance().populateSelect_($('fixed-font-family'), fonts
,
211 FontSettings
.getInstance().populateSelect_($('font-encoding'), encodings
,
215 FontSettings
.setUpStandardFontSample = function(font
, size
) {
216 FontSettings
.getInstance().setUpFontSample_($('standard-font-sample'), size
,
220 FontSettings
.setUpSerifFontSample = function(font
, size
) {
221 FontSettings
.getInstance().setUpFontSample_($('serif-font-sample'), size
,
225 FontSettings
.setUpSansSerifFontSample = function(font
, size
) {
226 FontSettings
.getInstance().setUpFontSample_($('sans-serif-font-sample'),
230 FontSettings
.setUpFixedFontSample = function(font
, size
) {
231 FontSettings
.getInstance().setUpFontSample_($('fixed-font-sample'),
235 FontSettings
.setUpMinimumFontSample = function(size
) {
236 // If size is less than 6, represent it as six in the sample to account
237 // for the minimum logical font size.
240 FontSettings
.getInstance().setUpFontSample_($('minimum-font-sample'), size
,
245 * @param {boolean} available Whether the Advanced Font Settings Extension
246 * is installed and enabled.
248 FontSettings
.notifyAdvancedFontSettingsAvailability = function(available
) {
249 $('advanced-font-settings-install').hidden
= available
;
250 $('advanced-font-settings-options').hidden
= !available
;
255 FontSettings
: FontSettings