/
options.js
402 lines (325 loc) · 12.4 KB
/
options.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* See license.txt for terms of usage */
define([
"firebug/lib/events",
"firebug/lib/trace"
],
function factoryOptions(Events, FBTrace) {
// ********************************************************************************************* //
// Constants
const Cc = Components.classes;
const Ci = Components.interfaces;
const nsIPrefBranch = Ci.nsIPrefBranch;
const nsIPrefBranch2 = Ci.nsIPrefBranch2;
const PrefService = Cc["@mozilla.org/preferences-service;1"];
const nsIPrefService = Ci.nsIPrefService;
const prefService = PrefService.getService(nsIPrefService);
const prefs = PrefService.getService(nsIPrefBranch2);
const getPref = Components.utils.import("resource://firebug/loader.js", {}).FirebugLoader.getPref;
const prefNames = // XXXjjb TODO distribute to modules
[
// Global
"defaultPanelName", "throttleMessages", "textSize", "showInfoTips",
"commandEditor", "textWrapWidth", "framePosition", "showErrorCount",
"activateSameOrigin", "allPagesActivation", "hiddenPanels",
"panelTabMinWidth", "sourceLinkLabelWidth", "currentVersion",
"useDefaultLocale", "toolbarCustomizationDone", "addonBarOpened",
"showBreakNotification", "showStatusIcon", "stringCropLength",
"showFirstRunPage",
// Search
"searchCaseSensitive", "searchGlobal", "searchUseRegularExpression",
"netSearchHeaders", "netSearchParameters", "netSearchResponseBody",
// Console
"showJSErrors", "showJSWarnings", "showCSSErrors", "showXMLErrors",
"showChromeErrors", "showChromeMessages", "showExternalErrors",
"showXMLHttpRequests", "showNetworkErrors", "tabularLogMaxHeight",
"consoleFilterTypes", "alwaysShowCommandLine",
// HTML
"showFullTextNodes", "showCommentNodes",
"showTextNodesWithWhitespace", "entityDisplay",
"highlightMutations", "expandMutations", "scrollToMutations", "shadeBoxModel",
"showQuickInfoBox", "displayedAttributeValueLimit", "multiHighlightLimit",
// CSS
"onlyShowAppliedStyles",
"showUserAgentCSS",
"expandShorthandProps",
"cssEditMode",
"colorDisplay",
// Computed
"computedStylesDisplay",
"showMozillaSpecificStyles",
// Script
"decompileEvals", "replaceTabs", "maxScriptLineLength",
// DOM
"showUserProps", "showUserFuncs", "showDOMProps", "showDOMFuncs", "showDOMConstants",
"ObjectShortIteratorMax", "showEnumerableProperties", "showOwnProperties",
"showInlineEventHandlers",
// Layout
"showRulers",
// Net
"netFilterCategory", "netDisplayedResponseLimit",
"netDisplayedPostBodyLimit", "netPhaseInterval", "sizePrecision",
"netParamNameLimit", "netShowPaintEvents", "netShowBFCacheResponses",
"netHtmlPreviewHeight",
// JSON Preview
"sortJsonPreview",
// Stack
"omitObjectPathStack",
"showStackTrace", // Console
"filterSystemURLs", // Stack
"breakOnErrors", "trackThrowCatch" // Script
];
var optionUpdateMap = {};
// ********************************************************************************************* //
/**
* Interface to preference storage.
* Panels send commands to request option change.
* Backend responds with events when the change is accepted.
*/
var Options =
/** @lends Options */
{
getPrefDomain: function()
{
return this.prefDomain;
},
initialize: function(prefDomain)
{
this.prefDomain = prefDomain;
if (FBTrace.DBG_INITIALIZE)
FBTrace.sysout("options.initialize with prefDomain " + this.prefDomain);
this.initializePrefs();
},
shutdown: function()
{
prefs.removeObserver(this.prefDomain, this, false);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Custom Listeners
listeners: [],
addListener: function(listener)
{
this.listeners.push(listener);
},
removeListener: function(listener)
{
for (var i=0; i<this.listeners.length; ++i)
if (this.listeners[i] == listener)
return this.listeners.splice(i, 1);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// nsIPrefObserver
observe: function(subject, topic, data)
{
if (data.indexOf(Options.prefDomain) === -1)
return;
var name = data.substr(Options.prefDomain.length+1); // +1 for .
var value = this.get(name);
if (FBTrace.DBG_OPTIONS)
FBTrace.sysout("options.observe name = value: "+name+"= "+value+"\n");
this.updatePref(name, value);
},
updatePref: function(name, value)
{
// Prevent infinite recursion due to pref observer
if (optionUpdateMap.hasOwnProperty(name))
return;
try
{
optionUpdateMap[name] = 1;
Firebug[name] = value;
Events.dispatch(this.listeners, "updateOption", [name, value]);
}
catch (err)
{
if (FBTrace.DBG_OPTIONS || FBTrace.DBG_ERRORS)
FBTrace.sysout("options.updatePref EXCEPTION:" + err, err);
}
finally
{
delete optionUpdateMap[name];
}
if (FBTrace.DBG_OPTIONS)
FBTrace.sysout("options.updatePref EXIT: "+name+"="+value+"\n");
},
register: function(name, value)
{
var currentValue = this.getPref(this.prefDomain, name);
if (FBTrace.DBG_INITIALIZE)
FBTrace.sysout("registerPreference "+name+" -> "+value+" type "+typeof(value)+
" with currentValue "+currentValue);
if (currentValue === undefined)
{
// https://developer.mozilla.org/en/Code_snippets/Preferences
// This is the reason why you should usually pass strings ending with a dot to
// getBranch(), like prefs.getBranch("accessibility.").
var defaultBranch = prefService.getDefaultBranch(this.prefDomain+"."); //
var type = this.getPreferenceTypeByExample(typeof(value));
if (this.setPreference(name, value, type, defaultBranch))
return true;
}
return false;
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Options
// TODO support per context options eg break on error
initializePrefs: function()
{
for (var i = 0; i < prefNames.length; ++i)
Firebug[prefNames[i]] = this.getPref(this.prefDomain, prefNames[i]);
prefs.addObserver(this.prefDomain, this, false);
var basePrefNames = prefNames.length;
for (var i = basePrefNames; i < prefNames.length; ++i)
Firebug[prefNames[i]] = this.getPref(this.prefDomain, prefNames[i]);
if (FBTrace.DBG_OPTIONS)
{
for (var i = 0; i < prefNames.length; ++i)
{
FBTrace.sysout("options.initialize option "+this.prefDomain+"."+prefNames[i]+"="+
Firebug[prefNames[i]]+"\n");
}
}
},
togglePref: function(name)
{
this.setPref(Options.prefDomain, name, !Firebug[name]);
},
get: function(name)
{
return Options.getPref(this.prefDomain, name);
},
getPref: getPref,
set: function(name, value)
{
Options.setPref(Options.prefDomain, name, value);
},
/**
* Set a preference value.
*
* @param prefDomain, e.g. "extensions.firebug"
* @param name Name of the preference (the part after prfDomain without dot)
* @param value New value for the preference.
* @param prefType optional pref type useful when adding a new preference.
*/
setPref: function(prefDomain, name, value, prefType)
{
var prefName = prefDomain + "." + name;
var type = this.getPreferenceTypeByExample((prefType ? prefType : typeof(value)));
if (!this.setPreference(prefName, value, type, prefs))
return;
if (FBTrace.DBG_OPTIONS)
FBTrace.sysout("options.setPref type="+type+" name="+prefName+" value="+value);
},
setPreference: function(prefName, value, type, prefBranch)
{
if (FBTrace.DBG_OPTIONS)
FBTrace.sysout("setPreference "+prefName, {prefName: prefName, value: value});
if (type == nsIPrefBranch.PREF_STRING)
prefBranch.setCharPref(prefName, value);
else if (type == nsIPrefBranch.PREF_INT)
prefBranch.setIntPref(prefName, value);
else if (type == nsIPrefBranch.PREF_BOOL)
prefBranch.setBoolPref(prefName, value);
else if (type == nsIPrefBranch.PREF_INVALID)
{
FBTrace.sysout("options.setPref FAILS: Invalid preference "+prefName+" with type "+
type+", check that it is listed in defaults/prefs.js");
return false;
}
return true;
},
getPreferenceTypeByExample: function(prefType)
{
if (prefType)
{
if (prefType === typeof("s"))
var type = nsIPrefBranch.PREF_STRING;
else if (prefType === typeof(1))
var type = nsIPrefBranch.PREF_INT;
else if (prefType === typeof (true))
var type = nsIPrefBranch.PREF_BOOL;
else
var type = nsIPrefBranch.PREF_INVALID;
}
else
{
var type = prefs.getPrefType(prefName);
}
return type;
},
getPreferenceTypeName: function(prefType)
{
if (prefType == Ci.nsIPrefBranch.PREF_STRING)
return "string";
else if (prefType == Ci.nsIPrefBranch.PREF_INT)
return "int";
else if (prefType == Ci.nsIPrefBranch.PREF_BOOL)
return "boolean";
},
clearPref: function(prefDomain, name)
{
var prefName = prefDomain + "." + name;
if (prefs.prefHasUserValue(prefName))
prefs.clearUserPref(prefName);
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Firebug UI text zoom
changeTextSize: function(amt)
{
var textSize = Options.get("textSize");
var newTextSize = textSize + amt;
if ((newTextSize < 0 && Math.abs(newTextSize) < this.negativeZoomFactors.length) ||
(newTextSize >= 0 && textSize+amt < this.positiveZoomFactors.length))
{
this.setTextSize(textSize+amt);
}
},
setTextSize: function(value)
{
var setValue = value;
if (value >= this.positiveZoomFactors.length)
setValue = this.positiveZoomFactors[this.positiveZoomFactors.length-1];
else if (value < 0 && Math.abs(value) >= this.negativeZoomFactors.length)
setValue = this.negativeZoomFactors[this.negativeZoomFactors.length-1];
this.set("textSize", setValue);
},
positiveZoomFactors: [1, 1.1, 1.2, 1.3, 1.5, 2, 3],
negativeZoomFactors: [1, 0.95, 0.8, 0.7, 0.5],
getZoomByTextSize: function(value)
{
var zoom = value >= 0 ? this.positiveZoomFactors[value] :
this.negativeZoomFactors[Math.abs(value)];
return zoom;
},
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
/**
* Resets all Firebug options to default state. Note that every option
* starting with "extensions.firebug" is considered as a Firebug option.
*
* Options starting with DBG_ are intended for Firebug Tracing Console (FBTrace)
* and ignored (their state is not changed).
*/
resetAllOptions: function()
{
var preferences = prefs.getChildList("extensions.firebug", {});
for (var i = 0; i < preferences.length; i++)
{
if (preferences[i].indexOf("DBG_") == -1)
{
if (FBTrace.DBG_OPTIONS)
FBTrace.sysout("Clearing option: " + i + ") " + preferences[i]);
if (prefs.prefHasUserValue(preferences[i])) // avoid exception
prefs.clearUserPref(preferences[i]);
}
else
{
if (FBTrace.DBG_OPTIONS)
FBTrace.sysout("Skipped clearing option: " + i + ") " + preferences[i]);
}
}
},
};
// ********************************************************************************************* //
// Registration
return Options;
// ********************************************************************************************* //
});