home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / toolkit.jar / content / global / nsDragAndDrop.js < prev    next >
Encoding:
JavaScript  |  2001-07-02  |  7.5 KB  |  210 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  *   Ben Goodger <ben@netscape.com> (Original Author)
  22.  */
  23.  
  24. ////////////////////////////////////////////////////////////////////////////
  25. // XXX - WARNING - DRAG AND DROP API CHANGE ALERT - XXX
  26. // This file has been extensively modified in a checkin planned for Mozilla
  27. // 0.8, and the API has been modified. DO NOT MODIFY THIS FILE without 
  28. // approval from ben@netscape.com, otherwise your changes will be lost. 
  29.  
  30. /**
  31.  * XXX - until load is supported in chrome, you also need to include 
  32.  *       these files:
  33.  *       chrome://global/content/nsTransferable.js
  34.  **/
  35.  
  36.  
  37.  
  38. /**
  39.  * nsDragAndDrop - a convenience wrapper for nsTransferable, nsITransferable
  40.  *                 and nsIDragService/nsIDragSession. 
  41.  *
  42.  * USAGE INFORMATION: see 'README-nsDragAndDrop.html' in the same source directory
  43.  *                    as this file (typically xpfe/global/resources/content)
  44.  */
  45.  
  46. var nsDragAndDrop = {
  47.   
  48.   _mDS: null,
  49.   get mDragService()
  50.     {
  51.       if (!this._mDS) 
  52.         {
  53.           const kDSContractID = "@mozilla.org/widget/dragservice;1";
  54.           const kDSIID = Components.interfaces.nsIDragService;
  55.           this._mDS = Components.classes[kDSContractID].getService(kDSIID);
  56.         }
  57.       return this._mDS;
  58.     },
  59.  
  60.   /**
  61.    * void startDrag (DOMEvent aEvent, Object aDragDropObserver) ;
  62.    *
  63.    * called when a drag on an element is started.
  64.    *
  65.    * @param DOMEvent aEvent
  66.    *        the DOM event fired by the drag init
  67.    * @param Object aDragDropObserver
  68.    *        javascript object of format described above that specifies
  69.    *        the way in which the element responds to drag events.
  70.    **/  
  71.   startDrag: function (aEvent, aDragDropObserver)
  72.     {
  73.       if (!("onDragStart" in aDragDropObserver))
  74.         return;
  75.  
  76.       const kDSIID = Components.interfaces.nsIDragService;
  77.       var dragAction = { action: kDSIID.DRAGDROP_ACTION_COPY + kDSIID.DRAGDROP_ACTION_MOVE + kDSIID.DRAGDROP_ACTION_LINK };
  78.  
  79.       var transferData = { data: null };
  80.       try 
  81.         {
  82.           aDragDropObserver.onDragStart(aEvent, transferData, dragAction);
  83.         }
  84.       catch (e) 
  85.         {
  86.           return;  // not a draggable item, bail!
  87.         }
  88.  
  89.       if (!transferData.data) return;
  90.       transferData = transferData.data;
  91.       
  92.       var transArray = Components.classes["@mozilla.org/supports-array;1"]
  93.                                  .createInstance(Components.interfaces.nsISupportsArray);
  94.  
  95.       var count = 0;
  96.       do 
  97.         {
  98.           var trans = nsTransferable.set(transferData._XferID == "TransferData" 
  99.                                          ? transferData 
  100.                                          : transferData.dataList[count++]);
  101.           transArray.AppendElement(trans.QueryInterface(Components.interfaces.nsISupports));
  102.         }
  103.       while (transferData._XferID == "TransferDataSet" && 
  104.              count < transferData.dataList.length);
  105.       
  106.       this.mDragService.invokeDragSession(aEvent.target, transArray, null, dragAction.action);
  107.       aEvent.preventBubble();
  108.     },
  109.  
  110.   /** 
  111.    * void dragOver (DOMEvent aEvent, Object aDragDropObserver) ;
  112.    *
  113.    * called when a drag passes over this element
  114.    *
  115.    * @param DOMEvent aEvent
  116.    *        the DOM event fired by the drag init
  117.    * @param Object aDragDropObserver
  118.    *        javascript object of format described above that specifies
  119.    *        the way in which the element responds to drag events.
  120.    **/
  121.   dragOver: function (aEvent, aDragDropObserver)
  122.     { 
  123.       if (!("onDragOver" in aDragDropObserver)) 
  124.         return;
  125.       if (!this.mDragSession) 
  126.         this.mDragSession = this.mDragService.getCurrentSession();
  127.       if (this.mDragSession)
  128.         {
  129.           var flavourSet = aDragDropObserver.getSupportedFlavours();
  130.           for (var flavour in flavourSet.flavourTable)
  131.             {
  132.               if (this.mDragSession.isDataFlavorSupported(flavour))
  133.                 {
  134.                   this.mDragSession.canDrop = (this.mDragSession.sourceNode != aEvent.target);
  135.                   aDragDropObserver.onDragOver(aEvent, 
  136.                                                flavourSet.flavourTable[flavour], 
  137.                                                this.mDragSession);
  138.                   aEvent.preventBubble();
  139.                   break;
  140.                 }
  141.             }
  142.         }
  143.     },
  144.  
  145.   mDragSession: null,
  146.  
  147.   /** 
  148.    * void drop (DOMEvent aEvent, Object aDragDropObserver) ;
  149.    *
  150.    * called when the user drops on the element
  151.    *
  152.    * @param DOMEvent aEvent
  153.    *        the DOM event fired by the drag init
  154.    * @param Object aDragDropObserver
  155.    *        javascript object of format described above that specifies
  156.    *        the way in which the element responds to drag events.
  157.    **/
  158.   drop: function (aEvent, aDragDropObserver)
  159.     {
  160.       if (!("onDrop" in aDragDropObserver))
  161.         return;
  162.         
  163.       if (!this.mDragSession) 
  164.         this.mDragSession = this.mDragService.getCurrentSession();
  165.       if (this.mDragSession)
  166.         {
  167.           var flavourSet = aDragDropObserver.getSupportedFlavours();
  168.           var transferData = nsTransferable.get(flavourSet, this.getDragData, true);
  169.           aEvent.preventBubble();
  170.           // hand over to the client to respond to dropped data
  171.           var multiple = "canHandleMultipleItems" in aDragDropObserver && aDragDropObserver.canHandleMultipleItems;
  172.           var dropData = multiple ? transferData : transferData.first.first;
  173.           aDragDropObserver.onDrop(aEvent, dropData, this.mDragSession);
  174.         }
  175.     },
  176.  
  177.   dragExit: function (aEvent, aDragDropObserver)
  178.     {
  179.       if ("onDragExit" in aDragDropObserver)
  180.         aDragDropObserver.onDragExit(aEvent, this.mDragSession);
  181.     },  
  182.     
  183.   /** 
  184.    * nsISupportsArray getDragData (Object aFlavourList)
  185.    *
  186.    * Creates a nsISupportsArray of all droppable items for the given
  187.    * set of supported flavours.
  188.    * 
  189.    * @param FlavourSet aFlavourSet
  190.    *        formatted flavour list.
  191.    **/  
  192.   getDragData: function (aFlavourSet)
  193.     {
  194.       var supportsArray = Components.classes["@mozilla.org/supports-array;1"]
  195.                                     .createInstance(Components.interfaces.nsISupportsArray);
  196.  
  197.       for (var i = 0; i < nsDragAndDrop.mDragSession.numDropItems; ++i)
  198.         {
  199.           var trans = nsTransferable.createTransferable();
  200.           for (var j = 0; j < aFlavourSet.flavours.length; ++j)
  201.             trans.addDataFlavor(aFlavourSet.flavours[j].contentType);
  202.           nsDragAndDrop.mDragSession.getData(trans, i);
  203.           supportsArray.AppendElement(trans);
  204.         }
  205.       return supportsArray;
  206.     }
  207.  
  208. };
  209.  
  210.