Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | this.EXPORTED_SYMBOLS = ["RecentlyClosedTabsAndWindowsMenuUtils"]; |
michael@0 | 6 | |
michael@0 | 7 | const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; |
michael@0 | 8 | |
michael@0 | 9 | var Ci = Components.interfaces; |
michael@0 | 10 | var Cc = Components.classes; |
michael@0 | 11 | var Cr = Components.results; |
michael@0 | 12 | var Cu = Components.utils; |
michael@0 | 13 | |
michael@0 | 14 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 15 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 16 | |
michael@0 | 17 | XPCOMUtils.defineLazyModuleGetter(this, "PluralForm", |
michael@0 | 18 | "resource://gre/modules/PluralForm.jsm"); |
michael@0 | 19 | |
michael@0 | 20 | let navigatorBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); |
michael@0 | 21 | let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore); |
michael@0 | 22 | |
michael@0 | 23 | this.RecentlyClosedTabsAndWindowsMenuUtils = { |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * Builds up a document fragment of UI items for the recently closed tabs. |
michael@0 | 27 | * @param aWindow |
michael@0 | 28 | * The window that the tabs were closed in. |
michael@0 | 29 | * @param aTagName |
michael@0 | 30 | * The tag name that will be used when creating the UI items. |
michael@0 | 31 | * @param aPrefixRestoreAll (defaults to false) |
michael@0 | 32 | * Whether the 'restore all tabs' item is suffixed or prefixed to the list. |
michael@0 | 33 | * If suffixed (the default) a separator will be inserted before it. |
michael@0 | 34 | * @param aRestoreAllLabel (defaults to "menuRestoreAllTabs.label") |
michael@0 | 35 | * Which localizable string to use for the 'restore all tabs' item. |
michael@0 | 36 | * @returns A document fragment with UI items for each recently closed tab. |
michael@0 | 37 | */ |
michael@0 | 38 | getTabsFragment: function(aWindow, aTagName, aPrefixRestoreAll=false, |
michael@0 | 39 | aRestoreAllLabel="menuRestoreAllTabs.label") { |
michael@0 | 40 | let doc = aWindow.document; |
michael@0 | 41 | let fragment = doc.createDocumentFragment(); |
michael@0 | 42 | if (ss.getClosedTabCount(aWindow) != 0) { |
michael@0 | 43 | let closedTabs = JSON.parse(ss.getClosedTabData(aWindow)); |
michael@0 | 44 | for (let i = 0; i < closedTabs.length; i++) { |
michael@0 | 45 | let element = doc.createElementNS(kNSXUL, aTagName); |
michael@0 | 46 | element.setAttribute("label", closedTabs[i].title); |
michael@0 | 47 | if (closedTabs[i].image) { |
michael@0 | 48 | setImage(closedTabs[i], element); |
michael@0 | 49 | } |
michael@0 | 50 | element.setAttribute("value", i); |
michael@0 | 51 | if (aTagName == "menuitem") { |
michael@0 | 52 | element.setAttribute("class", "menuitem-iconic bookmark-item menuitem-with-favicon"); |
michael@0 | 53 | } |
michael@0 | 54 | element.setAttribute("oncommand", "undoCloseTab(" + i + ");"); |
michael@0 | 55 | |
michael@0 | 56 | // Set the targetURI attribute so it will be shown in tooltip and trigger |
michael@0 | 57 | // onLinkHovered. SessionStore uses one-based indexes, so we need to |
michael@0 | 58 | // normalize them. |
michael@0 | 59 | let tabData = closedTabs[i].state; |
michael@0 | 60 | let activeIndex = (tabData.index || tabData.entries.length) - 1; |
michael@0 | 61 | if (activeIndex >= 0 && tabData.entries[activeIndex]) { |
michael@0 | 62 | element.setAttribute("targetURI", tabData.entries[activeIndex].url); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | element.addEventListener("click", this._undoCloseMiddleClick, false); |
michael@0 | 66 | if (i == 0) |
michael@0 | 67 | element.setAttribute("key", "key_undoCloseTab"); |
michael@0 | 68 | fragment.appendChild(element); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | let restoreAllTabs = doc.createElementNS(kNSXUL, aTagName); |
michael@0 | 72 | restoreAllTabs.classList.add("restoreallitem"); |
michael@0 | 73 | restoreAllTabs.setAttribute("label", navigatorBundle.GetStringFromName(aRestoreAllLabel)); |
michael@0 | 74 | restoreAllTabs.setAttribute("oncommand", |
michael@0 | 75 | "for (var i = 0; i < " + closedTabs.length + "; i++) undoCloseTab();"); |
michael@0 | 76 | if (!aPrefixRestoreAll) { |
michael@0 | 77 | fragment.appendChild(doc.createElementNS(kNSXUL, "menuseparator")); |
michael@0 | 78 | fragment.appendChild(restoreAllTabs); |
michael@0 | 79 | } else { |
michael@0 | 80 | fragment.insertBefore(restoreAllTabs, fragment.firstChild); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | return fragment; |
michael@0 | 84 | }, |
michael@0 | 85 | |
michael@0 | 86 | /** |
michael@0 | 87 | * Builds up a document fragment of UI items for the recently closed windows. |
michael@0 | 88 | * @param aWindow |
michael@0 | 89 | * A window that can be used to create the elements and document fragment. |
michael@0 | 90 | * @param aTagName |
michael@0 | 91 | * The tag name that will be used when creating the UI items. |
michael@0 | 92 | * @param aPrefixRestoreAll (defaults to false) |
michael@0 | 93 | * Whether the 'restore all windows' item is suffixed or prefixed to the list. |
michael@0 | 94 | * If suffixed (the default) a separator will be inserted before it. |
michael@0 | 95 | * @param aRestoreAllLabel (defaults to "menuRestoreAllWindows.label") |
michael@0 | 96 | * Which localizable string to use for the 'restore all windows' item. |
michael@0 | 97 | * @returns A document fragment with UI items for each recently closed window. |
michael@0 | 98 | */ |
michael@0 | 99 | getWindowsFragment: function(aWindow, aTagName, aPrefixRestoreAll=false, |
michael@0 | 100 | aRestoreAllLabel="menuRestoreAllWindows.label") { |
michael@0 | 101 | let closedWindowData = JSON.parse(ss.getClosedWindowData()); |
michael@0 | 102 | let fragment = aWindow.document.createDocumentFragment(); |
michael@0 | 103 | if (closedWindowData.length != 0) { |
michael@0 | 104 | let menuLabelString = navigatorBundle.GetStringFromName("menuUndoCloseWindowLabel"); |
michael@0 | 105 | let menuLabelStringSingleTab = |
michael@0 | 106 | navigatorBundle.GetStringFromName("menuUndoCloseWindowSingleTabLabel"); |
michael@0 | 107 | |
michael@0 | 108 | let doc = aWindow.document; |
michael@0 | 109 | for (let i = 0; i < closedWindowData.length; i++) { |
michael@0 | 110 | let undoItem = closedWindowData[i]; |
michael@0 | 111 | let otherTabsCount = undoItem.tabs.length - 1; |
michael@0 | 112 | let label = (otherTabsCount == 0) ? menuLabelStringSingleTab |
michael@0 | 113 | : PluralForm.get(otherTabsCount, menuLabelString); |
michael@0 | 114 | let menuLabel = label.replace("#1", undoItem.title) |
michael@0 | 115 | .replace("#2", otherTabsCount); |
michael@0 | 116 | let item = doc.createElementNS(kNSXUL, aTagName); |
michael@0 | 117 | item.setAttribute("label", menuLabel); |
michael@0 | 118 | let selectedTab = undoItem.tabs[undoItem.selected - 1]; |
michael@0 | 119 | if (selectedTab.image) { |
michael@0 | 120 | setImage(selectedTab, item); |
michael@0 | 121 | } |
michael@0 | 122 | if (aTagName == "menuitem") { |
michael@0 | 123 | item.setAttribute("class", "menuitem-iconic bookmark-item menuitem-with-favicon"); |
michael@0 | 124 | } |
michael@0 | 125 | item.setAttribute("oncommand", "undoCloseWindow(" + i + ");"); |
michael@0 | 126 | |
michael@0 | 127 | // Set the targetURI attribute so it will be shown in tooltip. |
michael@0 | 128 | // SessionStore uses one-based indexes, so we need to normalize them. |
michael@0 | 129 | let activeIndex = (selectedTab.index || selectedTab.entries.length) - 1; |
michael@0 | 130 | if (activeIndex >= 0 && selectedTab.entries[activeIndex]) |
michael@0 | 131 | item.setAttribute("targetURI", selectedTab.entries[activeIndex].url); |
michael@0 | 132 | |
michael@0 | 133 | if (i == 0) |
michael@0 | 134 | item.setAttribute("key", "key_undoCloseWindow"); |
michael@0 | 135 | fragment.appendChild(item); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | // "Open All in Windows" |
michael@0 | 139 | let restoreAllWindows = doc.createElementNS(kNSXUL, aTagName); |
michael@0 | 140 | restoreAllWindows.classList.add("restoreallitem"); |
michael@0 | 141 | restoreAllWindows.setAttribute("label", navigatorBundle.GetStringFromName(aRestoreAllLabel)); |
michael@0 | 142 | restoreAllWindows.setAttribute("oncommand", |
michael@0 | 143 | "for (var i = 0; i < " + closedWindowData.length + "; i++) undoCloseWindow();"); |
michael@0 | 144 | if (!aPrefixRestoreAll) { |
michael@0 | 145 | fragment.appendChild(doc.createElementNS(kNSXUL, "menuseparator")); |
michael@0 | 146 | fragment.appendChild(restoreAllWindows); |
michael@0 | 147 | } else { |
michael@0 | 148 | fragment.insertBefore(restoreAllWindows, fragment.firstChild); |
michael@0 | 149 | } |
michael@0 | 150 | } |
michael@0 | 151 | return fragment; |
michael@0 | 152 | }, |
michael@0 | 153 | |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * Re-open a closed tab and put it to the end of the tab strip. |
michael@0 | 157 | * Used for a middle click. |
michael@0 | 158 | * @param aEvent |
michael@0 | 159 | * The event when the user clicks the menu item |
michael@0 | 160 | */ |
michael@0 | 161 | _undoCloseMiddleClick: function(aEvent) { |
michael@0 | 162 | if (aEvent.button != 1) |
michael@0 | 163 | return; |
michael@0 | 164 | |
michael@0 | 165 | aEvent.view.undoCloseTab(aEvent.originalTarget.getAttribute("value")); |
michael@0 | 166 | aEvent.view.gBrowser.moveTabToEnd(); |
michael@0 | 167 | }, |
michael@0 | 168 | }; |
michael@0 | 169 | |
michael@0 | 170 | function setImage(aItem, aElement) { |
michael@0 | 171 | let iconURL = aItem.image; |
michael@0 | 172 | // don't initiate a connection just to fetch a favicon (see bug 467828) |
michael@0 | 173 | if (/^https?:/.test(iconURL)) |
michael@0 | 174 | iconURL = "moz-anno:favicon:" + iconURL; |
michael@0 | 175 | aElement.setAttribute("image", iconURL); |
michael@0 | 176 | } |