forked from protz/thunderbird-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestartlessMenuItems.js
More file actions
279 lines (248 loc) · 9.92 KB
/
RestartlessMenuItems.js
File metadata and controls
279 lines (248 loc) · 9.92 KB
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is about:telemetry integration code for Thunderbird
*
* The Initial Developer of the Original Code is
* Jonathan Protzenko <jonathan.protzenko@gmail.com>
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Szabolcs Hubai <szab.hu@gmail.com>
* Mike Hommey <mh@glandium.org>
* Erik Vold <erikvvold@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
* @fileoverview Provides a wrapper for easily adding and
* removing menu items in a restartless fashion.
* @author Jonathan Protzenko
*/
var EXPORTED_SYMBOLS = ["RestartlessMenuItems"];
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm", null);
let _menuItems = [];
function isThunderbird() {
let APP_ID = Services.appinfo.QueryInterface(Ci.nsIXULRuntime).ID;
return APP_ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
}
/**
* Adds a menuitem to a window.
* @param w {nsIDOMWindow} A window to patch.
* @param loadedAlready {bool} The window above is fully loaded,
* or we should wait to be loaded.
* @param options {Object} Options for the <tt>menuitem</tt>, with the following parameters:
* @param options.id {String} (optional) An id for the <tt>menuitem</tt> in taskPopup menu, this should be namespaced, you should either fill id or idAppMenu or both.
* @param options.idAppMenu {String} (optional) An id for the <tt>menuitem</tt> in Appmenu taskPopup menu, this should be namespaced, you should either fill id or idAppMenu or both.
* @param options.label {String} A label for the <tt>menuitem</tt>.
* @param options.url {String} (optional, preferred) An URL where the <tt>oncommand</tt> should navigate to.
* @param options.onCommand {String} (optional) A function callback what the <tt>menuitem</tt>'s oncommand will call.
* @param options.accesskey {String} (optional) An access key for the <tt>menuitem</tt>.
* @param options.key {String} (optional) A shortcut key for the <tt>menuitem</tt>.
* @param options.image {String} (optional) An URL for the <tt>menuitem</tt>.
*/
function monkeyPatchWindow(w, loadedAlready, options) {
let doIt = function() {
let id = options.id;
let idAppMenu = options.idAppMenu;
let taskPopup = w.document.getElementById("taskPopup");
let appMenuPopup = w.document.getElementById("appmenu_taskPopup");
let tabmail = w.document.getElementById("tabmail");
// Check the windows is a mail:3pane
if ((!taskPopup && !appMenuPopup) || !tabmail)
return;
let openTabUrl = function() {
return (options.url) ?
tabmail.openTab("contentTab",
{ contentPage: options.url }
)
: false;
};
let onCmd = function() {
openTabUrl() || options.onCommand && options.onCommand();
};
if (id && taskPopup)
addMenuItem(w, onCmd, options, id, taskPopup);
if (idAppMenu && appMenuPopup)
addMenuItem(w, onCmd, options, idAppMenu, appMenuPopup);
};
if (loadedAlready)
doIt();
else
w.addEventListener("load", doIt);
}
function addMenuItem(w, onCmd, options, id, taskPopup) {
let oldMenuitem = w.document.getElementById(id);
let menuitem = w.document.createElement("menuitem");
menuitem.addEventListener("command", onCmd);
menuitem.setAttribute("label", options.label);
menuitem.setAttribute("id", id);
if (options.accesskey)
menuitem.setAttribute("accesskey", options.accesskey);
if (options.key)
menuitem.setAttribute("key", options.key);
if (options.image) {
menuitem.setAttribute("class", "menuitem-iconic");
menuitem.style.listStyleImage = "url('" + options.image + "')";
}
if (taskPopup) {
if (oldMenuitem)
taskPopup.replaceChild(menuitem, oldMenuitem);
else
taskPopup.appendChild(menuitem);
}
}
/**
* Removes a menuitem from a window.
* @param w {nsIDOMWindow} A window to patch.
* @param options {Object} Options for the <tt>menuitem</tt>, with the following parameter:
* @param options.id {String} An id for the <tt>menuitem</tt>, this should be namespaced.
* @param options.url {String} (optional) An URL for the <tt>menuitem</tt>, tabs with this URL will be closed.
* @param options.onUnload {Function} (optional) A function for the <tt>menuitem</tt>, which redoes all the stuff
* except the removing of menuitem.
*/
function unMonkeyPatchWindow(w, options) {
let id = options.id;
let menuitem = w.document.getElementById(id);
let tabmail = w.document.getElementById("tabmail");
// Remove all menuitem with this id
while (menuitem) {
menuitem.parentNode.removeChild(menuitem);
menuitem = w.document.getElementById(id);
}
// Close all tab with options.url URL
let removeTabUrl = function() {
let tabMode = tabmail.tabModes.contentTab;
let shouldSwitchToFunc = tabMode.shouldSwitchTo ||
tabMode.tabType.shouldSwitchTo;
if (shouldSwitchToFunc) {
let tabIndex = shouldSwitchToFunc.apply(tabMode.tabType, [{ contentPage: options.url }]);
while (tabIndex >= 0) {
tabmail.closeTab(tabIndex, true);
tabIndex = shouldSwitchToFunc.apply(tabMode.tabType, [{ contentPage: options.url }]);
}
}
};
if (options.url)
removeTabUrl();
else
options.onUnload && options.onUnload();
}
/**
* This is Our observer. It catches the newly opened windows
* and tries to run the patcher on them.
* @observes "domwindowopened"
*
* @prop observe An nsIWindowWatcher will notify this method. It will call the {@link monkeyPatchWindow}.
* @prop register Start listening to notifications.
* @prop unregister Stop listening to notifications.
*/
function monkeyPatchWindowObserver() {}
monkeyPatchWindowObserver.prototype = {
observe(aSubject, aTopic, aData) {
if (aTopic == "domwindowopened") {
aSubject.QueryInterface(Ci.nsIDOMWindow);
for (let aMenuItem of _menuItems)
monkeyPatchWindow(aSubject.window, false, aMenuItem);
}
},
register() {
Services.ww.registerNotification(this);
},
unregister() {
Services.ww.unregisterNotification(this);
},
};
/**
* This is the observer Object.
*/
let monkeyPatchFutureWindow = new monkeyPatchWindowObserver();
/**
* This is the public interface of the RestartlessMenuItems module.
*
* @prop add Adds a parameterized <tt>menuitem</tt> to existing and
* newly created windows.
* @prop remove Removes an identified <tt>menuitem</tt> from
* existing window, and will not add to new ones.
* @prop removeAll Removes all the <tt>menuitem</tt>s currently added.
*/
var RestartlessMenuItems = {
add: function _RestartlessMenuItems_add(options) {
// For Thunderbird, since there's no URL bar, we add a menu item to make it
// more discoverable.
if (isThunderbird()) {
// Thunderbird-specific JSM
let {fixIterator} = ChromeUtils.import("resource:///modules/iteratorUtils.jsm", null);
// Push it to our list
_menuItems.push(options);
// Patch all existing windows
for (let w of fixIterator(Services.wm.getEnumerator("mail:3pane"), Ci.nsIDOMWindow)) {
// True means the window's been loaded already, so add the menu item right
// away (the default is: wait for the "load" event).
monkeyPatchWindow(w.window, true, options);
}
// Patch all future windows
// with our list of menuItems
if (_menuItems.length == 1)
monkeyPatchFutureWindow.register();
}
},
remove: function _RestartlessMenuItems_remove(options, keepArray) {
if (isThunderbird()) {
// Find the menuitem in our list by id
let found = false;
let index = -1;
found = _menuItems.some( function isOurMenuItem(element, arrayIndex) {
if (element.id == options.id)
index = arrayIndex;
return (element.id == options.id);
});
// Un-patch all existing windows
if (found) {
let {fixIterator} = ChromeUtils.import("resource:///modules/iteratorUtils.jsm", {});
for (let w of fixIterator(Services.wm.getEnumerator("mail:3pane"))) {
unMonkeyPatchWindow(w, _menuItems[index]);
}
}
if (!keepArray) {
// Pop out from our list
if (found)
_menuItems.splice(index, 1);
// Stop patching future windows if our list is empty
if (_menuItems.length == 0)
monkeyPatchFutureWindow.unregister();
}
}
},
removeAll: function _RestartlessMenuItems_removeAll() {
if (isThunderbird()) {
// Remove all added menuitems
for (let aMenuItem of _menuItems)
this.remove(aMenuItem, true);
_menuItems = [];
monkeyPatchFutureWindow.unregister();
}
},
};