forked from Steve-Fenton/jQuery-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.draganddropsort.js
150 lines (127 loc) · 4.06 KB
/
jquery.draganddropsort.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
(function($)
{
// This script was written by Steve Fenton
// http://www.stevefenton.co.uk/Content/Jquery-Drag-And-Drop-Sort/
// Feel free to use this jQuery Plugin
// Version: 3.1.3
// Contributions by: Brian Wong
// Current mouse position (at all times!)
var mouseX = 0;
var mouseY = 0;
// May be used to adjust offset of the ghost item
var liftX = 5;
var liftY = 5;
$(document).mousemove(function(e){
mouseX = e.pageX;
mouseY = e.pageY;
$("#ghost").css({ top: (mouseY+liftY)+"px", left: (mouseX+liftX)+"px" });
});
$.fn.draganddropsort = function (settings) {
var config = {
classmodifier: "dds",
appendlastline: true,
containsInputs: false,
addhandles: false,
ondrop: function () {}
};
if (settings) {
$.extend(config, settings);
}
var nextSetIdentifier = 0;
var currentItem = null;
var currentTarget = null;
var itemInTransit = false;
var insertClass;
var movingClass;
var itemClass;
insertClass = config.classmodifier + "insert";
movingClass = config.classmodifier + "moving";
itemClass = config.classmodifier + "item";
// Captures drops
$(document).mouseup(function () {
if (itemInTransit && currentTarget != null) {
var clone = $(currentItem).clone();
bindEvents(clone);
$(currentTarget).before(clone);
$(currentItem).remove();
$("#ghost").remove();
config.ondrop();
}
currentItem = null;
currentTarget = null;
itemInTransit = false;
$("." + movingClass).removeClass(movingClass);
});
// Bind the drag drop events
function bindEvents(item) {
var $Item = $(item);
if (config.addhandles) {
$Item.children('.' + config.classmodifier + 'handle').mousedown(function () {
currentItem = $(this).parent();
itemInTransit = true;
currentItem.addClass(movingClass);
$("#ghost").remove();
currentItem.clone().attr("id", "ghost").css({
position: "absolute",
width: currentItem.width() + "px",
height: currentItem.height() + "px"
}).appendTo("body").fadeTo(0, 0.5);
return config.containsInputs;
});
} else {
$Item.mousedown(function () {
currentItem = $(this);
itemInTransit = true;
currentItem.addClass(movingClass);
$("#ghost").remove();
currentItem.clone().attr("id", "ghost").css({
position: "absolute",
width: currentItem.width() + "px",
height: currentItem.height() + "px"
}).appendTo("body").fadeTo(0, 0.5);
return config.containsInputs;
});
}
$Item.mouseenter(function () {
if (itemInTransit) {
currentTarget = $(this);
currentTarget.addClass(insertClass);
return false;
}
});
$Item.mouseleave(function () {
currentTarget = null;
$(this).removeClass(insertClass);
});
}
return this.each(function () {
var $This = $(this);
// Append a spare line, which allows items to be dragged past the last item
if (config.appendlastline) {
var clone = $This.children().first().clone();
var children = $(clone).children();
if (children.length == 0) {
$(clone).html(" ");
} else {
$(clone).children().each(function () {
if (this.value) {
$(this).remove();
} else {
$(this).html(" ");
}
});
}
$This.append(clone);
}
// Bind events for sortable items
$This.children().each(function () {
if (config.addhandles) {
$(this).prepend('<div class="' + config.classmodifier + 'handle">[+]</div>');
}
bindEvents(this);
$(this).addClass(itemClass);
});
nextSetIdentifier++;
});
};
})(jQuery);