michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = ["RecentlyClosedTabsAndWindowsMenuUtils"]; michael@0: michael@0: const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; michael@0: michael@0: var Ci = Components.interfaces; michael@0: var Cc = Components.classes; michael@0: var Cr = Components.results; michael@0: var Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "PluralForm", michael@0: "resource://gre/modules/PluralForm.jsm"); michael@0: michael@0: let navigatorBundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); michael@0: let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore); michael@0: michael@0: this.RecentlyClosedTabsAndWindowsMenuUtils = { michael@0: michael@0: /** michael@0: * Builds up a document fragment of UI items for the recently closed tabs. michael@0: * @param aWindow michael@0: * The window that the tabs were closed in. michael@0: * @param aTagName michael@0: * The tag name that will be used when creating the UI items. michael@0: * @param aPrefixRestoreAll (defaults to false) michael@0: * Whether the 'restore all tabs' item is suffixed or prefixed to the list. michael@0: * If suffixed (the default) a separator will be inserted before it. michael@0: * @param aRestoreAllLabel (defaults to "menuRestoreAllTabs.label") michael@0: * Which localizable string to use for the 'restore all tabs' item. michael@0: * @returns A document fragment with UI items for each recently closed tab. michael@0: */ michael@0: getTabsFragment: function(aWindow, aTagName, aPrefixRestoreAll=false, michael@0: aRestoreAllLabel="menuRestoreAllTabs.label") { michael@0: let doc = aWindow.document; michael@0: let fragment = doc.createDocumentFragment(); michael@0: if (ss.getClosedTabCount(aWindow) != 0) { michael@0: let closedTabs = JSON.parse(ss.getClosedTabData(aWindow)); michael@0: for (let i = 0; i < closedTabs.length; i++) { michael@0: let element = doc.createElementNS(kNSXUL, aTagName); michael@0: element.setAttribute("label", closedTabs[i].title); michael@0: if (closedTabs[i].image) { michael@0: setImage(closedTabs[i], element); michael@0: } michael@0: element.setAttribute("value", i); michael@0: if (aTagName == "menuitem") { michael@0: element.setAttribute("class", "menuitem-iconic bookmark-item menuitem-with-favicon"); michael@0: } michael@0: element.setAttribute("oncommand", "undoCloseTab(" + i + ");"); michael@0: michael@0: // Set the targetURI attribute so it will be shown in tooltip and trigger michael@0: // onLinkHovered. SessionStore uses one-based indexes, so we need to michael@0: // normalize them. michael@0: let tabData = closedTabs[i].state; michael@0: let activeIndex = (tabData.index || tabData.entries.length) - 1; michael@0: if (activeIndex >= 0 && tabData.entries[activeIndex]) { michael@0: element.setAttribute("targetURI", tabData.entries[activeIndex].url); michael@0: } michael@0: michael@0: element.addEventListener("click", this._undoCloseMiddleClick, false); michael@0: if (i == 0) michael@0: element.setAttribute("key", "key_undoCloseTab"); michael@0: fragment.appendChild(element); michael@0: } michael@0: michael@0: let restoreAllTabs = doc.createElementNS(kNSXUL, aTagName); michael@0: restoreAllTabs.classList.add("restoreallitem"); michael@0: restoreAllTabs.setAttribute("label", navigatorBundle.GetStringFromName(aRestoreAllLabel)); michael@0: restoreAllTabs.setAttribute("oncommand", michael@0: "for (var i = 0; i < " + closedTabs.length + "; i++) undoCloseTab();"); michael@0: if (!aPrefixRestoreAll) { michael@0: fragment.appendChild(doc.createElementNS(kNSXUL, "menuseparator")); michael@0: fragment.appendChild(restoreAllTabs); michael@0: } else { michael@0: fragment.insertBefore(restoreAllTabs, fragment.firstChild); michael@0: } michael@0: } michael@0: return fragment; michael@0: }, michael@0: michael@0: /** michael@0: * Builds up a document fragment of UI items for the recently closed windows. michael@0: * @param aWindow michael@0: * A window that can be used to create the elements and document fragment. michael@0: * @param aTagName michael@0: * The tag name that will be used when creating the UI items. michael@0: * @param aPrefixRestoreAll (defaults to false) michael@0: * Whether the 'restore all windows' item is suffixed or prefixed to the list. michael@0: * If suffixed (the default) a separator will be inserted before it. michael@0: * @param aRestoreAllLabel (defaults to "menuRestoreAllWindows.label") michael@0: * Which localizable string to use for the 'restore all windows' item. michael@0: * @returns A document fragment with UI items for each recently closed window. michael@0: */ michael@0: getWindowsFragment: function(aWindow, aTagName, aPrefixRestoreAll=false, michael@0: aRestoreAllLabel="menuRestoreAllWindows.label") { michael@0: let closedWindowData = JSON.parse(ss.getClosedWindowData()); michael@0: let fragment = aWindow.document.createDocumentFragment(); michael@0: if (closedWindowData.length != 0) { michael@0: let menuLabelString = navigatorBundle.GetStringFromName("menuUndoCloseWindowLabel"); michael@0: let menuLabelStringSingleTab = michael@0: navigatorBundle.GetStringFromName("menuUndoCloseWindowSingleTabLabel"); michael@0: michael@0: let doc = aWindow.document; michael@0: for (let i = 0; i < closedWindowData.length; i++) { michael@0: let undoItem = closedWindowData[i]; michael@0: let otherTabsCount = undoItem.tabs.length - 1; michael@0: let label = (otherTabsCount == 0) ? menuLabelStringSingleTab michael@0: : PluralForm.get(otherTabsCount, menuLabelString); michael@0: let menuLabel = label.replace("#1", undoItem.title) michael@0: .replace("#2", otherTabsCount); michael@0: let item = doc.createElementNS(kNSXUL, aTagName); michael@0: item.setAttribute("label", menuLabel); michael@0: let selectedTab = undoItem.tabs[undoItem.selected - 1]; michael@0: if (selectedTab.image) { michael@0: setImage(selectedTab, item); michael@0: } michael@0: if (aTagName == "menuitem") { michael@0: item.setAttribute("class", "menuitem-iconic bookmark-item menuitem-with-favicon"); michael@0: } michael@0: item.setAttribute("oncommand", "undoCloseWindow(" + i + ");"); michael@0: michael@0: // Set the targetURI attribute so it will be shown in tooltip. michael@0: // SessionStore uses one-based indexes, so we need to normalize them. michael@0: let activeIndex = (selectedTab.index || selectedTab.entries.length) - 1; michael@0: if (activeIndex >= 0 && selectedTab.entries[activeIndex]) michael@0: item.setAttribute("targetURI", selectedTab.entries[activeIndex].url); michael@0: michael@0: if (i == 0) michael@0: item.setAttribute("key", "key_undoCloseWindow"); michael@0: fragment.appendChild(item); michael@0: } michael@0: michael@0: // "Open All in Windows" michael@0: let restoreAllWindows = doc.createElementNS(kNSXUL, aTagName); michael@0: restoreAllWindows.classList.add("restoreallitem"); michael@0: restoreAllWindows.setAttribute("label", navigatorBundle.GetStringFromName(aRestoreAllLabel)); michael@0: restoreAllWindows.setAttribute("oncommand", michael@0: "for (var i = 0; i < " + closedWindowData.length + "; i++) undoCloseWindow();"); michael@0: if (!aPrefixRestoreAll) { michael@0: fragment.appendChild(doc.createElementNS(kNSXUL, "menuseparator")); michael@0: fragment.appendChild(restoreAllWindows); michael@0: } else { michael@0: fragment.insertBefore(restoreAllWindows, fragment.firstChild); michael@0: } michael@0: } michael@0: return fragment; michael@0: }, michael@0: michael@0: michael@0: /** michael@0: * Re-open a closed tab and put it to the end of the tab strip. michael@0: * Used for a middle click. michael@0: * @param aEvent michael@0: * The event when the user clicks the menu item michael@0: */ michael@0: _undoCloseMiddleClick: function(aEvent) { michael@0: if (aEvent.button != 1) michael@0: return; michael@0: michael@0: aEvent.view.undoCloseTab(aEvent.originalTarget.getAttribute("value")); michael@0: aEvent.view.gBrowser.moveTabToEnd(); michael@0: }, michael@0: }; michael@0: michael@0: function setImage(aItem, aElement) { michael@0: let iconURL = aItem.image; michael@0: // don't initiate a connection just to fetch a favicon (see bug 467828) michael@0: if (/^https?:/.test(iconURL)) michael@0: iconURL = "moz-anno:favicon:" + iconURL; michael@0: aElement.setAttribute("image", iconURL); michael@0: }