Drag and Drop to sort slides : Drag Drop « GUI Components « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Event
7. Event onMethod
8. Form Control
9. GUI Components
10. HTML
11. Javascript Collections
12. Javascript Objects
13. Language Basics
14. Node Operation
15. Object Oriented
16. Page Components
17. Security
18. Style Layout
19. Table
20. Utilities
21. Window Browser
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
JavaScript DHTML » GUI Components » Drag Drop 
Drag and Drop to sort slides

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
<html><head><!-- Copyright (c2005 Tim Taylor Consulting (see LICENSE.txt--><title>Edit in Place with JavaScript and CSS</title>

<!-- common.css -->
<STYLE rel="stylesheet" type="text/css">
body {
  font-family: Verdana, Arial, sans-serif;
  font-size: 14px;
  margin: 0px;
  padding: 0px 20px;
  min-width: 30em;
}

h1, h2, h3, h4, h5, h6 {
  margin: 0px;
}
h2, h3, h4, h5, h6 {
  font-family: Optima, Verdana, sans-serif;
  margin: 1em -10px 0.5em -10px;
}
h2 margin-top: 1.5em; }
h1 {
  font-size: 22px;
  margin-top: 0px;
  margin-bottom: 0px;
  padding: 5px 10px 5px 10px;
  color: #fff;
  text-shadow: #8760c1 0px 0px 5px;
}
.breadcrumb {
  list-style-type: none;
  padding: 0px;
  padding-left: 20px;
  margin: 0px;
  margin-bottom: 10px;
  border-style: solid;
  border-color: #8760c1;
  border-width: 1px 0px;
  height: 19px;
}
.breadcrumb li.first {
  border-left: 1px solid #8760c1;
}
.breadcrumb li {
  float: left;
  border-right: 1px solid #8760c1;
  padding: 3px 10px;
}
.breadcrumb li a {
  display: block;
  text-decoration: none;
}
.breadcrumb li a:hover {
  text-decoration: underline;
}
h1, .breadcrumb {
  font-family: Skia;
  background-color: #ccaaff;
  margin-left: -20px;
  margin-right: -20px;
}

h2 font-size: 22px; }
h3 font-size: 20px; }
h4 font-size: 18px; }
h5 font-size: 16px; }
h6 font-size: 14px; }
{
  margin-top: 0px;
  margin-bottom: 1em;
  text-align: justify;
  max-width: 40em;
}
li p {
  margin-bottom: 0.75em;
}
br.clear {
  clear: both;
  margin: 0px;
}
.sidebar {
  margin: 0px 10px 30px 30px;
  clear: right;
  float: right;
  width: 134px;
  border: 1px solid #8760c1;
  background-color: #ccaaff;
  padding: 5px;
  font-size: 11px;
    font-family: 'Lucida Grande', Geneva, Verdana, Helvetica, Arial, sans-serif;
    -moz-border-radius: 0px 10px 0px 10px;
    border-radius: 0px 10px 0px 10px;
}
.sidebar, .sidebar p, .sidebar li {
  text-align: left;
}
.sidebar ul {
  margin: 0px;
  margin-left: 1.5em;
  padding: 0px;
  list-style-type: circle;
}

td.caption {
  font-size: 12px;
  text-align: center;
}
#copyright {
  margin-bottom: 1em;
  text-align: center;
  font-size: 11px;
}
blockquote {
  font-size: 13px;
  font-style: italic;
}
blockquote .attribution {
  font-weight: normal;
  font-style: normal;
  text-align: right;
}
text-decoration: none; }
a:hover text-decoration: underline; }
a:active background-color: #ffd700; }

</STYLE>

<!-- lists.css -->
<style rel="stylesheet" type="text/css">
ul.sortable li {
  position: relative;
}

ul.boxy {
  list-style-type: none;
  padding: 0px;
  margin: 0px;
  width: 10em;
  font-size: 13px;
  font-family: Arial, sans-serif;
}
ul.boxy li {
  cursor:move;
  padding: 2px 2px;
  border: 1px solid #ccc;
  background-color: #eee;
}
.clickable a {
  display: block;
  text-decoration: none;
  cursor: pointer;
  cursor: hand;
}
.clickable li:hover {
  background-color: #f6f6f6;
}

</STYLE>
<style type="text/css"><!--
/* TODO: this CSS is in need of major cleanup */
.slideshow {
  list-style-type: none;
  margin: 0px;
  padding: 0px;
}

.slide {
  position: relative;
  float: left;
  width: 172px;
  margin-bottom: 10px;
  margin-right: 10px;
}

.slide div.thumb {
  background: #fff;
  width: 170px;
  height: 120px;
  border: 1px solid #000;
  font-size: 5px;
  font-family: "Times New Roman", serif;
  overflow: hidden;
}

.slide .view {
  padding: 2px 2px;
  margin: 2px 0px;
  cursor: text;
  border-width: 1px;
  border-style: solid;
  border-color: #ccc;
  background-color: #eee;
  height: 1em;
}
.view:hover {
  background-color: #ffffcc;
}
.view, .inplace, #list5 input {
  font-size: 14px;
  font-family: sans-serif;
}

.inplace {
  position: absolute;
  visibility: hidden;
  z-index: 10000;
}
.inplace, #list5 input:hover, #list5 input:focus {
  background-color: #ffffcc;
}
#slideEditors input.inplace {
  width: 12em;
  max-width: 12em;
  margin-left: 1px;
}
#slideEditors input.inplace, #slideshow .view {
  text-align: center;
}

#paragraphView, #paragraphEdit, #markupView, #markupEdit {
  font-family: "Times New Roman", serif;
  font-size: 14px;
}
#paragraphView, #markupView {
  border: 1px solid #fff;
  padding: 8px;
  width: 400px;
  max-width: 400px;
}
#paragraphView:hover, #markupView:hover {
  background-color: #ffffcc;
  border-color: #ccc;
}
#paragraphEdit, #markupEdit {
  width: 315px;
  background-color: #ffffcc;
}
#paragraphEdit {
  height: 5em;
}
#markupEdit {
  height: 15em;
}

#listExamples td {
  width: 9em;
  margin-right: 20px; 
  padding: 0px 20px;
  vertical-align: top;
}
#listExamples th {
  vertical-align: bottom;
  font-weight: normal;
  font-size: 14px;
  padding-top: 20px;
}
#listExamples td.caption {
  font-size: 12px;
  text-align: center;
}
#listExamples li {
  padding: 0px;
  height: 20px;
  min-height: 1em;
  width: 120px;
}
#listExamples li .view {
  height: 16px;
  vertical-align: middle;
  padding: 2px;
}
#list1 li:hover {
  background-color: #eee;
}
#listExamples input.inplace {
  width: 120px;
  max-width: 120px;
}

/* BugFix: Firefox: avoid bottom margin on draggable elements */
#listExamples #list4, #listExamples #list5 margin-top: -2px; }
#listExamples #list4 li, #listExamples #list5 li margin-top: 4px; }

#listExamples #list4 li cursor: default}
#listExamples #list4 .handle,
#listExamples #list5 .handle {
  float: right;
  background-color: #ccc;
  background-image: url(common/handle.png);
  background-repeat: repeat-y;
  width: 7px;
  height: 20px;
}
#listExamples #list4 li .view {
  cursor: text;
}
#listExamples #list4Editors input.inplace, #listExamples #list5 input {
  width: 104px;
  max-width: 104px;
}
#listExamples #list4Editors>input.inplace, #listExamples #list5>li>input {
  width: 111px;
  max-width: 111px;
}
#list5 input {
  background-color: #eee;
}
.inplace, #list5 input {
  margin: 0px;
  padding-left: 1px;
}
.handle {
  cursor: move;
}
--></style>


<!-- core.js -->
<script language="JavaScript" type="text/javascript">
/*
Copyright (c) 2005 Tim Taylor Consulting <http://tool-man.org/>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

var ToolMan = {
  events : function() {
    if (!ToolMan._eventsFactorythrow "ToolMan Events module isn't loaded";
    return ToolMan._eventsFactory
  },

  css : function() {
    if (!ToolMan._cssFactorythrow "ToolMan CSS module isn't loaded";
    return ToolMan._cssFactory
  },

  coordinates : function() {
    if (!ToolMan._coordinatesFactorythrow "ToolMan Coordinates module isn't loaded";
    return ToolMan._coordinatesFactory
  },

  drag : function() {
    if (!ToolMan._dragFactorythrow "ToolMan Drag module isn't loaded";
    return ToolMan._dragFactory
  },

  dragsort : function() {
    if (!ToolMan._dragsortFactorythrow "ToolMan DragSort module isn't loaded";
    return ToolMan._dragsortFactory
  },

  helpers : function() {
    return ToolMan._helpers
  },

  cookies : function() {
    if (!ToolMan._cookieOventhrow "ToolMan Cookie module isn't loaded";
    return ToolMan._cookieOven
  },

  junkdrawer : function() {
    return ToolMan._junkdrawer
  }

}

ToolMan._helpers = {
  map : function(array, func) {
    for (var i = 0, n = array.length; i < n; i++func(array[i])
  },

  nextItem : function(item, nodeName) {
    if (item == nullreturn
    var next = item.nextSibling
    while (next != null) {
      if (next.nodeName == nodeNamereturn next
      next = next.nextSibling
    }
    return null
  },

  previousItem : function(item, nodeName) {
    var previous = item.previousSibling
    while (previous != null) {
      if (previous.nodeName == nodeNamereturn previous
      previous = previous.previousSibling
    }
    return null
  },

  moveBefore : function(item1, item2) {
    var parent = item1.parentNode
    parent.removeChild(item1)
    parent.insertBefore(item1, item2)
  },

  moveAfter : function(item1, item2) {
    var parent = item1.parentNode
    parent.removeChild(item1)
    parent.insertBefore(item1, item2 ? item2.nextSibling : null)
  }
}

/** 
 * scripts without a proper home
 *
 * stuff here is subject to change unapologetically and without warning
 */
ToolMan._junkdrawer = {
  serializeList : function(list) {
    var items = list.getElementsByTagName("li")
    var array = new Array()
    for (var i = 0, n = items.length; i < n; i++) {
      var item = items[i]

      array.push(ToolMan.junkdrawer()._identifier(item))
    }
    return array.join('|')
  },

  inspectListOrder : function(id) {
    alert(ToolMan.junkdrawer().serializeList(document.getElementById(id)))
  },

  restoreListOrder : function(listID) {
    var list = document.getElementById(listID)
    if (list == nullreturn

    var cookie = ToolMan.cookies().get("list-" + listID)
    if (!cookiereturn;

    var IDs = cookie.split('|')
    var items = ToolMan.junkdrawer()._itemsByID(list)

    for (var i = 0, n = IDs.length; i < n; i++) {
      var itemID = IDs[i]
      if (itemID in items) {
        var item = items[itemID]
        list.removeChild(item)
        list.insertBefore(item, null)
      }
    }
  },

  _identifier : function(item) {
    var trim = ToolMan.junkdrawer().trim
    var identifier

    identifier = trim(item.getAttribute("id"))
    if (identifier != null && identifier.length > 0return identifier;
    
    identifier = trim(item.getAttribute("itemID"))
    if (identifier != null && identifier.length > 0return identifier;
    
    // FIXME: strip out special chars or make this an MD5 hash or something
    return trim(item.innerHTML)
  },

  _itemsByID : function(list) {
    var array = new Array()
    var items = list.getElementsByTagName('li')
    for (var i = 0, n = items.length; i < n; i++) {
      var item = items[i]
      array[ToolMan.junkdrawer()._identifier(item)] = item
    }
    return array
  },

  trim : function(text) {
    if (text == nullreturn null
    return text.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2')
  }
}

</script>
<!-- events.js -->
<script language="JavaScript" type="text/javascript">
/* Copyright (c) 2005 Tim Taylor Consulting (see LICENSE.txt) */

ToolMan._eventsFactory = {
  fix : function(event) {
    if (!eventevent = window.event

    if (event.target) {
      if (event.target.nodeType == 3event.target = event.target.parentNode
    else if (event.srcElement) {
      event.target = event.srcElement
    }

    return event
  },

  register : function(element, type, func) {
    if (element.addEventListener) {
      element.addEventListener(type, func, false)
    else if (element.attachEvent) {
      if (!element._listenerselement._listeners = new Array()
      if (!element._listeners[type]) element._listeners[typenew Array()
      var workaroundFunc = function() {
        func.apply(element, new Array())
      }
      element._listeners[type][func= workaroundFunc
      element.attachEvent('on' + type, workaroundFunc)
    }
  },

  unregister : function(element, type, func) {
    if (element.removeEventListener) {
      element.removeEventListener(type, func, false)
    else if (element.detachEvent) {
      if (element._listeners 
          && element._listeners[type
          && element._listeners[type][func]) {

        element.detachEvent('on' + type, 
            element._listeners[type][func])
      }
    }
  }
}

</script>
<!-- css.js -->
<script language="JavaScript" type="text/javascript">
/* Copyright (c) 2005 Tim Taylor Consulting (see LICENSE.txt) */

// TODO: write unit tests
ToolMan._cssFactory = {
  readStyle : function(element, property) {
    if (element.style[property]) {
      return element.style[property]
    else if (element.currentStyle) {
      return element.currentStyle[property]
    else if (document.defaultView && document.defaultView.getComputedStyle) {
      var style = document.defaultView.getComputedStyle(element, null)
      return style.getPropertyValue(property)
    else {
      return null
    }
  }
}

</script>
<!-- coordinates.js -->
<script language="JavaScript" type="text/javascript">
/* Copyright (c) 2005 Tim Taylor Consulting (see LICENSE.txt) */

/* FIXME: assumes position styles are specified in 'px' */

ToolMan._coordinatesFactory = {

  create function(x, y) {
    // FIXME: Safari won't parse 'throw' and aborts trying to do anything with this file
    //if (isNaN(x) || isNaN(y)) throw "invalid x,y: " + x + "," + y
    return new _ToolManCoordinate(this, x, y)
  },

  origin : function() {
    return this.create(00)
  },

  /*
   * FIXME: Safari 1.2, returns (0,0) on absolutely positioned elements
   */
  topLeftPosition : function(element) {
    var left = parseInt(ToolMan.css().readStyle(element, "left"))
    var left = isNaN(left: left
    var top = parseInt(ToolMan.css().readStyle(element, "top"))
    var top = isNaN(top: top

    return this.create(left, top)
  },

  bottomRightPosition : function(element) {
    return this.topLeftPosition(element).plus(this._size(element))
  },

  topLeftOffset : function(element) {
    var offset = this._offset(element

    var parent = element.offsetParent
    while (parent) {
      offset = offset.plus(this._offset(parent))
      parent = parent.offsetParent
    }
    return offset
  },

  bottomRightOffset : function(element) {
    return this.topLeftOffset(element).plus(
        this.create(element.offsetWidth, element.offsetHeight))
  },

  scrollOffset : function() {
    if (window.pageXOffset) {
      return this.create(window.pageXOffset, window.pageYOffset)
    else if (document.documentElement) {
      return this.create(
          document.body.scrollLeft + document.documentElement.scrollLeft, 
          document.body.scrollTop + document.documentElement.scrollTop)
    else if (document.body.scrollLeft >= 0) {
      return this.create(document.body.scrollLeft, document.body.scrollTop)
    else {
      return this.create(00)
    }
  },

  clientSize : function() {
    if (window.innerHeight >= 0) {
      return this.create(window.innerWidth, window.innerHeight)
    else if (document.documentElement) {
      return this.create(document.documentElement.clientWidth,
          document.documentElement.clientHeight)
    else if (document.body.clientHeight >= 0) {
      return this.create(document.body.clientWidth,
          document.body.clientHeight)
    else {
      return this.create(00)
    }
  },

  /**
   * mouse coordinate relative to the window (technically the
   * browser client area) i.e. the part showing your page
   *
   * NOTE: in Safari the coordinate is relative to the document
   */
  mousePosition : function(event) {
    event = ToolMan.events().fix(event)
    return this.create(event.clientX, event.clientY)
  },

  /**
   * mouse coordinate relative to the document
   */
  mouseOffset : function(event) {
    event = ToolMan.events().fix(event)
    if (event.pageX >= || event.pageX < 0) {
      return this.create(event.pageX, event.pageY)
    else if (event.clientX >= || event.clientX < 0) {
      return this.mousePosition(event).plus(this.scrollOffset())
    }
  },

  _size : function(element) {
  /* TODO: move to a Dimension class */
    return this.create(element.offsetWidth, element.offsetHeight)
  },

  _offset : function(element) {
    return this.create(element.offsetLeft, element.offsetTop)
  }
}

function _ToolManCoordinate(factory, x, y) {
  this.factory = factory
  this.x = isNaN(x: x
  this.y = isNaN(y: y
}

_ToolManCoordinate.prototype = {
  toString : function() {
    return "(" this.x + "," this.y + ")"
  },

  plus : function(that) {
    return this.factory.create(this.x + that.x, this.y + that.y)
  },

  minus : function(that) {
    return this.factory.create(this.x - that.x, this.y - that.y)
  },

  min : function(that) {
    return this.factory.create(
        Math.min(this.x , that.x), Math.min(this.y , that.y))
  },

  max : function(that) {
    return this.factory.create(
        Math.max(this.x , that.x), Math.max(this.y , that.y))
  },

  constrainTo : function (one, two) {
    var min = one.min(two)
    var max = one.max(two)

    return this.max(min).min(max)
  },

  distance : function (that) {
    return Math.sqrt(Math.pow(this.x - that.x, 2+ Math.pow(this.y - that.y, 2))
  },

  reposition : function(element) {
    element.style["top"this.y + "px"
    element.style["left"this.x + "px"
  }
}

</script>
<!-- drag.js -->
<script language="JavaScript" type="text/javascript">
/* Copyright (c) 2005 Tim Taylor Consulting (see LICENSE.txt) */

ToolMan._dragFactory = {
  createSimpleGroup : function(element, handle) {
    handle = handle ? handle : element
    var group = this.createGroup(element)
    group.setHandle(handle)
    group.transparentDrag()
    group.onTopWhileDragging()
    return group
  },

  createGroup : function(element) {
    var group = new _ToolManDragGroup(this, element)

    var position = ToolMan.css().readStyle(element, 'position')
    if (position == 'static') {
      element.style["position"'relative'
    else if (position == 'absolute') {
      /* for Safari 1.2 */
      ToolMan.coordinates().topLeftOffset(element).reposition(element)
    }

    // TODO: only if ToolMan.isDebugging()
    group.register('draginit', this._showDragEventStatus)
    group.register('dragmove', this._showDragEventStatus)
    group.register('dragend', this._showDragEventStatus)

    return group
  },

  _showDragEventStatus : function(dragEvent) {
    window.status = dragEvent.toString()
  },

  constraints : function() {
    return this._constraintFactory
  },

  _createEvent : function(type, event, group) {
    return new _ToolManDragEvent(type, event, group)
  }
}

function _ToolManDragGroup(factory, element) {
  this.factory = factory
  this.element = element
  this._handle = null
  this