TagSpaces is an offline, open source, document manager with tagging support
Imporoving the firefox's toolbar button integration
uggrock6c347642c304
diff --git a/data/js/settings.api.js b/data/js/settings.api.jsindex a76b154ea..5d23e13db 100644--- a/data/js/settings.api.js+++ b/data/js/settings.api.js@@ -1,4 +1,4 @@-/* Copyright (c) 2012 The Tagspaces Authors. All rights reserved.+/* Copyright (c) 2012-2013 The TagSpaces Authors. All rights reserved.* Use of this source code is governed by a AGPL3 license that* can be found in the LICENSE file. */define(function(require, exports, module) {diff --git a/lib/listen.js b/lib/listen.jsnew file mode 100644index 000000000..65c7eb2db--- /dev/null+++ b/lib/listen.js@@ -0,0 +1,62 @@+/* ***** 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 Home Dash Utility.+ *+ * The Initial Developer of the Original Code is The Mozilla Foundation.+ * Portions created by the Initial Developer are Copyright (C) 2011+ * the Initial Developer. All Rights Reserved.+ *+ * Contributor(s):+ * Edward Lee <[email protected]>+ * Erik Vold <[email protected]>+ *+ * 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 ***** */++"use strict";++var {unload} = require("unload+");++/**+ * Helper that adds event listeners and remembers to remove on unload+ */+exports.listen = function listen(window, node, event, func, capture) {+ // Default to use capture+ if (capture == null)+ capture = true;++ node.addEventListener(event, func, capture);+ function undoListen() {+ node.removeEventListener(event, func, capture);+ }++ // Undo the listener on unload and provide a way to undo everything+ let undoUnload = unload(undoListen, window);+ return function() {+ undoListen();+ undoUnload();+ };+}+diff --git a/lib/main.js b/lib/main.jsindex b3d3b4b81..65605df2e 100644--- a/lib/main.js+++ b/lib/main.js@@ -29,7 +29,7 @@ var toolbarButton;var toolbarButtonDestroyed = false;exports.main = function(options, callbacks) {- console.log(options.loadReason);+ console.log("Load reason: "+options.loadReason);var mainUIMod = require("page-mod");mainUIMod.PageMod({@@ -98,23 +98,21 @@ exports.main = function(options, callbacks) {// Adding toolbar buttoninitToobarButton();- if (options.loadReason == "install" || options.loadReason == "enable") {- installToolbarButton();- //} else {- // uninstallToolbarButton();- }-- if (require('self').loadReason == "install") {- toolbarButton.moveTo({- toolbarID: "nav-bar",- forceMove: true- });- }+ installToolbarButton();+ //if (options.loadReason == "install" || options.loadReason == "enable") {}+};+function installToolbarButton () {+ toolbarButton.moveTo({+ toolbarID: "nav-bar",+ forceMove: false+ });+}+function initToobarButton() {toolbarButton = require("toolbarbutton").ToolbarButton({- id: "TagspacesToolbarButton",+ id: "TSToolbarButton",label: "TagSpaces",image: data.url("icons/icon16.png"),onCommand: function() {@@ -126,24 +124,6 @@ function initToobarButton() {}});}--function installToolbarButton () {- if (toolbarButtonDestroyed) {- initToobarButton();- }-- toolbarButton.moveTo({- toolbarID: "nav-bar",- forceMove: false- });-- toolbarButtonDestroyed = false;-}--function uninstallToolbarButton() {- toolbarButton.destroy();- toolbarButtonDestroyed = true;-}exports.onUnload = function(reason) {console.log(reason);diff --git a/lib/menuitems.js b/lib/menuitems.jsnew file mode 100644index 000000000..aab37e955--- /dev/null+++ b/lib/menuitems.js@@ -0,0 +1,67 @@+/* ***** BEGIN LICENSE BLOCK *****+ * Version: MIT/X11 License+ *+ * Copyright (c) 2010 Erik Vold+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy+ * of this software and associated documentation files (the "Software"), to deal+ * in the Software without restriction, including without limitation the rights+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+ * copies of the Software, and to permit persons to whom the Software is+ * furnished to do so, subject to the following conditions:+ *+ * The above copyright notice and this permission notice shall be included in+ * all copies or substantial portions of the Software.+ *+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+ * THE SOFTWARE.+ *+ * Contributor(s):+ * Erik Vold <[email protected]> (Original Author)+ *+ * ***** END LICENSE BLOCK ***** */++const windowUtils = require("window-utils");+const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";++exports.Menuitem = function Menuitem(options) {+ new windowUtils.WindowTracker({+ onTrack: function (window) {+ if ("chrome://browser/content/browser.xul" != window.location) return;++ var onCmd = function() {+ options.onCommand && options.onCommand();+ };++ // add the new menuitem to a menu+ var menuitem = window.document.createElementNS(NS_XUL, "menuitem");+ menuitem.setAttribute("id", options.id);+ menuitem.setAttribute("label", options.label);+ 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 + "')";+ }+ menuitem.addEventListener("command", onCmd, true);++ if (options.menuid) {+ let ($ = function(id) window.document.getElementById(id)) {+ $(options.menuid).insertBefore(menuitem, $(options.insertbefore));+ }+ }++ // add unloader+ require("unload+").unload(function() {+ menuitem.parentNode.removeChild(menuitem);+ }, window);+ }+ });+};diff --git a/lib/toolbarbutton.js b/lib/toolbarbutton.jsnew file mode 100644index 000000000..04dfe5b4d--- /dev/null+++ b/lib/toolbarbutton.js@@ -0,0 +1,225 @@+/* ***** BEGIN LICENSE BLOCK *****+ * Version: MIT/X11 License+ *+ * Copyright (c) 2010 Erik Vold+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy+ * of this software and associated documentation files (the "Software"), to deal+ * in the Software without restriction, including without limitation the rights+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+ * copies of the Software, and to permit persons to whom the Software is+ * furnished to do so, subject to the following conditions:+ *+ * The above copyright notice and this permission notice shall be included in+ * all copies or substantial portions of the Software.+ *+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+ * THE SOFTWARE.+ *+ * Contributor(s):+ * Erik Vold <[email protected]> (Original Author)+ * Greg Parris <[email protected]>+ *+ * ***** END LICENSE BLOCK ***** */++const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";++const {unload} = require("unload+");+const {listen} = require("listen");+const winUtils = require("window-utils");++const browserURL = "chrome://browser/content/browser.xul";++exports.ToolbarButton = function ToolbarButton(options) {+ var unloaders = [],+ toolbarID = "",+ insertbefore = "",+ destroyed = false,+ destoryFuncs = [];++ var delegate = {+ onTrack: function (window) {+ if ("chrome://browser/content/browser.xul" != window.location || destroyed)+ return;++ let doc = window.document;+ let $ = function(id) doc.getElementById(id);++ options.tooltiptext = options.tooltiptext || '';++ // create toolbar button+ let tbb = doc.createElementNS(NS_XUL, "toolbarbutton");+ tbb.setAttribute("id", options.id);+ tbb.setAttribute("type", "button");+ if (options.image) tbb.setAttribute("image", options.image);+ tbb.setAttribute("class", "toolbarbutton-1 chromeclass-toolbar-additional");+ tbb.setAttribute("label", options.label);+ tbb.setAttribute('tooltiptext', options.tooltiptext);+ tbb.addEventListener("command", function() {+ if (options.onCommand)+ options.onCommand({}); // TODO: provide something?++ if (options.panel) {+ options.panel.show(tbb);+ }+ }, true);++ // add toolbarbutton to palette+ ($("navigator-toolbox") || $("mail-toolbox")).palette.appendChild(tbb);++ // find a toolbar to insert the toolbarbutton into+ if (toolbarID) {+ var tb = $(toolbarID);+ }+ if (!tb) {+ var tb = toolbarbuttonExists(doc, options.id);+ }++ // found a toolbar to use?+ if (tb) {+ let b4;++ // find the toolbarbutton to insert before+ if (insertbefore) {+ b4 = $(insertbefore);+ }+ if (!b4) {+ let currentset = tb.getAttribute("currentset").split(",");+ let i = currentset.indexOf(options.id) + 1;++ // was the toolbarbutton id found in the curent set?+ if (i > 0) {+ let len = currentset.length;+ // find a toolbarbutton to the right which actually exists+ for (; i < len; i++) {+ b4 = $(currentset[i]);+ if (b4) break;+ }+ }+ }++ tb.insertItem(options.id, b4, null, false);+ tb.setAttribute("currentset", tb.currentSet);+ doc.persist(tb.id, "currentset");+ }++ var saveTBNodeInfo = function(e) {+ toolbarID = tbb.parentNode.getAttribute("id") || "";+ insertbefore = (tbb.nextSibling || "")+ && tbb.nextSibling.getAttribute("id").replace(/^wrapper-/i, "");+ };++ window.addEventListener("aftercustomization", saveTBNodeInfo, false);++ // add unloader to unload+'s queue+ var unloadFunc = function() {+ tbb.parentNode.removeChild(tbb);+ window.removeEventListener("aftercustomization", saveTBNodeInfo, false);+ };+ var index = destoryFuncs.push(unloadFunc) - 1;+ listen(window, window, "unload", function() {+ destoryFuncs[index] = null;+ }, false);+ unloaders.push(unload(unloadFunc, window));+ },+ onUntrack: function (window) {}+ };+ var tracker = winUtils.WindowTracker(delegate);++ function setIcon(aOptions) {+ options.image = aOptions.image || aOptions.url;+ getToolbarButtons(function(tbb) {+ tbb.image = options.image;+ }, options.id);+ return options.image;+ }++ return {+ destroy: function() {+ if (destroyed) return;+ destroyed = true;++ if (options.panel)+ options.panel.destroy();++ // run unload functions+ destoryFuncs.forEach(function(f) f && f());+ destoryFuncs.length = 0;++ // remove unload functions from unload+'s queue+ unloaders.forEach(function(f) f());+ unloaders.length = 0;+ },+ moveTo: function(pos) {+ if (destroyed) return;++ // record the new position for future windows+ toolbarID = pos.toolbarID;+ insertbefore = pos.insertbefore;++ // change the current position for open windows+ for each (var window in winUtils.windowIterator()) {+ if (browserURL != window.location) return;++ let doc = window.document;+ let $ = function (id) doc.getElementById(id);++ // if the move isn't being forced and it is already in the window, abort+ if (!pos.forceMove && $(options.id)) return;++ var tb = $(toolbarID);+ var b4 = $(insertbefore);++ // TODO: if b4 dne, but insertbefore is in currentset, then find toolbar to right+ if (tb) {+ tb.insertItem(options.id, b4, null, false);+ tb.setAttribute("currentset", tb.currentSet);+ doc.persist(tb.id, "currentset");+ }+ };+ },+ get label() options.label,+ set label(value) {+ options.label = value;+ getToolbarButtons(function(tbb) {+ tbb.label = value;+ }, options.id);+ return value;+ },+ setIcon: setIcon,+ get image() options.image,+ set image(value) setIcon({image: value}),+ get tooltiptext() options.tooltiptext,+ set tooltiptext(value) {+ options.tooltiptext = value;+ getToolbarButtons(function(tbb) {+ tbb.setAttribute('tooltiptext', value);+ }, options.id);+ },+ };+};++function getToolbarButtons(callback, id) {+ let buttons = [];+ for each (var window in winUtils.windowIterator()) {+ if (browserURL != window.location) continue;+ let tbb = window.document.getElementById(id);+ if (tbb) buttons.push(tbb);+ }+ if (callback) buttons.forEach(callback);+ return buttons;+}++function toolbarbuttonExists(doc, id) {+ var toolbars = doc.getElementsByTagNameNS(NS_XUL, "toolbar");+ for (var i = toolbars.length - 1; ~i; i--) {+ if ((new RegExp("(?:^|,)" + id + "(?:,|$)")).test(toolbars[i].getAttribute("currentset")))+ return toolbars[i];+ }+ return false;+}diff --git a/lib/unload+.js b/lib/unload+.jsnew file mode 100644index 000000000..fa65f7eec--- /dev/null+++ b/lib/unload+.js@@ -0,0 +1,103 @@+/* ***** 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 Speak Words.+ *+ * The Initial Developer of the Original Code is The Mozilla Foundation.+ * Portions created by the Initial Developer are Copyright (C) 2010+ * the Initial Developer. All Rights Reserved.+ *+ * Contributor(s):+ * Edward Lee <[email protected]>+ * Erik Vold <[email protected]>+ *+ * 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 ***** */++"use strict";++var unloaders = [];++function unloadersUnlaod() {+ unloaders.slice().forEach(function(unloader) unloader());+ unloaders.length = 0;+}+require("unload").when(unloadersUnlaod);++/**+ * Save callbacks to run when unloading. Optionally scope the callback to a+ * container, e.g., window. Provide a way to run all the callbacks.+ *+ * @usage unload(): Run all callbacks and release them.+ *+ * @usage unload(callback): Add a callback to run on unload.+ * @param [function] callback: 0-parameter function to call on unload.+ * @return [function]: A 0-parameter function that undoes adding the callback.+ *+ * @usage unload(callback, container) Add a scoped callback to run on unload.+ * @param [function] callback: 0-parameter function to call on unload.+ * @param [node] container: Remove the callback when this container unloads.+ * @return [function]: A 0-parameter function that undoes adding the callback.+ */+exports.unload = function unload(callback, container) {+ // Calling with no arguments runs all the unloader callbacks+ if (callback == null) {+ unloadersUnlaod();+ return;+ }++ var remover = removeUnloader.bind(null, unloader);++ // The callback is bound to the lifetime of the container if we have one+ if (container != null) {+ // Remove the unloader when the container unloads+ container.addEventListener("unload", remover, false);++ // Wrap the callback to additionally remove the unload listener+ let origCallback = callback;+ callback = function() {+ container.removeEventListener("unload", remover, false);+ origCallback();+ }+ }++ // Wrap the callback in a function that ignores failures+ function unloader() {+ try {+ callback();+ }+ catch(ex) {}+ }+ unloaders.push(unloader);++ // Provide a way to remove the unloader+ return remover;+}++function removeUnloader(unloader) {+ let index = unloaders.indexOf(unloader);+ if (index != -1)+ unloaders.splice(index, 1);+}+