forked from simonmcmanus/nestedSortable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.smmNestedSortable.js
60 lines (54 loc) · 2 KB
/
jquery.smmNestedSortable.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
/*
smmNestedSortable jQuery Plugin
version : 0.1
By Simon McManus (simonmcmanus.com)
Licence: BSD Open Source Licence
TODO :
1: Pass in tolerance paramter.
2: Allow all params available in sortables to be passed into smmNestedSortable.
*/
(function($) {
$.fn.smmNestedSortable = function(options) {
settings = jQuery.extend({
serializer: undefined,
items: "li",
helper: "helper",
connectWith: '.sortable',
placeholder: 'placeholder',
sort: function(event, ui) {
$(ui.placeholder).empty();
if (ui.position.left - 10 > ui.originalPosition.left) // make child
$(ui.placeholder).append("<ul class='sortable'>" + $(ui.item).html() + "</ul>");
},
stop: function(event, ui) {
$.fn.smmNestedSortable.change(event, ui);
if (settings.serializer != undefined)
settings.serializer();
}
}, options);
this.each(function() {
$(this).sortable(settings);
});
return this;
};
$.fn.smmNestedSortable.change = function(event, ui) {
if (ui.position.left - 10 > ui.originalPosition.left) { // make child - could/should add some more check in here.
if ($(ui.item).prev().is('li')) {
$(ui.item).prev().append(ui.item);
$(ui.item).wrap("<ul class='sortable'></ul>");
}
} else if (ui.position.left - 10 < ui.originalPosition.left) { // kill child
if ($(ui.item).parent().is("ul")) { // confirm its not the base level element
if ($(ui.item).parent().children().length <= 1) { // if only item in the list
$(ui.item).parent().replaceWith($(ui.item)); // put outside not inside parent li
}
}
}
// cleanup unused lists
$('ul.sortable, ul.sortable li').each(function() {
if ($(this).text() == "") // if only item and item is empty.
$(this).remove(); // remove the parent li
});
$('.sortable').sortable("refresh");
};
})(jQuery);