<?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 Tree 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):
#   Ben Goodger <beng@google.com>
#   Annie Sullivan <annie.sullivan@gmail.com>
#   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 ***** 

<bindings id="placesTreeBindings"
          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-tree" extends="chrome://global/content/bindings/tree.xml#tree">
    <implementation>
      <constructor><![CDATA[
        // Force an initial build.
        if (this.place)
          this.place = this.place;
      ]]></constructor>

      <destructor><![CDATA[
        // Break the treeviewer->result->treeviewer cycle.
        // Note: unsetting the result's viewer also unsets
        // the viewer's reference to our treeBoxObject.
        var result = this.getResult();
        if (result) {
          result.viewer = null;
          result.root.containerOpen = false;
        }
        this.view = null;
      ]]></destructor>

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

      <!-- overriding -->
      <property name="view">
        <getter><![CDATA[
          return this.treeBoxObject.view.QueryInterface(Ci.nsINavHistoryResultTreeViewer);
        ]]></getter>
        <setter><![CDATA[ 
          return this.treeBoxObject.view = val;
        ]]></setter>
      </property>

      <method name="getBestOptions">
        <body><![CDATA[
          // Get the best set of grouping options to use, either reuse the 
          // existing ones or create new ones. 
          var options = this.getResult().queryOptions;
          if (!options)
            options = PlacesUtils.history.getNewQueryOptions();
          return options;
        ]]></body>
      </method>

      <method name="applyFilter">
        <parameter name="filterString"/>
        <parameter name="folderRestrict"/>
        <body><![CDATA[
          // preserve grouping
          var queryNode = asQuery(this.getResultNode());
          var options = queryNode.queryOptions.clone();

          // Make sure we're getting uri results.
          // We do not yet support searching into grouped queries or into
          // tag containers, so we must fall to the default case.
          if (PlacesUtils.nodeIsHistoryContainer(queryNode) ||
              options.resultType == options.RESULTS_AS_TAG_QUERY ||
              options.resultType == options.RESULTS_AS_TAG_CONTENTS)
            options.resultType = options.RESULT_TYPE_URI;

          var query = PlacesUtils.history.getNewQuery();
          query.searchTerms = filterString;

          if (folderRestrict) {
            query.setFolders(folderRestrict, folderRestrict.length);
            options.queryType = options.QUERY_TYPE_BOOKMARKS;
          }

          this.load([query], options);
        ]]></body>
      </method>
      
      <method name="load">
        <parameter name="queries"/>
        <parameter name="options"/>
        <body><![CDATA[
          // Cleanup old result if exists.
          var oldResult = this.getResult();
          if (oldResult)
            oldResult.root.containerOpen = false;

          var result = PlacesUtils.history
                                  .executeQueries(queries, queries.length,
                                                  options);
          var callback;
          if (this.flatList) {
            var onOpenFlatContainer = this.onOpenFlatContainer;
            if (onOpenFlatContainer)
              callback = new Function("aContainer", onOpenFlatContainer);
          }

          var treeView = new PlacesTreeView(this.flatList, callback);
          result.viewer = treeView;
          this.view = treeView;
          if (!this._controller) {
            this._controller = new PlacesController(this);
            this.controllers.appendController(this._controller);
          }
          this._cachedInsertionPoint = undefined;
        ]]></body>
      </method>

      <property name="flatList">
        <getter><![CDATA[
          return this.getAttribute("flatList") == "true";
        ]]></getter>
        <setter><![CDATA[
          if (this.flatList != val) {
            this.setAttribute("flatList", val);
            // reload with the last place set
            if (this.place)
              this.place = this.place;
          }
          return val;
        ]]></setter>
      </property>

      <property name="onOpenFlatContainer">
        <getter><![CDATA[
          return this.getAttribute("onopenflatcontainer");
        ]]></getter>
        <setter><![CDATA[
          if (this.onOpenFlatContainer != val) {
            this.setAttribute("onopenflatcontainer", val);
            // reload with the last place set
            if (this.place)
              this.place = this.place;
          }
          return val;
        ]]></setter>
      </property>

      <!-- 
        Causes a particular node represented by the specified placeURI to be
        selected in the tree. All containers above the node in the hierarchy
        will be opened, so that the node is visible. 
        -->
      <method name="selectPlaceURI">
        <parameter name="placeURI"/>
        <body><![CDATA[
          // Do nothing if a node matching the given uri is already selected
          if (this.hasSelection && this.selectedNode.uri == placeURI)
            return;

          function findNode(container, placeURI, nodesURIChecked) {
            var containerURI = container.uri;
            if (containerURI == placeURI)
              return container;
            if (nodesURIChecked.indexOf(containerURI) != -1)
              return null;

            // never check the contents of the same query
            nodesURIChecked.push(containerURI);

            var wasOpen = container.containerOpen;
            if (!wasOpen)
              container.containerOpen = true;
            for (var i = 0; i < container.childCount; ++i) {
              var child = container.getChild(i);
              var childURI = child.uri;
              if (childURI == placeURI)
                return child;
              else if (PlacesUtils.nodeIsContainer(child)) {
                var nested = findNode(asContainer(child), placeURI, nodesURIChecked);
                if (nested)
                  return nested;
              }
            }

            if (!wasOpen)
              container.containerOpen = false;

            return null;
          }

          var container = this.getResultNode();
          NS_ASSERT(container, "No result, cannot select place URI!");
          if (!container)
            return;

          var child = findNode(container, placeURI, []);
          if (child)
            this.selectNode(child);
          else {
            // If the specified child could not be located, clear the selection
            var selection = this.view.selection;
            selection.clearSelection();
          }
        ]]></body>
      </method>

      <!-- 
        Causes a particular node to be selected in the tree, resulting in all 
        containers above the node in the hierarchy to be opened, so that the
        node is visible. 
        -->
      <method name="selectNode">
        <parameter name="node"/>
        <body><![CDATA[ 
          var view = this.view;

          var parent = node.parent;
          if (parent && !parent.containerOpen) {
            // Build a list of all of the nodes that are the parent of this one 
            // in the result. 
            var parents = [];
            var root = this.getResultNode();
            while (parent && parent != root) {
              parents.push(parent);
              parent = parent.parent;
            }

            // Walk the list backwards (opening from the root of the hierarchy)
            // opening each folder as we go.
            for (var i = parents.length - 1; i >= 0; --i) {
              var index = view.treeIndexForNode(parents[i]);
              if (view.isContainer(index) && !view.isContainerOpen(index)) 
                view.toggleOpenState(index);
            }
            // Select the specified node...
          }

          var index = view.treeIndexForNode(node);
          view.selection.select(index);
          // ... and ensure it's visible, not scrolled off somewhere. 
          this.treeBoxObject.ensureRowIsVisible(index);
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <method name="getResult">
        <body><![CDATA[
          try {
            return this.view.QueryInterface(Ci.nsINavHistoryResultViewer).result;
          }
          catch (e) {
            return null;
          }
        ]]></body>
      </method>

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

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

          var queriesRef = { };
          var queryCountRef = { };
          var optionsRef = { };
          PlacesUtils.history.queryStringToQueries(val, queriesRef, queryCountRef, optionsRef);
          if (queryCountRef.value == 0)
            queriesRef.value = [PlacesUtils.history.getNewQuery()];
          if (!optionsRef.value)
            optionsRef.value = PlacesUtils.history.getNewQueryOptions();

          this.load(queriesRef.value, optionsRef.value);

          return val;
        ]]></setter>
      </property>

      <!-- nsIPlacesView -->
      <property name="hasSelection">
        <getter><![CDATA[
          return this.view.selection.count >= 1;
        ]]></getter>
      </property>
      
      <!-- nsIPlacesView -->
      <method name="getSelectionNodes">
        <body><![CDATA[ 
          var selection = this.view.selection;
          var rc = selection.getRangeCount();
          var nodes = [];
          var resultview = this.view;
          for (var i = 0; i < rc; ++i) {
            var min = { }, max = { };
            selection.getRangeAt(i, min, max);

            for (var j = min.value; j <= max.value; ++j)
              nodes.push(resultview.nodeForTreeIndex(j));
          }
          return nodes;
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <method name="getRemovableSelectionRanges">
        <body><![CDATA[ 
          // This function exists in addition to getSelectionNodes because it
          // encodes selection ranges (which only occur in list views) into
          // the return value. For each removed range, the index at which items
          // will be re-inserted upon the remove transaction being performed is
          // the first index of the range, so that the view updates correctly. 
          //
          // For example, if we remove rows 2,3,4 and 7,8 from a list, when we
          // undo that operation, if we insert what was at row 3 at row 3 again,
          // it will show up _after_ the item that was at row 5. So we need to 
          // insert all items at row 2, and the tree view will update correctly. 
          //
          // Also, this function collapses the selection to remove redundant
          // data, e.g. when deleting this selection:
          //
          //      http://www.foo.com/
          //  (-) Some Folder
          //        http://www.bar.com/
          //
          // ... returning http://www.bar.com/ as part of the selection is 
          // redundant because it is implied by removing "Some Folder". We 
          // filter out all such redundancies since some partial amount of
          // the folder's children may be selected.          
          //
          var selection = this.view.selection;
          var rc = selection.getRangeCount();
          var nodes = [];
          var resultview = this.view;
          // This list is kept independently of the range selected (i.e. OUTSIDE
          // the for loop) since the row index of a container is unique for the
          // entire view, and we could have some really wacky selection and we
          // don't want to blow up.
          var containers = { };
          for (var i = 0; i < rc; ++i) {
            var range = [];
            var min = { }, max = { };
            selection.getRangeAt(i, min, max);
            
            for (var j = min.value; j <= max.value; ++j) {
              if (this.view.isContainer(j))
                containers[j] = true;
              if (!(this.view.getParentIndex(j) in containers))
                range.push(resultview.nodeForTreeIndex(j));
            }
            nodes.push(range);
          }
          return nodes;
        ]]></body>
      </method>
      
      <!-- nsIPlacesView -->
      <method name="getDraggableSelection">
        <body><![CDATA[
          return this.getSelectionNodes();
        ]]></body>
      </method>
      
      <!-- nsIPlacesView -->
      <property name="selectedNode">
        <getter><![CDATA[
          var view = this.view;
          if (view.selection.count != 1)
            return null;

          var selection = view.selection;
          var min = { }, max = { };
          selection.getRangeAt(0, min, max);

          return this.view.nodeForTreeIndex(min.value);
        ]]></getter>
      </property>

      <!-- nsIPlacesView -->
      <property name="insertionPoint">
        <getter><![CDATA[
          // invalidated on selection and focus changes
          if (this._cachedInsertionPoint !== undefined)
            return this._cachedInsertionPoint;

          // there is no insertion point for history queries
          // so bail out now and save a lot of work when updating commands
          var resultNode = this.getResultNode();
          if (PlacesUtils.nodeIsQuery(resultNode) &&
              asQuery(resultNode).queryOptions.queryType ==
                Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY)
              return this._cachedInsertionPoint = null;

          var orientation = Ci.nsITreeView.DROP_BEFORE;
          // If there is no selection, insert at the end of the container. 
          if (!this.hasSelection) {
            var index = this.view.rowCount - 1;
            this._cachedInsertionPoint =
              this._getInsertionPoint(index, orientation);
            return this._cachedInsertionPoint;
          }

          // This is a two-part process. The first part is determining the drop
          // orientation.
          // * The default orientation is to drop _before_ the selected item.
          // * If the selected item is a container, the default orientation
          //   is to drop _into_ that container.
          //
          // Warning: It may be tempting to use tree indexes in this code, but
          //          you must not, since the tree is nested and as your tree 
          //          index may change when folders before you are opened and
          //          closed. You must convert your tree index to a node, and
          //          then use getIndexOfNode to find your absolute index in
          //          the parent container instead. 
          //
          var resultView = this.view;
          var selection = resultView.selection;
          var rc = selection.getRangeCount();
          var min = { }, max = { };
          selection.getRangeAt(rc - 1, min, max);
          
          // If the sole selection is a container, and we are not in
          // a flatlist, insert into it.
          // Note that this only applies to _single_ selections,
          // if the last element within a multi-selection is a
          // container, insert _adjacent_ to the selection.
          //
          // If the sole selection is the bookmarks toolbar folder, we insert
          // into it even if it is not opened
          var itemId =
            PlacesUtils.getConcreteItemId(resultView.nodeForTreeIndex(max.value));
          if (selection.count == 1 && resultView.isContainer(max.value) &&
              !this.flatList)
            orientation = Ci.nsITreeView.DROP_ON;

          this._cachedInsertionPoint =
            this._getInsertionPoint(max.value, orientation);
          return this._cachedInsertionPoint;
        ]]></getter>
      </property>

      <method name="_getInsertionPoint">
        <parameter name="index"/>
        <parameter name="orientation"/>
        <body><![CDATA[ 
          var result = this.getResult();
          var resultview = this.view;
          var container = result.root;
          var dropNearItemId = -1;
          NS_ASSERT(container, "null container");
          // When there's no selection, assume the container is the container
          // the view is populated from (i.e. the result's itemId).
          if (index != -1) {
            var lastSelected = resultview.nodeForTreeIndex(index);
            if (resultview.isContainer(index) && orientation == Ci.nsITreeView.DROP_ON) {
              // If the last selected item is an open container, append _into_
              // it, rather than insert adjacent to it. 
              container = lastSelected;
              index = -1;
            }
            else if (lastSelected.containerOpen &&
                     orientation == Ci.nsITreeView.DROP_AFTER &&
                     lastSelected.hasChildren) {
             // If the last selected item is an open container and the user is
             // trying to drag into it as a first item, really insert into it.
             container = lastSelected;
             orientation = Ci.nsITreeView.DROP_ON;
             index = 0;
            }
            else {
              // Use the last-selected node's container unless the root node
              // is selected, in which case we use the root node itself as the
              // insertion point.
              container = lastSelected.parent || container;

              // avoid the potentially expensive call to getIndexOfNode() 
              // if we know this container doesn't allow insertion
              if (PlacesControllerDragHelper.disallowInsertion(container))
                return null;

              var queryOptions = asQuery(result.root).queryOptions;
              if (queryOptions.sortingMode !=
                    Ci.nsINavHistoryQueryOptions.SORT_BY_NONE) {
                // If we are within a sorted view, insert at the end
                index = -1;
              }
              else if (queryOptions.excludeItems ||
                       queryOptions.excludeQueries ||
                       queryOptions.excludeReadOnlyFolders) {
                // Some item may be invisible, insert near last selected one.
                // We don't replace index here to avoid requests to the db,
                // instead it will be calculated later by the controller.
                index = -1;
                dropNearItemId = lastSelected.itemId;
              }
              else {
                var lsi = PlacesUtils.getIndexOfNode(lastSelected);
                index = orientation == Ci.nsITreeView.DROP_BEFORE ? lsi : lsi + 1;
              }
            }
          }

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

          return new InsertionPoint(PlacesUtils.getConcreteItemId(container),
                                    index, orientation,
                                    PlacesUtils.nodeIsTagQuery(container),
                                    dropNearItemId);
        ]]></body>
      </method>

      <!-- nsIPlacesView -->
      <method name="selectAll">
        <body><![CDATA[ 
          this.view.selection.selectAll();
        ]]></body>
      </method>

      <!-- This method will select the first node in the tree that matches
           each given item id. It will open any parent nodes that it needs
           to in order to show the selected items.
      -->
      <method name="selectItems">
        <parameter name="aIDs"/>
        <parameter name="aOpenContainers"/>
        <body><![CDATA[
          // By default, we do search and select within containers which were
          // closed (note that containers in which nodes were not found are
          // closed).
          if (aOpenContainers === undefined)
            aOpenContainers = true;

          var ids = aIDs; // don't manipulate the caller's array

          // Array of nodes found by findNodes which are to be selected
          var nodes = [];

          // Array of nodes found by findNodes which should be opened
          var nodesToOpen = [];

          // A set of URIs of container-nodes that were previously searched,
          // and thus shouldn't be searched again. This is empty at the initial
          // start of the recursion and gets filled in as the recursion
          // progresses.
          var nodesURIChecked = [];

          /**
           * Recursively search through a node's children for items
           * with the given IDs. When a matching item is found, remove its ID
           * from the IDs array, and add the found node to the nodes dictionary.
           *
           * NOTE: This method will leave open any node that had matching items
           * in its subtree.
           */
          function findNodes(node) {
            var foundOne = false;
            // See if node matches an ID we wanted; add to results.
            // For simple folder queries, check both itemId and the concrete
            // item id.
            var index = ids.indexOf(node.itemId);
            if (index == -1 &&
                node.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT)
              index = ids.indexOf(asQuery(node).folderItemId);

            if (index != -1) {
              nodes.push(node);
              foundOne = true;
              ids.splice(index, 1);
            }

            if (ids.length == 0 || !PlacesUtils.nodeIsContainer(node) ||
                nodesURIChecked.indexOf(node.uri) != -1)
              return foundOne;

            asContainer(node);
            if (!aOpenContainers && !node.containerOpen)
              return foundOne;

            nodesURIChecked.push(node.uri);

            // Remember the beginning state so that we can re-close
            // this node if we don't find any additional results here.
            var previousOpenness = node.containerOpen;
            node.containerOpen = true;
            for (var child = 0;  child < node.childCount && ids.length > 0;
                 child++) {
              var childNode = node.getChild(child);
              var found = findNodes(childNode);
              if (!foundOne)
                foundOne = found;
            }

            // If we didn't find any additional matches in this node's
            // subtree, revert the node to its previous openness.
            if (foundOne)
              nodesToOpen.unshift(node);
            node.containerOpen = previousOpenness;
            return foundOne;
          }

          // Null the viewer while looking for nodes
          var result = this.getResult();
          var oldViewer = result.viewer;
          result.viewer = null;
          findNodes(this.getResultNode());
          result.viewer = oldViewer;

          // For all the nodes we've found, highlight the corresponding
          // index in the tree.
          var resultview = this.view;
          var selection = this.view.selection;
          selection.selectEventsSuppressed = true;
          selection.clearSelection();
          // Open nodes containing found items
          for (var i = 0; i < nodesToOpen.length; i++) {
            nodesToOpen[i].containerOpen = true;
          }
          for (var i = 0; i < nodes.length; i++) {
            var index = resultview.treeIndexForNode(nodes[i]);
            selection.rangedSelect(index, index, true);
          }
          selection.selectEventsSuppressed = false;
        ]]></body>
      </method>

      <!-- nsDragAndDrop -->
      <method name="onDragStart">
        <parameter name="aEvent"/>
        <parameter name="aXferData"/>
        <parameter name="aDragAction"/>
        <body><![CDATA[ 
          var nodes = this.getSelectionNodes();
          for (var i = 0; i < nodes.length; ++i) {
            var node = nodes[i];

            // Disallow dragging the root node of a tree
            var parent = node.parent;
            if (!parent)
              return;

            // If this node is child of a readonly container (e.g. a livemark)
            // or cannot be moved, we must force a copy.
            if (!PlacesControllerDragHelper.canMoveNode(node)) {
              aEvent.dataTransfer.effectAllowed = "copyLink";
              break;
            }
          }

          // Stuff the encoded selection into the transferable data object
          this._controller.setDataTransfer(aEvent);
        ]]></body>
      </method>
      
      <!-- nsDragAndDrop -->
      <method name="canDrop">
        <parameter name="aEvent"/>
        <parameter name="aDragSession"/>
        <body><![CDATA[
          // Cache the dataTransfer for the view
          PlacesControllerDragHelper.currentDataTransfer = aEvent.dataTransfer;

          var row = { }, col = { }, child = { };
          this.treeBoxObject.getCellAt(aEvent.clientX, aEvent.clientY,
                                       row, col, child);
          var node = row.value != -1 ?
                     this.view.nodeForTreeIndex(row.value) :
                     this.getResultNode();
          // cache the dropTarget for the view
          PlacesControllerDragHelper.currentDropTarget = node;

          // We have to calculate the orientation since view.canDrop will use
          // it and we want to be consistent with the dropfeedback
          var tbo = this.treeBoxObject;
          var rowHeight = tbo.rowHeight;
          var eventY = aEvent.clientY - tbo.treeBody.boxObject.y -
                       rowHeight * (row.value - tbo.getFirstVisibleRow());

          var orientation = Ci.nsITreeView.DROP_BEFORE;

          if (row.value == -1) {
            // If the row is not valid we try to insert inside the resultNode.
            orientation = Ci.nsITreeView.DROP_ON;
          }
          else if (PlacesUtils.nodeIsContainer(node) &&
                   eventY > rowHeight * 0.75) {
            // If we are below the 75% of a container the treeview we try
            // to drop after the node.
            orientation = Ci.nsITreeView.DROP_AFTER;
          }
          else if (PlacesUtils.nodeIsContainer(node) &&
                   eventY > rowHeight * 0.25) {
            // If we are below the 25% of a container the treeview we try
            // to drop inside the node.
            orientation = Ci.nsITreeView.DROP_ON;
          }

          return this.view.canDrop(row.value, orientation, aEvent.dataTransfer);
        ]]></body>
      </method>
      
      <!-- nsDragAndDrop -->
      <method name="onDragOver">
        <parameter name="aEvent"/>
        <parameter name="aFlavour"/>
        <parameter name="aDragSession"/>
        <body><![CDATA[
          if (!this.canDrop(aEvent, aDragSession, aEvent.dataTransfer))
            aEvent.dataTransfer.effectAllowed = "none";
        ]]></body>
      </method>

      <!-- nsDragAndDrop -->
      <method name="getSupportedFlavours">
        <body><![CDATA[
          return PlacesControllerDragHelper.flavourSet;
        ]]></body>
      </method>

      <method name="buildContextMenu">
        <parameter name="aPopup"/>
        <body><![CDATA[
          return this.controller.buildContextMenu(aPopup);
        ]]></body>
      </method>

      <method name="destroyContextMenu">
        <parameter name="aPopup"/>
        <body/>
      </method>
    </implementation>
    <handlers>
      <handler event="focus"><![CDATA[
        this._cachedInsertionPoint = undefined;

        // See select handler. We need the sidebar's places commandset to be
        // updated as well
        document.commandDispatcher.updateCommands("focus");
      ]]></handler>
      <handler event="select"><![CDATA[
        this._cachedInsertionPoint = undefined;

        // This additional complexity is here for the sidebars
        var win = window;
        while (true) {
          win.document.commandDispatcher.updateCommands("focus");
          if (win == window.top)
            break;

          win = win.parent;
        }
      ]]></handler>
      <handler event="draggesture"><![CDATA[
        if (event.target.localName == "treechildren")
          nsDragAndDrop.startDrag(event, this);
      ]]></handler>
      <handler event="dragover"><![CDATA[
        if (event.target.localName == "treechildren")
          nsDragAndDrop.dragOver(event, this);
      ]]></handler>
      <handler event="drop"><![CDATA[
          PlacesControllerDragHelper.currentDataTransfer = event.dataTransfer;
      ]]></handler>
      <handler event="dragexit"><![CDATA[
        PlacesControllerDragHelper.currentDataTransfer = null;
        PlacesControllerDragHelper.currentDropTarget = null;
      ]]></handler>
    </handlers>
  </binding>

</bindings>

