Requires

Provides

Sortables.js

Class for creating a drag and drop sorting interface for lists of items.

License:
MIT-style license
Authors:
Tom Occhino, Jacob Thornton
  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
var Sortables = new Class({ Implements: [Events, Options], options: {/* onSort: function(element, clone){}, onStart: function(element, clone){}, onComplete: function(element){},*/ snap: 4, opacity: 1, clone: false, revert: false, handle: false, selector: 'li', constrain: false, preventDefault: false }, initialize: function(lists, options){ this.setOptions(options); this.elements = []; this.lists = []; this.idle = true; this.addLists($$(document.id(lists) || lists)); if (!this.options.clone) this.options.revert = false; if (this.options.revert) this.effect = new Fx.Morph(null, Object.merge({ duration: 250, link: 'cancel' }, this.options.revert)); }, attach: function(){ this.addLists(this.lists); return this; }, detach: function(){ this.lists = this.removeLists(this.lists); return this; }, addItems: function(){ Array.flatten(arguments).each(function(element){ this.elements.push(element); }, this); return this; }, addLists: function() { var selector = this.options.selector; Array.flatten(arguments).each(function(list) { this.lists.push(list); this.elements.append(Array.from(list.getChildren(selector))); var start = !this.options.handle ? this.start.bind(this) : function(event, element) { this.start(event, element.getParent(selector)); }.bind(this); if (this.options.handle) selector += ' ' + this.options.handle; list.addEvent('mousedown:relay(' + selector + ')', start); }, this); return this; }, removeItems: function(){ return $$(Array.flatten(arguments).map(function(element){ this.elements.erase(element); return element; }, this)); }, removeLists: function(){ return $$(Array.flatten(arguments).map(function(list){ this.lists.erase(list); return list; }, this)); }, getClone: function(event, element){ if (!this.options.clone) return new Element('div').inject(document.body); if (typeof this.options.clone == 'function') return this.options.clone.call(this, event, element, this.list); var clone = element.clone(true).setStyles({ margin: 0, position: 'absolute', visibility: 'hidden', width: element.getStyle('width') });

prevent the duplicated radio inputs from unchecking the real one

  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
  49. 169
  50. 170
  51. 171
  52. 172
  53. 173
  54. 174
  55. 175
  56. 176
  57. 177
  58. 178
  59. 179
  60. 180
  61. 181
  62. 182
  63. 183
  64. 184
  65. 185
  66. 186
  67. 187
  68. 188
  69. 189
  70. 190
  71. 191
  72. 192
  73. 193
  74. 194
  75. 195
  76. 196
  77. 197
  78. 198
  79. 199
  80. 200
  81. 201
  82. 202
  83. 203
  84. 204
  85. 205
  86. 206
  87. 207
  88. 208
  89. 209
  90. 210
  91. 211
  92. 212
  93. 213
  94. 214
  95. 215
  96. 216
  97. 217
  98. 218
  99. 219
  100. 220
  101. 221
if (clone.get('html').test('radio')){ clone.getElements('input[type=radio]').each(function(input, i){ input.set('name', 'clone_' + i); if (input.get('checked')) element.getElements('input[type=radio]')[i].set('checked', true); }); } return clone.inject(this.list).setPosition(element.getPosition(element.getOffsetParent())); }, getDroppables: function(){ var droppables = this.list.getChildren(); if (!this.options.constrain) droppables = this.lists.concat(droppables).erase(this.list); return droppables.erase(this.clone).erase(this.element); }, insert: function(dragging, element){ var where = 'inside'; if (this.lists.contains(element)){ this.list = element; this.drag.droppables = this.getDroppables(); } else { where = this.element.getAllPrevious().contains(element) ? 'before' : 'after'; } this.element.inject(element, where); this.fireEvent('sort', [this.element, this.clone]); }, start: function(event, element){ if (!this.idle || event.rightClick) return; this.idle = false; this.element = element; this.opacity = element.get('opacity'); this.list = element.getParent(); this.clone = this.getClone(event, element); this.drag = new Drag.Move(this.clone, { preventDefault: this.options.preventDefault, snap: this.options.snap, container: this.options.constrain && this.element.getParent(), droppables: this.getDroppables(), onSnap: function(){ event.stop(); this.clone.setStyle('visibility', 'visible'); this.element.set('opacity', this.options.opacity || 0); this.fireEvent('start', [this.element, this.clone]); }.bind(this), onEnter: this.insert.bind(this), onCancel: this.reset.bind(this), onComplete: this.end.bind(this) }); this.clone.inject(this.element, 'before'); this.drag.start(event); }, end: function(){ this.drag.detach(); this.element.set('opacity', this.opacity); if (this.effect){ var dim = this.element.getStyles('width', 'height'); var pos = this.clone.computePosition(this.element.getPosition(this.clone.offsetParent)); this.effect.element = this.clone; this.effect.start({ top: pos.top, left: pos.left, width: dim.width, height: dim.height, opacity: 0.25 }).chain(this.reset.bind(this)); } else { this.reset(); } }, reset: function(){ this.idle = true; this.clone.destroy(); this.fireEvent('complete', this.element); }, serialize: function(){ var params = Array.link(arguments, { modifier: Type.isFunction, index: function(obj){ return obj != null; } }); var serial = this.lists.map(function(list){ return list.getChildren().map(params.modifier || function(element){ return element.get('id'); }, this); }, this); var index = params.index; if (this.lists.length == 1) index = 0; return (index || index === 0) && index >= 0 && index < this.lists.length ? serial[index] : serial; } });