forked from ericu/jv-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharedsettings.js
218 lines (207 loc) · 6.11 KB
/
sharedsettings.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
function JvBoolPref(containerId, trueSuffix, falseSuffix, prefName, varName,
onUpdate) {
this.containerId = "jv-settings-" + containerId;
this.trueId = this.containerId + trueSuffix;
this.falseId = this.containerId + falseSuffix;
this.prefName = prefName;
this.varName = varName;
this.onUpdate = onUpdate;
this.isBool = true;
return this;
}
function JvStringPref(containerId, prefName, varName, onUpdate) {
this.containerId = "jv-settings-" + containerId;
this.prefName = prefName;
this.varName = varName;
this.onUpdate = onUpdate;
this.isBool = false;
return this;
}
var prefs = [
new JvBoolPref(
"tab-handling",
"-ignore", "-insert",
"tab.handling.ignore",
"neverHandleTab",
null),
new JvBoolPref(
"default-mode",
"-insert", "-normal",
"default.mode.insert",
"defaultModeInsert",
null),
new JvBoolPref(
"undo-mode",
"-vi", "-vim",
"undo.mode.vi",
"undoModeVi",
null),
new JvBoolPref(
"visual-bell",
"-inhibit", "-allow",
"visual.bell.inhibit",
"inhibitVisualBell",
null),
new JvBoolPref(
"status-bar",
"-show", "-hide",
"status.bar.show",
"showStatusBar",
function (jsvim) {
if (jsvim.showStatusBar) {
jsvim.setUpStatusBar();
} else {
jsvim.removeStatusBar();
}
}),
new JvBoolPref(
"change-textarea-appearance", "-on", "-off",
"change.textarea.appearance", "changeTextareaAppearance", null),
new JvBoolPref(
"change-divhack-appearance", "-on", "-off",
"change.divhack.appearance", "changeDivhackAppearance", null),
new JvBoolPref(
"debugging-asserts",
"-popup", "-ignore",
"debugging.asserts.popup",
"popupOnAssert",
null),
new JvBoolPref(
"disallowed-just-eat-esc", "-on", "-off",
"disallowed.justeatesc", "disallowedJustEatEsc", null),
new JvStringPref(
"disallowed-host-patterns",
"disallowed.host.patterns", "disallowedHostPatterns", null),
new JvBoolPref(
"divhack-disabled", "-on", "-off",
"divhack.disabled", "divhackDisabled", null),
new JvBoolPref(
"divhack-just-eat-esc", "-on", "-off",
"divhack.justeatesc", "divhackJustEatEsc", null),
new JvBoolPref(
"divhack-allow-html", "-on", "-off",
"divhack.allowhtml", "divhackAllowEditingAboveHTML", null),
new JvBoolPref(
"divhack-debug", "-on", "-off",
"divhack.debug", "divhackDebug", null),
new JvBoolPref(
"divhack-disable-status-bar", "-on", "-off",
"divhack.disablestatusbar", "divhackDisableStatusBar", null),
];
// This chrome prefs stuff shouldn't be shared; there should be a server side
// and a client side. Most of the existing code should be server-side. The
// notification on changes will be very custom--the call to setUpStatusBar, for
// example.
function getPrefs() {
if (isChrome()) {
if (!window.jvPrefs) {
window.jvPrefs = {
prefSet : function(prefName) {
return prefName in localStorage;
},
getBoolPref : function(prefName) {
var val = localStorage[prefName];
return typeof(val) == "string" ? (val == "true") : val;
},
setBoolPref : function(prefName, value) {
localStorage[prefName] =
(typeof(value) == "string") ? (value == "true") : value;
},
getStringPref : function(prefName) {
return localStorage[prefName];
},
setStringPref : function(prefName, value) {
localStorage[prefName] = value;
}
};
}
return window.jvPrefs;
} else {
var prefsvc = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefService);
return prefsvc.getBranch("extensions.jv.");
}
}
function getEnabledPref() {
// Default to on when installed.
if (isChrome() && !getPrefs().prefSet("enabled"))
return true;
return getPrefs().getBoolPref("enabled");
}
function saveEnabledPref(enabled) {
getPrefs().setBoolPref("enabled", enabled);
}
function isEnabled() {
if (isChrome()) {
if (!jsvim.enabled) {
return false;
}
} else if (!getEnabledPref()) {
return false;
}
return this.lenabled;
}
// Not used in Chrome.
function updateEditorPrefs() {
var i;
popup("in updateEditorPrefs");
if (!this.e) {
// disabled
return;
}
if (!this.updateEpoch) {
this.updateEpoch = 1;
}
if (this.updateEpoch != this.getVar(this.VarNames.UPDATE_EPOCH)) {
for (i = 0; i < prefs.length; ++i) {
var pref = prefs[i];
this[prefs[i].varName] =
(pref.isBool) ?
getPrefs().getBoolPref(prefs[i].prefName) :
getPrefs().getStringPref(prefs[i].prefName);
if (prefs[i].onUpdate) {
prefs[i].onUpdate(this);
}
}
this.setVar(this.VarNames.UPDATE_EPOCH, this.updateEpoch);
}
}
function notifyAll(enabled) {
try {
// Enabled is already stored, so we don't need to send it.
if (isChrome()) {
chrome.extension.sendRequest({"message" : "preferencesChanged"});
return;
}
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var enumerator = wm.getEnumerator("navigator:browser");
if (enumerator) {
while (enumerator.hasMoreElements()) {
var w = enumerator.getNext();
if (w && w.jsvim) {
var epoch = w.jsvim.updateEpoch;
if (!epoch) {
epoch = 1;
}
w.jsvim.updateEpoch = epoch + 1;
w.jsvim.updatePopup();
w.jsvim.updateIcon();
w.jsvim.updateEditorPrefs();
if (!enabled) {
w.removeEventListener("focus", w.jsvim, true);
w.removeEventListener("keypress", w.jsvim, true);
w.removeEventListener("click", w.jsvim, true);
} else {
w.addEventListener("focus", w.jsvim, true);
w.addEventListener("keypress", w.jsvim, true);
w.addEventListener("click", w.jsvim, true);
}
}
}
}
} catch (ex) {
// TODO: Should this be a popup?
_debug(stringifyObj(ex));
}
}