-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.sort.js
118 lines (92 loc) · 3.76 KB
/
jquery.sort.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
(function( $ ){
$.fn.sortable = function(settings) {
var scrollParent;
var sortableObject = $(this);
var offsetTop = $(this).offset().top;
var sortComplete = function() {
$$.trigger("sort:complete");
};
var config = {
scroll : true,
sortElement : 'li',
handle : '.handle',
scrollSensitivity : 40,
scrollSpeed : 20,
scrollParentClass : '.scrolling-el',
//callbacks
moveComplete : sortComplete
};
if (settings) {
$.extend(config, settings);
}
//Requires sortable object to have a unique id; add one if not present- made unique by referencing object y position
if(!sortableObject.attr("id")) {
objId = "sortable"+offsetTop;
objId = objId.split(".")[0];
sortableObject.attr("id", objId);
}
//build drop zone limiter for proper scoping
var sortScope = "#"+sortableObject.attr("id")+" > "+config.sortElement;
//since we're using an absolutely position proxy during dragging, we'll need the parent to have a declared position
sortElement = $(this).find(config.sortElement);
if ($(this).css('position') !='absolute' && $(this).css('position') != 'fixed') {
$(this).css('position', 'relative')
}
sortElement
.drag('dragstart', function(ev, dd) {
sortableObject.addClass('sorting');
//requires handle
if (!$(ev.target).is(config.handle)) { return false; }
//locate and name nested scroll-y parent in the dom, if present
scrollParent = $(this).parents().filter(config.scrollParentClass)[0];
//copy the dragged item so you'll see it as you drag. Make it semi-translucent
$(this).clone().addClass('active').css({opacity: .8, position: "absolute"}).insertAfter(this);
//hide the currently dragged item
$(this).css('opacity', .2);
})
.drag(function( ev, dd ){
//used for scrolling only (supports only vertical scrolling)
if (config.scroll) {
if (scrollParent) {
//scroll parent container that has y-overflow: scroll; lazy recogniztion of this container via class
if(($(scrollParent).offset().top + scrollParent.offsetHeight) - ev.pageY < config.scrollSensitivity)
scrollParent.scrollTop = scrolled = scrollParent.scrollTop + config.scrollSpeed;
else if(ev.pageY - $(scrollParent).offset().top < 40)
scrollParent.scrollTop = scrolled = scrollParent.scrollTop - config.scrollSpeed;
} else {
//scoll document
if(ev.pageY - $(document).scrollTop() < config.scrollSensitivity)
$(document).scrollTop($(document).scrollTop() - config.scrollSpeed);
else if($(window).height() - (ev.pageY - $(document).scrollTop()) < config.scrollSensitivity)
$(document).scrollTop($(document).scrollTop() + config.scrollSpeed);
}
}
//move the cloned element along with the mouse. Not tested across all browsers yet...
$('.active').css({top: dd.offsetY - offsetTop});
var drop = dd.drop[0],
method = $.data( drop || {}, 'drop+reorder' );
if (drop && ( drop != dd.current || method != dd.method)){
$( this )[ method ]( drop );
dd.current = drop;
dd.method = method;
dd.update();
}
}, { drop: sortScope})
.drag('end',function( ev, dd ){
$(this).css('opacity', 1);
sortableObject.removeClass('sorting');
sortableObject.find('.active').remove();
config.moveComplete();
})
.drop('init',function(ev, dd){
return !(this == dd.drag);
});
$.drop({
tolerance: function( event, proxy, target ){
var test = event.pageY > ( target.top + target.height / 3.2 );
$.data( target.elem, 'drop+reorder', test ? 'insertAfter' : 'insertBefore' );
return this.contains( target, [ event.pageX, event.pageY ] );
}
});
};
})( jQuery );