activeadmin_draggable 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ //= require jquery
2
+ //= require activeadmin_draggable/jquery.table_tree
3
+ //= require activeadmin_draggable/jquery.tablednd_0_5
4
+
5
+ $(document).ready(function(){
6
+ //sortable tables
7
+ $('a.reorder').click(function(event) {
8
+ event.preventDefault();
9
+ var url = this.href
10
+ TableTree.toggle($('table.list'), url);
11
+ return false;
12
+ });
13
+
14
+ $('#add_product_button').click(function(event) {
15
+ event.preventDefault();
16
+ $('div.modal').omniWindow() // create modal
17
+ .trigger('show'); // and show it
18
+ return false;
19
+ });
20
+
21
+ $('#search-attributes .layout-slider input').each(function(index, value) {
22
+ var sb = new SliderBar();
23
+ sb.initialize(value);
24
+ });
25
+
26
+
27
+ // calculate next matching value from given set of options
28
+
29
+ });
30
+
@@ -0,0 +1,305 @@
1
+ TableTree = {
2
+ tableDnDOptions: {
3
+ onDragClass: 'drag',
4
+ onDragStart: function(table, row) {
5
+ TableTree.startOffset = jQuery.tableDnD.mouseOffset.x;
6
+ $(row).mousemove(TableTree.mousemove);
7
+ if (node = $(row).ttnode()) node.dragStart();
8
+ },
9
+ onDrag: function(table, row) {
10
+ TableTree.current_table.dirty = true;
11
+ if (node = $(row).ttnode()) node.update_children();
12
+ },
13
+ onDrop: function(table, row) {
14
+ $(row).unbind('mousemove', TableTree.mousemove);
15
+ if (node = $(row).ttnode()) node.drop();
16
+ TableTree.current_table.rebuild();
17
+ TableTree.current_table.update_remote(row);
18
+ },
19
+ onAllowDrop: function(draggedRow, row, movingDown) {
20
+ var node = $(row).ttnode();
21
+ next = movingDown ? $(node.next_row_sibling()).ttnode() : node;
22
+ if (next && (next.parent.level >= $(draggedRow).ttnode().level)) return false;
23
+ return $(row).ttnode() ? true : false;
24
+ }
25
+ },
26
+ toggle: function(table, remote_url) {
27
+ TableTree.current_table ? TableTree.teardown(table) : TableTree.setup(table, remote_url);
28
+ },
29
+ setup: function(table, remote_url) {
30
+ $('tbody', table).tableDnD(TableTree.tableDnDOptions);
31
+ TableTree.current_table = new TableTree.Table($(table).get(0), remote_url);
32
+ TableTree.current_table.setSortable();
33
+ },
34
+ teardown: function(table) {
35
+ // TableTree.current_table.update_remote();
36
+ jQuery.tableDnD.teardown(table);
37
+ TableTree.current_table.setUnsortable();
38
+ TableTree.current_table = null;
39
+ },
40
+ level: function(element) {
41
+ var match = element.className.match(/level_([\d]+)/);
42
+ return match ? parseInt(match[1]) : 0;
43
+ },
44
+ mousemove: function(event) {
45
+ if (!TableTree.current_table.is_tree) return;
46
+
47
+ var offset = jQuery.tableDnD.getMouseOffset(this, event).x - TableTree.startOffset;
48
+ if(offset > 25) {
49
+ TableTree.current_table.dirty = true;
50
+ $(this).ttnode().increment_level(event);
51
+ } else if(offset < -25) {
52
+ TableTree.current_table.dirty = true;
53
+ $(this).ttnode().decrement_level(event);
54
+ }
55
+ },
56
+ Base: function() {},
57
+ Table: function(table, remote_url) {
58
+ this.is_tree = $(table).hasClass('tree')
59
+ // If table has batch actions, show second column
60
+ if ($("th:first input", table).length == 0) {
61
+ this.shown_column_id = 0;
62
+ } else {
63
+ this.shown_column_id = 1;
64
+ }
65
+ this.table = table; //$('tbody', table)
66
+ this.level = -1;
67
+ this.remote_url = remote_url;
68
+ this.rebuild();
69
+ },
70
+ Node: function(parent, element, level) {
71
+ var _this = this;
72
+ this.parent = parent;
73
+ this.element = element;
74
+ this.level = level;
75
+
76
+ this.children = this.find_children().map(function() {
77
+ var level = TableTree.level(this);
78
+ if(level == _this.level + 1) { return new TableTree.Node(_this, this, level); }
79
+ });
80
+ }
81
+ }
82
+
83
+ TableTree.Base.prototype = {
84
+ find_node: function(element) {
85
+ for (var i = 0; i < this.children.length; i++) {
86
+ var child = this.children[i];
87
+ if (this.children[i].element == element) {
88
+ return this.children[i];
89
+ } else {
90
+ var result = this.children[i].find_node(element);
91
+ if (result) return result;
92
+ }
93
+ }
94
+ }
95
+ }
96
+ TableTree.Table.prototype = jQuery.extend(new TableTree.Base(), {
97
+ rebuild: function() {
98
+ var _this = this;
99
+ this.children = $('tr', this.table).map(function() {
100
+ if(TableTree.level(this) == 0) { return new TableTree.Node(_this, this, TableTree.level(this)); }
101
+ });
102
+ },
103
+ setSortable: function() {
104
+ var self = this;
105
+ $('tr', this.table).each(function() {
106
+ // thead
107
+ cells = $('th', this);
108
+ cells.each(function(ix) {
109
+ if(ix == self.shown_column_id) {
110
+ this.setAttribute('colspan', cells.length - self.shown_column_id);
111
+ } else {
112
+ $(this).hide();
113
+ }
114
+ });
115
+
116
+ // tbody
117
+ cells = $('td', this);
118
+ cells.each(function(ix) {
119
+ if (ix == self.shown_column_id) {
120
+ element = this;
121
+ this.setAttribute('colspan', cells.length);
122
+ $('a', this).click(function(event) {
123
+ event.preventDefault();
124
+ return false;
125
+ })
126
+ } else {
127
+ $(this).hide();
128
+ }
129
+ });
130
+ });
131
+ },
132
+ setUnsortable: function() {
133
+ var self = this;
134
+ $('tr', this.table).each(function(ix) {
135
+ // thead
136
+ cells = $('th', this);
137
+ cells.each(function(ix) {
138
+ if(ix == self.shown_column_id) {
139
+ this.setAttribute('colspan', 1);
140
+ } else {
141
+ $(this).show();
142
+ }
143
+ });
144
+
145
+ // tbody
146
+ $('td', this).each(function(ix) {
147
+ if(ix == self.shown_column_id) {
148
+ $('img.spinner', this).remove();
149
+ $('a', this).unbind("click")
150
+ this.setAttribute('colspan', 1);
151
+ } else {
152
+ $(this).show();
153
+ }
154
+ });
155
+ });
156
+ },
157
+ update_remote: function(row) {
158
+ if(!this.dirty) return;
159
+ this.dirty = false;
160
+ _this = this;
161
+
162
+ this.show_spinner(row);
163
+
164
+ var dataArray = jQuery.extend(this.serialize(row), { '_method': 'put' });
165
+ var csrfParam = $('meta[name=csrf-param]').attr('content');
166
+ dataArray[csrfParam] = $('meta[name=csrf-token]').attr('content');
167
+ $.ajax({
168
+ type: "POST",
169
+ url: this.remote_url,
170
+ dataType: 'json',
171
+ data: dataArray,
172
+ success: function(msg) { _this.hide_spinner(row); },
173
+ error: function(msg) { _this.hide_spinner(row); }
174
+ });
175
+ },
176
+ serialize: function(row) {
177
+ var row = $(row).ttnode();
178
+ var data = {};
179
+ data['id'] = row.id() || '';
180
+ data['parent_id'] = row.parent_id() || '0';
181
+ data['left_id'] = row.left_id() || '0';
182
+ return data;
183
+ },
184
+ show_spinner: function(row) {
185
+ img = document.createElement('img');
186
+ img.src = '/assets/active_admin/indicator.gif';
187
+ img.className = 'spinner';
188
+ $('td', row)[0].appendChild(img);
189
+ },
190
+ hide_spinner: function(row) {
191
+ cell = $('td', row)[0];
192
+ cell.removeChild(cell.lastChild);
193
+ }
194
+ });
195
+
196
+ TableTree.Node.prototype = jQuery.extend(new TableTree.Base(), {
197
+ find_children: function() {
198
+ var lvl = this.level;
199
+ var stop = false;
200
+ return this.row_siblings().slice(this.row_index() + 1).filter(function() {
201
+ var level = TableTree.level(this);
202
+ if(lvl == level) stop = true; // how to break from a jquery iterator?
203
+ return !stop && lvl + 1 == level;
204
+ });
205
+ },
206
+ depth: function() {
207
+ if (this.children.length > 0) {
208
+ return Math.max.apply(Math, this.children.map(function() { return this.depth() }).get());
209
+ } else {
210
+ return this.level;
211
+ }
212
+ },
213
+ siblings: function() {
214
+ return this.parent.children;
215
+ },
216
+ id: function() {
217
+ return this.element ? this.to_int(this.element.id) : '0';
218
+ },
219
+ parent_id: function() {
220
+ return this.parent.element ? this.to_int(this.parent.element.id) : '0';
221
+ },
222
+ left_id: function() {
223
+ left = this.left()
224
+ return left ? this.to_int(left.element.id) : '0';
225
+ },
226
+ left: function() {
227
+ siblings = this.siblings().get();
228
+ ix = siblings.indexOf(this);
229
+ if(ix > 0) return siblings[ix - 1];
230
+ },
231
+ to_int: function(str) {
232
+ if(str) return str.replace(/[\D]+/, '')
233
+ },
234
+ next_row_sibling: function () {
235
+ return this.row_siblings()[this.row_index() + 1];
236
+ },
237
+ row_siblings: function() {
238
+ if(!this._row_siblings) { this._row_siblings = $(this.element).parent().children(); }
239
+ return this._row_siblings;
240
+ },
241
+ row_index: function() {
242
+ return this.row_siblings().get().indexOf(this.element);
243
+ },
244
+ dragStart: function() {
245
+ $(this.element).addClass('drag');
246
+ this.children.each(function() { this.dragStart(); })
247
+ },
248
+ drop: function() {
249
+ $(this.element).removeClass('drag');
250
+ this.children.each(function() { this.drop(); })
251
+ this.adjust_level();
252
+ },
253
+ increment_level: function(event) {
254
+ var prev = $(this.element).prev().ttnode();
255
+ if(!prev || prev.level < this.level || this.depth() >= 5) return;
256
+ this.update_level(event, this.level + 1);
257
+ },
258
+ decrement_level: function(event) {
259
+ if(this.level == 0) return;
260
+ this.update_level(event, this.level - 1);
261
+ },
262
+ update_level: function(event, level) {
263
+ if (event) TableTree.startOffset = jQuery.tableDnD.getMouseOffset(this.element, event).x;
264
+
265
+ $(this.element).removeClass('level_' + this.level);
266
+ $(this.element).addClass('level_' + level);
267
+
268
+ this.level = level;
269
+ this.children.each(function() { this.update_level(event, level + 1); });
270
+
271
+ TableTree.current_table.dirty = true;
272
+ },
273
+ adjust_level: function() {
274
+ var prev = $(this.element).prev().ttnode();
275
+ if(!prev) {
276
+ this.update_level(null, 0);
277
+ } else if(prev.level + 1 < this.level) {
278
+ // } else {
279
+ this.update_level(null, prev.level + 1);
280
+ }
281
+ },
282
+ update_children: function() {
283
+ this.children.each(function() { this.element.parentNode.removeChild(this.element); });
284
+ var _this = this;
285
+ var _next = _this.element.nextSibling;
286
+ this.children.each(function() { _this.element.parentNode.insertBefore(this.element, _next); });
287
+ this.children.each(function() { this.update_children() });
288
+ }
289
+ });
290
+
291
+ jQuery.fn.extend({
292
+ ttnode: function() {
293
+ var subject = this.push ? this[0] : this;
294
+ return TableTree.current_table.find_node(subject);
295
+ }
296
+ });
297
+
298
+ jQuery.extend(jQuery.tableDnD, {
299
+ teardown: function(table) {
300
+ jQuery('tr', table).each(function() { $(this).unbind('mousedown'); }).css('cursor', 'auto');
301
+ jQuery.tableDnD.dragObject = null;
302
+ jQuery.tableDnD.currentTable = null;
303
+ jQuery.tableDnD.mouseOffset = null;
304
+ }
305
+ });
@@ -0,0 +1,385 @@
1
+ /**
2
+ * TableDnD plug-in for JQuery, allows you to drag and drop table rows
3
+ * You can set up various options to control how the system will work
4
+ * Copyright (c) Denis Howlett <denish@isocra.com>
5
+ * Licensed like jQuery, see http://docs.jquery.com/License.
6
+ *
7
+ * Configuration options:
8
+ *
9
+ * onDragStyle
10
+ * This is the style that is assigned to the row during drag. There are limitations to the styles that can be
11
+ * associated with a row (such as you can't assign a border--well you can, but it won't be
12
+ * displayed). (So instead consider using onDragClass.) The CSS style to apply is specified as
13
+ * a map (as used in the jQuery css(...) function).
14
+ * onDropStyle
15
+ * This is the style that is assigned to the row when it is dropped. As for onDragStyle, there are limitations
16
+ * to what you can do. Also this replaces the original style, so again consider using onDragClass which
17
+ * is simply added and then removed on drop.
18
+ * onDragClass
19
+ * This class is added for the duration of the drag and then removed when the row is dropped. It is more
20
+ * flexible than using onDragStyle since it can be inherited by the row cells and other content. The default
21
+ * is class is tDnD_whileDrag. So to use the default, simply customise this CSS class in your
22
+ * stylesheet.
23
+ * onDrop
24
+ * Pass a function that will be called when the row is dropped. The function takes 2 parameters: the table
25
+ * and the row that was dropped. You can work out the new order of the rows by using
26
+ * table.rows.
27
+ * onDragStart
28
+ * Pass a function that will be called when the user starts dragging. The function takes 2 parameters: the
29
+ * table and the row which the user has started to drag.
30
+ * onAllowDrop
31
+ * Pass a function that will be called as a row is over another row. If the function returns true, allow
32
+ * dropping on that row, otherwise not. The function takes 2 parameters: the dragged row and the row under
33
+ * the cursor. It returns a boolean: true allows the drop, false doesn't allow it.
34
+ * scrollAmount
35
+ * This is the number of pixels to scroll if the user moves the mouse cursor to the top or bottom of the
36
+ * window. The page should automatically scroll up or down as appropriate (tested in IE6, IE7, Safari, FF2,
37
+ * FF3 beta
38
+ * dragHandle
39
+ * This is the name of a class that you assign to one or more cells in each row that is draggable. If you
40
+ * specify this class, then you are responsible for setting cursor: move in the CSS and only these cells
41
+ * will have the drag behaviour. If you do not specify a dragHandle, then you get the old behaviour where
42
+ * the whole row is draggable.
43
+ *
44
+ * Other ways to control behaviour:
45
+ *
46
+ * Add class="nodrop" to any rows for which you don't want to allow dropping, and class="nodrag" to any rows
47
+ * that you don't want to be draggable.
48
+ *
49
+ * Inside the onDrop method you can also call $.tableDnD.serialize() this returns a string of the form
50
+ * <tableID>[]=<rowID1>&<tableID>[]=<rowID2> so that you can send this back to the server. The table must have
51
+ * an ID as must all the rows.
52
+ *
53
+ * Other methods:
54
+ *
55
+ * $("...").tableDnDUpdate()
56
+ * Will update all the matching tables, that is it will reapply the mousedown method to the rows (or handle cells).
57
+ * This is useful if you have updated the table rows using Ajax and you want to make the table draggable again.
58
+ * The table maintains the original configuration (so you don't have to specify it again).
59
+ *
60
+ * $("...").tableDnDSerialize()
61
+ * Will serialize and return the serialized string as above, but for each of the matching tables--so it can be
62
+ * called from anywhere and isn't dependent on the currentTable being set up correctly before calling
63
+ *
64
+ * Known problems:
65
+ * - Auto-scoll has some problems with IE7 (it scrolls even when it shouldn't), work-around: set scrollAmount to 0
66
+ *
67
+ * Version 0.2: 2008-02-20 First public version
68
+ * Version 0.3: 2008-02-07 Added onDragStart option
69
+ * Made the scroll amount configurable (default is 5 as before)
70
+ * Version 0.4: 2008-03-15 Changed the noDrag/noDrop attributes to nodrag/nodrop classes
71
+ * Added onAllowDrop to control dropping
72
+ * Fixed a bug which meant that you couldn't set the scroll amount in both directions
73
+ * Added serialize method
74
+ * Version 0.5: 2008-05-16 Changed so that if you specify a dragHandle class it doesn't make the whole row
75
+ * draggable
76
+ * Improved the serialize method to use a default (and settable) regular expression.
77
+ * Added tableDnDupate() and tableDnDSerialize() to be called when you are outside the table
78
+ */
79
+ jQuery.tableDnD = {
80
+ /** Keep hold of the current table being dragged */
81
+ currentTable : null,
82
+ /** Keep hold of the current drag object if any */
83
+ dragObject: null,
84
+ /** The current mouse offset */
85
+ mouseOffset: null,
86
+ /** Remember the old value of Y so that we don't do too much processing */
87
+ oldY: 0,
88
+
89
+ /** Actually build the structure */
90
+ build: function(options) {
91
+ // Set up the defaults if any
92
+
93
+ this.each(function() {
94
+ // This is bound to each matching table, set up the defaults and override with user options
95
+ this.tableDnDConfig = $.extend({
96
+ onDragStyle: null,
97
+ onDropStyle: null,
98
+ // Add in the default class for whileDragging
99
+ onDragClass: "tDnD_whileDrag",
100
+ onDrop: null,
101
+ onDrag: null, // ADDED
102
+ onDragStart: null,
103
+ scrollAmount: 5,
104
+ serializeRegexp: /[^\-]*$/, // The regular expression to use to trim row IDs
105
+ serializeParamName: null, // If you want to specify another parameter name instead of the table ID
106
+ dragHandle: null // If you give the name of a class here, then only Cells with this class will be draggable
107
+ }, options || {});
108
+ // Now make the rows draggable
109
+ jQuery.tableDnD.makeDraggable(this);
110
+ });
111
+
112
+ // Now we need to capture the mouse up and mouse move event
113
+ // We can use bind so that we don't interfere with other event handlers
114
+ jQuery(document)
115
+ .bind('mousemove', jQuery.tableDnD.mousemove)
116
+ .bind('mouseup', jQuery.tableDnD.mouseup);
117
+
118
+ // Don't break the chain
119
+ return this;
120
+ },
121
+
122
+ /** This function makes all the rows on the table draggable apart from those marked as "NoDrag" */
123
+ makeDraggable: function(table) {
124
+ var config = table.tableDnDConfig;
125
+ if (table.tableDnDConfig.dragHandle) {
126
+ // We only need to add the event to the specified cells
127
+ var cells = $("td."+table.tableDnDConfig.dragHandle, table);
128
+ cells.each(function() {
129
+ // The cell is bound to "this"
130
+ jQuery(this).mousedown(function(ev) {
131
+ jQuery.tableDnD.dragObject = this.parentNode;
132
+ jQuery.tableDnD.currentTable = table;
133
+ jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
134
+ if (config.onDragStart) {
135
+ // Call the onDrop method if there is one
136
+ config.onDragStart(table, this);
137
+ }
138
+ return false;
139
+ });
140
+ })
141
+ } else {
142
+ // For backwards compatibility, we add the event to the whole row
143
+ var rows = jQuery("tr", table); // get all the rows as a wrapped set
144
+ rows.each(function() {
145
+ // Iterate through each row, the row is bound to "this"
146
+ var row = $(this);
147
+ if (! row.hasClass("nodrag")) {
148
+ row.mousedown(function(ev) {
149
+ if (ev.target.tagName == "TD") {
150
+ jQuery.tableDnD.dragObject = this;
151
+ jQuery.tableDnD.currentTable = table;
152
+ jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
153
+ if (config.onDragStart) {
154
+ // Call the onDrop method if there is one
155
+ config.onDragStart(table, this);
156
+ }
157
+ return false;
158
+ }
159
+ }).css("cursor", "move"); // Store the tableDnD object
160
+ }
161
+ });
162
+ }
163
+ },
164
+
165
+ updateTables: function() {
166
+ this.each(function() {
167
+ // this is now bound to each matching table
168
+ if (this.tableDnDConfig) {
169
+ jQuery.tableDnD.makeDraggable(this);
170
+ }
171
+ })
172
+ },
173
+
174
+ /** Get the mouse coordinates from the event (allowing for browser differences) */
175
+ mouseCoords: function(ev){
176
+ if(ev.pageX || ev.pageY){
177
+ return {x:ev.pageX, y:ev.pageY};
178
+ }
179
+ return {
180
+ x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
181
+ y:ev.clientY + document.body.scrollTop - document.body.clientTop
182
+ };
183
+ },
184
+
185
+ /** Given a target element and a mouse event, get the mouse offset from that element.
186
+ To do this we need the element's position and the mouse position */
187
+ getMouseOffset: function(target, ev) {
188
+ ev = ev || window.event;
189
+
190
+ var docPos = this.getPosition(target);
191
+ var mousePos = this.mouseCoords(ev);
192
+ return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
193
+ },
194
+
195
+ /** Get the position of an element by going up the DOM tree and adding up all the offsets */
196
+ getPosition: function(e){
197
+ var left = 0;
198
+ var top = 0;
199
+ /** Safari fix -- thanks to Luis Chato for this! */
200
+ if (e.offsetHeight == 0) {
201
+ /** Safari 2 doesn't correctly grab the offsetTop of a table row
202
+ this is detailed here:
203
+ http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/
204
+ the solution is likewise noted there, grab the offset of a table cell in the row - the firstChild.
205
+ note that firefox will return a text node as a first child, so designing a more thorough
206
+ solution may need to take that into account, for now this seems to work in firefox, safari, ie */
207
+ e = e.firstChild; // a table cell
208
+ }
209
+
210
+ while (e.offsetParent){
211
+ left += e.offsetLeft;
212
+ top += e.offsetTop;
213
+ e = e.offsetParent;
214
+ }
215
+
216
+ left += e.offsetLeft;
217
+ top += e.offsetTop;
218
+
219
+ return {x:left, y:top};
220
+ },
221
+
222
+ mousemove: function(ev) {
223
+ if (jQuery.tableDnD.dragObject == null) {
224
+ return;
225
+ }
226
+
227
+ var dragObj = jQuery(jQuery.tableDnD.dragObject);
228
+ var config = jQuery.tableDnD.currentTable.tableDnDConfig;
229
+ var mousePos = jQuery.tableDnD.mouseCoords(ev);
230
+ var y = mousePos.y - jQuery.tableDnD.mouseOffset.y;
231
+ //auto scroll the window
232
+ var yOffset = window.pageYOffset;
233
+ if (document.all) {
234
+ // Windows version
235
+ //yOffset=document.body.scrollTop;
236
+ if (typeof document.compatMode != 'undefined' &&
237
+ document.compatMode != 'BackCompat') {
238
+ yOffset = document.documentElement.scrollTop;
239
+ }
240
+ else if (typeof document.body != 'undefined') {
241
+ yOffset=document.body.scrollTop;
242
+ }
243
+
244
+ }
245
+
246
+ if (mousePos.y-yOffset < config.scrollAmount) {
247
+ window.scrollBy(0, -config.scrollAmount);
248
+ } else {
249
+ var windowHeight = window.innerHeight ? window.innerHeight
250
+ : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
251
+ if (windowHeight-(mousePos.y-yOffset) < config.scrollAmount) {
252
+ window.scrollBy(0, config.scrollAmount);
253
+ }
254
+ }
255
+
256
+
257
+ if (y != jQuery.tableDnD.oldY) {
258
+ // work out if we're going up or down...
259
+ var movingDown = y > jQuery.tableDnD.oldY;
260
+ // update the old value
261
+ jQuery.tableDnD.oldY = y;
262
+ // update the style to show we're dragging
263
+ if (config.onDragClass) {
264
+ dragObj.addClass(config.onDragClass);
265
+ } else {
266
+ dragObj.css(config.onDragStyle);
267
+ }
268
+ // If we're over a row then move the dragged row to there so that the user sees the
269
+ // effect dynamically
270
+ var currentRow = jQuery.tableDnD.findDropTargetRow(dragObj, y, movingDown);
271
+ if (currentRow) {
272
+ if (movingDown && jQuery.tableDnD.dragObject != currentRow) {
273
+ jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow.nextSibling);
274
+ } else if (! movingDown && jQuery.tableDnD.dragObject != currentRow) {
275
+ jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow);
276
+ }
277
+ if (config.onDrag) {
278
+ config.onDrag(jQuery.tableDnD.currentTable, jQuery.tableDnD.dragObject);
279
+ }
280
+ }
281
+ }
282
+
283
+ return false;
284
+ },
285
+
286
+ /** We're only worried about the y position really, because we can only move rows up and down */
287
+ findDropTargetRow: function(draggedRow, y, movingDown) {
288
+ var rows = jQuery.tableDnD.currentTable.rows;
289
+ for (var i=0; i<rows.length; i++) {
290
+ var row = rows[i];
291
+ var rowY = this.getPosition(row).y;
292
+ var rowHeight = parseInt(row.offsetHeight)/2;
293
+ if (row.offsetHeight == 0) {
294
+ rowY = this.getPosition(row.firstChild).y;
295
+ rowHeight = parseInt(row.firstChild.offsetHeight)/2;
296
+ }
297
+ // Because we always have to insert before, we need to offset the height a bit
298
+ if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
299
+ // that's the row we're over
300
+ // If it's the same as the current row, ignore it
301
+ if (row == draggedRow.get(0)) {return null;}
302
+ var config = jQuery.tableDnD.currentTable.tableDnDConfig;
303
+ if (config.onAllowDrop) {
304
+ if (config.onAllowDrop(draggedRow, row, movingDown)) {
305
+ return row;
306
+ } else {
307
+ return null;
308
+ }
309
+ } else {
310
+ // If a row has nodrop class, then don't allow dropping (inspired by John Tarr and Famic)
311
+ var nodrop = $(row).hasClass("nodrop");
312
+ if (! nodrop) {
313
+ return row;
314
+ } else {
315
+ return null;
316
+ }
317
+ }
318
+ return row;
319
+ }
320
+ }
321
+ return null;
322
+ },
323
+
324
+ mouseup: function(e) {
325
+ if (jQuery.tableDnD.currentTable && jQuery.tableDnD.dragObject) {
326
+ var droppedRow = jQuery.tableDnD.dragObject;
327
+ var config = jQuery.tableDnD.currentTable.tableDnDConfig;
328
+ // If we have a dragObject, then we need to release it,
329
+ // The row will already have been moved to the right place so we just reset stuff
330
+ if (config.onDragClass) {
331
+ jQuery(droppedRow).removeClass(config.onDragClass);
332
+ } else {
333
+ jQuery(droppedRow).css(config.onDropStyle);
334
+ }
335
+ jQuery.tableDnD.dragObject = null;
336
+ if (config.onDrop) {
337
+ // Call the onDrop method if there is one
338
+ config.onDrop(jQuery.tableDnD.currentTable, droppedRow);
339
+ }
340
+ jQuery.tableDnD.currentTable = null; // let go of the table too
341
+ }
342
+ },
343
+
344
+ serialize: function() {
345
+ if (jQuery.tableDnD.currentTable) {
346
+ return jQuery.tableDnD.serializeTable(jQuery.tableDnD.currentTable);
347
+ } else {
348
+ return "Error: No Table id set, you need to set an id on your table and every row";
349
+ }
350
+ },
351
+
352
+ serializeTable: function(table) {
353
+ var result = "";
354
+ var tableId = table.id;
355
+ var rows = table.rows;
356
+ for (var i=0; i<rows.length; i++) {
357
+ if (result.length > 0) result += "&";
358
+ var rowId = rows[i].id;
359
+ if (rowId && rowId && table.tableDnDConfig && table.tableDnDConfig.serializeRegexp) {
360
+ rowId = rowId.match(table.tableDnDConfig.serializeRegexp)[0];
361
+ }
362
+
363
+ result += tableId + '[]=' + rows[i].id;
364
+ }
365
+ return result;
366
+ },
367
+
368
+ serializeTables: function() {
369
+ var result = "";
370
+ this.each(function() {
371
+ // this is now bound to each matching table
372
+ result += jQuery.tableDnD.serializeTable(this);
373
+ });
374
+ return result;
375
+ }
376
+
377
+ }
378
+
379
+ jQuery.fn.extend(
380
+ {
381
+ tableDnD : jQuery.tableDnD.build,
382
+ tableDnDUpdate : jQuery.tableDnD.updateTables,
383
+ tableDnDSerialize: jQuery.tableDnD.serializeTables
384
+ }
385
+ );
@@ -0,0 +1,4 @@
1
+ module ActiveadminDraggable
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveadminDraggable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin_draggable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Krzysztof Bia\xC5\x82ek"
@@ -77,7 +77,11 @@ files:
77
77
  - README.md
78
78
  - Rakefile
79
79
  - activeadmin_draggable.gemspec
80
+ - app/assets/javascripts/activeadmin_draggable/activeadmin_draggable.js
81
+ - app/assets/javascripts/activeadmin_draggable/jquery.table_tree.js
82
+ - app/assets/javascripts/activeadmin_draggable/jquery.tablednd_0_5.js
80
83
  - lib/activeadmin_draggable.rb
84
+ - lib/activeadmin_draggable/engine.rb
81
85
  - lib/activeadmin_draggable/version.rb
82
86
  homepage: ""
83
87
  licenses: