jquery-dragsort-rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +71 -0
- data/Rakefile +46 -0
- data/lib/jquery-dragsort-rails.rb +8 -0
- data/lib/jquery/dragsort/rails/dragsortlist.rb +2 -0
- data/lib/jquery/dragsort/rails/engine.rb +8 -0
- data/lib/jquery/dragsort/rails/version.rb +7 -0
- data/vendor/assets/javascripts/jquery-dragsort/index.js +1 -0
- data/vendor/assets/javascripts/jquery-dragsort/jquery.dragsort.js +378 -0
- metadata +102 -0
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# jQuery Drag Sort for Rails 3.1 Asset Pipeline
|
2
|
+
|
3
|
+
[jQuery DragSort Plugin](http://dragsort.codeplex.com/) is a list drag and sort plugin made available by and on [CodePlex](http://dragsort.codeplex.com/). jQuery List Drag Sort features list element's dragging, dropping, and saving the sorted list by user using jQuery.
|
4
|
+
|
5
|
+
jquery-dragsort-rails is a library that integrates jQuery List Drag Sort for Rails 3.1 or above Asset Pipeline.
|
6
|
+
|
7
|
+
## Plugin version
|
8
|
+
|
9
|
+
* jQuery List Drag Sort Plugin v0.5.1
|
10
|
+
|
11
|
+
## Installing Gem
|
12
|
+
Add this in your Gemfile:
|
13
|
+
|
14
|
+
gem "jquery-dragsort-rails"
|
15
|
+
|
16
|
+
## How to use this plugin
|
17
|
+
To use this plugin follow these steps:
|
18
|
+
|
19
|
+
### Refer javascript
|
20
|
+
Require jquery-dragsort in your app/assets/application.js file.
|
21
|
+
|
22
|
+
//= require jquery-dragsort
|
23
|
+
|
24
|
+
### In your code:
|
25
|
+
|
26
|
+
Let's assume you are sorting products on your page, and your code looks something like this in your sort_products.html.erb template -
|
27
|
+
|
28
|
+
<%= form_tag save_order_products_path do -%>
|
29
|
+
<!-- save sort order here which can be retrieved from server on postback -->
|
30
|
+
<input id= "product_positions" name="product_positions" type="hidden" value="0" />
|
31
|
+
<div class="span2" id="save_order"></div><!-- To insert a button when list's position is changed. -->
|
32
|
+
<% end -%>
|
33
|
+
|
34
|
+
<ul id="products" class="thumbnails">
|
35
|
+
<% @products.each do |product| %>
|
36
|
+
<% unless product.pictures.blank? %>
|
37
|
+
<%= content_tag_for :li, product, :class=> "span2" do %>
|
38
|
+
<div class="span2 thumbnail" value="<%= product.id %>">
|
39
|
+
<%= image_tag(product.pictures.first.image(:small), :size => "260x180") %>
|
40
|
+
<div class="caption">
|
41
|
+
<h5><%= product.name %></h5>
|
42
|
+
<p><%= product.sku_code %></p>
|
43
|
+
<p><%= truncate(product.description, :length => 45, :separator => ' ') %></p>
|
44
|
+
</div>
|
45
|
+
</div>
|
46
|
+
<% end %>
|
47
|
+
<% end %>
|
48
|
+
<% end %>
|
49
|
+
</ul>
|
50
|
+
|
51
|
+
In your products.js.cofee file -
|
52
|
+
|
53
|
+
saveOrder = ->
|
54
|
+
data = $("#products li").map ->
|
55
|
+
$(this).children().attr("value")
|
56
|
+
.get()
|
57
|
+
$("input[name=product_positions]").val data.join(",")
|
58
|
+
$("#save_order").html '<button class="btn btn-primary"><i class="icon-ok icon-white"></i> Save</button>'
|
59
|
+
|
60
|
+
$("#products, #list2").dragsort
|
61
|
+
dragSelector: "div"
|
62
|
+
dragBetween: true
|
63
|
+
dragEnd: saveOrder
|
64
|
+
placeHolderTemplate: "<li class='placeHolder'><div></div></li>"
|
65
|
+
|
66
|
+
## Thanks
|
67
|
+
Thanks to [Codeplex](http://dragsort.codeplex.com/) for making list dragsort plugin available.
|
68
|
+
|
69
|
+
## License
|
70
|
+
This project is released under and uses [MIT-LICENSE](MIT-LICENSE).
|
71
|
+
Go to [http://dragsort.codeplex.com/license](http://dragsort.codeplex.com/license) for more licensing infromation.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
#!/usr/bin/env rake
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
begin
|
7
|
+
require 'bundler/setup'
|
8
|
+
require 'bundler'
|
9
|
+
rescue LoadError
|
10
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'rake'
|
14
|
+
require 'rake/rdoctask'
|
15
|
+
|
16
|
+
require 'rake/testtask'
|
17
|
+
|
18
|
+
Rake::TestTask.new(:test) do |t|
|
19
|
+
t.libs << 'lib'
|
20
|
+
t.libs << 'test'
|
21
|
+
t.pattern = 'test/**/*_test.rb'
|
22
|
+
t.verbose = false
|
23
|
+
end
|
24
|
+
|
25
|
+
task :default => :test
|
26
|
+
|
27
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
28
|
+
rdoc.rdoc_dir = 'rdoc'
|
29
|
+
rdoc.title = 'Jquery-dragsort-rails'
|
30
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
31
|
+
rdoc.rdoc_files.include('README.md')
|
32
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
33
|
+
end
|
34
|
+
|
35
|
+
Bundler::GemHelper.install_tasks
|
36
|
+
|
37
|
+
desc "Bundle the gem"
|
38
|
+
task :bundle do
|
39
|
+
sh('bundle install')
|
40
|
+
sh 'gem build *.gemspec'
|
41
|
+
sh 'gem install *.gem'
|
42
|
+
sh 'rm *.gem'
|
43
|
+
end
|
44
|
+
|
45
|
+
task(:default).clear
|
46
|
+
task :default => :bundle
|
@@ -0,0 +1 @@
|
|
1
|
+
//=require jquery-dragsort/jquery.dragsort
|
@@ -0,0 +1,378 @@
|
|
1
|
+
// jQuery List DragSort v0.5.1
|
2
|
+
// Website: http://dragsort.codeplex.com/
|
3
|
+
// License: http://dragsort.codeplex.com/license
|
4
|
+
|
5
|
+
(function($) {
|
6
|
+
|
7
|
+
$.fn.dragsort = function(options) {
|
8
|
+
if (options == "destroy") {
|
9
|
+
$(this.selector).trigger("dragsort-uninit");
|
10
|
+
return;
|
11
|
+
}
|
12
|
+
|
13
|
+
var opts = $.extend({}, $.fn.dragsort.defaults, options);
|
14
|
+
var lists = [];
|
15
|
+
var list = null, lastPos = null;
|
16
|
+
|
17
|
+
this.each(function(i, cont) {
|
18
|
+
|
19
|
+
//if list container is table, the browser automatically wraps rows in tbody if not specified so change list container to tbody so that children returns rows as user expected
|
20
|
+
if ($(cont).is("table") && $(cont).children().size() == 1 && $(cont).children().is("tbody"))
|
21
|
+
cont = $(cont).children().get(0);
|
22
|
+
|
23
|
+
var newList = {
|
24
|
+
draggedItem: null,
|
25
|
+
placeHolderItem: null,
|
26
|
+
pos: null,
|
27
|
+
offset: null,
|
28
|
+
offsetLimit: null,
|
29
|
+
scroll: null,
|
30
|
+
container: cont,
|
31
|
+
|
32
|
+
init: function() {
|
33
|
+
//set options to default values if not set
|
34
|
+
opts.tagName = $(this.container).children().size() == 0 ? "li" : $(this.container).children().get(0).tagName.toLowerCase();
|
35
|
+
if (opts.itemSelector == "")
|
36
|
+
opts.itemSelector = opts.tagName;
|
37
|
+
if (opts.dragSelector == "")
|
38
|
+
opts.dragSelector = opts.tagName;
|
39
|
+
if (opts.placeHolderTemplate == "")
|
40
|
+
opts.placeHolderTemplate = "<" + opts.tagName + "> </" + opts.tagName + ">";
|
41
|
+
|
42
|
+
//listidx allows reference back to correct list variable instance
|
43
|
+
$(this.container).attr("data-listidx", i).mousedown(this.grabItem).bind("dragsort-uninit", this.uninit);
|
44
|
+
this.styleDragHandlers(true);
|
45
|
+
},
|
46
|
+
|
47
|
+
uninit: function() {
|
48
|
+
var list = lists[$(this).attr("data-listidx")];
|
49
|
+
$(list.container).unbind("mousedown", list.grabItem).unbind("dragsort-uninit");
|
50
|
+
list.styleDragHandlers(false);
|
51
|
+
},
|
52
|
+
|
53
|
+
getItems: function() {
|
54
|
+
return $(this.container).children(opts.itemSelector);
|
55
|
+
},
|
56
|
+
|
57
|
+
styleDragHandlers: function(cursor) {
|
58
|
+
this.getItems().map(function() { return $(this).is(opts.dragSelector) ? this : $(this).find(opts.dragSelector).get(); }).css("cursor", cursor ? "pointer" : "");
|
59
|
+
},
|
60
|
+
|
61
|
+
grabItem: function(e) {
|
62
|
+
var list = lists[$(this).attr("data-listidx")];
|
63
|
+
var item = $(e.target).closest("[data-listidx] > " + opts.tagName).get(0);
|
64
|
+
var insideMoveableItem = list.getItems().filter(function() { return this == item; }).size() > 0;
|
65
|
+
|
66
|
+
//if not left click or if clicked on excluded element (e.g. text box) or not a moveable list item return
|
67
|
+
if (e.which != 1 || $(e.target).is(opts.dragSelectorExclude) || $(e.target).closest(opts.dragSelectorExclude).size() > 0 || !insideMoveableItem)
|
68
|
+
return;
|
69
|
+
|
70
|
+
//prevents selection, stops issue on Fx where dragging hyperlink doesn't work and on IE where it triggers mousemove even though mouse hasn't moved,
|
71
|
+
//does also stop being able to click text boxes hence dragging on text boxes by default is disabled in dragSelectorExclude
|
72
|
+
e.preventDefault();
|
73
|
+
|
74
|
+
//change cursor to move while dragging
|
75
|
+
var dragHandle = e.target;
|
76
|
+
while (!$(dragHandle).is(opts.dragSelector)) {
|
77
|
+
if (dragHandle == this) return;
|
78
|
+
dragHandle = dragHandle.parentNode;
|
79
|
+
}
|
80
|
+
$(dragHandle).attr("data-cursor", $(dragHandle).css("cursor"));
|
81
|
+
$(dragHandle).css("cursor", "move");
|
82
|
+
|
83
|
+
//on mousedown wait for movement of mouse before triggering dragsort script (dragStart) to allow clicking of hyperlinks to work
|
84
|
+
var listElem = this;
|
85
|
+
var trigger = function() {
|
86
|
+
list.dragStart.call(listElem, e);
|
87
|
+
$(list.container).unbind("mousemove", trigger);
|
88
|
+
};
|
89
|
+
$(list.container).mousemove(trigger).mouseup(function() { $(list.container).unbind("mousemove", trigger); $(dragHandle).css("cursor", $(dragHandle).attr("data-cursor")); });
|
90
|
+
},
|
91
|
+
|
92
|
+
dragStart: function(e) {
|
93
|
+
if (list != null && list.draggedItem != null)
|
94
|
+
list.dropItem();
|
95
|
+
|
96
|
+
list = lists[$(this).attr("data-listidx")];
|
97
|
+
list.draggedItem = $(e.target).closest("[data-listidx] > " + opts.tagName)
|
98
|
+
|
99
|
+
//record current position so on dragend we know if the dragged item changed position or not, not using getItems to allow dragsort to restore dragged item to original location in relation to fixed items
|
100
|
+
list.draggedItem.attr("data-origpos", $(this).attr("data-listidx") + "-" + $(list.container).children().index(list.draggedItem));
|
101
|
+
|
102
|
+
//calculate mouse offset relative to draggedItem
|
103
|
+
var mt = parseInt(list.draggedItem.css("marginTop"));
|
104
|
+
var ml = parseInt(list.draggedItem.css("marginLeft"));
|
105
|
+
list.offset = list.draggedItem.offset();
|
106
|
+
list.offset.top = e.pageY - list.offset.top + (isNaN(mt) ? 0 : mt) - 1;
|
107
|
+
list.offset.left = e.pageX - list.offset.left + (isNaN(ml) ? 0 : ml) - 1;
|
108
|
+
|
109
|
+
//calculate box the dragged item can't be dragged outside of
|
110
|
+
if (!opts.dragBetween) {
|
111
|
+
var containerHeight = $(list.container).outerHeight() == 0 ? Math.max(1, Math.round(0.5 + list.getItems().size() * list.draggedItem.outerWidth() / $(list.container).outerWidth())) * list.draggedItem.outerHeight() : $(list.container).outerHeight();
|
112
|
+
list.offsetLimit = $(list.container).offset();
|
113
|
+
list.offsetLimit.right = list.offsetLimit.left + $(list.container).outerWidth() - list.draggedItem.outerWidth();
|
114
|
+
list.offsetLimit.bottom = list.offsetLimit.top + containerHeight - list.draggedItem.outerHeight();
|
115
|
+
}
|
116
|
+
|
117
|
+
//create placeholder item
|
118
|
+
var h = list.draggedItem.height();
|
119
|
+
var w = list.draggedItem.width();
|
120
|
+
if (opts.tagName == "tr") {
|
121
|
+
list.draggedItem.children().each(function() { $(this).width($(this).width()); });
|
122
|
+
list.placeHolderItem = list.draggedItem.clone().attr("data-placeholder", true);
|
123
|
+
list.draggedItem.after(list.placeHolderItem);
|
124
|
+
list.placeHolderItem.children().each(function() { $(this).css({ borderWidth:0, width: $(this).width() + 1, height: $(this).height() + 1 }).html(" "); });
|
125
|
+
} else {
|
126
|
+
list.draggedItem.after(opts.placeHolderTemplate);
|
127
|
+
list.placeHolderItem = list.draggedItem.next().css({ height: h, width: w }).attr("data-placeholder", true);
|
128
|
+
}
|
129
|
+
|
130
|
+
if (opts.tagName == "td") {
|
131
|
+
var listTable = list.draggedItem.closest("table").get(0);
|
132
|
+
$("<table id='" + listTable.id + "' style='border-width: 0px;' class='dragSortItem " + listTable.className + "'><tr></tr></table>").appendTo("body").children().append(list.draggedItem);
|
133
|
+
}
|
134
|
+
|
135
|
+
//style draggedItem while dragging
|
136
|
+
var orig = list.draggedItem.attr("style");
|
137
|
+
list.draggedItem.attr("data-origstyle", orig ? orig : "");
|
138
|
+
list.draggedItem.css({ position: "absolute", opacity: 0.8, "z-index": 999, height: h, width: w });
|
139
|
+
|
140
|
+
//auto-scroll setup
|
141
|
+
list.scroll = { moveX: 0, moveY: 0, maxX: $(document).width() - $(window).width(), maxY: $(document).height() - $(window).height() };
|
142
|
+
list.scroll.scrollY = window.setInterval(function() {
|
143
|
+
if (opts.scrollContainer != window) {
|
144
|
+
$(opts.scrollContainer).scrollTop($(opts.scrollContainer).scrollTop() + list.scroll.moveY);
|
145
|
+
return;
|
146
|
+
}
|
147
|
+
var t = $(opts.scrollContainer).scrollTop();
|
148
|
+
if (list.scroll.moveY > 0 && t < list.scroll.maxY || list.scroll.moveY < 0 && t > 0) {
|
149
|
+
$(opts.scrollContainer).scrollTop(t + list.scroll.moveY);
|
150
|
+
list.draggedItem.css("top", list.draggedItem.offset().top + list.scroll.moveY + 1);
|
151
|
+
}
|
152
|
+
}, 10);
|
153
|
+
list.scroll.scrollX = window.setInterval(function() {
|
154
|
+
if (opts.scrollContainer != window) {
|
155
|
+
$(opts.scrollContainer).scrollLeft($(opts.scrollContainer).scrollLeft() + list.scroll.moveX);
|
156
|
+
return;
|
157
|
+
}
|
158
|
+
var l = $(opts.scrollContainer).scrollLeft();
|
159
|
+
if (list.scroll.moveX > 0 && l < list.scroll.maxX || list.scroll.moveX < 0 && l > 0) {
|
160
|
+
$(opts.scrollContainer).scrollLeft(l + list.scroll.moveX);
|
161
|
+
list.draggedItem.css("left", list.draggedItem.offset().left + list.scroll.moveX + 1);
|
162
|
+
}
|
163
|
+
}, 10);
|
164
|
+
|
165
|
+
//misc
|
166
|
+
$(lists).each(function(i, l) { l.createDropTargets(); l.buildPositionTable(); });
|
167
|
+
list.setPos(e.pageX, e.pageY);
|
168
|
+
$(document).bind("mousemove", list.swapItems);
|
169
|
+
$(document).bind("mouseup", list.dropItem);
|
170
|
+
if (opts.scrollContainer != window)
|
171
|
+
$(window).bind("DOMMouseScroll mousewheel", list.wheel);
|
172
|
+
},
|
173
|
+
|
174
|
+
//set position of draggedItem
|
175
|
+
setPos: function(x, y) {
|
176
|
+
//remove mouse offset so mouse cursor remains in same place on draggedItem instead of top left corner
|
177
|
+
var top = y - this.offset.top;
|
178
|
+
var left = x - this.offset.left;
|
179
|
+
|
180
|
+
//limit top, left to within box draggedItem can't be dragged outside of
|
181
|
+
if (!opts.dragBetween) {
|
182
|
+
top = Math.min(this.offsetLimit.bottom, Math.max(top, this.offsetLimit.top));
|
183
|
+
left = Math.min(this.offsetLimit.right, Math.max(left, this.offsetLimit.left));
|
184
|
+
}
|
185
|
+
|
186
|
+
//adjust top & left calculations to parent offset
|
187
|
+
var parent = this.draggedItem.offsetParent().not("body").offset(); //offsetParent returns body even when it's static, if not static offset is only factoring margin
|
188
|
+
if (parent != null) {
|
189
|
+
top -= parent.top;
|
190
|
+
left -= parent.left;
|
191
|
+
}
|
192
|
+
|
193
|
+
//set x or y auto-scroll amount
|
194
|
+
if (opts.scrollContainer == window) {
|
195
|
+
y -= $(window).scrollTop();
|
196
|
+
x -= $(window).scrollLeft();
|
197
|
+
y = Math.max(0, y - $(window).height() + 5) + Math.min(0, y - 5);
|
198
|
+
x = Math.max(0, x - $(window).width() + 5) + Math.min(0, x - 5);
|
199
|
+
} else {
|
200
|
+
var cont = $(opts.scrollContainer);
|
201
|
+
var offset = cont.offset();
|
202
|
+
y = Math.max(0, y - cont.height() - offset.top) + Math.min(0, y - offset.top);
|
203
|
+
x = Math.max(0, x - cont.width() - offset.left) + Math.min(0, x - offset.left);
|
204
|
+
}
|
205
|
+
|
206
|
+
list.scroll.moveX = x == 0 ? 0 : x * opts.scrollSpeed / Math.abs(x);
|
207
|
+
list.scroll.moveY = y == 0 ? 0 : y * opts.scrollSpeed / Math.abs(y);
|
208
|
+
|
209
|
+
//move draggedItem to new mouse cursor location
|
210
|
+
this.draggedItem.css({ top: top, left: left });
|
211
|
+
},
|
212
|
+
|
213
|
+
//if scroll container is a div allow mouse wheel to scroll div instead of window when mouse is hovering over
|
214
|
+
wheel: function(e) {
|
215
|
+
if (($.browser.safari || $.browser.mozilla) && list && opts.scrollContainer != window) {
|
216
|
+
var cont = $(opts.scrollContainer);
|
217
|
+
var offset = cont.offset();
|
218
|
+
if (e.pageX > offset.left && e.pageX < offset.left + cont.width() && e.pageY > offset.top && e.pageY < offset.top + cont.height()) {
|
219
|
+
var delta = e.detail ? e.detail * 5 : e.wheelDelta / -2;
|
220
|
+
cont.scrollTop(cont.scrollTop() + delta);
|
221
|
+
e.preventDefault();
|
222
|
+
}
|
223
|
+
}
|
224
|
+
},
|
225
|
+
|
226
|
+
//build a table recording all the positions of the moveable list items
|
227
|
+
buildPositionTable: function() {
|
228
|
+
var pos = [];
|
229
|
+
this.getItems().not([list.draggedItem[0], list.placeHolderItem[0]]).each(function(i) {
|
230
|
+
var loc = $(this).offset();
|
231
|
+
loc.right = loc.left + $(this).outerWidth();
|
232
|
+
loc.bottom = loc.top + $(this).outerHeight();
|
233
|
+
loc.elm = this;
|
234
|
+
pos[i] = loc;
|
235
|
+
});
|
236
|
+
this.pos = pos;
|
237
|
+
},
|
238
|
+
|
239
|
+
dropItem: function() {
|
240
|
+
if (list.draggedItem == null)
|
241
|
+
return;
|
242
|
+
|
243
|
+
//list.draggedItem.attr("style", "") doesn't work on IE8 and jQuery 1.5 or lower
|
244
|
+
//list.draggedItem.removeAttr("style") doesn't work on chrome and jQuery 1.6 (works jQuery 1.5 or lower)
|
245
|
+
var orig = list.draggedItem.attr("data-origstyle");
|
246
|
+
list.draggedItem.attr("style", orig);
|
247
|
+
if (orig == "")
|
248
|
+
list.draggedItem.removeAttr("style");
|
249
|
+
list.draggedItem.removeAttr("data-origstyle");
|
250
|
+
|
251
|
+
list.styleDragHandlers(true);
|
252
|
+
|
253
|
+
list.placeHolderItem.before(list.draggedItem);
|
254
|
+
list.placeHolderItem.remove();
|
255
|
+
|
256
|
+
$("[data-droptarget], .dragSortItem").remove();
|
257
|
+
|
258
|
+
window.clearInterval(list.scroll.scrollY);
|
259
|
+
window.clearInterval(list.scroll.scrollX);
|
260
|
+
|
261
|
+
//if position changed call dragEnd
|
262
|
+
if (list.draggedItem.attr("data-origpos") != $(lists).index(list) + "-" + $(list.container).children().index(list.draggedItem))
|
263
|
+
if (opts.dragEnd.apply(list.draggedItem) == false) { //if dragEnd returns false revert order
|
264
|
+
var pos = list.draggedItem.attr("data-origpos").split('-');
|
265
|
+
var nextItem = $(lists[pos[0]].container).children().not(list.draggedItem).eq(pos[1]);
|
266
|
+
if (nextItem.size() > 0)
|
267
|
+
nextItem.before(list.draggedItem);
|
268
|
+
else if (pos[1] == 0) //was the only item in list
|
269
|
+
$(lists[pos[0]].container).prepend(list.draggedItem);
|
270
|
+
else //was the last item in list
|
271
|
+
$(lists[pos[0]].container).append(list.draggedItem);
|
272
|
+
}
|
273
|
+
list.draggedItem.removeAttr("data-origpos");
|
274
|
+
|
275
|
+
list.draggedItem = null;
|
276
|
+
$(document).unbind("mousemove", list.swapItems);
|
277
|
+
$(document).unbind("mouseup", list.dropItem);
|
278
|
+
if (opts.scrollContainer != window)
|
279
|
+
$(window).unbind("DOMMouseScroll mousewheel", list.wheel);
|
280
|
+
return false;
|
281
|
+
},
|
282
|
+
|
283
|
+
//swap the draggedItem (represented visually by placeholder) with the list item the it has been dragged on top of
|
284
|
+
swapItems: function(e) {
|
285
|
+
if (list.draggedItem == null)
|
286
|
+
return false;
|
287
|
+
|
288
|
+
//move draggedItem to mouse location
|
289
|
+
list.setPos(e.pageX, e.pageY);
|
290
|
+
|
291
|
+
//retrieve list and item position mouse cursor is over
|
292
|
+
var ei = list.findPos(e.pageX, e.pageY);
|
293
|
+
var nlist = list;
|
294
|
+
for (var i = 0; ei == -1 && opts.dragBetween && i < lists.length; i++) {
|
295
|
+
ei = lists[i].findPos(e.pageX, e.pageY);
|
296
|
+
nlist = lists[i];
|
297
|
+
}
|
298
|
+
|
299
|
+
//if not over another moveable list item return
|
300
|
+
if (ei == -1)
|
301
|
+
return false;
|
302
|
+
|
303
|
+
//save fixed items locations
|
304
|
+
var children = function() { return $(nlist.container).children().not(nlist.draggedItem); };
|
305
|
+
var fixed = children().not(opts.itemSelector).each(function(i) { this.idx = children().index(this); });
|
306
|
+
|
307
|
+
//if moving draggedItem up or left place placeHolder before list item the dragged item is hovering over otherwise place it after
|
308
|
+
if (lastPos == null || lastPos.top > list.draggedItem.offset().top || lastPos.left > list.draggedItem.offset().left)
|
309
|
+
$(nlist.pos[ei].elm).before(list.placeHolderItem);
|
310
|
+
else
|
311
|
+
$(nlist.pos[ei].elm).after(list.placeHolderItem);
|
312
|
+
|
313
|
+
//restore fixed items location
|
314
|
+
fixed.each(function() {
|
315
|
+
var elm = children().eq(this.idx).get(0);
|
316
|
+
if (this != elm && children().index(this) < this.idx)
|
317
|
+
$(this).insertAfter(elm);
|
318
|
+
else if (this != elm)
|
319
|
+
$(this).insertBefore(elm);
|
320
|
+
});
|
321
|
+
|
322
|
+
//misc
|
323
|
+
$(lists).each(function(i, l) { l.createDropTargets(); l.buildPositionTable(); });
|
324
|
+
lastPos = list.draggedItem.offset();
|
325
|
+
return false;
|
326
|
+
},
|
327
|
+
|
328
|
+
//returns the index of the list item the mouse is over
|
329
|
+
findPos: function(x, y) {
|
330
|
+
for (var i = 0; i < this.pos.length; i++) {
|
331
|
+
if (this.pos[i].left < x && this.pos[i].right > x && this.pos[i].top < y && this.pos[i].bottom > y)
|
332
|
+
return i;
|
333
|
+
}
|
334
|
+
return -1;
|
335
|
+
},
|
336
|
+
|
337
|
+
//create drop targets which are placeholders at the end of other lists to allow dragging straight to the last position
|
338
|
+
createDropTargets: function() {
|
339
|
+
if (!opts.dragBetween)
|
340
|
+
return;
|
341
|
+
|
342
|
+
$(lists).each(function() {
|
343
|
+
var ph = $(this.container).find("[data-placeholder]");
|
344
|
+
var dt = $(this.container).find("[data-droptarget]");
|
345
|
+
if (ph.size() > 0 && dt.size() > 0)
|
346
|
+
dt.remove();
|
347
|
+
else if (ph.size() == 0 && dt.size() == 0) {
|
348
|
+
if (opts.tagName == "td")
|
349
|
+
$(opts.placeHolderTemplate).attr("data-droptarget", true).appendTo(this.container);
|
350
|
+
else
|
351
|
+
//list.placeHolderItem.clone().removeAttr("data-placeholder") crashes in IE7 and jquery 1.5.1 (doesn't in jquery 1.4.2 or IE8)
|
352
|
+
$(this.container).append(list.placeHolderItem.removeAttr("data-placeholder").clone().attr("data-droptarget", true));
|
353
|
+
|
354
|
+
list.placeHolderItem.attr("data-placeholder", true);
|
355
|
+
}
|
356
|
+
});
|
357
|
+
}
|
358
|
+
};
|
359
|
+
|
360
|
+
newList.init();
|
361
|
+
lists.push(newList);
|
362
|
+
});
|
363
|
+
|
364
|
+
return this;
|
365
|
+
};
|
366
|
+
|
367
|
+
$.fn.dragsort.defaults = {
|
368
|
+
itemSelector: "",
|
369
|
+
dragSelector: "",
|
370
|
+
dragSelectorExclude: "input, textarea",
|
371
|
+
dragEnd: function() { },
|
372
|
+
dragBetween: false,
|
373
|
+
placeHolderTemplate: "",
|
374
|
+
scrollContainer: window,
|
375
|
+
scrollSpeed: 5
|
376
|
+
};
|
377
|
+
|
378
|
+
})(jQuery);
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jquery-dragsort-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Surya Tripathi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: actionpack
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.1'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.1'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.1'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
description: jQuery List Drag Sort by http://dragsort.codeplex.com/, integrated for
|
63
|
+
Rails 3.1 Asset Pipeline
|
64
|
+
email:
|
65
|
+
- raj.surya19@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- lib/jquery/dragsort/rails/version.rb
|
71
|
+
- lib/jquery/dragsort/rails/dragsortlist.rb
|
72
|
+
- lib/jquery/dragsort/rails/engine.rb
|
73
|
+
- lib/jquery-dragsort-rails.rb
|
74
|
+
- vendor/assets/javascripts/jquery-dragsort/jquery.dragsort.js
|
75
|
+
- vendor/assets/javascripts/jquery-dragsort/index.js
|
76
|
+
- Rakefile
|
77
|
+
- README.md
|
78
|
+
homepage: https://github.com/suryart/jquery-dragsort-rails
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project: jquery-dragsort-rails
|
98
|
+
rubygems_version: 1.8.24
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: jQuery List Drag Sort for Rails 3.1 Asset Pipeline
|
102
|
+
test_files: []
|