asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Advanced UI Design Using XML and XSL - Part 4: Folder Tree Drag and Drop
By Joe Slovinski
Rating: 4.5 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction


    Welcome to Advanced UI Design Using XML and XSL. Each article in my series will demonstrate the creation of a specific user interface (UI) component using eXtensible Markup Language (XML) and eXtensible Stylesheet Language (XSL). This article expands on a previous one in this series, Folder Tree Creation (see http://www.15seconds.com/issue/010921.htm). This article explains how to drag and drop entities of a given folder tree within that tree or into another separate tree.

    Drag-and-Drop Control

    Our drag-and-drop control exists on the client and is defined as follows:

    
    function DragControl() {
      this.entity = null;
      this.target = null;
      this.origin = null;
      this.enabled = false;
    
      this.beginX = null;
      this.beginY = null;
    
      this.beginDrag = beginDrag;
      this.endDrag = endDrag;
      this.setTarget = setTarget;
      this.setPosition = setPosition;
      this.move = move;
      this.reset = reset;
    }
    
    
    Our control has the following members:

    Properties

    entity
    The entity property contains a reference to the object which is to be moved from one location to another, whether within its current tree or within another. This property is set within the beginDrag() method.

    target
    The target property contains a reference to the current target of the user's drag and drop. The property is changed as the user moves over entities. This property is set within the setTarget() method.

    origin
    The origin property contains a reference to the folder from which the object was dragged. Upon the release of the left mouse button without a target, the entity is reverted back to this location. The origin property is set with the beginDrag() method.

    enabled
    The enabled property initiates the actual drag and drop. This property is set to true when the user drags their mouse a certain amount of pixels with a selected entity. Within this code version, our drag range for initiation is 5 pixels. This property is changed within the move() method.

    beginX
    This property contains the X coordinate of the mouse at the time of the initiation of the drag-and-drop routine. The beginX property is set within the beginDrag() method.

    beginY
    This property contains the Y coordinate of the mouse at the time of the initiation of the drag-and-drop routine. The beginY property is set within the beginDrag() method.

    Methods

    beginDrag()
    The beginDrag() method is defined as follows.

    
    function beginDrag(obj) {
      if(window.event.button == 1) {
    
        document.onmousemove = function anonymous() { dragControl.move(obj) };
    
        this.origin = obj.parentNode;
        this.entity = obj;
    
        this.beginX = window.event.x;
        this.beginY = window.event.y;
    
        window.event.cancelBubble = true;
      }
    }
    
    
    This method is attached to our entities within our XSL Transformations (XSLT) style sheet.


    Figure 1. beginDrag() method placement

    This method first checks to ensure that the left mouse button fired the event, instead of the right (which is used for context menus). It then sets the proper property values and cancels the bubbling of events within the client browser.

    endDrag()
    The endDrag() method is defined follows:

    
    function endDrag() {
      if(this.entity != null) {
        this.entity.style.position = "static";
        this.entity.style.left = null;
        this.entity.style.top = null;
        this.entity = this.entity.removeNode(true);
    
        if(this.target != null) {
          dragControl.target.appendChild(this.entity);
          if(this.target.open != "true") {
            clickOnEntity(this.target);
          }
        }
        else {
          this.origin.appendChild(this.entity);
        }
    
        document.onmousemove = null;
        document.onmouseup = null;
    
        this.entity = null;
        this.target = null;
        this.enabled = false;
      }
    }
    
    
    This method first ensures that there is an entity selected. Depending on whether or not the user currently has a target selected, it either returns the entity to its origin (given no target) or moves the entity to its new location (given a target).

    This method is attached to our entities within our XSLT style sheet.


    Figure 2. endDrag() method placement

    setTarget()
    The setTarget() method is defined as follows:

    
    function setTarget(obj) {
      if(this.entity != null && this.entity != obj) {
          this.target = obj;
      }
      window.event.cancelBubble = true;
    }
    
    
    This method first ensures that there is a current entity and that the selected entity is not the entity being moved over. It then proceeds to set the target property of the DragControl, after which it cancels the bubbling of events within the client browser.

    This method is attached to our entities within our XSLT style sheet.


    Figure 3. setTarget() method placement

    setPosition()
    The setPosition() method is defined as follows:

    
    function setPosition() {
      this.entity.style.left = window.event.x;
      this.entity.style.top = window.event.y - 10;
    }
    
    
    This method simply sets the selected entity's X and Y coordinates to the current mouse event's X and Y coordinates. This method is called from within the move() method.

    This method is attached to our entities within our tree.js file.


    Figure 4. setPosition() method placement

    move()
    The move() method is defined as follows.

    
    function move(obj) {
      if(window.event.x < this.beginX - 5 || window.event.x > this.beginX + 5 ||
     window.event.y < this.beginY -5 || window.event.y > this.beginY + 5 &&
     this.enabled == false) {
        obj.style.position = "absolute";
        obj.style.filter = "alpha(opacity='60')";
        this.setPosition();
        this.enabled = true;
    
        obj = obj.removeNode(true);
        document.body.appendChild(obj);
        document.onmouseup = function anonymous() { dragControl.endDrag() };
      }
      else if(this.enabled == true) {
        this.setPosition();
      }
    }
    
    
    This method ensures that the user is actually dragging the entity by at least 5 pixels in any direction whether X, Y, positive, or negative. It then moves the entity out of the current tree and into the body of the document. It is continuously called via the "onmousemove" event of the document object and ensures that the entity is always displayed next to the mouse cursor.

    This method is attached to our entities within the tree.js file.


    Figure 5. move() method placement

    reset()
    The reset() method is defined as follows:

    
    function reset() {
      document.onmouseup = null;
      document.onmousemove = null;
    
      this.entity = null;
      this.origin = null;
      this.target = null;
    }
    
    
    This method simply cleans up the document and the DragControl object. It is called whenever a drag and drop is completed or canceled.

    This method is attached to our entities within our XSLT style sheet.


    Figure 6. reset() method placement

    View a live demo of the folder tree drag-and-drop control. For demonstrations purposes I have created two trees and placed entities titled numerically "One" to "Six" beneath them. Try dragging them so that "Two" is nested beneath "One," "Three" is nested beneath "Two," and so on. Below is a screen shot of the entity named "One" being dragged from Tree 1 into Tree 2 after all numbers have been logically ordered (nested) beneath one another by the user.


    Figure 7. Dragging and dropping from one tree to another

  • Download complete source code.
  • View a live demo

    Closing

    I hope the contents of this article will help increase the quality of your Web applications. If you have any questions, comments, suggestions, or requests, please feel free to email me at the address listed in the author section.

    Other Articles in the Series

    Advanced UI Design Using XML and XSL

    Part 1 -- Folder Tree Creation
    http://www.15seconds.com/issue/010921.htm

    Part 2 -- Custom Context Menu Creation
    http://www.15seconds.com/issue/010927.htm

    Part 3 -- Folder Tree Administration
    http://www.15seconds.com/issue/011113.htm

    Part 5 -- Progress Indicator Creation
    http://www.15seconds.com/issue/011212.htm

    Part 6 -- Progress Indicator Usage
    http://www.15seconds.com/issue/020109.htm

    Part 7 -- Relational Folder Tree Lines
    http://www.15seconds.com/issue/020424.htm

    About the Author

    Joe Slovinski has been programming Web applications since 1993. For more information on the code in this article, contact the Joe Slovinski.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    Stonebroom.ASP2XML
    Stonebroom.ASP2XML(c) is an interface component designed to make building applications that transport data in XML format much easier. It can be used to automatically pass updates back to the original data source.
    [Top]
    Other Articles
    Sep 22, 2005 - Implementing Remote Calling Without Using AJAX
    Right now the latest buzzword around town is AJAX. AJAX is an acronym for Asynchronous JavaScript and XML and is a method used to implement remote calling. The problem is that AJAX is only implemented in ASP.NET 2.0. This article will show you one way to implement remote calling without using AJAX or the XMLHttpRequest object. The technique outlined can even be used from classic ASP and is sufficient for most remote calling needs.
    [Read This Article]  [Top]
    Aug 18, 2005 - SQL Server 2005 XQuery and XML-DML - Part 3
    This article is the third and final installment of Alex Homer's series covering the new XML support in Microsoft SQL Server 2005. In it he covers updating the contents of xml columns, comparing traditional XML update techniques with XQuery, and using XQuery in a managed code stored procedure.
    [Read This Article]  [Top]
    Aug 11, 2005 - SQL Server 2005 XQuery and XML-DML - Part 2
    In the second part of his series on SQL Server 2005's new XML support, Alex Homer looks at extracting data from XML columns, comparing traditional XML data access approaches with XQuery, and combining XQuery and XSL-T.
    [Read This Article]  [Top]
    Aug 3, 2005 - SQL Server 2005 XQuery and XML-DML - Part 1
    Microsoft SQL Server 2005 now offers great support for and close integration with XML as a data persistence format. In the first article of his series examining this new support, Alex Homer offers an overview of how SQL Server 2005 stores XML documents and schemas, examines how it supports querying and manipulating XML documents, and provides a simple test application that allows you to experiment with XQuery.
    [Read This Article]  [Top]
    Jun 30, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 3, Cont'd
    In the final article of his series on reading and writing XML in .NET 2.0, Alex Homer looks at how the updated XML document store objects XmlDocument, XmlDataDocument and PathDocument can be used to read, persist and write XML documents and fragments more easily and more efficiently than in .NET 1.x.
    [Read This Article]  [Top]
    Jun 29, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 3
    In the final article of his series on reading and writing XML in .NET 2.0, Alex Homer looks at how the updated XML document store objects XmlDocument, XmlDataDocument and PathDocument can be used to read, persist and write XML documents and fragments more easily and more efficiently than in .NET 1.x.
    [Read This Article]  [Top]
    Jun 16, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 2, Cont'd
    Alex Homer continues his series on reading and writing XML in .NET 2.0. In part one, we focused on the reading side of things, examining the XmlReader and XmlReaderSettings classes. In this article, we move on to look at the XmlWriter and XmlWriterSettings classes, and how they can be used to write XML documents and fragments more easily and more efficiently than in version 1.x of .NET.
    [Read This Article]  [Top]
    Jun 15, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 2
    Alex Homer continues his series on reading and writing XML in .NET 2.0. In part one, we focused on the reading side of things, examining the XmlReader and XmlReaderSettings classes. In this article, we move on to look at the XmlWriter and XmlWriterSettings classes, and how they can be used to write XML documents and fragments more easily and more efficiently than in version 1.x of .NET.
    [Read This Article]  [Top]
    Jun 2, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 1, Cont'd
    In the first part of his series on reading and writing XML in .NET 2.0, Alex Homer discusses the XmlReader and XmlReaderSettings classes. The XmlReader exposes several useful new features and the all new XmlReaderSettings class makes it easy to generate single or multiple instances of an XmlReader with a range of useful properties.
    [Read This Article]  [Top]
    Jun 1, 2005 - Reading and Writing XML in .NET Version 2.0 - Part 1
    In the first part of his series on reading and writing XML in .NET 2.0, Alex Homer discusses the XmlReader and XmlReaderSettings classes. The XmlReader exposes several useful new features and the all new XmlReaderSettings class makes it easy to generate single or multiple instances of an XmlReader with a range of useful properties.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs