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: //////////////////////////////////////////////////////////////////////////////// michael@0: //// StarUI michael@0: michael@0: var StarUI = { michael@0: _itemId: -1, michael@0: uri: null, michael@0: _batching: false, michael@0: michael@0: _element: function(aID) { michael@0: return document.getElementById(aID); michael@0: }, michael@0: michael@0: // Edit-bookmark panel michael@0: get panel() { michael@0: delete this.panel; michael@0: var element = this._element("editBookmarkPanel"); michael@0: // initially the panel is hidden michael@0: // to avoid impacting startup / new window performance michael@0: element.hidden = false; michael@0: element.addEventListener("popuphidden", this, false); michael@0: element.addEventListener("keypress", this, false); michael@0: return this.panel = element; michael@0: }, michael@0: michael@0: // Array of command elements to disable when the panel is opened. michael@0: get _blockedCommands() { michael@0: delete this._blockedCommands; michael@0: return this._blockedCommands = michael@0: ["cmd_close", "cmd_closeWindow"].map(function (id) this._element(id), this); michael@0: }, michael@0: michael@0: _blockCommands: function SU__blockCommands() { michael@0: this._blockedCommands.forEach(function (elt) { michael@0: // make sure not to permanently disable this item (see bug 409155) michael@0: if (elt.hasAttribute("wasDisabled")) michael@0: return; michael@0: if (elt.getAttribute("disabled") == "true") { michael@0: elt.setAttribute("wasDisabled", "true"); michael@0: } else { michael@0: elt.setAttribute("wasDisabled", "false"); michael@0: elt.setAttribute("disabled", "true"); michael@0: } michael@0: }); michael@0: }, michael@0: michael@0: _restoreCommandsState: function SU__restoreCommandsState() { michael@0: this._blockedCommands.forEach(function (elt) { michael@0: if (elt.getAttribute("wasDisabled") != "true") michael@0: elt.removeAttribute("disabled"); michael@0: elt.removeAttribute("wasDisabled"); michael@0: }); michael@0: }, michael@0: michael@0: // nsIDOMEventListener michael@0: handleEvent: function SU_handleEvent(aEvent) { michael@0: switch (aEvent.type) { michael@0: case "popuphidden": michael@0: if (aEvent.originalTarget == this.panel) { michael@0: if (!this._element("editBookmarkPanelContent").hidden) michael@0: this.quitEditMode(); michael@0: michael@0: if (this._anchorToolbarButton) { michael@0: this._anchorToolbarButton.removeAttribute("open"); michael@0: this._anchorToolbarButton = null; michael@0: } michael@0: this._restoreCommandsState(); michael@0: this._itemId = -1; michael@0: if (this._batching) { michael@0: PlacesUtils.transactionManager.endBatch(false); michael@0: this._batching = false; michael@0: } michael@0: michael@0: switch (this._actionOnHide) { michael@0: case "cancel": { michael@0: PlacesUtils.transactionManager.undoTransaction(); michael@0: break; michael@0: } michael@0: case "remove": { michael@0: // Remove all bookmarks for the bookmark's url, this also removes michael@0: // the tags for the url. michael@0: PlacesUtils.transactionManager.beginBatch(null); michael@0: let itemIds = PlacesUtils.getBookmarksForURI(this._uriForRemoval); michael@0: for (let i = 0; i < itemIds.length; i++) { michael@0: let txn = new PlacesRemoveItemTransaction(itemIds[i]); michael@0: PlacesUtils.transactionManager.doTransaction(txn); michael@0: } michael@0: PlacesUtils.transactionManager.endBatch(false); michael@0: break; michael@0: } michael@0: } michael@0: this._actionOnHide = ""; michael@0: } michael@0: break; michael@0: case "keypress": michael@0: if (aEvent.defaultPrevented) { michael@0: // The event has already been consumed inside of the panel. michael@0: break; michael@0: } michael@0: switch (aEvent.keyCode) { michael@0: case KeyEvent.DOM_VK_ESCAPE: michael@0: if (!this._element("editBookmarkPanelContent").hidden) michael@0: this.cancelButtonOnCommand(); michael@0: break; michael@0: case KeyEvent.DOM_VK_RETURN: michael@0: if (aEvent.target.classList.contains("expander-up") || michael@0: aEvent.target.classList.contains("expander-down") || michael@0: aEvent.target.id == "editBMPanel_newFolderButton") { michael@0: //XXX Why is this necessary? The defaultPrevented check should michael@0: // be enough. michael@0: break; michael@0: } michael@0: this.panel.hidePopup(); michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: _overlayLoaded: false, michael@0: _overlayLoading: false, michael@0: showEditBookmarkPopup: michael@0: function SU_showEditBookmarkPopup(aItemId, aAnchorElement, aPosition) { michael@0: // Performance: load the overlay the first time the panel is opened michael@0: // (see bug 392443). michael@0: if (this._overlayLoading) michael@0: return; michael@0: michael@0: if (this._overlayLoaded) { michael@0: this._doShowEditBookmarkPanel(aItemId, aAnchorElement, aPosition); michael@0: return; michael@0: } michael@0: michael@0: this._overlayLoading = true; michael@0: document.loadOverlay( michael@0: "chrome://browser/content/places/editBookmarkOverlay.xul", michael@0: (function (aSubject, aTopic, aData) { michael@0: // Move the header (star, title, button) into the grid, michael@0: // so that it aligns nicely with the other items (bug 484022). michael@0: let header = this._element("editBookmarkPanelHeader"); michael@0: let rows = this._element("editBookmarkPanelGrid").lastChild; michael@0: rows.insertBefore(header, rows.firstChild); michael@0: header.hidden = false; michael@0: michael@0: this._overlayLoading = false; michael@0: this._overlayLoaded = true; michael@0: this._doShowEditBookmarkPanel(aItemId, aAnchorElement, aPosition); michael@0: }).bind(this) michael@0: ); michael@0: }, michael@0: michael@0: _doShowEditBookmarkPanel: michael@0: function SU__doShowEditBookmarkPanel(aItemId, aAnchorElement, aPosition) { michael@0: if (this.panel.state != "closed") michael@0: return; michael@0: michael@0: this._blockCommands(); // un-done in the popuphiding handler michael@0: michael@0: // Set panel title: michael@0: // if we are batching, i.e. the bookmark has been added now, michael@0: // then show Page Bookmarked, else if the bookmark did already exist, michael@0: // we are about editing it, then use Edit This Bookmark. michael@0: this._element("editBookmarkPanelTitle").value = michael@0: this._batching ? michael@0: gNavigatorBundle.getString("editBookmarkPanel.pageBookmarkedTitle") : michael@0: gNavigatorBundle.getString("editBookmarkPanel.editBookmarkTitle"); michael@0: michael@0: // No description; show the Done, Cancel; michael@0: this._element("editBookmarkPanelDescription").textContent = ""; michael@0: this._element("editBookmarkPanelBottomButtons").hidden = false; michael@0: this._element("editBookmarkPanelContent").hidden = false; michael@0: michael@0: // The remove button is shown only if we're not already batching, i.e. michael@0: // if the cancel button/ESC does not remove the bookmark. michael@0: this._element("editBookmarkPanelRemoveButton").hidden = this._batching; michael@0: michael@0: // The label of the remove button differs if the URI is bookmarked michael@0: // multiple times. michael@0: var bookmarks = PlacesUtils.getBookmarksForURI(gBrowser.currentURI); michael@0: var forms = gNavigatorBundle.getString("editBookmark.removeBookmarks.label"); michael@0: var label = PluralForm.get(bookmarks.length, forms).replace("#1", bookmarks.length); michael@0: this._element("editBookmarkPanelRemoveButton").label = label; michael@0: michael@0: // unset the unstarred state, if set michael@0: this._element("editBookmarkPanelStarIcon").removeAttribute("unstarred"); michael@0: michael@0: this._itemId = aItemId !== undefined ? aItemId : this._itemId; michael@0: this.beginBatch(); michael@0: michael@0: if (aAnchorElement) { michael@0: // Set the open=true attribute if the anchor is a michael@0: // descendent of a toolbarbutton. michael@0: let parent = aAnchorElement.parentNode; michael@0: while (parent) { michael@0: if (parent.localName == "toolbarbutton") { michael@0: break; michael@0: } michael@0: parent = parent.parentNode; michael@0: } michael@0: if (parent) { michael@0: this._anchorToolbarButton = parent; michael@0: parent.setAttribute("open", "true"); michael@0: } michael@0: } michael@0: this.panel.openPopup(aAnchorElement, aPosition); michael@0: michael@0: gEditItemOverlay.initPanel(this._itemId, michael@0: { hiddenRows: ["description", "location", michael@0: "loadInSidebar", "keyword"] }); michael@0: }, michael@0: michael@0: panelShown: michael@0: function SU_panelShown(aEvent) { michael@0: if (aEvent.target == this.panel) { michael@0: if (!this._element("editBookmarkPanelContent").hidden) { michael@0: let fieldToFocus = "editBMPanel_" + michael@0: gPrefService.getCharPref("browser.bookmarks.editDialog.firstEditField"); michael@0: var elt = this._element(fieldToFocus); michael@0: elt.focus(); michael@0: elt.select(); michael@0: } michael@0: else { michael@0: // Note this isn't actually used anymore, we should remove this michael@0: // once we decide not to bring back the page bookmarked notification michael@0: this.panel.focus(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: quitEditMode: function SU_quitEditMode() { michael@0: this._element("editBookmarkPanelContent").hidden = true; michael@0: this._element("editBookmarkPanelBottomButtons").hidden = true; michael@0: gEditItemOverlay.uninitPanel(true); michael@0: }, michael@0: michael@0: cancelButtonOnCommand: function SU_cancelButtonOnCommand() { michael@0: this._actionOnHide = "cancel"; michael@0: this.panel.hidePopup(); michael@0: }, michael@0: michael@0: removeBookmarkButtonCommand: function SU_removeBookmarkButtonCommand() { michael@0: this._uriForRemoval = PlacesUtils.bookmarks.getBookmarkURI(this._itemId); michael@0: this._actionOnHide = "remove"; michael@0: this.panel.hidePopup(); michael@0: }, michael@0: michael@0: beginBatch: function SU_beginBatch() { michael@0: if (!this._batching) { michael@0: PlacesUtils.transactionManager.beginBatch(null); michael@0: this._batching = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// PlacesCommandHook michael@0: michael@0: var PlacesCommandHook = { michael@0: /** michael@0: * Adds a bookmark to the page loaded in the given browser. michael@0: * michael@0: * @param aBrowser michael@0: * a element. michael@0: * @param [optional] aParent michael@0: * The folder in which to create a new bookmark if the page loaded in michael@0: * aBrowser isn't bookmarked yet, defaults to the unfiled root. michael@0: * @param [optional] aShowEditUI michael@0: * whether or not to show the edit-bookmark UI for the bookmark item michael@0: */ michael@0: bookmarkPage: function PCH_bookmarkPage(aBrowser, aParent, aShowEditUI) { michael@0: var uri = aBrowser.currentURI; michael@0: var itemId = PlacesUtils.getMostRecentBookmarkForURI(uri); michael@0: if (itemId == -1) { michael@0: // Copied over from addBookmarkForBrowser: michael@0: // Bug 52536: We obtain the URL and title from the nsIWebNavigation michael@0: // associated with a rather than from a DOMWindow. michael@0: // This is because when a full page plugin is loaded, there is michael@0: // no DOMWindow (?) but information about the loaded document michael@0: // may still be obtained from the webNavigation. michael@0: var webNav = aBrowser.webNavigation; michael@0: var url = webNav.currentURI; michael@0: var title; michael@0: var description; michael@0: var charset; michael@0: try { michael@0: let isErrorPage = /^about:(neterror|certerror|blocked)/ michael@0: .test(webNav.document.documentURI); michael@0: title = isErrorPage ? PlacesUtils.history.getPageTitle(url) michael@0: : webNav.document.title; michael@0: title = title || url.spec; michael@0: description = PlacesUIUtils.getDescriptionFromDocument(webNav.document); michael@0: charset = webNav.document.characterSet; michael@0: } michael@0: catch (e) { } michael@0: michael@0: if (aShowEditUI) { michael@0: // If we bookmark the page here (i.e. page was not "starred" already) michael@0: // but open right into the "edit" state, start batching here, so michael@0: // "Cancel" in that state removes the bookmark. michael@0: StarUI.beginBatch(); michael@0: } michael@0: michael@0: var parent = aParent != undefined ? michael@0: aParent : PlacesUtils.unfiledBookmarksFolderId; michael@0: var descAnno = { name: PlacesUIUtils.DESCRIPTION_ANNO, value: description }; michael@0: var txn = new PlacesCreateBookmarkTransaction(uri, parent, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: title, null, [descAnno]); michael@0: PlacesUtils.transactionManager.doTransaction(txn); michael@0: itemId = txn.item.id; michael@0: // Set the character-set michael@0: if (charset && !PrivateBrowsingUtils.isWindowPrivate(aBrowser.contentWindow)) michael@0: PlacesUtils.setCharsetForURI(uri, charset); michael@0: } michael@0: michael@0: // Revert the contents of the location bar michael@0: if (gURLBar) michael@0: gURLBar.handleRevert(); michael@0: michael@0: // If it was not requested to open directly in "edit" mode, we are done. michael@0: if (!aShowEditUI) michael@0: return; michael@0: michael@0: // Try to dock the panel to: michael@0: // 1. the bookmarks menu button michael@0: // 2. the page-proxy-favicon michael@0: // 3. the content area michael@0: if (BookmarkingUI.anchor) { michael@0: StarUI.showEditBookmarkPopup(itemId, BookmarkingUI.anchor, michael@0: "bottomcenter topright"); michael@0: return; michael@0: } michael@0: michael@0: let pageProxyFavicon = document.getElementById("page-proxy-favicon"); michael@0: if (isElementVisible(pageProxyFavicon)) { michael@0: StarUI.showEditBookmarkPopup(itemId, pageProxyFavicon, michael@0: "bottomcenter topright"); michael@0: } else { michael@0: StarUI.showEditBookmarkPopup(itemId, aBrowser, "overlap"); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Adds a bookmark to the page loaded in the current tab. michael@0: */ michael@0: bookmarkCurrentPage: function PCH_bookmarkCurrentPage(aShowEditUI, aParent) { michael@0: this.bookmarkPage(gBrowser.selectedBrowser, aParent, aShowEditUI); michael@0: }, michael@0: michael@0: /** michael@0: * Adds a bookmark to the page targeted by a link. michael@0: * @param aParent michael@0: * The folder in which to create a new bookmark if aURL isn't michael@0: * bookmarked. michael@0: * @param aURL (string) michael@0: * the address of the link target michael@0: * @param aTitle michael@0: * The link text michael@0: */ michael@0: bookmarkLink: function PCH_bookmarkLink(aParent, aURL, aTitle) { michael@0: var linkURI = makeURI(aURL); michael@0: var itemId = PlacesUtils.getMostRecentBookmarkForURI(linkURI); michael@0: if (itemId == -1) { michael@0: PlacesUIUtils.showBookmarkDialog({ action: "add" michael@0: , type: "bookmark" michael@0: , uri: linkURI michael@0: , title: aTitle michael@0: , hiddenRows: [ "description" michael@0: , "location" michael@0: , "loadInSidebar" michael@0: , "keyword" ] michael@0: }, window); michael@0: } michael@0: else { michael@0: PlacesUIUtils.showBookmarkDialog({ action: "edit" michael@0: , type: "bookmark" michael@0: , itemId: itemId michael@0: }, window); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * List of nsIURI objects characterizing the tabs currently open in the michael@0: * browser, modulo pinned tabs. The URIs will be in the order in which their michael@0: * corresponding tabs appeared and duplicates are discarded. michael@0: */ michael@0: get uniqueCurrentPages() { michael@0: let uniquePages = {}; michael@0: let URIs = []; michael@0: gBrowser.visibleTabs.forEach(function (tab) { michael@0: let spec = tab.linkedBrowser.currentURI.spec; michael@0: if (!tab.pinned && !(spec in uniquePages)) { michael@0: uniquePages[spec] = null; michael@0: URIs.push(tab.linkedBrowser.currentURI); michael@0: } michael@0: }); michael@0: return URIs; michael@0: }, michael@0: michael@0: /** michael@0: * Adds a folder with bookmarks to all of the currently open tabs in this michael@0: * window. michael@0: */ michael@0: bookmarkCurrentPages: function PCH_bookmarkCurrentPages() { michael@0: let pages = this.uniqueCurrentPages; michael@0: if (pages.length > 1) { michael@0: PlacesUIUtils.showBookmarkDialog({ action: "add" michael@0: , type: "folder" michael@0: , URIList: pages michael@0: , hiddenRows: [ "description" ] michael@0: }, window); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Updates disabled state for the "Bookmark All Tabs" command. michael@0: */ michael@0: updateBookmarkAllTabsCommand: michael@0: function PCH_updateBookmarkAllTabsCommand() { michael@0: // There's nothing to do in non-browser windows. michael@0: if (window.location.href != getBrowserURL()) michael@0: return; michael@0: michael@0: // Disable "Bookmark All Tabs" if there are less than two michael@0: // "unique current pages". michael@0: goSetCommandEnabled("Browser:BookmarkAllTabs", michael@0: this.uniqueCurrentPages.length >= 2); michael@0: }, michael@0: michael@0: /** michael@0: * Adds a Live Bookmark to a feed associated with the current page. michael@0: * @param url michael@0: * The nsIURI of the page the feed was attached to michael@0: * @title title michael@0: * The title of the feed. Optional. michael@0: * @subtitle subtitle michael@0: * A short description of the feed. Optional. michael@0: */ michael@0: addLiveBookmark: function PCH_addLiveBookmark(url, feedTitle, feedSubtitle) { michael@0: var feedURI = makeURI(url); michael@0: michael@0: var doc = gBrowser.contentDocument; michael@0: var title = (arguments.length > 1) ? feedTitle : doc.title; michael@0: michael@0: var description; michael@0: if (arguments.length > 2) michael@0: description = feedSubtitle; michael@0: else michael@0: description = PlacesUIUtils.getDescriptionFromDocument(doc); michael@0: michael@0: var toolbarIP = new InsertionPoint(PlacesUtils.toolbarFolderId, -1); michael@0: PlacesUIUtils.showBookmarkDialog({ action: "add" michael@0: , type: "livemark" michael@0: , feedURI: feedURI michael@0: , siteURI: gBrowser.currentURI michael@0: , title: title michael@0: , description: description michael@0: , defaultInsertionPoint: toolbarIP michael@0: , hiddenRows: [ "feedLocation" michael@0: , "siteLocation" michael@0: , "description" ] michael@0: }, window); michael@0: }, michael@0: michael@0: /** michael@0: * Opens the Places Organizer. michael@0: * @param aLeftPaneRoot michael@0: * The query to select in the organizer window - options michael@0: * are: History, AllBookmarks, BookmarksMenu, BookmarksToolbar, michael@0: * UnfiledBookmarks, Tags and Downloads. michael@0: */ michael@0: showPlacesOrganizer: function PCH_showPlacesOrganizer(aLeftPaneRoot) { michael@0: var organizer = Services.wm.getMostRecentWindow("Places:Organizer"); michael@0: if (!organizer) { michael@0: // No currently open places window, so open one with the specified mode. michael@0: openDialog("chrome://browser/content/places/places.xul", michael@0: "", "chrome,toolbar=yes,dialog=no,resizable", aLeftPaneRoot); michael@0: } michael@0: else { michael@0: organizer.PlacesOrganizer.selectLeftPaneContainerByHierarchy(aLeftPaneRoot); michael@0: organizer.focus(); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// HistoryMenu michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "RecentlyClosedTabsAndWindowsMenuUtils", michael@0: "resource:///modules/sessionstore/RecentlyClosedTabsAndWindowsMenuUtils.jsm"); michael@0: michael@0: // View for the history menu. michael@0: function HistoryMenu(aPopupShowingEvent) { michael@0: // Workaround for Bug 610187. The sidebar does not include all the Places michael@0: // views definitions, and we don't need them there. michael@0: // Defining the prototype inheritance in the prototype itself would cause michael@0: // browser.js to halt on "PlacesMenu is not defined" error. michael@0: this.__proto__.__proto__ = PlacesMenu.prototype; michael@0: PlacesMenu.call(this, aPopupShowingEvent, michael@0: "place:sort=4&maxResults=15"); michael@0: } michael@0: michael@0: HistoryMenu.prototype = { michael@0: toggleRecentlyClosedTabs: function HM_toggleRecentlyClosedTabs() { michael@0: // enable/disable the Recently Closed Tabs sub menu michael@0: var undoMenu = this._rootElt.getElementsByClassName("recentlyClosedTabsMenu")[0]; michael@0: michael@0: // no restorable tabs, so disable menu michael@0: if (SessionStore.getClosedTabCount(window) == 0) michael@0: undoMenu.setAttribute("disabled", true); michael@0: else michael@0: undoMenu.removeAttribute("disabled"); michael@0: }, michael@0: michael@0: /** michael@0: * Populate when the history menu is opened michael@0: */ michael@0: populateUndoSubmenu: function PHM_populateUndoSubmenu() { michael@0: var undoMenu = this._rootElt.getElementsByClassName("recentlyClosedTabsMenu")[0]; michael@0: var undoPopup = undoMenu.firstChild; michael@0: michael@0: // remove existing menu items michael@0: while (undoPopup.hasChildNodes()) michael@0: undoPopup.removeChild(undoPopup.firstChild); michael@0: michael@0: // no restorable tabs, so make sure menu is disabled, and return michael@0: if (SessionStore.getClosedTabCount(window) == 0) { michael@0: undoMenu.setAttribute("disabled", true); michael@0: return; michael@0: } michael@0: michael@0: // enable menu michael@0: undoMenu.removeAttribute("disabled"); michael@0: michael@0: // populate menu michael@0: let tabsFragment = RecentlyClosedTabsAndWindowsMenuUtils.getTabsFragment(window, "menuitem"); michael@0: undoPopup.appendChild(tabsFragment); michael@0: }, michael@0: michael@0: toggleRecentlyClosedWindows: function PHM_toggleRecentlyClosedWindows() { michael@0: // enable/disable the Recently Closed Windows sub menu michael@0: var undoMenu = this._rootElt.getElementsByClassName("recentlyClosedWindowsMenu")[0]; michael@0: michael@0: // no restorable windows, so disable menu michael@0: if (SessionStore.getClosedWindowCount() == 0) michael@0: undoMenu.setAttribute("disabled", true); michael@0: else michael@0: undoMenu.removeAttribute("disabled"); michael@0: }, michael@0: michael@0: /** michael@0: * Populate when the history menu is opened michael@0: */ michael@0: populateUndoWindowSubmenu: function PHM_populateUndoWindowSubmenu() { michael@0: let undoMenu = this._rootElt.getElementsByClassName("recentlyClosedWindowsMenu")[0]; michael@0: let undoPopup = undoMenu.firstChild; michael@0: let menuLabelString = gNavigatorBundle.getString("menuUndoCloseWindowLabel"); michael@0: let menuLabelStringSingleTab = michael@0: gNavigatorBundle.getString("menuUndoCloseWindowSingleTabLabel"); michael@0: michael@0: // remove existing menu items michael@0: while (undoPopup.hasChildNodes()) michael@0: undoPopup.removeChild(undoPopup.firstChild); michael@0: michael@0: // no restorable windows, so make sure menu is disabled, and return michael@0: if (SessionStore.getClosedWindowCount() == 0) { michael@0: undoMenu.setAttribute("disabled", true); michael@0: return; michael@0: } michael@0: michael@0: // enable menu michael@0: undoMenu.removeAttribute("disabled"); michael@0: michael@0: // populate menu michael@0: let windowsFragment = RecentlyClosedTabsAndWindowsMenuUtils.getWindowsFragment(window, "menuitem"); michael@0: undoPopup.appendChild(windowsFragment); michael@0: }, michael@0: michael@0: toggleTabsFromOtherComputers: function PHM_toggleTabsFromOtherComputers() { michael@0: // This is a no-op if MOZ_SERVICES_SYNC isn't defined michael@0: #ifdef MOZ_SERVICES_SYNC michael@0: // Enable/disable the Tabs From Other Computers menu. Some of the menus handled michael@0: // by HistoryMenu do not have this menuitem. michael@0: let menuitem = this._rootElt.getElementsByClassName("syncTabsMenuItem")[0]; michael@0: if (!menuitem) michael@0: return; michael@0: michael@0: if (!PlacesUIUtils.shouldShowTabsFromOtherComputersMenuitem()) { michael@0: menuitem.setAttribute("hidden", true); michael@0: return; michael@0: } michael@0: michael@0: let enabled = PlacesUIUtils.shouldEnableTabsFromOtherComputersMenuitem(); michael@0: menuitem.setAttribute("disabled", !enabled); michael@0: menuitem.setAttribute("hidden", false); michael@0: #endif michael@0: }, michael@0: michael@0: _onPopupShowing: function HM__onPopupShowing(aEvent) { michael@0: PlacesMenu.prototype._onPopupShowing.apply(this, arguments); michael@0: michael@0: // Don't handle events for submenus. michael@0: if (aEvent.target != aEvent.currentTarget) michael@0: return; michael@0: michael@0: this.toggleRecentlyClosedTabs(); michael@0: this.toggleRecentlyClosedWindows(); michael@0: this.toggleTabsFromOtherComputers(); michael@0: }, michael@0: michael@0: _onCommand: function HM__onCommand(aEvent) { michael@0: let placesNode = aEvent.target._placesNode; michael@0: if (placesNode) { michael@0: if (!PrivateBrowsingUtils.isWindowPrivate(window)) michael@0: PlacesUIUtils.markPageAsTyped(placesNode.uri); michael@0: openUILink(placesNode.uri, aEvent, { ignoreAlt: true }); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// BookmarksEventHandler michael@0: michael@0: /** michael@0: * Functions for handling events in the Bookmarks Toolbar and menu. michael@0: */ michael@0: var BookmarksEventHandler = { michael@0: /** michael@0: * Handler for click event for an item in the bookmarks toolbar or menu. michael@0: * Menus and submenus from the folder buttons bubble up to this handler. michael@0: * Left-click is handled in the onCommand function. michael@0: * When items are middle-clicked (or clicked with modifier), open in tabs. michael@0: * If the click came through a menu, close the menu. michael@0: * @param aEvent michael@0: * DOMEvent for the click michael@0: * @param aView michael@0: * The places view which aEvent should be associated with. michael@0: */ michael@0: onClick: function BEH_onClick(aEvent, aView) { michael@0: // Only handle middle-click or left-click with modifiers. michael@0: #ifdef XP_MACOSX michael@0: var modifKey = aEvent.metaKey || aEvent.shiftKey; michael@0: #else michael@0: var modifKey = aEvent.ctrlKey || aEvent.shiftKey; michael@0: #endif michael@0: if (aEvent.button == 2 || (aEvent.button == 0 && !modifKey)) michael@0: return; michael@0: michael@0: var target = aEvent.originalTarget; michael@0: // If this event bubbled up from a menu or menuitem, close the menus. michael@0: // Do this before opening tabs, to avoid hiding the open tabs confirm-dialog. michael@0: if (target.localName == "menu" || target.localName == "menuitem") { michael@0: for (node = target.parentNode; node; node = node.parentNode) { michael@0: if (node.localName == "menupopup") michael@0: node.hidePopup(); michael@0: else if (node.localName != "menu" && michael@0: node.localName != "splitmenu" && michael@0: node.localName != "hbox" && michael@0: node.localName != "vbox" ) michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (target._placesNode && PlacesUtils.nodeIsContainer(target._placesNode)) { michael@0: // Don't open the root folder in tabs when the empty area on the toolbar michael@0: // is middle-clicked or when a non-bookmark item except for Open in Tabs) michael@0: // in a bookmarks menupopup is middle-clicked. michael@0: if (target.localName == "menu" || target.localName == "toolbarbutton") michael@0: PlacesUIUtils.openContainerNodeInTabs(target._placesNode, aEvent, aView); michael@0: } michael@0: else if (aEvent.button == 1) { michael@0: // left-clicks with modifier are already served by onCommand michael@0: this.onCommand(aEvent, aView); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * Handler for command event for an item in the bookmarks toolbar. michael@0: * Menus and submenus from the folder buttons bubble up to this handler. michael@0: * Opens the item. michael@0: * @param aEvent michael@0: * DOMEvent for the command michael@0: * @param aView michael@0: * The places view which aEvent should be associated with. michael@0: */ michael@0: onCommand: function BEH_onCommand(aEvent, aView) { michael@0: var target = aEvent.originalTarget; michael@0: if (target._placesNode) michael@0: PlacesUIUtils.openNodeWithEvent(target._placesNode, aEvent, aView); michael@0: }, michael@0: michael@0: fillInBHTooltip: function BEH_fillInBHTooltip(aDocument, aEvent) { michael@0: var node; michael@0: var cropped = false; michael@0: var targetURI; michael@0: michael@0: if (aDocument.tooltipNode.localName == "treechildren") { michael@0: var tree = aDocument.tooltipNode.parentNode; michael@0: var row = {}, column = {}; michael@0: var tbo = tree.treeBoxObject; michael@0: tbo.getCellAt(aEvent.clientX, aEvent.clientY, row, column, {}); michael@0: if (row.value == -1) michael@0: return false; michael@0: node = tree.view.nodeForTreeIndex(row.value); michael@0: cropped = tbo.isCellCropped(row.value, column.value); michael@0: } michael@0: else { michael@0: // Check whether the tooltipNode is a Places node. michael@0: // In such a case use it, otherwise check for targetURI attribute. michael@0: var tooltipNode = aDocument.tooltipNode; michael@0: if (tooltipNode._placesNode) michael@0: node = tooltipNode._placesNode; michael@0: else { michael@0: // This is a static non-Places node. michael@0: targetURI = tooltipNode.getAttribute("targetURI"); michael@0: } michael@0: } michael@0: michael@0: if (!node && !targetURI) michael@0: return false; michael@0: michael@0: // Show node.label as tooltip's title for non-Places nodes. michael@0: var title = node ? node.title : tooltipNode.label; michael@0: michael@0: // Show URL only for Places URI-nodes or nodes with a targetURI attribute. michael@0: var url; michael@0: if (targetURI || PlacesUtils.nodeIsURI(node)) michael@0: url = targetURI || node.uri; michael@0: michael@0: // Show tooltip for containers only if their title is cropped. michael@0: if (!cropped && !url) michael@0: return false; michael@0: michael@0: var tooltipTitle = aDocument.getElementById("bhtTitleText"); michael@0: tooltipTitle.hidden = (!title || (title == url)); michael@0: if (!tooltipTitle.hidden) michael@0: tooltipTitle.textContent = title; michael@0: michael@0: var tooltipUrl = aDocument.getElementById("bhtUrlText"); michael@0: tooltipUrl.hidden = !url; michael@0: if (!tooltipUrl.hidden) michael@0: tooltipUrl.value = url; michael@0: michael@0: // Show tooltip. michael@0: return true; michael@0: } michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// PlacesMenuDNDHandler michael@0: michael@0: // Handles special drag and drop functionality for Places menus that are not michael@0: // part of a Places view (e.g. the bookmarks menu in the menubar). michael@0: var PlacesMenuDNDHandler = { michael@0: _springLoadDelayMs: 350, michael@0: _closeDelayMs: 500, michael@0: _loadTimer: null, michael@0: _closeTimer: null, michael@0: _closingTimerNode: null, michael@0: michael@0: /** michael@0: * Called when the user enters the element during a drag. michael@0: * @param event michael@0: * The DragEnter event that spawned the opening. michael@0: */ michael@0: onDragEnter: function PMDH_onDragEnter(event) { michael@0: // Opening menus in a Places popup is handled by the view itself. michael@0: if (!this._isStaticContainer(event.target)) michael@0: return; michael@0: michael@0: // If we re-enter the same menu or anchor before the close timer runs out, michael@0: // we should ensure that we do not close: michael@0: if (this._closeTimer && this._closingTimerNode === event.currentTarget) { michael@0: this._closeTimer.cancel(); michael@0: this._closingTimerNode = null; michael@0: this._closeTimer = null; michael@0: } michael@0: michael@0: PlacesControllerDragHelper.currentDropTarget = event.target; michael@0: let popup = event.target.lastChild; michael@0: if (this._loadTimer || popup.state === "showing" || popup.state === "open") michael@0: return; michael@0: michael@0: this._loadTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: this._loadTimer.initWithCallback(() => { michael@0: this._loadTimer = null; michael@0: popup.setAttribute("autoopened", "true"); michael@0: popup.showPopup(popup); michael@0: }, this._springLoadDelayMs, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: event.preventDefault(); michael@0: event.stopPropagation(); michael@0: }, michael@0: michael@0: /** michael@0: * Handles dragleave on the element. michael@0: */ michael@0: onDragLeave: function PMDH_onDragLeave(event) { michael@0: // Handle menu-button separate targets. michael@0: if (event.relatedTarget === event.currentTarget || michael@0: (event.relatedTarget && michael@0: event.relatedTarget.parentNode === event.currentTarget)) michael@0: return; michael@0: michael@0: // Closing menus in a Places popup is handled by the view itself. michael@0: if (!this._isStaticContainer(event.target)) michael@0: return; michael@0: michael@0: PlacesControllerDragHelper.currentDropTarget = null; michael@0: let popup = event.target.lastChild; michael@0: michael@0: if (this._loadTimer) { michael@0: this._loadTimer.cancel(); michael@0: this._loadTimer = null; michael@0: } michael@0: this._closeTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: this._closingTimerNode = event.currentTarget; michael@0: this._closeTimer.initWithCallback(function() { michael@0: this._closeTimer = null; michael@0: this._closingTimerNode = null; michael@0: let node = PlacesControllerDragHelper.currentDropTarget; michael@0: let inHierarchy = false; michael@0: while (node && !inHierarchy) { michael@0: inHierarchy = node == event.target; michael@0: node = node.parentNode; michael@0: } michael@0: if (!inHierarchy && popup && popup.hasAttribute("autoopened")) { michael@0: popup.removeAttribute("autoopened"); michael@0: popup.hidePopup(); michael@0: } michael@0: }, this._closeDelayMs, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: }, michael@0: michael@0: /** michael@0: * Determines if a XUL element represents a static container. michael@0: * @returns true if the element is a container element (menu or michael@0: *` menu-toolbarbutton), false otherwise. michael@0: */ michael@0: _isStaticContainer: function PMDH__isContainer(node) { michael@0: let isMenu = node.localName == "menu" || michael@0: (node.localName == "toolbarbutton" && michael@0: (node.getAttribute("type") == "menu" || michael@0: node.getAttribute("type") == "menu-button")); michael@0: let isStatic = !("_placesNode" in node) && node.lastChild && michael@0: node.lastChild.hasAttribute("placespopup") && michael@0: !node.parentNode.hasAttribute("placespopup"); michael@0: return isMenu && isStatic; michael@0: }, michael@0: michael@0: /** michael@0: * Called when the user drags over the element. michael@0: * @param event michael@0: * The DragOver event. michael@0: */ michael@0: onDragOver: function PMDH_onDragOver(event) { michael@0: let ip = new InsertionPoint(PlacesUtils.bookmarksMenuFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: Ci.nsITreeView.DROP_ON); michael@0: if (ip && PlacesControllerDragHelper.canDrop(ip, event.dataTransfer)) michael@0: event.preventDefault(); michael@0: michael@0: event.stopPropagation(); michael@0: }, michael@0: michael@0: /** michael@0: * Called when the user drops on the element. michael@0: * @param event michael@0: * The Drop event. michael@0: */ michael@0: onDrop: function PMDH_onDrop(event) { michael@0: // Put the item at the end of bookmark menu. michael@0: let ip = new InsertionPoint(PlacesUtils.bookmarksMenuFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: Ci.nsITreeView.DROP_ON); michael@0: PlacesControllerDragHelper.onDrop(ip, event.dataTransfer); michael@0: PlacesControllerDragHelper.currentDropTarget = null; michael@0: event.stopPropagation(); michael@0: } michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// PlacesToolbarHelper michael@0: michael@0: /** michael@0: * This object handles the initialization and uninitialization of the bookmarks michael@0: * toolbar. michael@0: */ michael@0: let PlacesToolbarHelper = { michael@0: _place: "place:folder=TOOLBAR", michael@0: michael@0: get _viewElt() { michael@0: return document.getElementById("PlacesToolbar"); michael@0: }, michael@0: michael@0: get _placeholder() { michael@0: return document.getElementById("bookmarks-toolbar-placeholder"); michael@0: }, michael@0: michael@0: init: function PTH_init(forceToolbarOverflowCheck) { michael@0: let viewElt = this._viewElt; michael@0: if (!viewElt || viewElt._placesView) michael@0: return; michael@0: michael@0: // CustomizableUI.addListener is idempotent, so we can safely michael@0: // call this multiple times. michael@0: CustomizableUI.addListener(this); michael@0: michael@0: // If the bookmarks toolbar item is: michael@0: // - not in a toolbar, or; michael@0: // - the toolbar is collapsed, or; michael@0: // - the toolbar is hidden some other way: michael@0: // don't initialize. Also, there is no need to initialize the toolbar if michael@0: // customizing, because that will happen when the customization is done. michael@0: let toolbar = this._getParentToolbar(viewElt); michael@0: if (!toolbar || toolbar.collapsed || this._isCustomizing || michael@0: getComputedStyle(toolbar, "").display == "none") michael@0: return; michael@0: michael@0: new PlacesToolbar(this._place); michael@0: if (forceToolbarOverflowCheck) { michael@0: viewElt._placesView.updateOverflowStatus(); michael@0: } michael@0: this._setupPlaceholder(); michael@0: }, michael@0: michael@0: uninit: function PTH_uninit() { michael@0: CustomizableUI.removeListener(this); michael@0: }, michael@0: michael@0: customizeStart: function PTH_customizeStart() { michael@0: try { michael@0: let viewElt = this._viewElt; michael@0: if (viewElt && viewElt._placesView) michael@0: viewElt._placesView.uninit(); michael@0: } finally { michael@0: this._isCustomizing = true; michael@0: } michael@0: this._shouldWrap = this._getShouldWrap(); michael@0: }, michael@0: michael@0: customizeChange: function PTH_customizeChange() { michael@0: this._setupPlaceholder(); michael@0: }, michael@0: michael@0: _setupPlaceholder: function PTH_setupPlaceholder() { michael@0: let placeholder = this._placeholder; michael@0: if (!placeholder) { michael@0: return; michael@0: } michael@0: michael@0: let shouldWrapNow = this._getShouldWrap(); michael@0: if (this._shouldWrap != shouldWrapNow) { michael@0: if (shouldWrapNow) { michael@0: placeholder.setAttribute("wrap", "true"); michael@0: } else { michael@0: placeholder.removeAttribute("wrap"); michael@0: } michael@0: this._shouldWrap = shouldWrapNow; michael@0: } michael@0: }, michael@0: michael@0: customizeDone: function PTH_customizeDone() { michael@0: this._isCustomizing = false; michael@0: this.init(true); michael@0: }, michael@0: michael@0: _getShouldWrap: function PTH_getShouldWrap() { michael@0: let placement = CustomizableUI.getPlacementOfWidget("personal-bookmarks"); michael@0: let area = placement && placement.area; michael@0: let areaType = area && CustomizableUI.getAreaType(area); michael@0: return !area || CustomizableUI.TYPE_MENU_PANEL == areaType; michael@0: }, michael@0: michael@0: onPlaceholderCommand: function () { michael@0: let widgetGroup = CustomizableUI.getWidget("personal-bookmarks"); michael@0: let widget = widgetGroup.forWindow(window); michael@0: if (widget.overflowed || michael@0: widgetGroup.areaType == CustomizableUI.TYPE_MENU_PANEL) { michael@0: PlacesCommandHook.showPlacesOrganizer("BookmarksToolbar"); michael@0: } michael@0: }, michael@0: michael@0: _getParentToolbar: function(element) { michael@0: while (element) { michael@0: if (element.localName == "toolbar") { michael@0: return element; michael@0: } michael@0: element = element.parentNode; michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: onWidgetUnderflow: function(aNode, aContainer) { michael@0: // The view gets broken by being removed and reinserted by the overflowable michael@0: // toolbar, so we have to force an uninit and reinit. michael@0: let win = aNode.ownerDocument.defaultView; michael@0: if (aNode.id == "personal-bookmarks" && win == window) { michael@0: this._resetView(); michael@0: } michael@0: }, michael@0: michael@0: onWidgetAdded: function(aWidgetId, aArea, aPosition) { michael@0: if (aWidgetId == "personal-bookmarks" && !this._isCustomizing) { michael@0: // It's possible (with the "Add to Menu", "Add to Toolbar" context michael@0: // options) that the Places Toolbar Items have been moved without michael@0: // letting us prepare and handle it with with customizeStart and michael@0: // customizeDone. If that's the case, we need to reset the views michael@0: // since they're probably broken from the DOM reparenting. michael@0: this._resetView(); michael@0: } michael@0: }, michael@0: michael@0: _resetView: function() { michael@0: if (this._viewElt) { michael@0: // It's possible that the placesView might not exist, and we need to michael@0: // do a full init. This could happen if the Bookmarks Toolbar Items are michael@0: // moved to the Menu Panel, and then to the toolbar with the "Add to Toolbar" michael@0: // context menu option, outside of customize mode. michael@0: if (this._viewElt._placesView) { michael@0: this._viewElt._placesView.uninit(); michael@0: } michael@0: this.init(true); michael@0: } michael@0: }, michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// BookmarkingUI michael@0: michael@0: /** michael@0: * Handles the bookmarks menu-button in the toolbar. michael@0: */ michael@0: michael@0: let BookmarkingUI = { michael@0: BOOKMARK_BUTTON_ID: "bookmarks-menu-button", michael@0: get button() { michael@0: delete this.button; michael@0: let widgetGroup = CustomizableUI.getWidget(this.BOOKMARK_BUTTON_ID); michael@0: return this.button = widgetGroup.forWindow(window).node; michael@0: }, michael@0: michael@0: /* Can't make this a self-deleting getter because it's anonymous content michael@0: * and might lose/regain bindings at some point. */ michael@0: get star() { michael@0: return document.getAnonymousElementByAttribute(this.button, "anonid", michael@0: "button"); michael@0: }, michael@0: michael@0: get anchor() { michael@0: if (!this._shouldUpdateStarState()) { michael@0: return null; michael@0: } michael@0: let widget = CustomizableUI.getWidget(this.BOOKMARK_BUTTON_ID) michael@0: .forWindow(window); michael@0: if (widget.overflowed) michael@0: return widget.anchor; michael@0: michael@0: let star = this.star; michael@0: return star ? document.getAnonymousElementByAttribute(star, "class", michael@0: "toolbarbutton-icon") michael@0: : null; michael@0: }, michael@0: michael@0: get notifier() { michael@0: delete this.notifier; michael@0: return this.notifier = document.getElementById("bookmarked-notification-anchor"); michael@0: }, michael@0: michael@0: get dropmarkerNotifier() { michael@0: delete this.dropmarkerNotifier; michael@0: return this.dropmarkerNotifier = document.getElementById("bookmarked-notification-dropmarker-anchor"); michael@0: }, michael@0: michael@0: get broadcaster() { michael@0: delete this.broadcaster; michael@0: let broadcaster = document.getElementById("bookmarkThisPageBroadcaster"); michael@0: return this.broadcaster = broadcaster; michael@0: }, michael@0: michael@0: STATUS_UPDATING: -1, michael@0: STATUS_UNSTARRED: 0, michael@0: STATUS_STARRED: 1, michael@0: get status() { michael@0: if (!this._shouldUpdateStarState()) { michael@0: return this.STATUS_UNSTARRED; michael@0: } michael@0: if (this._pendingStmt) michael@0: return this.STATUS_UPDATING; michael@0: return this.button.hasAttribute("starred") ? this.STATUS_STARRED michael@0: : this.STATUS_UNSTARRED; michael@0: }, michael@0: michael@0: get _starredTooltip() michael@0: { michael@0: delete this._starredTooltip; michael@0: return this._starredTooltip = michael@0: gNavigatorBundle.getString("starButtonOn.tooltip"); michael@0: }, michael@0: michael@0: get _unstarredTooltip() michael@0: { michael@0: delete this._unstarredTooltip; michael@0: return this._unstarredTooltip = michael@0: gNavigatorBundle.getString("starButtonOff.tooltip"); michael@0: }, michael@0: michael@0: /** michael@0: * The type of the area in which the button is currently located. michael@0: * When in the panel, we don't update the button's icon. michael@0: */ michael@0: _currentAreaType: null, michael@0: _shouldUpdateStarState: function() { michael@0: return this._currentAreaType == CustomizableUI.TYPE_TOOLBAR; michael@0: }, michael@0: michael@0: /** michael@0: * The popup contents must be updated when the user customizes the UI, or michael@0: * changes the personal toolbar collapsed status. In such a case, any needed michael@0: * change should be handled in the popupshowing helper, for performance michael@0: * reasons. michael@0: */ michael@0: _popupNeedsUpdate: true, michael@0: onToolbarVisibilityChange: function BUI_onToolbarVisibilityChange() { michael@0: this._popupNeedsUpdate = true; michael@0: }, michael@0: michael@0: onPopupShowing: function BUI_onPopupShowing(event) { michael@0: // Don't handle events for submenus. michael@0: if (event.target != event.currentTarget) michael@0: return; michael@0: michael@0: // Ideally this code would never be reached, but if you click the outer michael@0: // button's border, some cpp code for the menu button's so-called XBL binding michael@0: // decides to open the popup even though the dropmarker is invisible. michael@0: if (this._currentAreaType == CustomizableUI.TYPE_MENU_PANEL) { michael@0: this._showSubview(); michael@0: event.preventDefault(); michael@0: event.stopPropagation(); michael@0: return; michael@0: } michael@0: michael@0: let widget = CustomizableUI.getWidget(this.BOOKMARK_BUTTON_ID) michael@0: .forWindow(window); michael@0: if (widget.overflowed) { michael@0: // Don't open a popup in the overflow popup, rather just open the Library. michael@0: event.preventDefault(); michael@0: widget.node.removeAttribute("closemenu"); michael@0: PlacesCommandHook.showPlacesOrganizer("BookmarksMenu"); michael@0: return; michael@0: } michael@0: michael@0: if (!this._popupNeedsUpdate) michael@0: return; michael@0: this._popupNeedsUpdate = false; michael@0: michael@0: let popup = event.target; michael@0: let getPlacesAnonymousElement = michael@0: aAnonId => document.getAnonymousElementByAttribute(popup.parentNode, michael@0: "placesanonid", michael@0: aAnonId); michael@0: michael@0: let viewToolbarMenuitem = getPlacesAnonymousElement("view-toolbar"); michael@0: if (viewToolbarMenuitem) { michael@0: // Update View bookmarks toolbar checkbox menuitem. michael@0: viewToolbarMenuitem.classList.add("subviewbutton"); michael@0: let personalToolbar = document.getElementById("PersonalToolbar"); michael@0: viewToolbarMenuitem.setAttribute("checked", !personalToolbar.collapsed); michael@0: } michael@0: }, michael@0: michael@0: attachPlacesView: function(event, node) { michael@0: // If the view is already there, bail out early. michael@0: if (node.parentNode._placesView) michael@0: return; michael@0: michael@0: new PlacesMenu(event, "place:folder=BOOKMARKS_MENU", { michael@0: extraClasses: { michael@0: entry: "subviewbutton", michael@0: footer: "panel-subview-footer" michael@0: }, michael@0: insertionPoint: ".panel-subview-footer" michael@0: }); michael@0: }, michael@0: michael@0: /** michael@0: * Handles star styling based on page proxy state changes. michael@0: */ michael@0: onPageProxyStateChanged: function BUI_onPageProxyStateChanged(aState) { michael@0: if (!this._shouldUpdateStarState() || !this.star) { michael@0: return; michael@0: } michael@0: michael@0: if (aState == "invalid") { michael@0: this.star.setAttribute("disabled", "true"); michael@0: this.button.removeAttribute("starred"); michael@0: this.button.setAttribute("buttontooltiptext", ""); michael@0: } michael@0: else { michael@0: this.star.removeAttribute("disabled"); michael@0: this._updateStar(); michael@0: } michael@0: this._updateToolbarStyle(); michael@0: }, michael@0: michael@0: _updateCustomizationState: function BUI__updateCustomizationState() { michael@0: let placement = CustomizableUI.getPlacementOfWidget(this.BOOKMARK_BUTTON_ID); michael@0: this._currentAreaType = placement && CustomizableUI.getAreaType(placement.area); michael@0: }, michael@0: michael@0: _updateToolbarStyle: function BUI__updateToolbarStyle() { michael@0: let onPersonalToolbar = false; michael@0: if (this._currentAreaType == CustomizableUI.TYPE_TOOLBAR) { michael@0: let personalToolbar = document.getElementById("PersonalToolbar"); michael@0: onPersonalToolbar = this.button.parentNode == personalToolbar || michael@0: this.button.parentNode.parentNode == personalToolbar; michael@0: } michael@0: michael@0: if (onPersonalToolbar) michael@0: this.button.classList.add("bookmark-item"); michael@0: else michael@0: this.button.classList.remove("bookmark-item"); michael@0: }, michael@0: michael@0: _uninitView: function BUI__uninitView() { michael@0: // When an element with a placesView attached is removed and re-inserted, michael@0: // XBL reapplies the binding causing any kind of issues and possible leaks, michael@0: // so kill current view and let popupshowing generate a new one. michael@0: if (this.button._placesView) michael@0: this.button._placesView.uninit(); michael@0: michael@0: // We have to do the same thing for the "special" views underneath the michael@0: // the bookmarks menu. michael@0: const kSpecialViewNodeIDs = ["BMB_bookmarksToolbar", "BMB_unsortedBookmarks"]; michael@0: for (let viewNodeID of kSpecialViewNodeIDs) { michael@0: let elem = document.getElementById(viewNodeID); michael@0: if (elem && elem._placesView) { michael@0: elem._placesView.uninit(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onCustomizeStart: function BUI_customizeStart(aWindow) { michael@0: if (aWindow == window) { michael@0: this._uninitView(); michael@0: this._isCustomizing = true; michael@0: } michael@0: }, michael@0: michael@0: onWidgetAdded: function BUI_widgetAdded(aWidgetId) { michael@0: if (aWidgetId == this.BOOKMARK_BUTTON_ID) { michael@0: this._onWidgetWasMoved(); michael@0: } michael@0: }, michael@0: michael@0: onWidgetRemoved: function BUI_widgetRemoved(aWidgetId) { michael@0: if (aWidgetId == this.BOOKMARK_BUTTON_ID) { michael@0: this._onWidgetWasMoved(); michael@0: } michael@0: }, michael@0: michael@0: onWidgetReset: function BUI_widgetReset(aNode, aContainer) { michael@0: if (aNode == this.button) { michael@0: this._onWidgetWasMoved(); michael@0: } michael@0: }, michael@0: michael@0: onWidgetUndoMove: function BUI_undoWidgetUndoMove(aNode, aContainer) { michael@0: if (aNode == this.button) { michael@0: this._onWidgetWasMoved(); michael@0: } michael@0: }, michael@0: michael@0: _onWidgetWasMoved: function BUI_widgetWasMoved() { michael@0: let usedToUpdateStarState = this._shouldUpdateStarState(); michael@0: this._updateCustomizationState(); michael@0: if (!usedToUpdateStarState && this._shouldUpdateStarState()) { michael@0: this.updateStarState(); michael@0: } else if (usedToUpdateStarState && !this._shouldUpdateStarState()) { michael@0: this._updateStar(); michael@0: } michael@0: // If we're moved outside of customize mode, we need to uninit michael@0: // our view so it gets reconstructed. michael@0: if (!this._isCustomizing) { michael@0: this._uninitView(); michael@0: } michael@0: this._updateToolbarStyle(); michael@0: }, michael@0: michael@0: onCustomizeEnd: function BUI_customizeEnd(aWindow) { michael@0: if (aWindow == window) { michael@0: this._isCustomizing = false; michael@0: this.onToolbarVisibilityChange(); michael@0: this._updateToolbarStyle(); michael@0: } michael@0: }, michael@0: michael@0: init: function() { michael@0: CustomizableUI.addListener(this); michael@0: this._updateCustomizationState(); michael@0: }, michael@0: michael@0: _hasBookmarksObserver: false, michael@0: _itemIds: [], michael@0: uninit: function BUI_uninit() { michael@0: this._updateBookmarkPageMenuItem(true); michael@0: CustomizableUI.removeListener(this); michael@0: michael@0: this._uninitView(); michael@0: michael@0: if (this._hasBookmarksObserver) { michael@0: PlacesUtils.removeLazyBookmarkObserver(this); michael@0: } michael@0: michael@0: if (this._pendingStmt) { michael@0: this._pendingStmt.cancel(); michael@0: delete this._pendingStmt; michael@0: } michael@0: }, michael@0: michael@0: onLocationChange: function BUI_onLocationChange() { michael@0: if (this._uri && gBrowser.currentURI.equals(this._uri)) { michael@0: return; michael@0: } michael@0: this.updateStarState(); michael@0: }, michael@0: michael@0: updateStarState: function BUI_updateStarState() { michael@0: // Reset tracked values. michael@0: this._uri = gBrowser.currentURI; michael@0: this._itemIds = []; michael@0: michael@0: if (this._pendingStmt) { michael@0: this._pendingStmt.cancel(); michael@0: delete this._pendingStmt; michael@0: } michael@0: michael@0: // We can load about:blank before the actual page, but there is no point in handling that page. michael@0: if (isBlankPageURL(this._uri.spec)) { michael@0: return; michael@0: } michael@0: michael@0: this._pendingStmt = PlacesUtils.asyncGetBookmarkIds(this._uri, (aItemIds, aURI) => { michael@0: // Safety check that the bookmarked URI equals the tracked one. michael@0: if (!aURI.equals(this._uri)) { michael@0: Components.utils.reportError("BookmarkingUI did not receive current URI"); michael@0: return; michael@0: } michael@0: michael@0: // It's possible that onItemAdded gets called before the async statement michael@0: // calls back. For such an edge case, retain all unique entries from both michael@0: // arrays. michael@0: this._itemIds = this._itemIds.filter( michael@0: function (id) aItemIds.indexOf(id) == -1 michael@0: ).concat(aItemIds); michael@0: michael@0: this._updateStar(); michael@0: michael@0: // Start observing bookmarks if needed. michael@0: if (!this._hasBookmarksObserver) { michael@0: try { michael@0: PlacesUtils.addLazyBookmarkObserver(this); michael@0: this._hasBookmarksObserver = true; michael@0: } catch(ex) { michael@0: Components.utils.reportError("BookmarkingUI failed adding a bookmarks observer: " + ex); michael@0: } michael@0: } michael@0: michael@0: delete this._pendingStmt; michael@0: }); michael@0: }, michael@0: michael@0: _updateStar: function BUI__updateStar() { michael@0: if (!this._shouldUpdateStarState()) { michael@0: if (this.button.hasAttribute("starred")) { michael@0: this.button.removeAttribute("starred"); michael@0: this.button.removeAttribute("buttontooltiptext"); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (this._itemIds.length > 0) { michael@0: this.button.setAttribute("starred", "true"); michael@0: this.button.setAttribute("buttontooltiptext", this._starredTooltip); michael@0: if (this.button.getAttribute("overflowedItem") == "true") { michael@0: this.button.setAttribute("label", this._starButtonOverflowedStarredLabel); michael@0: } michael@0: } michael@0: else { michael@0: this.button.removeAttribute("starred"); michael@0: this.button.setAttribute("buttontooltiptext", this._unstarredTooltip); michael@0: if (this.button.getAttribute("overflowedItem") == "true") { michael@0: this.button.setAttribute("label", this._starButtonOverflowedLabel); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * forceReset is passed when we're destroyed and the label should go back michael@0: * to the default (Bookmark This Page) for OS X. michael@0: */ michael@0: _updateBookmarkPageMenuItem: function BUI__updateBookmarkPageMenuItem(forceReset) { michael@0: let isStarred = !forceReset && this._itemIds.length > 0; michael@0: let label = isStarred ? "editlabel" : "bookmarklabel"; michael@0: this.broadcaster.setAttribute("label", this.broadcaster.getAttribute(label)); michael@0: }, michael@0: michael@0: onMainMenuPopupShowing: function BUI_onMainMenuPopupShowing(event) { michael@0: this._updateBookmarkPageMenuItem(); michael@0: PlacesCommandHook.updateBookmarkAllTabsCommand(); michael@0: }, michael@0: michael@0: _showBookmarkedNotification: function BUI_showBookmarkedNotification() { michael@0: function getCenteringTransformForRects(rectToPosition, referenceRect) { michael@0: let topDiff = referenceRect.top - rectToPosition.top; michael@0: let leftDiff = referenceRect.left - rectToPosition.left; michael@0: let heightDiff = referenceRect.height - rectToPosition.height; michael@0: let widthDiff = referenceRect.width - rectToPosition.width; michael@0: return [(leftDiff + .5 * widthDiff) + "px", (topDiff + .5 * heightDiff) + "px"]; michael@0: } michael@0: michael@0: if (this._notificationTimeout) { michael@0: clearTimeout(this._notificationTimeout); michael@0: } michael@0: michael@0: if (this.notifier.style.transform == '') { michael@0: // Get all the relevant nodes and computed style objects michael@0: let dropmarker = document.getAnonymousElementByAttribute(this.button, "anonid", "dropmarker"); michael@0: let dropmarkerIcon = document.getAnonymousElementByAttribute(dropmarker, "class", "dropmarker-icon"); michael@0: let dropmarkerStyle = getComputedStyle(dropmarkerIcon); michael@0: michael@0: // Check for RTL and get bounds michael@0: let isRTL = getComputedStyle(this.button).direction == "rtl"; michael@0: let buttonRect = this.button.getBoundingClientRect(); michael@0: let notifierRect = this.notifier.getBoundingClientRect(); michael@0: let dropmarkerRect = dropmarkerIcon.getBoundingClientRect(); michael@0: let dropmarkerNotifierRect = this.dropmarkerNotifier.getBoundingClientRect(); michael@0: michael@0: // Compute, but do not set, transform for star icon michael@0: let [translateX, translateY] = getCenteringTransformForRects(notifierRect, buttonRect); michael@0: let starIconTransform = "translate(" + translateX + ", " + translateY + ")"; michael@0: if (isRTL) { michael@0: starIconTransform += " scaleX(-1)"; michael@0: } michael@0: michael@0: // Compute, but do not set, transform for dropmarker michael@0: [translateX, translateY] = getCenteringTransformForRects(dropmarkerNotifierRect, dropmarkerRect); michael@0: let dropmarkerTransform = "translate(" + translateX + ", " + translateY + ")"; michael@0: michael@0: // Do all layout invalidation in one go: michael@0: this.notifier.style.transform = starIconTransform; michael@0: this.dropmarkerNotifier.style.transform = dropmarkerTransform; michael@0: michael@0: let dropmarkerAnimationNode = this.dropmarkerNotifier.firstChild; michael@0: dropmarkerAnimationNode.style.MozImageRegion = dropmarkerStyle.MozImageRegion; michael@0: dropmarkerAnimationNode.style.listStyleImage = dropmarkerStyle.listStyleImage; michael@0: } michael@0: michael@0: let isInOverflowPanel = this.button.getAttribute("overflowedItem") == "true"; michael@0: if (!isInOverflowPanel) { michael@0: this.notifier.setAttribute("notification", "finish"); michael@0: this.button.setAttribute("notification", "finish"); michael@0: this.dropmarkerNotifier.setAttribute("notification", "finish"); michael@0: } michael@0: michael@0: this._notificationTimeout = setTimeout( () => { michael@0: this.notifier.removeAttribute("notification"); michael@0: this.dropmarkerNotifier.removeAttribute("notification"); michael@0: this.button.removeAttribute("notification"); michael@0: michael@0: this.dropmarkerNotifier.style.transform = ''; michael@0: this.notifier.style.transform = ''; michael@0: }, 1000); michael@0: }, michael@0: michael@0: _showSubview: function() { michael@0: let view = document.getElementById("PanelUI-bookmarks"); michael@0: view.addEventListener("ViewShowing", this); michael@0: view.addEventListener("ViewHiding", this); michael@0: let anchor = document.getElementById(this.BOOKMARK_BUTTON_ID); michael@0: anchor.setAttribute("closemenu", "none"); michael@0: PanelUI.showSubView("PanelUI-bookmarks", anchor, michael@0: CustomizableUI.AREA_PANEL); michael@0: }, michael@0: michael@0: onCommand: function BUI_onCommand(aEvent) { michael@0: if (aEvent.target != aEvent.currentTarget) { michael@0: return; michael@0: } michael@0: michael@0: // Handle special case when the button is in the panel. michael@0: let isBookmarked = this._itemIds.length > 0; michael@0: michael@0: if (this._currentAreaType == CustomizableUI.TYPE_MENU_PANEL) { michael@0: this._showSubview(); michael@0: return; michael@0: } michael@0: let widget = CustomizableUI.getWidget(this.BOOKMARK_BUTTON_ID) michael@0: .forWindow(window); michael@0: if (widget.overflowed) { michael@0: // Allow to close the panel if the page is already bookmarked, cause michael@0: // we are going to open the edit bookmark panel. michael@0: if (isBookmarked) michael@0: widget.node.removeAttribute("closemenu"); michael@0: else michael@0: widget.node.setAttribute("closemenu", "none"); michael@0: } michael@0: michael@0: // Ignore clicks on the star if we are updating its state. michael@0: if (!this._pendingStmt) { michael@0: if (!isBookmarked) michael@0: this._showBookmarkedNotification(); michael@0: PlacesCommandHook.bookmarkCurrentPage(isBookmarked); michael@0: } michael@0: }, michael@0: michael@0: handleEvent: function BUI_handleEvent(aEvent) { michael@0: switch (aEvent.type) { michael@0: case "ViewShowing": michael@0: this.onPanelMenuViewShowing(aEvent); michael@0: break; michael@0: case "ViewHiding": michael@0: this.onPanelMenuViewHiding(aEvent); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: onPanelMenuViewShowing: function BUI_onViewShowing(aEvent) { michael@0: this._updateBookmarkPageMenuItem(); michael@0: // Update checked status of the toolbar toggle. michael@0: let viewToolbar = document.getElementById("panelMenu_viewBookmarksToolbar"); michael@0: let personalToolbar = document.getElementById("PersonalToolbar"); michael@0: if (personalToolbar.collapsed) michael@0: viewToolbar.removeAttribute("checked"); michael@0: else michael@0: viewToolbar.setAttribute("checked", "true"); michael@0: // Setup the Places view. michael@0: this._panelMenuView = new PlacesPanelMenuView("place:folder=BOOKMARKS_MENU", michael@0: "panelMenu_bookmarksMenu", michael@0: "panelMenu_bookmarksMenu", { michael@0: extraClasses: { michael@0: entry: "subviewbutton", michael@0: footer: "panel-subview-footer" michael@0: } michael@0: }); michael@0: aEvent.target.removeEventListener("ViewShowing", this); michael@0: }, michael@0: michael@0: onPanelMenuViewHiding: function BUI_onViewHiding(aEvent) { michael@0: this._panelMenuView.uninit(); michael@0: delete this._panelMenuView; michael@0: aEvent.target.removeEventListener("ViewHiding", this); michael@0: }, michael@0: michael@0: onPanelMenuViewCommand: function BUI_onPanelMenuViewCommand(aEvent, aView) { michael@0: let target = aEvent.originalTarget; michael@0: if (!target._placesNode) michael@0: return; michael@0: if (PlacesUtils.nodeIsContainer(target._placesNode)) michael@0: PlacesCommandHook.showPlacesOrganizer([ "BookmarksMenu", target._placesNode.itemId ]); michael@0: else michael@0: PlacesUIUtils.openNodeWithEvent(target._placesNode, aEvent, aView); michael@0: PanelUI.hide(); michael@0: }, michael@0: michael@0: // nsINavBookmarkObserver michael@0: onItemAdded: function BUI_onItemAdded(aItemId, aParentId, aIndex, aItemType, michael@0: aURI) { michael@0: if (aURI && aURI.equals(this._uri)) { michael@0: // If a new bookmark has been added to the tracked uri, register it. michael@0: if (this._itemIds.indexOf(aItemId) == -1) { michael@0: this._itemIds.push(aItemId); michael@0: // Only need to update the UI if it wasn't marked as starred before: michael@0: if (this._itemIds.length == 1) { michael@0: this._updateStar(); michael@0: } michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onItemRemoved: function BUI_onItemRemoved(aItemId) { michael@0: let index = this._itemIds.indexOf(aItemId); michael@0: // If one of the tracked bookmarks has been removed, unregister it. michael@0: if (index != -1) { michael@0: this._itemIds.splice(index, 1); michael@0: // Only need to update the UI if the page is no longer starred michael@0: if (this._itemIds.length == 0) { michael@0: this._updateStar(); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onItemChanged: function BUI_onItemChanged(aItemId, aProperty, michael@0: aIsAnnotationProperty, aNewValue) { michael@0: if (aProperty == "uri") { michael@0: let index = this._itemIds.indexOf(aItemId); michael@0: // If the changed bookmark was tracked, check if it is now pointing to michael@0: // a different uri and unregister it. michael@0: if (index != -1 && aNewValue != this._uri.spec) { michael@0: this._itemIds.splice(index, 1); michael@0: // Only need to update the UI if the page is no longer starred michael@0: if (this._itemIds.length == 0) { michael@0: this._updateStar(); michael@0: } michael@0: } michael@0: // If another bookmark is now pointing to the tracked uri, register it. michael@0: else if (index == -1 && aNewValue == this._uri.spec) { michael@0: this._itemIds.push(aItemId); michael@0: // Only need to update the UI if it wasn't marked as starred before: michael@0: if (this._itemIds.length == 1) { michael@0: this._updateStar(); michael@0: } michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onBeginUpdateBatch: function () {}, michael@0: onEndUpdateBatch: function () {}, michael@0: onBeforeItemRemoved: function () {}, michael@0: onItemVisited: function () {}, michael@0: onItemMoved: function () {}, michael@0: michael@0: // CustomizableUI events: michael@0: _starButtonLabel: null, michael@0: get _starButtonOverflowedLabel() { michael@0: delete this._starButtonOverflowedLabel; michael@0: return this._starButtonOverflowedLabel = michael@0: gNavigatorBundle.getString("starButtonOverflowed.label"); michael@0: }, michael@0: get _starButtonOverflowedStarredLabel() { michael@0: delete this._starButtonOverflowedStarredLabel; michael@0: return this._starButtonOverflowedStarredLabel = michael@0: gNavigatorBundle.getString("starButtonOverflowedStarred.label"); michael@0: }, michael@0: onWidgetOverflow: function(aNode, aContainer) { michael@0: let win = aNode.ownerDocument.defaultView; michael@0: if (aNode.id != this.BOOKMARK_BUTTON_ID || win != window) michael@0: return; michael@0: michael@0: let currentLabel = aNode.getAttribute("label"); michael@0: if (!this._starButtonLabel) michael@0: this._starButtonLabel = currentLabel; michael@0: michael@0: if (currentLabel == this._starButtonLabel) { michael@0: let desiredLabel = this._itemIds.length > 0 ? this._starButtonOverflowedStarredLabel michael@0: : this._starButtonOverflowedLabel; michael@0: aNode.setAttribute("label", desiredLabel); michael@0: } michael@0: }, michael@0: michael@0: onWidgetUnderflow: function(aNode, aContainer) { michael@0: let win = aNode.ownerDocument.defaultView; michael@0: if (aNode.id != this.BOOKMARK_BUTTON_ID || win != window) michael@0: return; michael@0: michael@0: // The view gets broken by being removed and reinserted. Uninit michael@0: // here so popupshowing will generate a new one: michael@0: this._uninitView(); michael@0: michael@0: if (aNode.getAttribute("label") != this._starButtonLabel) michael@0: aNode.setAttribute("label", this._starButtonLabel); michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsINavBookmarkObserver michael@0: ]) michael@0: };