michael@0: # -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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: var SidebarUtils = { michael@0: handleTreeClick: function SU_handleTreeClick(aTree, aEvent, aGutterSelect) { michael@0: // right-clicks are not handled here michael@0: if (aEvent.button == 2) michael@0: return; michael@0: michael@0: var tbo = aTree.treeBoxObject; michael@0: var row = { }, col = { }, obj = { }; michael@0: tbo.getCellAt(aEvent.clientX, aEvent.clientY, row, col, obj); michael@0: michael@0: if (row.value == -1 || obj.value == "twisty") michael@0: return; michael@0: michael@0: var mouseInGutter = false; michael@0: if (aGutterSelect) { michael@0: var x = { }, y = { }, w = { }, h = { }; michael@0: tbo.getCoordsForCellItem(row.value, col.value, "image", michael@0: x, y, w, h); michael@0: // getCoordsForCellItem returns the x coordinate in logical coordinates michael@0: // (i.e., starting from the left and right sides in LTR and RTL modes, michael@0: // respectively.) Therefore, we make sure to exclude the blank area michael@0: // before the tree item icon (that is, to the left or right of it in michael@0: // LTR and RTL modes, respectively) from the click target area. michael@0: var isRTL = window.getComputedStyle(aTree, null).direction == "rtl"; michael@0: if (isRTL) michael@0: mouseInGutter = aEvent.clientX > x.value; michael@0: else michael@0: mouseInGutter = aEvent.clientX < x.value; michael@0: } michael@0: 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: michael@0: var isContainer = tbo.view.isContainer(row.value); michael@0: var openInTabs = isContainer && michael@0: (aEvent.button == 1 || michael@0: (aEvent.button == 0 && modifKey)) && michael@0: PlacesUtils.hasChildURIs(tbo.view.nodeForTreeIndex(row.value)); michael@0: michael@0: if (aEvent.button == 0 && isContainer && !openInTabs) { michael@0: tbo.view.toggleOpenState(row.value); michael@0: return; michael@0: } michael@0: else if (!mouseInGutter && openInTabs && michael@0: aEvent.originalTarget.localName == "treechildren") { michael@0: tbo.view.selection.select(row.value); michael@0: PlacesUIUtils.openContainerNodeInTabs(aTree.selectedNode, aEvent, aTree); michael@0: } michael@0: else if (!mouseInGutter && !isContainer && michael@0: aEvent.originalTarget.localName == "treechildren") { michael@0: // Clear all other selection since we're loading a link now. We must michael@0: // do this *before* attempting to load the link since openURL uses michael@0: // selection as an indication of which link to load. michael@0: tbo.view.selection.select(row.value); michael@0: PlacesUIUtils.openNodeWithEvent(aTree.selectedNode, aEvent, aTree); michael@0: } michael@0: }, michael@0: michael@0: handleTreeKeyPress: function SU_handleTreeKeyPress(aEvent) { michael@0: // XXX Bug 627901: Post Fx4, this method should take a tree parameter. michael@0: let tree = aEvent.target; michael@0: let node = tree.selectedNode; michael@0: if (node) { michael@0: if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN) michael@0: PlacesUIUtils.openNodeWithEvent(node, aEvent, tree); michael@0: } michael@0: }, michael@0: michael@0: /** michael@0: * The following function displays the URL of a node that is being michael@0: * hovered over. michael@0: */ michael@0: handleTreeMouseMove: function SU_handleTreeMouseMove(aEvent) { michael@0: if (aEvent.target.localName != "treechildren") michael@0: return; michael@0: michael@0: var tree = aEvent.target.parentNode; michael@0: var tbo = tree.treeBoxObject; michael@0: var row = { }, col = { }, obj = { }; michael@0: tbo.getCellAt(aEvent.clientX, aEvent.clientY, row, col, obj); michael@0: michael@0: // row.value is -1 when the mouse is hovering an empty area within the tree. michael@0: // To avoid showing a URL from a previously hovered node for a currently michael@0: // hovered non-url node, we must clear the moused-over URL in these cases. michael@0: if (row.value != -1) { michael@0: var node = tree.view.nodeForTreeIndex(row.value); michael@0: if (PlacesUtils.nodeIsURI(node)) michael@0: this.setMouseoverURL(node.uri); michael@0: else michael@0: this.setMouseoverURL(""); michael@0: } michael@0: else michael@0: this.setMouseoverURL(""); michael@0: }, michael@0: michael@0: setMouseoverURL: function SU_setMouseoverURL(aURL) { michael@0: // When the browser window is closed with an open sidebar, the sidebar michael@0: // unload event happens after the browser's one. In this case michael@0: // top.XULBrowserWindow has been nullified already. michael@0: if (top.XULBrowserWindow) { michael@0: top.XULBrowserWindow.setOverLink(aURL, null); michael@0: } michael@0: } michael@0: };