OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 */ |
| 17 var 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 */ |
| 34 PendingChanges.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 */ |
| 48 PendingChanges.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 */ |
| 59 PendingChanges.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 */ |
| 74 PendingChanges.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 */ |
| 84 PendingChanges.prototype.apply = function() { |
| 85 for (var script in this.pendingFontChanges_) { |
| 86 for (var genericFamily in this.pendingFontChanges_[script]) { |
| 87 var details = {}; |
| 88 details.script = script; |
| 89 details.genericFamily = genericFamily; |
| 90 details.fontId = this.pendingFontChanges_[script][genericFamily]; |
| 91 chrome.fontSettings.setFont(details); |
| 92 } |
| 93 } |
| 94 |
| 95 var size = this.pendingFontSizeChanges_['defaultFontSize']; |
| 96 if (size != null) |
| 97 chrome.fontSettings.setDefaultFontSize({pixelSize: size}); |
| 98 |
| 99 size = this.pendingFontSizeChanges_['defaultFixedFontSize']; |
| 100 if (size != null) |
| 101 chrome.fontSettings.setDefaultFixedFontSize({pixelSize: size}); |
| 102 |
| 103 size = this.pendingFontSizeChanges_['minFontSize']; |
| 104 if (size != null) |
| 105 chrome.fontSettings.setMinimumFontSize({pixelSize: size}); |
| 106 |
| 107 this.clear(); |
| 108 }; |
| 109 |
| 110 /** |
| 111 * Clears the pending font changes for a single script. |
| 112 * |
| 113 * @param {string} script The script code, like "Cyrl". |
| 114 */ |
| 115 PendingChanges.prototype.clearOneScript = function(script) { |
| 116 this.pendingFontChanges_[script] = {}; |
| 117 }; |
| 118 |
| 119 /** |
| 120 * Clears all pending font changes. |
| 121 */ |
| 122 PendingChanges.prototype.clear = function() { |
| 123 this.pendingFontChanges_ = {}; |
| 124 this.pendingFontSizeChanges_ = {}; |
| 125 }; |
| 126 |
| 127 /** |
| 128 * @return {boolean} True if there are no pending changes, otherwise false. |
| 129 */ |
| 130 PendingChanges.prototype.isEmpty = function() { |
| 131 for (var script in this.pendingFontChanges_) { |
| 132 for (var genericFamily in this.pendingFontChanges_[script]) { |
| 133 if (this.pendingFontChanges_[script][genericFamily] != null) |
| 134 return false; |
| 135 } |
| 136 } |
| 137 for (var name in this.pendingFontSizeChanges_) { |
| 138 if (this.pendingFontSizeChanges_[name] != null) |
| 139 return false; |
| 140 } |
| 141 return true; |
| 142 }; |
OLD | NEW |