DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (920bcf17a9e1)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
<?xml version="1.0"?>

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is the Places Toolbar View.
#
# The Initial Developer of the Original Code is Google Inc.
# Portions created by the Initial Developer are Copyright (C) 2005-2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Annie Sullivan <annie.sullivan@gmail.com>
#   Ben Goodger <beng@google.com>
#   Myk Melez <myk@mozilla.org>
#   Marco Bonardo <mak77@bonardo.net>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK ***** 


<!DOCTYPE bindings [
<!ENTITY % globalDTD SYSTEM "chrome://global/locale/global.dtd" >
%globalDTD;
<!ENTITY % browserDTD SYSTEM "chrome://browser/locale/browser.dtd" >
%browserDTD;
]>

<bindings id="placesToolbarBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xbl="http://www.mozilla.org/xbl"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="places-bar">
    <resources>
      <stylesheet src="chrome://browser/skin/places/places.css"/>
    </resources>

    <content>
      <xul:vbox>
        <xul:hbox class="toolbar-drop-indicator-bar">
          <xul:hbox class="toolbar-drop-indicator"/>
        </xul:hbox>
        <xul:hbox flex="1">
          <xul:hbox class="bookmarks-toolbar-items places-toolbar-items" flex="1">
            <children/>
          </xul:hbox>
          <xul:toolbarbutton class="bookmark-item bookmarks-toolbar-customize"
                             mousethrough="never"
                             label="&bookmarksToolbarItem.label;"/>
        </xul:hbox>
      </xul:vbox>
      <xul:hbox mousethrough="always"
                flex="1"
                pack="end">
        <xul:toolbarbutton type="menu"
                           class="chevron"
                           mousethrough="never"
                           collapsed="true"
                           tooltiptext="&bookmarksToolbarChevron.tooltip;"
                           chromedir="&locale.dir;"
                           onpopupshowing="chevronPopupShowing(event);">
          <xul:menupopup anonid="chevronPopup"
                         xbl:inherits="tooltip"
#ifndef XP_MACOSX
                         context="placesContext"
#endif
          />
        </xul:toolbarbutton>
      </xul:hbox>
    </content>

    <implementation implements="nsIAccessibleProvider, nsITimerCallback">
      <constructor><![CDATA[
        this._init();
      ]]></constructor>

      <destructor><![CDATA[
        if (this._result) {
          this._result.viewer = null;
          this._result = null;
        }
      ]]></destructor>

      <property name="controller"
                readonly="true"
                onget="return this._controller;"/>

      <method name="_init">
        <body><![CDATA[
        this._controller = new PlacesController(this);
        this.controllers.appendController(this._controller);

        var t = this;
        window.addEventListener("resize",
                                function f(e) { t.updateChevron(e); },
                                false);

        if (this.hasAttribute("place")) {
          // Do the initial build. 
          this.place = this.place;
        }
        ]]></body>
      </method>

      <field name="_dropIndicatorBar">document.getAnonymousElementByAttribute(this, "class", "toolbar-drop-indicator-bar")</field>
      <field name="_chevron">document.getAnonymousElementByAttribute(this, "class", "chevron")</field>

      <field name="_openedMenuButton">null</field>

      <field name="_result">null</field>

      <!-- nsIPlacesView -->
      <method name="getResult">
        <body><![CDATA[
          return this._result;
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <method name="getResultNode">
        <body><![CDATA[
          return this._result.root;
        ]]></body>
      </method>

      <method name="_rebuild">
        <body><![CDATA[
          // Clear out references to existing nodes, since we'll be deleting and re-adding.
          if (this._overFolder.node)
            this._clearOverFolder();
          this._openedMenuButton = null;

          while (this.hasChildNodes())
            this.removeChild(this.firstChild);

          var rootNode = this._result.root;
          var cc = rootNode.childCount;
          for (var i = 0; i < cc; ++i)
            this.insertNewItem(rootNode.getChild(i), null);

          var chevronPopup = this._chevron.lastChild;
          if (chevronPopup.hasAttribute("type")) {
            // Otherwise we'll set it when the chevron is enabled (see updateChevron)
            chevronPopup.place = this.place;
          }

          while (chevronPopup.hasChildNodes())
            chevronPopup.removeChild(chevronPopup.lastChild);

          // This needs to be in a timeout to make sure our boxObject has time
          // to get its proper size
          var t = this;
          setTimeout(function() { t.updateChevron(); }, 0);
        ]]></body>
      </method>

      <method name="insertNewItem">
        <parameter name="aChild"/>
        <parameter name="aBefore"/>
        <body><![CDATA[
          var type = aChild.type;
          var button;
          if (type == Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR)
            button = document.createElement("toolbarseparator");
          else {
            button = document.createElement("toolbarbutton");
            button.className = "bookmark-item";
            button.setAttribute("label", aChild.title);
            var iconURI = aChild.icon;
            var iconURISpec = "";
            if (iconURI) {
              iconURISpec = iconURI.spec;
              button.setAttribute("image", iconURISpec);
            }

            if (PlacesUtils.containerTypes.indexOf(type) != -1) {
              button.setAttribute("type", "menu");
              button.setAttribute("container", "true");

              if (PlacesUtils.nodeIsQuery(aChild)) {
                button.setAttribute("query", "true");
                if (PlacesUtils.nodeIsTagQuery(aChild))
                  button.setAttribute("tagContainer", "true");
              }
              else if (PlacesUtils.nodeIsLivemarkContainer(aChild))
                button.setAttribute("livemark", "true");

              var popup = document.createElement("menupopup");
              popup.setAttribute("placespopup", "true");
              button.appendChild(popup);
              popup._result = this._result;
              popup._resultNode = asContainer(aChild);
#ifndef XP_MACOSX
              popup.setAttribute("context", "placesContext");
#endif
              this._containerNodesMap.push({ resultNode: aChild,
                                             domNode: popup });
            }
            else if (PlacesUtils.nodeIsURI(aChild)) {
              button.setAttribute("scheme", PlacesUIUtils.guessUrlSchemeForUI(aChild.uri));
            }
          }

          button.node = aChild;
          button.node.viewIndex = 0;
          if (aBefore)
            this.insertBefore(button, aBefore);
          else
            this.appendChild(button);
        ]]></body>
      </method>

      <method name="removeItem">
        <parameter name="child"/>
        <body><![CDATA[
          if (PlacesUtils.nodeIsContainer(child.node)) {
            for (var i=0; i < this._containerNodesMap.length; i++) {
              if (this._containerNodesMap[i].resultNode == child.node) {
                this._containerNodesMap.splice(i, 1);
                break;
              }
            }
          }

          // if document.popupNode pointed to this child, null it out,
          // otherwise controller's command-updating may rely on the removed
          // item still being "selected".
          if (document.popupNode == child)
            document.popupNode = null;
          child.parentNode.removeChild(child);
        ]]></body>
      </method>

      <method name="chevronPopupShowing">
        <parameter name="aEvent"/>
        <body><![CDATA[
          var popup = aEvent.target;
          if (popup != this._chevron.firstChild)
            return;

          for (var i = 0; i < popup.childNodes.length; i++)
            popup.childNodes[i].hidden = !this.childNodes[i].collapsed;
        ]]></body>
      </method>

      <method name="getElementWidth">
        <parameter name="element"/>
        <body><![CDATA[
          var style = document.defaultView.getComputedStyle(element, "");
          var leftMargin = style.getPropertyValue("margin-left");
          leftMargin = leftMargin ? Math.round(parseFloat(leftMargin)) : 0;
          var rightMargin = style.getPropertyValue("margin-right");
          rightMargin = rightMargin ? Math.round(parseFloat(rightMargin)) : 0;
          return element.boxObject.width + leftMargin + rightMargin;
        ]]></body>
      </method>

      <method name="updateChevron">
        <parameter name="event"/>
        <body><![CDATA[
          // Ignore events that aren't on the document or the window
          // (html document, tooltips, etc)
          // Do not ignore content window resizes, because they may
          // be the result of the toolbar being shown/hidden
          if (event && event.target != document && event.target != window &&
              event.target != content)
            return;

          if (this.childNodes.length == 0) {
            this._chevron.collapsed = true;
            return;
          }

          var spaceLeft = this.boxObject.width;
          this._chevron.collapsed = false;
          var chevronWidth = this._chevron.boxObject.width;
          var overflowed = false;
          for (var i = 0; i < this.childNodes.length; i++) {
            var child = this.childNodes[i];
            child.collapsed = false;
            spaceLeft -= this.getElementWidth(child);
            var spaceNeeded = (i == this.childNodes.length - 1) ? 0 : chevronWidth;
            if (spaceLeft < spaceNeeded) {
              overflowed = true;
              child.collapsed = true;
            }
          }
          if (!(this._chevron.collapsed = !overflowed)) {
            // Attach the popup binding to the chevron popup
            var popup = this._chevron.firstChild;
            if (!popup.hasAttribute("type")) {
              popup.setAttribute("place", this.place);
              popup.setAttribute("type", "places");
            }
          }

          // We rebuild the chevron on popupShowing, so if it is open
          // we must force a rebuild
          if (this._chevron.open) {
            var popup = this._chevron.firstChild;
            for (var i = 0; i < popup.childNodes.length; i++)
              popup.childNodes[i].hidden = !this.childNodes[i].collapsed;
          }
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <property name="place">
        <getter><![CDATA[
          return this.getAttribute("place");
        ]]></getter>
        <setter><![CDATA[ 
          this.setAttribute("place", val);

          var history = PlacesUtils.history;
          var queries = { }, options = { };
          history.queryStringToQueries(val, queries, { }, options);
          if (!queries.value.length) 
            queries.value = [history.getNewQuery()];
          try {
            var result =
              history.executeQueries(queries.value, queries.value.length,
                                     options.value);
            result.viewer = this._viewer;
          }
          catch(ex) {
            // Invalid query, or had no results.
            // This is valid, eg: user deletes their bookmarks toolbar folder. 
          }
          return val;
        ]]></setter>
      </property>

      <!-- nsIPlacesView -->
      <property name="hasSelection">
        <getter><![CDATA[ 
          return this.selectedNode != null;
        ]]></getter>
      </property>

      <!-- nsIPlacesView -->
      <method name="getSelectionNodes">
        <body><![CDATA[
          var selectedNode = this.selectedNode;
          return selectedNode ? [selectedNode] : [];
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <method name="getRemovableSelectionRanges">
        <body><![CDATA[
          // On static content the current selectedNode would be the selection's
          // parent node. We don't want to allow removing a node when the
          // selection is not explicit.
          if (document.popupNode &&
              (document.popupNode == "menupopup" || !document.popupNode.node))
            return [];

          return [this.getSelectionNodes()];
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <method name="getDragableSelection">
        <body><![CDATA[
          return [this._draggedNode];
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <property name="selectedNode">
        <getter><![CDATA[
          if (this._contextMenuShown) {
            var popupNode = document.popupNode;
            if (popupNode == this)
              return this.getResultNode();

            return popupNode.node || popupNode.parentNode._resultNode || null;
          }
          return null;
        ]]></getter>
      </property>

      <!-- nsIPlacesView -->
      <property name="insertionPoint">
        <getter><![CDATA[
          // By default, the insertion point is at the top level, at the end. 
          var index = PlacesUtils.bookmarks.DEFAULT_INDEX;
          var container = this._result.root;
          var orientation = Ci.nsITreeView.DROP_BEFORE;
          var isTag = false;

          var selectedNode = this.selectedNode;
          if (selectedNode) {
            var popupNode = document.popupNode;
            if (!popupNode.node) {
              // If a static menuitem is selected the insertion point
              // is inside the folder, at the end.
              container = selectedNode;
              orientation = Ci.nsITreeView.DROP_ON;
            }
            else {
              // In all other cases the insertion point is before that node.
              container = selectedNode.parent;
              index = PlacesUtils.getIndexOfNode(selectedNode);
              isTag = PlacesUtils.nodeIsTagQuery(selectedNode.parent);
            }
          }

          if (PlacesControllerDragHelper.disallowInsertion(container))
            return null;

          return new InsertionPoint(PlacesUtils.getConcreteItemId(container),
                                    index, orientation, isTag);
        ]]></getter>
      </property>

      <!-- nsIPlacesView -->
      <method name="selectAll">
        <body><![CDATA[ 
          // Nothing
        ]]></body>
      </method>

      <method name="selectItems">
        <body><![CDATA[
          // Nothing
        ]]></body>
      </method>

      <!-- nsINavHistoryResultViewer -->
      <field name="_viewer"><![CDATA[({
        _self: this,

        _getPopupForContainer:
        function PMV__getPopupForContainer(aNode) {
          if (this._self._resultNode == aNode)
            return this._self;

          for (var i=0; i < this._self._containerNodesMap.length; i++) {
            if (this._self._containerNodesMap[i].resultNode == aNode)
              return this._self._containerNodesMap[i].domNode;
          }
          throw("Container view not found");
        },

        get result() {
          return this._self._result;
        },

        set result(val) {
          // some methods (e.g. getURLsFromContainer) temporarily null out the
          // viewer when they do temporary changes to the view, this does _not_
          // call setResult(null), but then, we're called again with the result
          // object which is already set for this viewer. At that point,
          // we should do nothing.
          if (this._self._result != val) {
            this._self._containerNodesMap = [];
            this._self._result = val;
            if (val) // this calls _rebuild through invalidateContainer
              val.root.containerOpen = true;
          }
          return val;
        },

        itemInserted: function TV_V_itemInserted(aParentNode, aNode, aIndex) {
          // don't insert new items into the toolbar
          // if the parent is not the root 
          if (aParentNode == this._self.getResultNode()) {
            var children = this._self.childNodes;
            this._self.insertNewItem(aNode,
              aIndex < children.length ? children[aIndex] : null);
            this._self.updateChevron();
          }
          else {
            var popup = this._getPopupForContainer(aParentNode);
            if (!popup._built)
              return;

            var before = popup.childNodes[aIndex] || null;
            this._self.insertNewItemToPopup(aNode, popup, before);
            if (popup._emptyMenuItem)
              popup._emptyMenuItem.hidden = true;
          }
        },

        itemRemoved: function TV_V_itemRemoved(aParentNode, aNode, aIndex) {
          if (aParentNode == this._self.getResultNode()) {
            var children = this._self.childNodes;
            for (var i = 0; i < children.length; i++) {
              if (children[i].node == aNode) {
                this._self.removeItem(children[i]);
                this._self.updateChevron();
                return;
              }
            }
          }
          else {
            var popup = this._getPopupForContainer(aParentNode);
            if (!popup._built)
              return;

            var children = popup.childNodes;
            for (var i = 0; i < children.length; i++) {
              if (children[i].node == aNode) {
                this._self.removeItem(children[i]);
                if (!popup.hasChildNodes() ||
                    (popup.childNodes.length == 1 &&
                     popup.firstChild == popup._emptyMenuItem)) {
                  this._self._showEmptyMenuItem(popup);
                }
                if (popup._endMarker != -1)
                  popup._endMarker--;
                return;
              }
            }
          }
        },

        itemMoved:
        function TV_V_itemMoved(aItem, aOldParent, aOldIndex, aNewParent,
                                aNewIndex) {
          // This cannot actually happen yet (see IDL)
          if (aNewParent != aOldParent)
            return;

          if (aNewParent == this._self.getResultNode()) {
            var children = this._self.childNodes;
            var chevronPopup = this._self._chevron.firstChild;
            for (var i = 0; i < children.length; i++) {
              var button = children[i];
              if (button.node == aItem) {
                this._self.removeChild(button);
                this._self.insertBefore(button, children[aNewIndex]);
                if (chevronPopup) {
                  // Maintain chevron in sync
                  menuitem = chevronPopup.childNodes[i];
                  chevronPopup.removeChild(menuitem);
                  chevronPopup.insertBefore(menuitem,
                                            chevronPopup.childNodes[aNewIndex]);
                }
                this._self.updateChevron();
                return;
              }
            }
          }
          var popup = this._getPopupForContainer(aNewParent);
          var children = popup.childNodes;
          for (var i = 0; i < children.length; i++) {
            var menuItem = children[i];
            if (menuItem.node == aItem) {
              popup.removeChild(menuItem);
              popup.insertBefore(menuItem, children[aNewIndex]);
              return;
            }
          }
        },

        itemChanged: function TV_V_itemChanged(aNode) {
          // this check can be removed once we fix bug #382397
          var parentNode = aNode.parent;
          if (!parentNode)
            return;

          var element;
          var onToolbar = false;
          if (parentNode == this._self.getResultNode()) {
            onToolbar = true;
            var children = this._self.childNodes;
            for (var i = 0; i < children.length; i++) {
              if (children[i].node == aNode) {
                element = children[i];
                break;
              }
            }
            // Don't replace title on toolbarbuttons
            var title = aNode.title;
          }
          else {
            var popup = this._getPopupForContainer(parentNode);
            if (!popup._built)
              return;

            var children = popup.childNodes;
            for (var i = 0; i < children.length; i++) {
              if (children[i].node == aNode) {
                element = children[i];
                break;
              }
            }
            var title = PlacesUIUtils.getBestTitle(aNode);
          }

          if (PlacesUtils.nodeIsSeparator(aNode)) {
            // nothing to do when a separator changes
            return;
          }

          var iconURI = aNode.icon;
          if (iconURI) {
            var spec = iconURI.spec;
            if (element.getAttribute("image") != spec)
              element.setAttribute("image", spec);
          }
          else
            element.removeAttribute("image");

          if (element.getAttribute("label") != title) {
            element.setAttribute("label", title);
            if (onToolbar)
              this._self.updateChevron();
          }

          if (PlacesUtils.nodeIsLivemarkContainer(aNode)) {
            if (!element.hasAttribute("livemark"))
              element.setAttribute("livemark", "true");
            // If this is a livemark container check if the status menuitem has
            // to be added or removed.
            PlacesUIUtils.ensureLivemarkStatusMenuItem(element.firstChild);
          }
          else if (PlacesUtils.nodeIsURI(aNode)) {
            element.setAttribute("scheme", PlacesUIUtils.guessUrlSchemeForUI(aNode.uri));
          }
        },

        itemReplaced:
        function TV_V_itemReplaced(aParentNode, aOldNode, aNewNode, aIndex) {
          if (aParentNode == this._self.getResultNode()) {
            var children = this._self.childNodes;
            for (var i = 0; i < children.length; i++) {
              if (children[i].node == aOldNode) {
                var next = children[i].nextSibling;
                this._self.removeItem(children[i]);
                this._self.insertNewItem(aNewNode, next);
                this._self.updateChevron();
                return;
              }
            }
          }
          else
            this._forwardToChildView(aParentNode, "itemReplaced", arguments);
        },

        containerOpened: function TV_V_containerOpened(aNode) {
          this.invalidateContainer(aNode);
        },

        containerClosed: function TV_V_containerClosed(aNode) {
          this.invalidateContainer(aNode);
        },

        invalidateContainer: function TV_V_invalidateContainer(aContainer) {
          if (aContainer == this._self.getResultNode()) {
            this._self._containerNodesMap.splice(0);
            this._self._rebuild();
            return;
          }

          function isChildOf(node, container) {
            var parent = node.parent;
            while (parent) {
              if (parent == container)
                return true;
              parent = parent.parent;
            }
            return false;
          }

          var popupToRebuild = null;
          for (var i=0; i < this._self._containerNodesMap.length; i++) {
            var node = this._self._containerNodesMap[i].resultNode;
            
            if (node == aContainer)
              popupToRebuild = this._self._containerNodesMap[i].domNode;
            if (isChildOf(node, aContainer)) {
              this._self._containerNodesMap.splice(i,1);
              i--;
            }
          }

          if (popupToRebuild) {
            popupToRebuild._built = false;

            // if the menupopup is open we should live-update it
            if (popupToRebuild.parentNode.open)
              this._self._rebuildPopup(popupToRebuild);
          }
        },

        invalidateAll: function TV_V_invalidateAll() {
          this._self._containerNodesMap.splice(0);
          this._self._rebuild();
        },

        sortingChanged: function TV_V_sortingChanged(aSortingMode) {
        }
      })]]></field>

      <property name="selType" onget="return 'single';"/>

      <method name="buildContextMenu">
        <parameter name="aPopup"/>
        <body><![CDATA[
          this._contextMenuShown = true;
          // Activate the controller
          this.focus();
          // The above call may not always fire a consumable event for
          // commandUpdater, so we force a command update.
          window.updateCommands("focus");
          return this.controller.buildContextMenu(aPopup);
        ]]></body>
      </method>

      <method name="destroyContextMenu">
        <parameter name="aPopup"/>
        <body><![CDATA[
          this._contextMenuShown = false;
          if (window.content)
            window.content.focus();
        ]]></body>
      </method>

      <method name="_showEmptyMenuItem">
        <parameter name="aPopup"/>
        <body><![CDATA[
          if (aPopup._emptyMenuItem) {
            aPopup._emptyMenuItem.hidden = false;
            return;
          }

          var label = PlacesUIUtils.getString("bookmarksMenuEmptyFolder");
          aPopup._emptyMenuItem = document.createElement("menuitem");
          aPopup._emptyMenuItem.setAttribute("label", label);
          aPopup._emptyMenuItem.setAttribute("disabled", true);
          aPopup.appendChild(aPopup._emptyMenuItem);
        ]]></body>
      </method>

      <method name="insertNewItemToPopup">
        <parameter name="aChild"/>
        <parameter name="aParentPopup"/>
        <parameter name="aBefore"/>
        <body><![CDATA[
          var element =
            PlacesUIUtils.createMenuItemForNode(aChild, this._containerNodesMap);

          if (aBefore)
            aParentPopup.insertBefore(element, aBefore);
          else {
            // Add the new element to the menu.  If there is static content at
            // the end of the menu, add the element before that.  Otherwise,
            // just add to the end.
            if (aParentPopup._endMarker != -1) {
              aParentPopup.insertBefore(element,
                                        aParentPopup.childNodes[aParentPopup._endMarker]);
            }
            else
              aParentPopup.appendChild(element);
          }

          if (aParentPopup._endMarker != -1)
            aParentPopup._endMarker++;
        ]]></body>
      </method>

      <method name="_containerPopupShowing">
        <parameter name="aPopup"/>
        <body><![CDATA[
          if (!aPopup._built)
            this._rebuildPopup(aPopup);
        ]]></body>
      </method>

      <method name="_rebuildPopup">
        <parameter name="aPopup"/>
        <body><![CDATA[
          PlacesUIUtils.cleanPlacesPopup(aPopup);

          // If this is a livemark container check if the status menuitem has
          // to be added or removed.
          if (PlacesUtils.nodeIsLivemarkContainer(aPopup._resultNode))
            PlacesUIUtils.ensureLivemarkStatusMenuItem(aPopup);

          var resultNode = aPopup._resultNode;
          if (!resultNode.containerOpen)
            resultNode.containerOpen = true;

          var cc = resultNode.childCount;
          if (cc > 0) {
            if (aPopup._emptyMenuItem)
              aPopup._emptyMenuItem.hidden = true;

            for (var i = 0; i < cc; ++i) {
              var child = resultNode.getChild(i);
              this.insertNewItemToPopup(child, aPopup, null);
            }
          }
          else {
            // This menu is empty.  If there is no static content, add
            // an element to show it is empty.
            if (aPopup._startMarker == -1 && aPopup._endMarker == -1)
              this._showEmptyMenuItem(aPopup);
          }
          aPopup._built = true;
        ]]></body>
      </method>

      <field name="_overFolder"><![CDATA[
       (
         // Menu buttons should be opened when the mouse drags over them, and
         // closed when the mouse drags off. This object manages opening and
         // closing of folders when the mouse hovers.
         { node: null, openTimer: null, hoverTime: 350, closeTimer: null }
       );
      ]]></field>

      <method name="_clearOverFolder">
        <body><![CDATA[
          // The mouse is no longer dragging over the stored menubutton.
          // Close the menubutton, clear out drag styles, and clear all
          // timers for opening/closing it.
          if (this._overFolder.node && this._overFolder.node.lastChild) {
            if (!this._overFolder.node.lastChild.hasAttribute("dragover")) {
              this._overFolder.node.lastChild.hidePopup();
            }
            this._overFolder.node.removeAttribute("dragover");
            this._overFolder.node = null;
          }
          if (this._overFolder.openTimer) {
            this._overFolder.openTimer.cancel();
            this._overFolder.openTimer = null;
          }
          if (this._overFolder.closeTimer) {
            this._overFolder.closeTimer.cancel();
            this._overFolder.closeTimer = null;
          }
        ]]></body>
      </method>

      <method name="_getDropPoint">
        <parameter name="aEvent"/>
        <body><![CDATA[
          // This function returns information about where to drop when
          // dragging over this menu--insertion point, child index to drop
          // before, and folder to drop into.
          // Can't drop if the toolbar isn't a folder.
          var result = this.getResult();
          if (!PlacesUtils.nodeIsFolder(result.root))
            return null;

          var isRTL = document.defaultView
                              .getComputedStyle(this.parentNode, "")
                              .direction == "rtl";

          var dropPoint = { ip: null, beforeIndex: null, folderNode: null };
          // Loop through all the nodes to see which one this should
          // get dropped in/next to
          for (var i = 0; i < this.childNodes.length; i++) {
            var xulNode = this.childNodes[i];
            if (PlacesUtils.nodeIsFolder(xulNode.node) &&
                !PlacesUtils.nodeIsReadOnly(xulNode.node)) {
              // This is a folder. If the mouse is in the left 25% of the
              // node (or 25% of the right, in RTL UI), drop before the folder.
              // If it's in the middle 50%, drop into the folder. If it's past
              // that, drop after.
              if ((isRTL && aEvent.clientX > xulNode.boxObject.x +
                                             (xulNode.boxObject.width * 0.75)) ||
                  (!isRTL && aEvent.clientX < xulNode.boxObject.x + 
                                              (xulNode.boxObject.width * 0.25))) {
                // Drop to the left of this folder.
                dropPoint.ip =
                  new InsertionPoint(PlacesUtils.getConcreteItemId(result.root),
                                     i, -1);
                dropPoint.beforeIndex = i;
                return dropPoint;
              }
              else if ((isRTL && aEvent.clientX > xulNode.boxObject.x + 
                                                  (xulNode.boxObject.width * 0.25)) ||
                       (!isRTL && aEvent.clientX < xulNode.boxObject.x +
                                                   (xulNode.boxObject.width * 0.75))) {
                // Drop inside this folder.
                dropPoint.ip =
                  new InsertionPoint(PlacesUtils.getConcreteItemId(xulNode.node),
                                     -1, 1,
                                     PlacesUtils.nodeIsTagQuery(xulNode.node));
                dropPoint.beforeIndex = i;
                dropPoint.folderNode = xulNode;
                return dropPoint;
              }
            }
            else {
              // This is a non-folder node. If the mouse is left (or right, in
              // RTL UI) of the middle, drop before the folder.  Otehrwise,
              // we'll drop after
              if ((isRTL && aEvent.clientX > xulNode.boxObject.x + (xulNode.boxObject.width / 2)) ||
                  (!isRTL && aEvent.clientX < xulNode.boxObject.x + (xulNode.boxObject.width / 2))) {
                // Drop before this bookmark.
                dropPoint.ip =
	                new InsertionPoint(PlacesUtils.getConcreteItemId(result.root),
	                                   i, -1);
                dropPoint.beforeIndex = i;
                return dropPoint;
              }
            }
          }
          // Should drop after the last node.
          dropPoint.ip =
        	  new InsertionPoint(PlacesUtils.getConcreteItemId(result.root),
	                             -1, 1);
          dropPoint.beforeIndex = -1;
          return dropPoint;
        ]]></body>
      </method>

      <method name="_setTimer">
        <parameter name="aTime"/>
        <body><![CDATA[
          var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
          timer.initWithCallback(this, aTime, timer.TYPE_ONE_SHOT);
          return timer;
        ]]></body>
      </method>

      <!-- nsITimerCallback -->
      <method name="notify">
        <parameter name="aTimer"/>
        <body><![CDATA[
          // Function to process all timer notifications.

          // * Timer to turn off indicator bar.
          if (aTimer == this._ibTimer) {
            ib = this._dropIndicatorBar.removeAttribute('dragging');
            this._ibTimer = null;
          }

          // * Timer to open a menubutton that's being dragged over.
          if (aTimer == this._overFolder.openTimer) {
            // Set the autoopen attribute on the folder's menupopup so that
            // the menu will automatically close when the mouse drags off of it.
            this._overFolder.node.lastChild.setAttribute("autoopened", "true");
            this._overFolder.node.open = true;
            this._overFolder.openTimer = null;
          }

          // * Timer to close a menubutton that's been dragged off of.
          if (aTimer == this._overFolder.closeTimer) {
            // Only close the menubutton if the drag session isn't currently over
            // it or one of its children.  (The autoopened attribute will let the menu
            // know to close later if the menu is still being dragged over.)
            var currentNode = PlacesControllerDragHelper.currentDropTarget;
            var inHierarchy = false;
            while (currentNode) {
              if (currentNode == this) {
                inHierarchy = true;
                break;
              }
              currentNode = currentNode.parentNode;
            }
            // The _clearOverFolder() function will close the menu for _overFolder.node.
            // So null it out if we don't want to close it.
            if (inHierarchy)
              this._overFolder.node = null;

            // Clear out the folder and all associated timers.
            this._clearOverFolder();
          }
        ]]></body>
      </method>
    </implementation>

    <handlers>
      <handler event="mouseover"><![CDATA[
        var button = event.target;
        if (button.parentNode == this && button.node &&
            PlacesUtils.nodeIsURI(button.node))
          window.XULBrowserWindow.setOverLink(event.target.node.uri, null);
      ]]></handler>

      <handler event="mouseout"><![CDATA[
        window.XULBrowserWindow.setOverLink("", null);
      ]]></handler>

      <handler event="dragstart"><![CDATA[
        // sub menus have their own d&d handlers
        var draggedDOMNode = event.target;
        if (draggedDOMNode.parentNode != this || !draggedDOMNode.node)
          return;

        if (draggedDOMNode.localName == "toolbarbutton" &&
            draggedDOMNode.getAttribute("type") == "menu") {
          // If the drag gesture on a container is toward down we open instead
          // of dragging.
          if (this._mouseDownTimer) {
            this._mouseDownTimer.cancel();
            this._mouseDownTimer = null;
          }
          var translateY = this._cachedMouseMoveEvent.clientY - event.clientY;
          var translateX = this._cachedMouseMoveEvent.clientX - event.clientX;
          if ((translateY) >= Math.abs(translateX/2)) {
            // Don't start the drag
            event.preventDefault();
            // Open the menu
            draggedDOMNode.open = true;
            return;
          }
          // if the menu is open, close it
          if (draggedDOMNode.open) {
            draggedDOMNode.firstChild.hidePopup();
            draggedDOMNode.open = false;
          }
        }

        // activate the view and cache the dragged node
        this._draggedNode = draggedDOMNode.node;
        this.focus();

        this._controller.setDataTransfer(event);
        event.stopPropagation();
      ]]></handler>

      <handler event="dragover"><![CDATA[
        // Cache the dataTransfer
        var dt = PlacesControllerDragHelper.currentDataTransfer =
                 event.dataTransfer;

        var ib = this._dropIndicatorBar;
        var ip = this.insertionPoint;
        if (!ip || !PlacesControllerDragHelper.canDrop(ip)) {
          ib.removeAttribute("dragging");
          PlacesControllerDragHelper.currentDataTransfer = null;
          return;
        }

        PlacesControllerDragHelper.currentDropTarget = event.target;
        var dropPoint = this._getDropPoint(event);

        if (this._ibTimer) {
          this._ibTimer.cancel();
          this._ibTimer = null;
        }

        if (dropPoint.folderNode ||
            event.originalTarget == this._chevron) {
          // Dropping over a menubutton or chevron button
          // set styles and timer to open relative menupopup
          var overNode = dropPoint.folderNode || this._chevron;
          if (this._overFolder.node != overNode) {
            this._clearOverFolder();
            this._overFolder.node = overNode;
            this._overFolder.openTimer = this._setTimer(this._overFolder.hoverTime);
          }
          if (!this._overFolder.node.hasAttribute("dragover"))
            this._overFolder.node.setAttribute("dragover", "true");

          ib.removeAttribute("dragging");
        }
        else {
          // Dragging over a normal toolbarbutton,
          // show indicator bar and move it to the appropriate drop point.
          if (!ib.hasAttribute("dragging"))
            ib.setAttribute("dragging", "true");
          var ind = ib.firstChild;
          var halfInd = ind.boxObject.width / 2;
          var direction = document.defaultView.getComputedStyle(this.parentNode, "").direction;
          if (direction == "ltr") {
            halfInd = Math.ceil(halfInd);
          if (!this.childNodes.length)
            ind.style.marginLeft = 0 - this.boxObject.x - halfInd + 'px'
          else if (dropPoint.beforeIndex == -1)
            ind.style.marginLeft = this.lastChild.boxObject.x + 
                                   this.lastChild.boxObject.width - this.boxObject.x - halfInd + 'px';
          else
            ind.style.marginLeft = this.childNodes[dropPoint.beforeIndex].boxObject.x -
                                   this.boxObject.x - halfInd + 'px';
          }
          else {
            halfInd = Math.floor(halfInd);
            if (this.childNodes.length == 0)
              ind.style.marginRight = this.boxObject.width + 'px';
            else if (dropPoint.beforeIndex == -1) {
              ind.style.marginRight = this.boxObject.width -
                                      (this.childNodes[this.childNodes.length - 1].boxObject.x +
                                      halfInd) +'px';
            }
            else {
              ind.style.marginRight = this.boxObject.width -
                                      (this.childNodes[dropPoint.beforeIndex].boxObject.x +
                                       this.childNodes[dropPoint.beforeIndex].boxObject.width -
                                      this.boxObject.x + halfInd) + 'px';
            }
          }
          // Clear out old folder information
          this._clearOverFolder();
        }

        dt.effectAllowed = "all";
        event.preventDefault();
        event.stopPropagation();
      ]]></handler>

      <handler event="drop"><![CDATA[
        PlacesControllerDragHelper.currentDataTransfer = event.dataTransfer;
        PlacesControllerDragHelper.currentDropTarget = event.target;

        var ip = this.insertionPoint;
        if (!ip || !PlacesControllerDragHelper.canDrop(ip))
          return;

        var dropPoint = this._getDropPoint(event);
        if (!dropPoint)
          return;

        PlacesControllerDragHelper.onDrop(dropPoint.ip);
        event.stopPropagation();
      ]]></handler>

      <handler event="dragleave"><![CDATA[
        PlacesControllerDragHelper.currentDropTarget = null;
        PlacesControllerDragHelper.currentDataTransfer = null;

        // Set timer to turn off indicator bar (if we turn it off
        // here, dragenter might be called immediately after, creating
        // flicker.)
        if (this._ibTimer)
          this._ibTimer.cancel();
        this._ibTimer = this._setTimer(10);

        // If we hovered over a folder, close it now
        if (this._overFolder.node)
            this._overFolder.closeTimer = this._setTimer(this._overFolder.hoverTime);
      ]]></handler>

      <handler event="dragend"><![CDATA[
        this._draggedNode = null;
      ]]></handler>

      <handler event="popupshowing" phase="capturing"><![CDATA[      
        if (!this._allowPopupShowing) {
          this._allowPopupShowing = true;
          event.preventDefault();
          return;
        }

        var popup = event.originalTarget;

        // Avoid handling popupshowing of inner views
        if (popup._resultNode && PlacesUIUtils.getViewForNode(popup) == this)
          this._containerPopupShowing(popup);

        var parent = popup.parentNode;
        if (parent.localName == "toolbarbutton")
          this._openedMenuButton = parent;
      ]]></handler>

      <handler event="popuphidden"><![CDATA[
        var popup = event.originalTarget;

        // Avoid handling popuphidden of inner views
        if (popup._resultNode && PlacesUIUtils.getViewForNode(popup) == this) {
          // UI performance: folder queries are cheap, keep the resultnode open
          // so we don't rebuild its contents whenever the popup is reopened.
          if (!PlacesUtils.nodeIsFolder(popup._resultNode))
            popup._resultNode.containerOpen = false;
        }

        var parent = popup.parentNode;
        if (parent.localName == "toolbarbutton") {
          this._openedMenuButton = null;
          // Clear the dragover attribute if present, if we are dragging into a
          // folder in the hierachy of current opened popup we don't clear
          // this attribute on clearOverFolder.  See Notify for closeTimer.
          if (parent.hasAttribute("dragover"))
            parent.removeAttribute("dragover");
        }
      ]]></handler>

      <handler event="mousedown"><![CDATA[
        var target = event.target;
        if (event.button == 0 &&
            target.localName == "toolbarbutton" &&
            target.getAttribute("type") == "menu") {
          this._allowPopupShowing = false;
          // This is a container, we will open the menu if the user clicks
          // or drag toward down or after a delay
          this._mouseDownTimer = Cc["@mozilla.org/timer;1"]
                                   .createInstance(Ci.nsITimer);
          var callback = {
            _self: this,
		        _target: target,
		        notify: function(timer) {
              this._target.open = true;
              this._self._mouseDownTimer = null;
            }
          };

          this._mouseDownTimer.initWithCallback(callback, 500,
                                                Ci.nsITimer.TYPE_ONE_SHOT);
        }
        else {
          // allow opening popups, like the chevron and context menus
          this._allowPopupShowing = true;
        }
      ]]></handler>

      <handler event="mouseup"><![CDATA[
        if (event.button != 0)
          return;

        if (this._mouseDownTimer) {
          // On a click (down/up) we should open the menu popup
          this._mouseDownTimer.cancel();
          this._mouseDownTimer = null;
          event.target.open = true;
        }
      ]]></handler>

      <handler event="mousemove"><![CDATA[
        // Used in dragStart to prevent dragging folders when dragging down
        this._cachedMouseMoveEvent = event;

        if (this._openedMenuButton == null ||
            PlacesControllerDragHelper.getSession())
          return;

        var target = event.originalTarget;
        if (this._openedMenuButton != target &&
            target.localName == "toolbarbutton" &&
            target.type == "menu") {
          this._openedMenuButton.open = false;
          target.open = true;
        }
      ]]></handler>
    </handlers>
  </binding>

</bindings>