• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5'use strict';
6
7/**
8 * @fileoverview PendingChanges class tracks changes to be applied when an
9 * "Apply Changes" button is clicked.
10 */
11
12/**
13 * Creates a PendingChanges object with no pending changes.
14 *
15 * @constructor
16 */
17var PendingChanges = function() {
18  // Format: pendingFontChanges_.Cyrl.sansserif = "My SansSerif Cyrillic Font"
19  this.pendingFontChanges_ = {};
20
21  // Format: pendingFontSizeChanges_.defaultFontSize = 12
22  this.pendingFontSizeChanges_ = {};
23};
24
25/**
26 * Returns the pending font setting change for the specified script and family,
27 * or null if it doesn't exist.
28 *
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.
33 */
34PendingChanges.prototype.getFont = function(script, genericFamily) {
35  if (this.pendingFontChanges_[script])
36    return this.pendingFontChanges_[script][genericFamily];
37  return null;
38};
39
40/**
41 * Returns the pending font size setting change, or null if it doesn't exist.
42 *
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
46 *     doesn't exist.
47 */
48PendingChanges.prototype.getFontSize = function(fontSizeKey) {
49  return this.pendingFontSizeChanges_[fontSizeKey];
50};
51
52/**
53 * Sets the pending font change for the specified script and family.
54 *
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.
58 */
59PendingChanges.prototype.setFont = function(script, genericFamily, font) {
60  if (!this.pendingFontChanges_[script])
61    this.pendingFontChanges_[script] = {};
62  if (this.pendingFontChanges_[script][genericFamily] == font)
63    return;
64  this.pendingFontChanges_[script][genericFamily] = font;
65};
66
67/**
68 * Sets the pending font size change.
69 *
70 * @param {string} fontSizeKey The font size setting key. See
71 *     getFontSize().
72 * @param {number} size The font size to set the setting to.
73 */
74PendingChanges.prototype.setFontSize = function(fontSizeKey, size) {
75  if (this.pendingFontSizeChanges_[fontSizeKey] == size)
76    return;
77  this.pendingFontSizeChanges_[fontSizeKey] = size;
78};
79
80/**
81 * Commits the pending changes to Chrome. After this function is called, there
82 * are no pending changes.
83 */
84PendingChanges.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];
88      if (fontId == null)
89        continue;
90      var details = {};
91      details.script = script;
92      details.genericFamily = genericFamily;
93      details.fontId = fontId;
94      chrome.fontSettings.setFont(details);
95    }
96  }
97
98  var size = this.pendingFontSizeChanges_['defaultFontSize'];
99  if (size != null)
100    chrome.fontSettings.setDefaultFontSize({pixelSize: size});
101
102  size = this.pendingFontSizeChanges_['defaultFixedFontSize'];
103  if (size != null)
104    chrome.fontSettings.setDefaultFixedFontSize({pixelSize: size});
105
106  size = this.pendingFontSizeChanges_['minFontSize'];
107  if (size != null)
108    chrome.fontSettings.setMinimumFontSize({pixelSize: size});
109
110  this.clear();
111};
112
113/**
114 * Clears the pending font changes for a single script.
115 *
116 * @param {string} script The script code, like "Cyrl".
117 */
118PendingChanges.prototype.clearOneScript = function(script) {
119  this.pendingFontChanges_[script] = {};
120};
121
122/**
123 * Clears all pending font changes.
124 */
125PendingChanges.prototype.clear = function() {
126  this.pendingFontChanges_ = {};
127  this.pendingFontSizeChanges_ = {};
128};
129
130/**
131 * @return {boolean} True if there are no pending changes, otherwise false.
132 */
133PendingChanges.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)
137        return false;
138    }
139  }
140  for (var name in this.pendingFontSizeChanges_) {
141    if (this.pendingFontSizeChanges_[name] != null)
142      return false;
143  }
144  return true;
145};
146