dragsort.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Drag and drop tools by Tim Taylor. Code is under public license.
  2. http://tool-man.org/examples/sorting.html */
  3. // TODO: refactor away duplicationg in DragSort and DragSortX
  4. var dragSort = {
  5. getSortableItems : function(e) {
  6. var items = e.getElementsByTagName('div');
  7. var res = Array();
  8. for (var i = 0; i < items.length; i++) {
  9. if (items[i].className.match(/(^|\s)sort(\s|$)/)) {
  10. res.push(items[i]);
  11. }
  12. }
  13. return res;
  14. },
  15. getOrder : function(e) {
  16. var items = dragSort.getSortableItems(e);
  17. var res = '';
  18. for (var i = 0; i< items.length; i++) {
  19. res += items[i].id+';';
  20. }
  21. return res.replace(/;$/,'');
  22. },
  23. makeElementSortable : function(e) {
  24. var items = dragSort.getSortableItems(e);
  25. for (var i = 0; i < items.length; i++) {
  26. dragSort.makeItemSortable(items[i]);
  27. }
  28. },
  29. prepareItem : function(e) {
  30. for (var i=0; i < e.childNodes.length; i++) {
  31. if (e.childNodes[i].nodeName == 'P' &&
  32. e.childNodes[i].className == 'nojsfield') {
  33. e.removeChild(e.childNodes[i]);
  34. }
  35. }
  36. e.className += ' sortJS';
  37. },
  38. makeItemSortable : function(item) {
  39. Drag.makeDraggable(item);
  40. dragSort.prepareItem(item);
  41. item.setDragThresholdY(5);
  42. item.style.position = 'relative';
  43. item.onDragStart = dragSort.onDragStart;
  44. item.onDrag = dragSort.onDrag;
  45. item.onDragEnd = dragSort.onDragEnd;
  46. },
  47. onDragStart : function(nwPosition, sePosition, nwOffset, seOffset) {
  48. var items = dragSort.getSortableItems(this.parentNode);
  49. var minOffset = Coordinates.northwestOffset(items[0], true);
  50. var maxOffset = Coordinates.northwestOffset(items[items.length - 1], true);
  51. this.constrain(minOffset, maxOffset);
  52. },
  53. onDrag : function(nwPosition, sePosition, nwOffset, seOffset) {
  54. var parent = this.parentNode;
  55. var item = this;
  56. var next = DragUtils.nextItem(item);
  57. while (next != null && this.offsetTop >= next.offsetTop - 2) {
  58. var item = next;
  59. var next = DragUtils.nextItem(item);
  60. }
  61. if (this != item) {
  62. DragUtils.swap(this, next);
  63. return;
  64. }
  65. var item = this;
  66. var previous = DragUtils.previousItem(item);
  67. while (previous != null && this.offsetTop <= previous.offsetTop + 2) {
  68. var item = previous;
  69. var previous = DragUtils.previousItem(item);
  70. }
  71. if (this != item) {
  72. DragUtils.swap(this, item);
  73. return;
  74. }
  75. },
  76. onDragEnd : function(nwPosition, sePosition, nwOffset, seOffset) {
  77. this.style["top"] = "0px";
  78. this.style["left"] = "0px";
  79. if (dragSort.dest != null) {
  80. dragSort.dest.value = dragSort.getOrder(this.parentNode);
  81. }
  82. }
  83. };
  84. var DragUtils = {
  85. swap : function(item1, item2) {
  86. var parent = item1.parentNode;
  87. parent.removeChild(item1);
  88. parent.insertBefore(item1, item2);
  89. item1.style["top"] = "0px";
  90. item1.style["left"] = "0px";
  91. },
  92. nextItem : function(item) {
  93. var sibling = item.nextSibling;
  94. while (sibling != null) {
  95. if (sibling.nodeName == item.nodeName) return sibling;
  96. sibling = sibling.nextSibling;
  97. }
  98. return null;
  99. },
  100. previousItem : function(item) {
  101. var sibling = item.previousSibling;
  102. while (sibling != null) {
  103. if (sibling.nodeName == item.nodeName) return sibling;
  104. sibling = sibling.previousSibling;
  105. }
  106. return null;
  107. }
  108. };