Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Back to the roots #4

Merged
merged 5 commits into from Feb 19, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
266 changes: 266 additions & 0 deletions RestartlessMenuItems.js
@@ -0,0 +1,266 @@
/* ***** 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
*/

let EXPORTED_SYMBOLS = ['RestartlessMenuItems'];

const {classes: Cc, interfaces: Ci, utils: Cu} = Components;

Cu.import("resource://gre/modules/Services.jsm");

let _menuItems = [];

function isThunderbird()
{
let APP_ID = Services.appinfo.QueryInterface(Ci.nsIXULRuntime).ID;
return APP_ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
}

var global = this;

/**
* 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} An id for the <tt>menuitem</tt>, this should be namespaced.
* @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 taskPopup = w.document.getElementById("taskPopup");
let tabmail = w.document.getElementById("tabmail");
let oldMenuitem = w.document.getElementById(id);

// Check the windows is a mail:3pane
if (!taskPopup || !tabmail)
return;

let openTabUrl = function() {
return (options.url) ?
tabmail.openTab("contentTab",
{ contentPage: options.url }
)
: false;
};

let onCmd = function() {
openTabUrl() || options.onCommand && options.onCommand();
};

let menuitem = w.document.createElement("menuitem");
menuitem.addEventListener("command", onCmd, false);
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 (!oldMenuitem)
taskPopup.appendChild(menuitem);
else
taskPopup.replaceChild(menuitem, oldMenuitem);
};
if (loadedAlready)
doIt();
else
w.addEventListener("load", doIt, false);
}

/**
* 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: function (aSubject, aTopic, aData) {
if (aTopic == "domwindowopened") {
aSubject.QueryInterface(Ci.nsIDOMWindow);
for each (let aMenuItem in _menuItems)
monkeyPatchWindow(aSubject.window, false, aMenuItem);
}
},
register: function() {
Services.ww.registerNotification(this);
},
unregister: function() {
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
Cu.import("resource:///modules/iteratorUtils.jsm", global);

// Push it to our list
_menuItems.push(options);

// Patch all existing windows
for each (let w in 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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a function.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The remove function needs serious cleanup...

// Find the menuitem by id
let index = -1;
_menuItems.filter( function isOurMenuItem (element, arrayIndex){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter returns a new array, it doesn't modify the original one in-place... so I don't understand what your code is doing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. I wanted to search for an item in the Array here.
At first priority I wanted to use Array.length in the add to correctly decide when to register the observer.
My second priority was the easy search - to remove menuitems by their id. So I tried the associated Array,
but sadly Array.length and someArray["menuitemID"] are opposite of each other.

Re-read the code again, a simple linear search will do the job here - with Array.some.
Could You show me a preferred way to search in Array?

if (element.id == options.id)
index = arrayIndex;
return (element.id == options.id);
});

// Un-patch all existing windows
if (index != -1)
for each (let w in fixIterator(Services.wm.getEnumerator("mail:3pane")))
unMonkeyPatchWindow(w, _menuItems[index]);

if (!keepArray) {
// Pop out from our list
if (index != -1)
_menuItems.splice(index, 1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

splice also returns a new value...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I wanted just to delete from Array the previously removed menuitem element.
I found this method here.
Could you show a preferred method for this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Array.shift] ? Really? But why? :)

I would like to remove an element from a specified index, not the first!
As I read, shift (and pop) removes one element from the near (or the far) end of Array.

But [Array.splice] removes element from the needed position. See the "After removing 1:" Example at MDN!
I would like to do exactly that: searching for an element, and remove it!

Of course I could use [Array.shift], but then a temporary Array and a loop is needed too.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no sorry I misread the order of the arguments :)


// Stop patching future windows if our list is empty
if (_menuItems.length == 0)
monkeyPatchFutureWindow.unregister();
}
}
},

removeAll: function _RestartlessMenuItems_removeAll () {
if (isThunderbird) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In removeAll I wanted to reuse the remove function. Therefore remove shouldn't always pop the required menuitem from the Array,
because removeAll's loop would mess up.
Therefore I don't care about Array.slice()'s new value in remove. Of course it's Array.filter() was overthinked. :)

// Remove all added menuitems
for each (let aMenuItem in _menuItems)
this.remove(aMenuItem, true);
_menuItems = [];
monkeyPatchFutureWindow.unregister();
}
},

};