Requires

Provides

Fx.Sort.js

Defines Fx.Sort, a class that reorders lists with a transition.

License:
MIT-style license
Authors:
Aaron Newton
  1. 26
  2. 27
  3. 28
  4. 29
  5. 30
  6. 31
  7. 32
  8. 33
  9. 34
  10. 35
  11. 36
  12. 37
  13. 38
  14. 39
  15. 40
  16. 41
  17. 42
  18. 43
  19. 44
  20. 45
  21. 46
  22. 47
  23. 48
  24. 49
  25. 50
  26. 51
  27. 52
  28. 53
  29. 54
  30. 55
  31. 56
  32. 57
  33. 58
  34. 59
  35. 60
  36. 61
  37. 62
  38. 63
  39. 64
  40. 65
  41. 66
  42. 67
  43. 68
  44. 69
  45. 70
  46. 71
  47. 72
  48. 73
  49. 74
  50. 75
  51. 76
  52. 77
  53. 78
  54. 79
  55. 80
  56. 81
  57. 82
  58. 83
  59. 84
  60. 85
  61. 86
  62. 87
  63. 88
  64. 89
  65. 90
  66. 91
  67. 92
  68. 93
  69. 94
  70. 95
  71. 96
  72. 97
  73. 98
  74. 99
  75. 100
  76. 101
  77. 102
  78. 103
  79. 104
  80. 105
  81. 106
  82. 107
  83. 108
  84. 109
  85. 110
  86. 111
  87. 112
  88. 113
  89. 114
  90. 115
  91. 116
  92. 117
  93. 118
  94. 119
Fx.Sort = new Class({ Extends: Fx.Elements, options: { mode: 'vertical' }, initialize: function(elements, options){ this.parent(elements, options); this.elements.each(function(el){ if (el.getStyle('position') == 'static') el.setStyle('position', 'relative'); }); this.setDefaultOrder(); }, setDefaultOrder: function(){ this.currentOrder = this.elements.map(function(el, index){ return index; }); }, sort: function(){ if (!this.check(arguments)) return this; var newOrder = Array.flatten(arguments); var top = 0, left = 0, next = {}, zero = {}, vert = this.options.mode == 'vertical'; var current = this.elements.map(function(el, index){ var size = el.getComputedSize({styles: ['border', 'padding', 'margin']}); var val; if (vert){ val = { top: top, margin: size['margin-top'], height: size.totalHeight }; top += val.height - size['margin-top']; } else { val = { left: left, margin: size['margin-left'], width: size.totalWidth }; left += val.width; } var plane = vert ? 'top' : 'left'; zero[index] = {}; var start = el.getStyle(plane).toInt(); zero[index][plane] = start || 0; return val; }, this); this.set(zero); newOrder = newOrder.map(function(i){ return i.toInt(); }); if (newOrder.length != this.elements.length){ this.currentOrder.each(function(index){ if (!newOrder.contains(index)) newOrder.push(index); }); if (newOrder.length > this.elements.length) newOrder.splice(this.elements.length-1, newOrder.length - this.elements.length); } var margin = top = left = 0; newOrder.each(function(item, index){ var newPos = {}; if (vert){ newPos.top = top - current[item].top - margin; top += current[item].height; } else { newPos.left = left - current[item].left; left += current[item].width; } margin = margin + current[item].margin; next[item]=newPos; }, this); var mapped = {}; Array.clone(newOrder).sort().each(function(index){ mapped[index] = next[index]; }); this.start(mapped); this.currentOrder = newOrder; return this; }, rearrangeDOM: function(newOrder){ newOrder = newOrder || this.currentOrder; var parent = this.elements[0].getParent(); var rearranged = []; this.elements.setStyle('opacity', 0);

move each element and store the new default order

  1. 121
  2. 122
  3. 123
  4. 124
  5. 125
  6. 126
  7. 127
  8. 128
  9. 129
  10. 130
  11. 131
  12. 132
  13. 133
  14. 134
  15. 135
  16. 136
  17. 137
  18. 138
  19. 139
  20. 140
  21. 141
  22. 142
  23. 143
  24. 144
  25. 145
  26. 146
  27. 147
  28. 148
  29. 149
  30. 150
  31. 151
  32. 152
  33. 153
  34. 154
  35. 155
  36. 156
  37. 157
  38. 158
  39. 159
  40. 160
  41. 161
  42. 162
  43. 163
  44. 164
  45. 165
  46. 166
  47. 167
  48. 168
newOrder.each(function(index){ rearranged.push(this.elements[index].inject(parent).setStyles({ top: 0, left: 0 })); }, this); this.elements.setStyle('opacity', 1); this.elements = $$(rearranged); this.setDefaultOrder(); return this; }, getDefaultOrder: function(){ return this.elements.map(function(el, index){ return index; }); }, forward: function(){ return this.sort(this.getDefaultOrder()); }, backward: function(){ return this.sort(this.getDefaultOrder().reverse()); }, reverse: function(){ return this.sort(this.currentOrder.reverse()); }, sortByElements: function(elements){ return this.sort(elements.map(function(el){ return this.elements.indexOf(el); }, this)); }, swap: function(one, two){ if (typeOf(one) == 'element') one = this.elements.indexOf(one); if (typeOf(two) == 'element') two = this.elements.indexOf(two); var newOrder = Array.clone(this.currentOrder); newOrder[this.currentOrder.indexOf(one)] = two; newOrder[this.currentOrder.indexOf(two)] = one; return this.sort(newOrder); } });