sidebarUtils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. // This Source Code Form is subject to the terms of the Mozilla Public
  3. // License, v. 2.0. If a copy of the MPL was not distributed with this
  4. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. var SidebarUtils = {
  6. handleTreeClick: function(aTree, aEvent, aGutterSelect) {
  7. // right-clicks are not handled here
  8. if (aEvent.button == 2)
  9. return;
  10. var tbo = aTree.treeBoxObject;
  11. var cell = tbo.getCellAt(aEvent.clientX, aEvent.clientY);
  12. if (cell.row == -1 || cell.childElt == "twisty")
  13. return;
  14. var mouseInGutter = false;
  15. if (aGutterSelect) {
  16. var rect = tbo.getCoordsForCellItem(cell.row, cell.col, "image");
  17. // getCoordsForCellItem returns the x coordinate in logical coordinates
  18. // (i.e., starting from the left and right sides in LTR and RTL modes,
  19. // respectively.) Therefore, we make sure to exclude the blank area
  20. // before the tree item icon (that is, to the left or right of it in
  21. // LTR and RTL modes, respectively) from the click target area.
  22. var isRTL = window.getComputedStyle(aTree, null).direction == "rtl";
  23. if (isRTL)
  24. mouseInGutter = aEvent.clientX > rect.x;
  25. else
  26. mouseInGutter = aEvent.clientX < rect.x;
  27. }
  28. var modifKey = aEvent.ctrlKey || aEvent.shiftKey;
  29. var isContainer = tbo.view.isContainer(cell.row);
  30. var openInTabs = isContainer &&
  31. (aEvent.button == 1 ||
  32. (aEvent.button == 0 && modifKey)) &&
  33. PlacesUtils.hasChildURIs(tbo.view.nodeForTreeIndex(cell.row));
  34. if (aEvent.button == 0 && isContainer && !openInTabs) {
  35. tbo.view.toggleOpenState(cell.row);
  36. return;
  37. }
  38. else if (!mouseInGutter && openInTabs &&
  39. aEvent.originalTarget.localName == "treechildren") {
  40. tbo.view.selection.select(cell.row);
  41. PlacesUIUtils.openContainerNodeInTabs(aTree.selectedNode, aEvent, aTree);
  42. }
  43. else if (!mouseInGutter && !isContainer &&
  44. aEvent.originalTarget.localName == "treechildren") {
  45. // Clear all other selection since we're loading a link now. We must
  46. // do this *before* attempting to load the link since openURL uses
  47. // selection as an indication of which link to load.
  48. tbo.view.selection.select(cell.row);
  49. PlacesUIUtils.openNodeWithEvent(aTree.selectedNode, aEvent, aTree);
  50. }
  51. },
  52. handleTreeKeyPress: function(aEvent) {
  53. // XXX Bug 627901: Post Fx4, this method should take a tree parameter.
  54. let tree = aEvent.target;
  55. let node = tree.selectedNode;
  56. if (node) {
  57. if (aEvent.keyCode == KeyEvent.DOM_VK_RETURN)
  58. PlacesUIUtils.openNodeWithEvent(node, aEvent, tree);
  59. }
  60. },
  61. /**
  62. * The following function displays the URL of a node that is being
  63. * hovered over.
  64. */
  65. handleTreeMouseMove: function(aEvent) {
  66. if (aEvent.target.localName != "treechildren")
  67. return;
  68. var tree = aEvent.target.parentNode;
  69. var tbo = tree.treeBoxObject;
  70. var cell = tbo.getCellAt(aEvent.clientX, aEvent.clientY);
  71. // cell.row is -1 when the mouse is hovering an empty area within the tree.
  72. // To avoid showing a URL from a previously hovered node for a currently
  73. // hovered non-url node, we must clear the moused-over URL in these cases.
  74. if (cell.row != -1) {
  75. var node = tree.view.nodeForTreeIndex(cell.row);
  76. if (PlacesUtils.nodeIsURI(node))
  77. this.setMouseoverURL(node.uri);
  78. else
  79. this.setMouseoverURL("");
  80. }
  81. else
  82. this.setMouseoverURL("");
  83. },
  84. setMouseoverURL: function(aURL) {
  85. // When the browser window is closed with an open sidebar, the sidebar
  86. // unload event happens after the browser's one. In this case
  87. // top.XULBrowserWindow has been nullified already.
  88. if (top.XULBrowserWindow) {
  89. top.XULBrowserWindow.setOverLink(aURL, null);
  90. }
  91. }
  92. };