PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/admin/media/js/admin/ordering.js

https://code.google.com/p/mango-py/
JavaScript | 137 lines | 114 code | 11 blank | 12 comment | 20 complexity | c7eead0bda8304a6e92986498f7ac9f7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. addEvent(window, 'load', reorder_init);
  2. var lis;
  3. var top = 0;
  4. var left = 0;
  5. var height = 30;
  6. function reorder_init() {
  7. lis = document.getElementsBySelector('ul#orderthese li');
  8. var input = document.getElementsBySelector('input[name=order_]')[0];
  9. setOrder(input.value.split(','));
  10. input.disabled = true;
  11. draw();
  12. // Now initialise the dragging behaviour
  13. var limit = (lis.length - 1) * height;
  14. for (var i = 0; i < lis.length; i++) {
  15. var li = lis[i];
  16. var img = document.getElementById('handle'+li.id);
  17. li.style.zIndex = 1;
  18. Drag.init(img, li, left + 10, left + 10, top + 10, top + 10 + limit);
  19. li.onDragStart = startDrag;
  20. li.onDragEnd = endDrag;
  21. img.style.cursor = 'move';
  22. }
  23. }
  24. function submitOrderForm() {
  25. var inputOrder = document.getElementsBySelector('input[name=order_]')[0];
  26. inputOrder.value = getOrder();
  27. inputOrder.disabled=false;
  28. }
  29. function startDrag() {
  30. this.style.zIndex = '10';
  31. this.className = 'dragging';
  32. }
  33. function endDrag(x, y) {
  34. this.style.zIndex = '1';
  35. this.className = '';
  36. // Work out how far along it has been dropped, using x co-ordinate
  37. var oldIndex = this.index;
  38. var newIndex = Math.round((y - 10 - top) / height);
  39. // 'Snap' to the correct position
  40. this.style.top = (10 + top + newIndex * height) + 'px';
  41. this.index = newIndex;
  42. moveItem(oldIndex, newIndex);
  43. }
  44. function moveItem(oldIndex, newIndex) {
  45. // Swaps two items, adjusts the index and left co-ord for all others
  46. if (oldIndex == newIndex) {
  47. return; // Nothing to swap;
  48. }
  49. var direction, lo, hi;
  50. if (newIndex > oldIndex) {
  51. lo = oldIndex;
  52. hi = newIndex;
  53. direction = -1;
  54. } else {
  55. direction = 1;
  56. hi = oldIndex;
  57. lo = newIndex;
  58. }
  59. var lis2 = new Array(); // We will build the new order in this array
  60. for (var i = 0; i < lis.length; i++) {
  61. if (i < lo || i > hi) {
  62. // Position of items not between the indexes is unaffected
  63. lis2[i] = lis[i];
  64. continue;
  65. } else if (i == newIndex) {
  66. lis2[i] = lis[oldIndex];
  67. continue;
  68. } else {
  69. // Item is between the two indexes - move it along 1
  70. lis2[i] = lis[i - direction];
  71. }
  72. }
  73. // Re-index everything
  74. reIndex(lis2);
  75. lis = lis2;
  76. draw();
  77. // document.getElementById('hiddenOrder').value = getOrder();
  78. document.getElementsBySelector('input[name=order_]')[0].value = getOrder();
  79. }
  80. function reIndex(lis) {
  81. for (var i = 0; i < lis.length; i++) {
  82. lis[i].index = i;
  83. }
  84. }
  85. function draw() {
  86. for (var i = 0; i < lis.length; i++) {
  87. var li = lis[i];
  88. li.index = i;
  89. li.style.position = 'absolute';
  90. li.style.left = (10 + left) + 'px';
  91. li.style.top = (10 + top + (i * height)) + 'px';
  92. }
  93. }
  94. function getOrder() {
  95. var order = new Array(lis.length);
  96. for (var i = 0; i < lis.length; i++) {
  97. order[i] = lis[i].id.substring(1, 100);
  98. }
  99. return order.join(',');
  100. }
  101. function setOrder(id_list) {
  102. /* Set the current order to match the lsit of IDs */
  103. var temp_lis = new Array();
  104. for (var i = 0; i < id_list.length; i++) {
  105. var id = 'p' + id_list[i];
  106. temp_lis[temp_lis.length] = document.getElementById(id);
  107. }
  108. reIndex(temp_lis);
  109. lis = temp_lis;
  110. draw();
  111. }
  112. function addEvent(elm, evType, fn, useCapture)
  113. // addEvent and removeEvent
  114. // cross-browser event handling for IE5+, NS6 and Mozilla
  115. // By Scott Andrew
  116. {
  117. if (elm.addEventListener){
  118. elm.addEventListener(evType, fn, useCapture);
  119. return true;
  120. } else if (elm.attachEvent){
  121. var r = elm.attachEvent("on"+evType, fn);
  122. return r;
  123. } else {
  124. elm['on'+evType] = fn;
  125. }
  126. }