1 // Copyright 2013 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.
8 * @fileoverview PendingChanges class tracks changes to be applied when an
9 * "Apply Changes" button is clicked.
13 * Creates a PendingChanges object with no pending changes.
17 var PendingChanges = function() {
18 // Format: pendingFontChanges_.Cyrl.sansserif = "My SansSerif Cyrillic Font"
19 this.pendingFontChanges_ = {};
21 // Format: pendingFontSizeChanges_.defaultFontSize = 12
22 this.pendingFontSizeChanges_ = {};
26 * Returns the pending font setting change for the specified script and family,
27 * or null if it doesn't exist.
29 * @param {string} script The script code, like "Cyrl".
30 * @param {string} genericFamily The generic family, like "sansserif".
31 * @return {?string} The pending font setting, like "My Cyrillic SansSerif Font"
32 * or null if it doesn't exist.
34 PendingChanges.prototype.getFont = function(script, genericFamily) {
35 if (this.pendingFontChanges_[script])
36 return this.pendingFontChanges_[script][genericFamily];
41 * Returns the pending font size setting change, or null if it doesn't exist.
43 * @param {string} fontSizeKey The font size setting key. One of
44 * 'defaultFontSize', 'defaultFixedFontSize', or 'minFontSize'.
45 * @return {?number} The pending font size setting in pixels, or null if it
48 PendingChanges.prototype.getFontSize = function(fontSizeKey) {
49 return this.pendingFontSizeChanges_[fontSizeKey];
53 * Sets the pending font change for the specified script and family.
55 * @param {string} script The script code, like "Cyrl".
56 * @param {string} genericFamily The generic family, like "sansserif".
57 * @param {?string} font The font to set the setting to, or null to clear it.
59 PendingChanges.prototype.setFont = function(script, genericFamily, font) {
60 if (!this.pendingFontChanges_[script])
61 this.pendingFontChanges_[script] = {};
62 if (this.pendingFontChanges_[script][genericFamily] == font)
64 this.pendingFontChanges_[script][genericFamily] = font;
68 * Sets the pending font size change.
70 * @param {string} fontSizeKey The font size setting key. See
72 * @param {number} size The font size to set the setting to.
74 PendingChanges.prototype.setFontSize = function(fontSizeKey, size) {
75 if (this.pendingFontSizeChanges_[fontSizeKey] == size)
77 this.pendingFontSizeChanges_[fontSizeKey] = size;
81 * Commits the pending changes to Chrome. After this function is called, there
82 * are no pending changes.
84 PendingChanges.prototype.apply = function() {
85 for (var script in this.pendingFontChanges_) {
86 for (var genericFamily in this.pendingFontChanges_[script]) {
87 var fontId = this.pendingFontChanges_[script][genericFamily];
91 details.script = script;
92 details.genericFamily = genericFamily;
93 details.fontId = fontId;
94 chrome.fontSettings.setFont(details);
98 var size = this.pendingFontSizeChanges_['defaultFontSize'];
100 chrome.fontSettings.setDefaultFontSize({pixelSize: size});
102 size = this.pendingFontSizeChanges_['defaultFixedFontSize'];
104 chrome.fontSettings.setDefaultFixedFontSize({pixelSize: size});
106 size = this.pendingFontSizeChanges_['minFontSize'];
108 chrome.fontSettings.setMinimumFontSize({pixelSize: size});
114 * Clears the pending font changes for a single script.
116 * @param {string} script The script code, like "Cyrl".
118 PendingChanges.prototype.clearOneScript = function(script) {
119 this.pendingFontChanges_[script] = {};
123 * Clears all pending font changes.
125 PendingChanges.prototype.clear = function() {
126 this.pendingFontChanges_ = {};
127 this.pendingFontSizeChanges_ = {};
131 * @return {boolean} True if there are no pending changes, otherwise false.
133 PendingChanges.prototype.isEmpty = function() {
134 for (var script in this.pendingFontChanges_) {
135 for (var genericFamily in this.pendingFontChanges_[script]) {
136 if (this.pendingFontChanges_[script][genericFamily] != null)
140 for (var name in this.pendingFontSizeChanges_) {
141 if (this.pendingFontSizeChanges_[name] != null)