forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pending_changes.js
145 lines (129 loc) · 4.41 KB
/
pending_changes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview PendingChanges class tracks changes to be applied when an
* "Apply Changes" button is clicked.
*/
/**
* Creates a PendingChanges object with no pending changes.
*
* @constructor
*/
var PendingChanges = function() {
// Format: pendingFontChanges_.Cyrl.sansserif = "My SansSerif Cyrillic Font"
this.pendingFontChanges_ = {};
// Format: pendingFontSizeChanges_.defaultFontSize = 12
this.pendingFontSizeChanges_ = {};
};
/**
* Returns the pending font setting change for the specified script and family,
* or null if it doesn't exist.
*
* @param {string} script The script code, like "Cyrl".
* @param {string} genericFamily The generic family, like "sansserif".
* @return {?string} The pending font setting, like "My Cyrillic SansSerif Font"
* or null if it doesn't exist.
*/
PendingChanges.prototype.getFont = function(script, genericFamily) {
if (this.pendingFontChanges_[script])
return this.pendingFontChanges_[script][genericFamily];
return null;
};
/**
* Returns the pending font size setting change, or null if it doesn't exist.
*
* @param {string} fontSizeKey The font size setting key. One of
* 'defaultFontSize', 'defaultFixedFontSize', or 'minFontSize'.
* @return {?number} The pending font size setting in pixels, or null if it
* doesn't exist.
*/
PendingChanges.prototype.getFontSize = function(fontSizeKey) {
return this.pendingFontSizeChanges_[fontSizeKey];
};
/**
* Sets the pending font change for the specified script and family.
*
* @param {string} script The script code, like "Cyrl".
* @param {string} genericFamily The generic family, like "sansserif".
* @param {?string} font The font to set the setting to, or null to clear it.
*/
PendingChanges.prototype.setFont = function(script, genericFamily, font) {
if (!this.pendingFontChanges_[script])
this.pendingFontChanges_[script] = {};
if (this.pendingFontChanges_[script][genericFamily] == font)
return;
this.pendingFontChanges_[script][genericFamily] = font;
};
/**
* Sets the pending font size change.
*
* @param {string} fontSizeKey The font size setting key. See
* getFontSize().
* @param {number} size The font size to set the setting to.
*/
PendingChanges.prototype.setFontSize = function(fontSizeKey, size) {
if (this.pendingFontSizeChanges_[fontSizeKey] == size)
return;
this.pendingFontSizeChanges_[fontSizeKey] = size;
};
/**
* Commits the pending changes to Chrome. After this function is called, there
* are no pending changes.
*/
PendingChanges.prototype.apply = function() {
for (var script in this.pendingFontChanges_) {
for (var genericFamily in this.pendingFontChanges_[script]) {
var fontId = this.pendingFontChanges_[script][genericFamily];
if (fontId == null)
continue;
var details = {};
details.script = script;
details.genericFamily = genericFamily;
details.fontId = fontId;
chrome.fontSettings.setFont(details);
}
}
var size = this.pendingFontSizeChanges_['defaultFontSize'];
if (size != null)
chrome.fontSettings.setDefaultFontSize({pixelSize: size});
size = this.pendingFontSizeChanges_['defaultFixedFontSize'];
if (size != null)
chrome.fontSettings.setDefaultFixedFontSize({pixelSize: size});
size = this.pendingFontSizeChanges_['minFontSize'];
if (size != null)
chrome.fontSettings.setMinimumFontSize({pixelSize: size});
this.clear();
};
/**
* Clears the pending font changes for a single script.
*
* @param {string} script The script code, like "Cyrl".
*/
PendingChanges.prototype.clearOneScript = function(script) {
this.pendingFontChanges_[script] = {};
};
/**
* Clears all pending font changes.
*/
PendingChanges.prototype.clear = function() {
this.pendingFontChanges_ = {};
this.pendingFontSizeChanges_ = {};
};
/**
* @return {boolean} True if there are no pending changes, otherwise false.
*/
PendingChanges.prototype.isEmpty = function() {
for (var script in this.pendingFontChanges_) {
for (var genericFamily in this.pendingFontChanges_[script]) {
if (this.pendingFontChanges_[script][genericFamily] != null)
return false;
}
}
for (var name in this.pendingFontSizeChanges_) {
if (this.pendingFontSizeChanges_[name] != null)
return false;
}
return true;
};