-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.listreorder.js
289 lines (235 loc) · 8.1 KB
/
jquery.listreorder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Copyright (c) 2009 Jordan Bach, http://www.utdallas.edu/~jrb048000/
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.
*/
/*
List Reorder <http://www.utdallas.edu/~jrb048000/ListReorder/>
Enables drag-and-drop reordering of list items for any simple ordered <ol> or unordered <li> list.
Author: Jordan Bach
Version: 0.1
Created: January 1, 2009
License: MIT License
Constructor
$(expr).ListOrder(options)
Methods:
makeDefaultOrder - sets the current list order to [0, 1, 2, ...]
restoreOrder - returns the list to its original order
Events:
listorderchanged - Fired after a list item is dropped. The 2nd argument
is a jQuery object representing the list that fired the event.
The 3rd argument is an array where the values represent
the original index of each list item.
Options:
itemHoverClass : 'itemHover',
dragTargetClass : 'dragTarget',
dropTargetClass : 'dropTarget',
dragHandleClass : 'dragHandle'
*/
(function($){
$.fn.ListReorder = function (options) {
$.fn.ListReorder.defaults = {
itemHoverClass : 'itemHover',
dragTargetClass : 'dragTarget',
dropTargetClass : 'dropTarget',
dragHandleClass : 'dragHandle',
useDefaultDragHandle : false
};
var opts = $.extend({}, $.fn.ListReorder.defaults, options);
return this.each(function() {
var theList = $(this), // The list (<ul>|<ol>)
theItems = $('li', theList), // All <li> elements in the list
dragActive = false, // Are we currently dragging an item?
dropTarget = null, // The list placeholder
dragTarget = null, // The element currently being dragged
dropIndex = -1, // The list index of the dropTarget
offset = {}, // Positions the mouse in the dragTarget
listOrder = [], // Keeps track of order relative to original order
ref = this;
theList.mouseout(ul_mouseout);
// Create the drag target
dragTarget = $('<div></div>');
dragTarget.insertAfter(theList);
dragTarget.hide();
dragTarget.css('position', 'absolute');
dragTarget.addClass(opts.dragTargetClass);
for (var i = 0; i < theItems.length; i++)
listOrder.push(i);
resetList();
function resetList() {
theItems = $('li', theList),
// For each <li> in the list
theItems.each(function() {
var li = $(this);
var dragHandle = $('<span></span>');
dragHandle.addClass(opts.dragHandleClass)
.mouseover(li_mouseover)
.mousedown(dragHandle_mousedown);
if (opts.useDefaultDragHandle)
dragHandle.css({
'display' : 'block',
'float' : 'right',
'width' : '10px',
'height' : '10px',
'border' : '2px solid #333',
'background' : '#ccc',
'cursor' : 'move'
});
$('.' + opts.dragHandleClass, li).remove();
li.prepend(dragHandle);
});
clearListItemStyles();
}
// Return all list items to their default state
function clearListItemStyles() {
theItems.each(function() {
var li = $(this);
li.removeClass(opts.itemHoverClass);
li.removeClass(opts.dropTargetClass);
});
}
// Handle any cleanup when the mouse leaves the list
function ul_mouseout() {
if (!dragActive)
clearListItemStyles();
}
// Add a hover class to a list item on mouseover
function li_mouseover() {
if (!dragActive) {
clearListItemStyles();
$(this).parent().addClass(opts.itemHoverClass);
}
}
// Prepare the list for dragging an item
function dragHandle_mousedown(e) {
var li = $(this).parent();
dragActive = true;
dropIndex = theItems.index(li);
// Show the drag target
dragTarget.html(li.html());
dragTarget.css('display', 'block');
offset.top = e.pageY - li.position().top;
offset.left = e.pageX - li.position().left;
updateDragTargetPos(e);
// Insert the placeholder
dropTarget = li;
dropTarget.html('');
dropTarget.css('height', dragTarget.css('height'));
dragTarget.css('width', dropTarget.width() + 'px');
dropTarget.addClass(opts.dropTargetClass);
// Disable Text and DOM selection
$(document).disableTextSelect();
$(document).mouseup(dragHandle_mouseup);
$(document).mousemove(document_mousemove);
}
// If this were on the element, we could lose the drag on the element
// if we move the mouse too fast
function document_mousemove(e) {
if (dragActive) {
// drag target follows mouse cursor
updateDragTargetPos(e);
// Don't do mess with drop index if we are above or below the list
if (y_mid(dragTarget) > y_bot(theList)
|| y_mid(dragTarget) < y_top(theList)) {
return;
}
// detect position of drag target relative to list items
// and swap drop target and neighboring item if necessary
if (y_mid(dragTarget) + 5 < y_top(dropTarget)) {
swapListItems(dropIndex, --dropIndex);
} else if (y_mid(dragTarget) - 5 > y_bot(dropTarget)) {
swapListItems(dropIndex, ++dropIndex);
}
}
}
function dragHandle_mouseup() {
// Restore the drop target
dropTarget.html(dragTarget.html());
dropTarget.removeClass(opts.dragTargetClass);
dropTarget = null;
// Hide the drag target
dragTarget.css('display', 'none');
dragActive = false;
dragTarget.unbind('mouseup', dragHandle_mouseup);
$(document).unbind('mousemove', document_mousemove);
resetList();
theList.trigger('listorderchanged', [theList, listOrder]);
// Re-enable text selection
$(document).enableTextSelect();
$(document).unbind('mouseup', dragHandle_mouseup);
}
function updateDragTargetPos(e) {
dragTarget.css({
'top' : e.pageY - offset.top + 'px',
'left' : e.pageX - offset.left + 'px'
});
}
// Change the order of two list items
function swapListItems(oldDropIndex, newDropIndex) {
// keep indices in bounds
if (dropIndex < 0) {
dropIndex = 0;
return;
} else if (dropIndex >= theItems.length) {
dropIndex = theItems.length - 1;
return;
}
var t = listOrder[oldDropIndex];
listOrder[oldDropIndex] = listOrder[newDropIndex];
listOrder[newDropIndex] = t;
// swap list items
var oldDropTarget = theItems.get(oldDropIndex),
newDropTarget = theItems.get(newDropIndex),
temp1 = $(oldDropTarget).clone(true);
temp2 = $(newDropTarget).clone(true);
$(oldDropTarget).replaceWith(temp2)
.mouseover(li_mouseover)
.mousedown(dragHandle_mousedown);
$(newDropTarget).replaceWith(temp1)
.mouseover(li_mouseover)
.mousedown(dragHandle_mousedown);
// reset so it is valid on next use
theItems = $('li', theList);
dropTarget = $(theItems.get(newDropIndex));
}
function y_top(jq) {
return jq.offset().top;
}
function y_mid(jq) {
return (y_top(jq) + y_bot(jq)) / 2
}
function y_bot(jq) {
return jq.offset().top + jq.outerHeight();
}
this.makeDefaultOrder = function() {
for (var i = 0; i < listOrder.length; i++)
listOrder[i] = i;
}
this.restoreOrder = function() {
for (var i = 0; i < theItems.length; i++) {
if (i != listOrder[i]) {
var k = 0;
for (; k < listOrder.length; k++)
if (listOrder[k] == i)
break;
swapListItems(i, k);
}
}
theList.trigger('listorderchanged', [theList, listOrder]);
}
});
}
})(jQuery);