webmaster@1
|
1 // $Id: tabledrag.js,v 1.13.2.1 2008/02/08 18:54:10 goba Exp $ |
webmaster@1
|
2 |
webmaster@1
|
3 /** |
webmaster@1
|
4 * Drag and drop table rows with field manipulation. |
webmaster@1
|
5 * |
webmaster@1
|
6 * Using the drupal_add_tabledrag() function, any table with weights or parent |
webmaster@1
|
7 * relationships may be made into draggable tables. Columns containing a field |
webmaster@1
|
8 * may optionally be hidden, providing a better user experience. |
webmaster@1
|
9 * |
webmaster@1
|
10 * Created tableDrag instances may be modified with custom behaviors by |
webmaster@1
|
11 * overriding the .onDrag, .onDrop, .row.onSwap, and .row.onIndent methods. |
webmaster@1
|
12 * See blocks.js for an example of adding additional functionality to tableDrag. |
webmaster@1
|
13 */ |
webmaster@1
|
14 Drupal.behaviors.tableDrag = function(context) { |
webmaster@1
|
15 for (var base in Drupal.settings.tableDrag) { |
webmaster@1
|
16 if (!$('#' + base + '.tabledrag-processed', context).size()) { |
webmaster@1
|
17 var tableSettings = Drupal.settings.tableDrag[base]; |
webmaster@1
|
18 |
webmaster@1
|
19 $('#' + base).filter(':not(.tabledrag-processed)').each(function() { |
webmaster@1
|
20 // Create the new tableDrag instance. Save in the Drupal variable |
webmaster@1
|
21 // to allow other scripts access to the object. |
webmaster@1
|
22 Drupal.tableDrag[base] = new Drupal.tableDrag(this, tableSettings); |
webmaster@1
|
23 }); |
webmaster@1
|
24 |
webmaster@1
|
25 $('#' + base).addClass('tabledrag-processed'); |
webmaster@1
|
26 } |
webmaster@1
|
27 } |
webmaster@1
|
28 }; |
webmaster@1
|
29 |
webmaster@1
|
30 /** |
webmaster@1
|
31 * Constructor for the tableDrag object. Provides table and field manipulation. |
webmaster@1
|
32 * |
webmaster@1
|
33 * @param table |
webmaster@1
|
34 * DOM object for the table to be made draggable. |
webmaster@1
|
35 * @param tableSettings |
webmaster@1
|
36 * Settings for the table added via drupal_add_dragtable(). |
webmaster@1
|
37 */ |
webmaster@1
|
38 Drupal.tableDrag = function(table, tableSettings) { |
webmaster@1
|
39 var self = this; |
webmaster@1
|
40 |
webmaster@1
|
41 // Required object variables. |
webmaster@1
|
42 this.table = table; |
webmaster@1
|
43 this.tableSettings = tableSettings; |
webmaster@1
|
44 this.dragObject = null; // Used to hold information about a current drag operation. |
webmaster@1
|
45 this.rowObject = null; // Provides operations for row manipulation. |
webmaster@1
|
46 this.oldRowElement = null; // Remember the previous element. |
webmaster@1
|
47 this.oldY = 0; // Used to determine up or down direction from last mouse move. |
webmaster@1
|
48 this.changed = false; // Whether anything in the entire table has changed. |
webmaster@1
|
49 this.maxDepth = 0; // Maximum amount of allowed parenting. |
webmaster@1
|
50 this.rtl = $(this.table).css('direction') == 'rtl' ? -1 : 1; // Direction of the table. |
webmaster@1
|
51 |
webmaster@1
|
52 // Configure the scroll settings. |
webmaster@1
|
53 this.scrollSettings = { amount: 4, interval: 50, trigger: 70 }; |
webmaster@1
|
54 this.scrollInterval = null; |
webmaster@1
|
55 this.scrollY = 0; |
webmaster@1
|
56 this.windowHeight = 0; |
webmaster@1
|
57 |
webmaster@1
|
58 // Check this table's settings to see if there are parent relationships in |
webmaster@1
|
59 // this table. For efficiency, large sections of code can be skipped if we |
webmaster@1
|
60 // don't need to track horizontal movement and indentations. |
webmaster@1
|
61 this.indentEnabled = false; |
webmaster@1
|
62 for (group in tableSettings) { |
webmaster@1
|
63 for (n in tableSettings[group]) { |
webmaster@1
|
64 if (tableSettings[group][n]['relationship'] == 'parent') { |
webmaster@1
|
65 this.indentEnabled = true; |
webmaster@1
|
66 } |
webmaster@1
|
67 if (tableSettings[group][n]['limit'] > 0) { |
webmaster@1
|
68 this.maxDepth = tableSettings[group][n]['limit']; |
webmaster@1
|
69 } |
webmaster@1
|
70 } |
webmaster@1
|
71 } |
webmaster@1
|
72 if (this.indentEnabled) { |
webmaster@1
|
73 this.indentCount = 1; // Total width of indents, set in makeDraggable. |
webmaster@1
|
74 // Find the width of indentations to measure mouse movements against. |
webmaster@1
|
75 // Because the table doesn't need to start with any indentations, we |
webmaster@1
|
76 // manually create an empty div, check it's width, then remove. |
webmaster@1
|
77 var indent = $(Drupal.theme('tableDragIndentation')).appendTo('body'); |
webmaster@1
|
78 this.indentAmount = parseInt(indent.css('width')); |
webmaster@1
|
79 indent.remove(); |
webmaster@1
|
80 } |
webmaster@1
|
81 |
webmaster@1
|
82 // Make each applicable row draggable. |
webmaster@1
|
83 $('tr.draggable', table).each(function() { self.makeDraggable(this); }); |
webmaster@1
|
84 |
webmaster@1
|
85 // Hide columns containing affected form elements. |
webmaster@1
|
86 this.hideColumns(); |
webmaster@1
|
87 |
webmaster@1
|
88 // Add mouse bindings to the document. The self variable is passed along |
webmaster@1
|
89 // as event handlers do not have direct access to the tableDrag object. |
webmaster@1
|
90 $(document).bind('mousemove', function(event) { self.dragRow(event, self); return false; }); |
webmaster@1
|
91 $(document).bind('mouseup', function(event) { self.dropRow(event, self); }); |
webmaster@1
|
92 }; |
webmaster@1
|
93 |
webmaster@1
|
94 /** |
webmaster@1
|
95 * Hide the columns containing form elements according to the settings for |
webmaster@1
|
96 * this tableDrag instance. |
webmaster@1
|
97 */ |
webmaster@1
|
98 Drupal.tableDrag.prototype.hideColumns = function() { |
webmaster@1
|
99 for (var group in this.tableSettings) { |
webmaster@1
|
100 // Find the first field in this group. |
webmaster@1
|
101 for (var d in this.tableSettings[group]) { |
webmaster@1
|
102 if ($('.' + this.tableSettings[group][d]['target'], this.table).size()) { |
webmaster@1
|
103 var hidden = this.tableSettings[group][d]['hidden']; |
webmaster@1
|
104 var field = $('.' + this.tableSettings[group][d]['target'] + ':first', this.table); |
webmaster@1
|
105 var cell = field.parents('td:first, th:first'); |
webmaster@1
|
106 break; |
webmaster@1
|
107 } |
webmaster@1
|
108 } |
webmaster@1
|
109 // Hide the column containing this field. |
webmaster@1
|
110 if (hidden && cell[0] && cell.css('display') != 'none') { |
webmaster@1
|
111 // Add 1 to our indexes. The nth-child selector is 1 based, not 0 based. |
webmaster@1
|
112 var columnIndex = $('td', field.parents('tr:first')).index(cell.get(0)) + 1; |
webmaster@1
|
113 var headerIndex = $('td:not(:hidden)', field.parents('tr:first')).index(cell.get(0)) + 1; |
webmaster@1
|
114 $('tbody tr', this.table).each(function() { |
webmaster@1
|
115 // Find and hide the cell in the table body. |
webmaster@1
|
116 var cell = $('td:nth-child('+ columnIndex +')', this); |
webmaster@1
|
117 if (cell.size()) { |
webmaster@1
|
118 cell.css('display', 'none'); |
webmaster@1
|
119 } |
webmaster@1
|
120 // We might be dealing with a row spanning the entire table. |
webmaster@1
|
121 // Reduce the colspan on the first cell to prevent the cell from |
webmaster@1
|
122 // overshooting the table. |
webmaster@1
|
123 else { |
webmaster@1
|
124 cell = $('td:first', this); |
webmaster@1
|
125 cell.attr('colspan', cell.attr('colspan') - 1); |
webmaster@1
|
126 } |
webmaster@1
|
127 }); |
webmaster@1
|
128 $('thead tr', this.table).each(function() { |
webmaster@1
|
129 // Remove table header cells entirely (Safari doesn't hide properly). |
webmaster@1
|
130 var th = $('th:nth-child('+ headerIndex +')', this); |
webmaster@1
|
131 if (th.size()) { |
webmaster@1
|
132 th.remove(); |
webmaster@1
|
133 } |
webmaster@1
|
134 }); |
webmaster@1
|
135 } |
webmaster@1
|
136 } |
webmaster@1
|
137 }; |
webmaster@1
|
138 |
webmaster@1
|
139 /** |
webmaster@1
|
140 * Find the target used within a particular row and group. |
webmaster@1
|
141 */ |
webmaster@1
|
142 Drupal.tableDrag.prototype.rowSettings = function(group, row) { |
webmaster@1
|
143 var field = $('.' + group, row); |
webmaster@1
|
144 for (delta in this.tableSettings[group]) { |
webmaster@1
|
145 var targetClass = this.tableSettings[group][delta]['target']; |
webmaster@1
|
146 if (field.is('.' + targetClass)) { |
webmaster@1
|
147 // Return a copy of the row settings. |
webmaster@1
|
148 var rowSettings = new Object(); |
webmaster@1
|
149 for (var n in this.tableSettings[group][delta]) { |
webmaster@1
|
150 rowSettings[n] = this.tableSettings[group][delta][n]; |
webmaster@1
|
151 } |
webmaster@1
|
152 return rowSettings; |
webmaster@1
|
153 } |
webmaster@1
|
154 } |
webmaster@1
|
155 }; |
webmaster@1
|
156 |
webmaster@1
|
157 /** |
webmaster@1
|
158 * Take an item and add event handlers to make it become draggable. |
webmaster@1
|
159 */ |
webmaster@1
|
160 Drupal.tableDrag.prototype.makeDraggable = function(item) { |
webmaster@1
|
161 var self = this; |
webmaster@1
|
162 |
webmaster@1
|
163 // Create the handle. |
webmaster@1
|
164 var handle = $('<a href="#" class="tabledrag-handle"><div class="handle"> </div></a>').attr('title', Drupal.t('Drag to re-order')); |
webmaster@1
|
165 // Insert the handle after indentations (if any). |
webmaster@1
|
166 if ($('td:first .indentation:last', item).after(handle).size()) { |
webmaster@1
|
167 // Update the total width of indentation in this entire table. |
webmaster@1
|
168 self.indentCount = Math.max($('.indentation', item).size(), self.indentCount); |
webmaster@1
|
169 } |
webmaster@1
|
170 else { |
webmaster@1
|
171 $('td:first', item).prepend(handle); |
webmaster@1
|
172 } |
webmaster@1
|
173 |
webmaster@1
|
174 // Add hover action for the handle. |
webmaster@1
|
175 handle.hover(function() { |
webmaster@1
|
176 self.dragObject == null ? $(this).addClass('tabledrag-handle-hover') : null; |
webmaster@1
|
177 }, function() { |
webmaster@1
|
178 self.dragObject == null ? $(this).removeClass('tabledrag-handle-hover') : null; |
webmaster@1
|
179 }); |
webmaster@1
|
180 |
webmaster@1
|
181 // Add the mousedown action for the handle. |
webmaster@1
|
182 handle.mousedown(function(event) { |
webmaster@1
|
183 // Create a new dragObject recording the event information. |
webmaster@1
|
184 self.dragObject = new Object(); |
webmaster@1
|
185 self.dragObject.initMouseOffset = self.getMouseOffset(item, event); |
webmaster@1
|
186 self.dragObject.initMouseCoords = self.mouseCoords(event); |
webmaster@1
|
187 if (self.indentEnabled) { |
webmaster@1
|
188 self.dragObject.indentMousePos = self.dragObject.initMouseCoords; |
webmaster@1
|
189 } |
webmaster@1
|
190 |
webmaster@1
|
191 // If there's a lingering row object from the keyboard, remove its focus. |
webmaster@1
|
192 if (self.rowObject) { |
webmaster@1
|
193 $('a.tabledrag-handle', self.rowObject.element).blur(); |
webmaster@1
|
194 } |
webmaster@1
|
195 |
webmaster@1
|
196 // Create a new rowObject for manipulation of this row. |
webmaster@1
|
197 self.rowObject = new self.row(item, 'mouse', self.indentEnabled, self.maxDepth, true); |
webmaster@1
|
198 |
webmaster@1
|
199 // Save the position of the table. |
webmaster@1
|
200 self.table.topY = self.getPosition(self.table).y; |
webmaster@1
|
201 self.table.bottomY = self.table.topY + self.table.offsetHeight; |
webmaster@1
|
202 |
webmaster@1
|
203 // Add classes to the handle and row. |
webmaster@1
|
204 $(this).addClass('tabledrag-handle-hover'); |
webmaster@1
|
205 $(item).addClass('drag'); |
webmaster@1
|
206 |
webmaster@1
|
207 // Set the document to use the move cursor during drag. |
webmaster@1
|
208 $('body').addClass('drag'); |
webmaster@1
|
209 if (self.oldRowElement) { |
webmaster@1
|
210 $(self.oldRowElement).removeClass('drag-previous'); |
webmaster@1
|
211 } |
webmaster@1
|
212 |
webmaster@1
|
213 // Hack for IE6 that flickers uncontrollably if select lists are moved. |
webmaster@1
|
214 if (navigator.userAgent.indexOf('MSIE 6.') != -1) { |
webmaster@1
|
215 $('select', this.table).css('display', 'none'); |
webmaster@1
|
216 } |
webmaster@1
|
217 |
webmaster@1
|
218 // Hack for Konqueror, prevent the blur handler from firing. |
webmaster@1
|
219 // Konqueror always gives links focus, even after returning false on mousedown. |
webmaster@1
|
220 self.safeBlur = false; |
webmaster@1
|
221 |
webmaster@1
|
222 // Call optional placeholder function. |
webmaster@1
|
223 self.onDrag(); |
webmaster@1
|
224 return false; |
webmaster@1
|
225 }); |
webmaster@1
|
226 |
webmaster@1
|
227 // Prevent the anchor tag from jumping us to the top of the page. |
webmaster@1
|
228 handle.click(function() { |
webmaster@1
|
229 return false; |
webmaster@1
|
230 }); |
webmaster@1
|
231 |
webmaster@1
|
232 // Similar to the hover event, add a class when the handle is focused. |
webmaster@1
|
233 handle.focus(function() { |
webmaster@1
|
234 $(this).addClass('tabledrag-handle-hover'); |
webmaster@1
|
235 self.safeBlur = true; |
webmaster@1
|
236 }); |
webmaster@1
|
237 |
webmaster@1
|
238 // Remove the handle class on blur and fire the same function as a mouseup. |
webmaster@1
|
239 handle.blur(function(event) { |
webmaster@1
|
240 $(this).removeClass('tabledrag-handle-hover'); |
webmaster@1
|
241 if (self.rowObject && self.safeBlur) { |
webmaster@1
|
242 self.dropRow(event, self); |
webmaster@1
|
243 } |
webmaster@1
|
244 }); |
webmaster@1
|
245 |
webmaster@1
|
246 // Add arrow-key support to the handle. |
webmaster@1
|
247 handle.keydown(function(event) { |
webmaster@1
|
248 // If a rowObject doesn't yet exist and this isn't the tab key. |
webmaster@1
|
249 if (event.keyCode != 9 && !self.rowObject) { |
webmaster@1
|
250 self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true); |
webmaster@1
|
251 } |
webmaster@1
|
252 |
webmaster@1
|
253 var keyChange = false; |
webmaster@1
|
254 switch (event.keyCode) { |
webmaster@1
|
255 case 37: // Left arrow. |
webmaster@1
|
256 case 63234: // Safari left arrow. |
webmaster@1
|
257 keyChange = true; |
webmaster@1
|
258 self.rowObject.indent(-1 * self.rtl); |
webmaster@1
|
259 break; |
webmaster@1
|
260 case 38: // Up arrow. |
webmaster@1
|
261 case 63232: // Safari up arrow. |
webmaster@1
|
262 var previousRow = $(self.rowObject.element).prev('tr').get(0); |
webmaster@1
|
263 while (previousRow && $(previousRow).is(':hidden')) { |
webmaster@1
|
264 previousRow = $(previousRow).prev('tr').get(0); |
webmaster@1
|
265 } |
webmaster@1
|
266 if (previousRow) { |
webmaster@1
|
267 self.safeBlur = false; // Do not allow the onBlur cleanup. |
webmaster@1
|
268 self.rowObject.direction = 'up'; |
webmaster@1
|
269 keyChange = true; |
webmaster@1
|
270 |
webmaster@1
|
271 if ($(item).is('.tabledrag-root')) { |
webmaster@1
|
272 // Swap with the previous top-level row.. |
webmaster@1
|
273 var groupHeight = 0; |
webmaster@1
|
274 while (previousRow && $('.indentation', previousRow).size()) { |
webmaster@1
|
275 previousRow = $(previousRow).prev('tr').get(0); |
webmaster@1
|
276 groupHeight += $(previousRow).is(':hidden') ? 0 : previousRow.offsetHeight; |
webmaster@1
|
277 } |
webmaster@1
|
278 if (previousRow) { |
webmaster@1
|
279 self.rowObject.swap('before', previousRow); |
webmaster@1
|
280 // No need to check for indentation, 0 is the only valid one. |
webmaster@1
|
281 window.scrollBy(0, -groupHeight); |
webmaster@1
|
282 } |
webmaster@1
|
283 } |
webmaster@1
|
284 else if (self.table.tBodies[0].rows[0] != previousRow || $(previousRow).is('.draggable')) { |
webmaster@1
|
285 // Swap with the previous row (unless previous row is the first one |
webmaster@1
|
286 // and undraggable). |
webmaster@1
|
287 self.rowObject.swap('before', previousRow); |
webmaster@1
|
288 self.rowObject.interval = null; |
webmaster@1
|
289 self.rowObject.indent(0); |
webmaster@1
|
290 window.scrollBy(0, -parseInt(item.offsetHeight)); |
webmaster@1
|
291 } |
webmaster@1
|
292 handle.get(0).focus(); // Regain focus after the DOM manipulation. |
webmaster@1
|
293 } |
webmaster@1
|
294 break; |
webmaster@1
|
295 case 39: // Right arrow. |
webmaster@1
|
296 case 63235: // Safari right arrow. |
webmaster@1
|
297 keyChange = true; |
webmaster@1
|
298 self.rowObject.indent(1 * self.rtl); |
webmaster@1
|
299 break; |
webmaster@1
|
300 case 40: // Down arrow. |
webmaster@1
|
301 case 63233: // Safari down arrow. |
webmaster@1
|
302 var nextRow = $(self.rowObject.group).filter(':last').next('tr').get(0); |
webmaster@1
|
303 while (nextRow && $(nextRow).is(':hidden')) { |
webmaster@1
|
304 nextRow = $(nextRow).next('tr').get(0); |
webmaster@1
|
305 } |
webmaster@1
|
306 if (nextRow) { |
webmaster@1
|
307 self.safeBlur = false; // Do not allow the onBlur cleanup. |
webmaster@1
|
308 self.rowObject.direction = 'down'; |
webmaster@1
|
309 keyChange = true; |
webmaster@1
|
310 |
webmaster@1
|
311 if ($(item).is('.tabledrag-root')) { |
webmaster@1
|
312 // Swap with the next group (necessarily a top-level one). |
webmaster@1
|
313 var groupHeight = 0; |
webmaster@1
|
314 nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false); |
webmaster@1
|
315 if (nextGroup) { |
webmaster@1
|
316 $(nextGroup.group).each(function () {groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight}); |
webmaster@1
|
317 nextGroupRow = $(nextGroup.group).filter(':last').get(0); |
webmaster@1
|
318 self.rowObject.swap('after', nextGroupRow); |
webmaster@1
|
319 // No need to check for indentation, 0 is the only valid one. |
webmaster@1
|
320 window.scrollBy(0, parseInt(groupHeight)); |
webmaster@1
|
321 } |
webmaster@1
|
322 } |
webmaster@1
|
323 else { |
webmaster@1
|
324 // Swap with the next row. |
webmaster@1
|
325 self.rowObject.swap('after', nextRow); |
webmaster@1
|
326 self.rowObject.interval = null; |
webmaster@1
|
327 self.rowObject.indent(0); |
webmaster@1
|
328 window.scrollBy(0, parseInt(item.offsetHeight)); |
webmaster@1
|
329 } |
webmaster@1
|
330 handle.get(0).focus(); // Regain focus after the DOM manipulation. |
webmaster@1
|
331 } |
webmaster@1
|
332 break; |
webmaster@1
|
333 } |
webmaster@1
|
334 |
webmaster@1
|
335 if (self.rowObject && self.rowObject.changed == true) { |
webmaster@1
|
336 $(item).addClass('drag'); |
webmaster@1
|
337 if (self.oldRowElement) { |
webmaster@1
|
338 $(self.oldRowElement).removeClass('drag-previous'); |
webmaster@1
|
339 } |
webmaster@1
|
340 self.oldRowElement = item; |
webmaster@1
|
341 self.restripeTable(); |
webmaster@1
|
342 self.onDrag(); |
webmaster@1
|
343 } |
webmaster@1
|
344 |
webmaster@1
|
345 // Returning false if we have an arrow key to prevent scrolling. |
webmaster@1
|
346 if (keyChange) { |
webmaster@1
|
347 return false; |
webmaster@1
|
348 } |
webmaster@1
|
349 }); |
webmaster@1
|
350 |
webmaster@1
|
351 // Compatibility addition, return false on keypress to prevent unwanted scrolling. |
webmaster@1
|
352 // IE and Safari will supress scrolling on keydown, but all other browsers |
webmaster@1
|
353 // need to return false on keypress. http://www.quirksmode.org/js/keys.html |
webmaster@1
|
354 handle.keypress(function(event) { |
webmaster@1
|
355 switch (event.keyCode) { |
webmaster@1
|
356 case 37: // Left arrow. |
webmaster@1
|
357 case 38: // Up arrow. |
webmaster@1
|
358 case 39: // Right arrow. |
webmaster@1
|
359 case 40: // Down arrow. |
webmaster@1
|
360 return false; |
webmaster@1
|
361 } |
webmaster@1
|
362 }); |
webmaster@1
|
363 }; |
webmaster@1
|
364 |
webmaster@1
|
365 /** |
webmaster@1
|
366 * Mousemove event handler, bound to document. |
webmaster@1
|
367 */ |
webmaster@1
|
368 Drupal.tableDrag.prototype.dragRow = function(event, self) { |
webmaster@1
|
369 if (self.dragObject) { |
webmaster@1
|
370 self.currentMouseCoords = self.mouseCoords(event); |
webmaster@1
|
371 |
webmaster@1
|
372 var y = self.currentMouseCoords.y - self.dragObject.initMouseOffset.y; |
webmaster@1
|
373 var x = self.currentMouseCoords.x - self.dragObject.initMouseOffset.x; |
webmaster@1
|
374 |
webmaster@1
|
375 // Check for row swapping and vertical scrolling. |
webmaster@1
|
376 if (y != self.oldY) { |
webmaster@1
|
377 self.rowObject.direction = y > self.oldY ? 'down' : 'up'; |
webmaster@1
|
378 self.oldY = y; // Update the old value. |
webmaster@1
|
379 |
webmaster@1
|
380 // Check if the window should be scrolled (and how fast). |
webmaster@1
|
381 var scrollAmount = self.checkScroll(self.currentMouseCoords.y); |
webmaster@1
|
382 // Stop any current scrolling. |
webmaster@1
|
383 clearInterval(self.scrollInterval); |
webmaster@1
|
384 // Continue scrolling if the mouse has moved in the scroll direction. |
webmaster@1
|
385 if (scrollAmount > 0 && self.rowObject.direction == 'down' || scrollAmount < 0 && self.rowObject.direction == 'up') { |
webmaster@1
|
386 self.setScroll(scrollAmount); |
webmaster@1
|
387 } |
webmaster@1
|
388 |
webmaster@1
|
389 // If we have a valid target, perform the swap and restripe the table. |
webmaster@1
|
390 var currentRow = self.findDropTargetRow(x, y); |
webmaster@1
|
391 if (currentRow) { |
webmaster@1
|
392 if (self.rowObject.direction == 'down') { |
webmaster@1
|
393 self.rowObject.swap('after', currentRow, self); |
webmaster@1
|
394 } |
webmaster@1
|
395 else { |
webmaster@1
|
396 self.rowObject.swap('before', currentRow, self); |
webmaster@1
|
397 } |
webmaster@1
|
398 self.restripeTable(); |
webmaster@1
|
399 } |
webmaster@1
|
400 } |
webmaster@1
|
401 |
webmaster@1
|
402 // Similar to row swapping, handle indentations. |
webmaster@1
|
403 if (self.indentEnabled) { |
webmaster@1
|
404 var xDiff = self.currentMouseCoords.x - self.dragObject.indentMousePos.x; |
webmaster@1
|
405 // Set the number of indentations the mouse has been moved left or right. |
webmaster@1
|
406 var indentDiff = parseInt(xDiff / self.indentAmount * self.rtl); |
webmaster@1
|
407 // Indent the row with our estimated diff, which may be further |
webmaster@1
|
408 // restricted according to the rows around this row. |
webmaster@1
|
409 var indentChange = self.rowObject.indent(indentDiff); |
webmaster@1
|
410 // Update table and mouse indentations. |
webmaster@1
|
411 self.dragObject.indentMousePos.x += self.indentAmount * indentChange * self.rtl; |
webmaster@1
|
412 self.indentCount = Math.max(self.indentCount, self.rowObject.indents); |
webmaster@1
|
413 } |
webmaster@1
|
414 } |
webmaster@1
|
415 }; |
webmaster@1
|
416 |
webmaster@1
|
417 /** |
webmaster@1
|
418 * Mouseup event handler, bound to document. |
webmaster@1
|
419 * Blur event handler, bound to drag handle for keyboard support. |
webmaster@1
|
420 */ |
webmaster@1
|
421 Drupal.tableDrag.prototype.dropRow = function(event, self) { |
webmaster@1
|
422 // Drop row functionality shared between mouseup and blur events. |
webmaster@1
|
423 if (self.rowObject != null) { |
webmaster@1
|
424 var droppedRow = self.rowObject.element; |
webmaster@1
|
425 // The row is already in the right place so we just release it. |
webmaster@1
|
426 if (self.rowObject.changed == true) { |
webmaster@1
|
427 // Update the fields in the dropped row. |
webmaster@1
|
428 self.updateFields(droppedRow); |
webmaster@1
|
429 |
webmaster@1
|
430 // If a setting exists for affecting the entire group, update all the |
webmaster@1
|
431 // fields in the entire dragged group. |
webmaster@1
|
432 for (var group in self.tableSettings) { |
webmaster@1
|
433 var rowSettings = self.rowSettings(group, droppedRow); |
webmaster@1
|
434 if (rowSettings.relationship == 'group') { |
webmaster@1
|
435 for (n in self.rowObject.children) { |
webmaster@1
|
436 self.updateField(self.rowObject.children[n], group); |
webmaster@1
|
437 } |
webmaster@1
|
438 } |
webmaster@1
|
439 } |
webmaster@1
|
440 |
webmaster@1
|
441 self.rowObject.markChanged(); |
webmaster@1
|
442 if (self.changed == false) { |
webmaster@1
|
443 $(Drupal.theme('tableDragChangedWarning')).insertAfter(self.table).hide().fadeIn('slow'); |
webmaster@1
|
444 self.changed = true; |
webmaster@1
|
445 } |
webmaster@1
|
446 } |
webmaster@1
|
447 |
webmaster@1
|
448 if (self.indentEnabled) { |
webmaster@1
|
449 self.rowObject.removeIndentClasses(); |
webmaster@1
|
450 } |
webmaster@1
|
451 if (self.oldRowElement) { |
webmaster@1
|
452 $(self.oldRowElement).removeClass('drag-previous'); |
webmaster@1
|
453 } |
webmaster@1
|
454 $(droppedRow).removeClass('drag').addClass('drag-previous'); |
webmaster@1
|
455 self.oldRowElement = droppedRow; |
webmaster@1
|
456 self.onDrop(); |
webmaster@1
|
457 self.rowObject = null; |
webmaster@1
|
458 } |
webmaster@1
|
459 |
webmaster@1
|
460 // Functionality specific only to mouseup event. |
webmaster@1
|
461 if (self.dragObject != null) { |
webmaster@1
|
462 $('.tabledrag-handle', droppedRow).removeClass('tabledrag-handle-hover'); |
webmaster@1
|
463 |
webmaster@1
|
464 self.dragObject = null; |
webmaster@1
|
465 $('body').removeClass('drag'); |
webmaster@1
|
466 clearInterval(self.scrollInterval); |
webmaster@1
|
467 |
webmaster@1
|
468 // Hack for IE6 that flickers uncontrollably if select lists are moved. |
webmaster@1
|
469 if (navigator.userAgent.indexOf('MSIE 6.') != -1) { |
webmaster@1
|
470 $('select', this.table).css('display', 'block'); |
webmaster@1
|
471 } |
webmaster@1
|
472 } |
webmaster@1
|
473 }; |
webmaster@1
|
474 |
webmaster@1
|
475 /** |
webmaster@1
|
476 * Get the position of an element by adding up parent offsets in the DOM tree. |
webmaster@1
|
477 */ |
webmaster@1
|
478 Drupal.tableDrag.prototype.getPosition = function(element){ |
webmaster@1
|
479 var left = 0; |
webmaster@1
|
480 var top = 0; |
webmaster@1
|
481 // Because Safari doesn't report offsetHeight on table rows, but does on table |
webmaster@1
|
482 // cells, grab the firstChild of the row and use that instead. |
webmaster@1
|
483 // http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari |
webmaster@1
|
484 if (element.offsetHeight == 0) { |
webmaster@1
|
485 element = element.firstChild; // a table cell |
webmaster@1
|
486 } |
webmaster@1
|
487 |
webmaster@1
|
488 while (element.offsetParent){ |
webmaster@1
|
489 left += element.offsetLeft; |
webmaster@1
|
490 top += element.offsetTop; |
webmaster@1
|
491 element = element.offsetParent; |
webmaster@1
|
492 } |
webmaster@1
|
493 |
webmaster@1
|
494 left += element.offsetLeft; |
webmaster@1
|
495 top += element.offsetTop; |
webmaster@1
|
496 |
webmaster@1
|
497 return {x:left, y:top}; |
webmaster@1
|
498 }; |
webmaster@1
|
499 |
webmaster@1
|
500 /** |
webmaster@1
|
501 * Get the mouse coordinates from the event (allowing for browser differences). |
webmaster@1
|
502 */ |
webmaster@1
|
503 Drupal.tableDrag.prototype.mouseCoords = function(event){ |
webmaster@1
|
504 if (event.pageX || event.pageY) { |
webmaster@1
|
505 return {x:event.pageX, y:event.pageY}; |
webmaster@1
|
506 } |
webmaster@1
|
507 return { |
webmaster@1
|
508 x:event.clientX + document.body.scrollLeft - document.body.clientLeft, |
webmaster@1
|
509 y:event.clientY + document.body.scrollTop - document.body.clientTop |
webmaster@1
|
510 }; |
webmaster@1
|
511 }; |
webmaster@1
|
512 |
webmaster@1
|
513 /** |
webmaster@1
|
514 * Given a target element and a mouse event, get the mouse offset from that |
webmaster@1
|
515 * element. To do this we need the element's position and the mouse position. |
webmaster@1
|
516 */ |
webmaster@1
|
517 Drupal.tableDrag.prototype.getMouseOffset = function(target, event) { |
webmaster@1
|
518 var docPos = this.getPosition(target); |
webmaster@1
|
519 var mousePos = this.mouseCoords(event); |
webmaster@1
|
520 return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y}; |
webmaster@1
|
521 }; |
webmaster@1
|
522 |
webmaster@1
|
523 /** |
webmaster@1
|
524 * Find the row the mouse is currently over. This row is then taken and swapped |
webmaster@1
|
525 * with the one being dragged. |
webmaster@1
|
526 * |
webmaster@1
|
527 * @param x |
webmaster@1
|
528 * The x coordinate of the mouse on the page (not the screen). |
webmaster@1
|
529 * @param y |
webmaster@1
|
530 * The y coordinate of the mouse on the page (not the screen). |
webmaster@1
|
531 */ |
webmaster@1
|
532 Drupal.tableDrag.prototype.findDropTargetRow = function(x, y) { |
webmaster@1
|
533 var rows = this.table.tBodies[0].rows; |
webmaster@1
|
534 for (var n=0; n<rows.length; n++) { |
webmaster@1
|
535 var row = rows[n]; |
webmaster@1
|
536 var indentDiff = 0; |
webmaster@1
|
537 // Safari fix see Drupal.tableDrag.prototype.getPosition() |
webmaster@1
|
538 if (row.offsetHeight == 0) { |
webmaster@1
|
539 var rowY = this.getPosition(row.firstChild).y; |
webmaster@1
|
540 var rowHeight = parseInt(row.firstChild.offsetHeight)/2; |
webmaster@1
|
541 } |
webmaster@1
|
542 // Other browsers. |
webmaster@1
|
543 else { |
webmaster@1
|
544 var rowY = this.getPosition(row).y; |
webmaster@1
|
545 var rowHeight = parseInt(row.offsetHeight)/2; |
webmaster@1
|
546 } |
webmaster@1
|
547 |
webmaster@1
|
548 // Because we always insert before, we need to offset the height a bit. |
webmaster@1
|
549 if ((y > (rowY - rowHeight)) && (y < (rowY + rowHeight))) { |
webmaster@1
|
550 if (this.indentEnabled) { |
webmaster@1
|
551 // Check that this row is not a child of the row being dragged. |
webmaster@1
|
552 for (n in this.rowObject.group) { |
webmaster@1
|
553 if (this.rowObject.group[n] == row) { |
webmaster@1
|
554 return null; |
webmaster@1
|
555 } |
webmaster@1
|
556 } |
webmaster@1
|
557 } |
webmaster@1
|
558 // Check that swapping with this row is allowed. |
webmaster@1
|
559 if (!this.rowObject.isValidSwap(row)) { |
webmaster@1
|
560 return null; |
webmaster@1
|
561 } |
webmaster@1
|
562 |
webmaster@1
|
563 // We may have found the row the mouse just passed over, but it doesn't |
webmaster@1
|
564 // take into account hidden rows. Skip backwards until we find a draggable |
webmaster@1
|
565 // row. |
webmaster@1
|
566 while ($(row).is(':hidden') && $(row).prev('tr').is(':hidden')) { |
webmaster@1
|
567 row = $(row).prev('tr').get(0); |
webmaster@1
|
568 } |
webmaster@1
|
569 return row; |
webmaster@1
|
570 } |
webmaster@1
|
571 } |
webmaster@1
|
572 return null; |
webmaster@1
|
573 }; |
webmaster@1
|
574 |
webmaster@1
|
575 /** |
webmaster@1
|
576 * After the row is dropped, update the table fields according to the settings |
webmaster@1
|
577 * set for this table. |
webmaster@1
|
578 * |
webmaster@1
|
579 * @param changedRow |
webmaster@1
|
580 * DOM object for the row that was just dropped. |
webmaster@1
|
581 */ |
webmaster@1
|
582 Drupal.tableDrag.prototype.updateFields = function(changedRow) { |
webmaster@1
|
583 for (var group in this.tableSettings) { |
webmaster@1
|
584 // Each group may have a different setting for relationship, so we find |
webmaster@1
|
585 // the source rows for each seperately. |
webmaster@1
|
586 this.updateField(changedRow, group); |
webmaster@1
|
587 } |
webmaster@1
|
588 }; |
webmaster@1
|
589 |
webmaster@1
|
590 /** |
webmaster@1
|
591 * After the row is dropped, update a single table field according to specific |
webmaster@1
|
592 * settings. |
webmaster@1
|
593 * |
webmaster@1
|
594 * @param changedRow |
webmaster@1
|
595 * DOM object for the row that was just dropped. |
webmaster@1
|
596 * @param group |
webmaster@1
|
597 * The settings group on which field updates will occur. |
webmaster@1
|
598 */ |
webmaster@1
|
599 Drupal.tableDrag.prototype.updateField = function(changedRow, group) { |
webmaster@1
|
600 var rowSettings = this.rowSettings(group, changedRow); |
webmaster@1
|
601 |
webmaster@1
|
602 // Set the row as it's own target. |
webmaster@1
|
603 if (rowSettings.relationship == 'self' || rowSettings.relationship == 'group') { |
webmaster@1
|
604 var sourceRow = changedRow; |
webmaster@1
|
605 } |
webmaster@1
|
606 // Siblings are easy, check previous and next rows. |
webmaster@1
|
607 else if (rowSettings.relationship == 'sibling') { |
webmaster@1
|
608 var previousRow = $(changedRow).prev('tr').get(0); |
webmaster@1
|
609 var nextRow = $(changedRow).next('tr').get(0); |
webmaster@1
|
610 var sourceRow = changedRow; |
webmaster@1
|
611 if ($(previousRow).is('.draggable') && $('.' + group, previousRow).length) { |
webmaster@1
|
612 if (this.indentEnabled) { |
webmaster@1
|
613 if ($('.indentations', previousRow).size() == $('.indentations', changedRow)) { |
webmaster@1
|
614 sourceRow = previousRow; |
webmaster@1
|
615 } |
webmaster@1
|
616 } |
webmaster@1
|
617 else { |
webmaster@1
|
618 sourceRow = previousRow; |
webmaster@1
|
619 } |
webmaster@1
|
620 } |
webmaster@1
|
621 else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) { |
webmaster@1
|
622 if (this.indentEnabled) { |
webmaster@1
|
623 if ($('.indentations', nextRow).size() == $('.indentations', changedRow)) { |
webmaster@1
|
624 sourceRow = nextRow; |
webmaster@1
|
625 } |
webmaster@1
|
626 } |
webmaster@1
|
627 else { |
webmaster@1
|
628 sourceRow = nextRow; |
webmaster@1
|
629 } |
webmaster@1
|
630 } |
webmaster@1
|
631 } |
webmaster@1
|
632 // Parents, look up the tree until we find a field not in this group. |
webmaster@1
|
633 // Go up as many parents as indentations in the changed row. |
webmaster@1
|
634 else if (rowSettings.relationship == 'parent') { |
webmaster@1
|
635 var previousRow = $(changedRow).prev('tr'); |
webmaster@1
|
636 while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) { |
webmaster@1
|
637 previousRow = previousRow.prev('tr'); |
webmaster@1
|
638 } |
webmaster@1
|
639 // If we found a row. |
webmaster@1
|
640 if (previousRow.length) { |
webmaster@1
|
641 sourceRow = previousRow[0]; |
webmaster@1
|
642 } |
webmaster@1
|
643 // Otherwise we went all the way to the left of the table without finding |
webmaster@1
|
644 // a parent, meaning this item has been placed at the root level. |
webmaster@1
|
645 else { |
webmaster@1
|
646 // Use the first row in the table as source, because it's garanteed to |
webmaster@1
|
647 // be at the root level. Find the first item, then compare this row |
webmaster@1
|
648 // against it as a sibling. |
webmaster@1
|
649 sourceRow = $('tr.draggable:first').get(0); |
webmaster@1
|
650 if (sourceRow == this.rowObject.element) { |
webmaster@1
|
651 sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0); |
webmaster@1
|
652 } |
webmaster@1
|
653 var useSibling = true; |
webmaster@1
|
654 } |
webmaster@1
|
655 } |
webmaster@1
|
656 |
webmaster@1
|
657 // Because we may have moved the row from one category to another, |
webmaster@1
|
658 // take a look at our sibling and borrow its sources and targets. |
webmaster@1
|
659 this.copyDragClasses(sourceRow, changedRow, group); |
webmaster@1
|
660 rowSettings = this.rowSettings(group, changedRow); |
webmaster@1
|
661 |
webmaster@1
|
662 // In the case that we're looking for a parent, but the row is at the top |
webmaster@1
|
663 // of the tree, copy our sibling's values. |
webmaster@1
|
664 if (useSibling) { |
webmaster@1
|
665 rowSettings.relationship = 'sibling'; |
webmaster@1
|
666 rowSettings.source = rowSettings.target; |
webmaster@1
|
667 } |
webmaster@1
|
668 |
webmaster@1
|
669 var targetClass = '.' + rowSettings.target; |
webmaster@1
|
670 var targetElement = $(targetClass, changedRow).get(0); |
webmaster@1
|
671 |
webmaster@1
|
672 // Check if a target element exists in this row. |
webmaster@1
|
673 if (targetElement) { |
webmaster@1
|
674 var sourceClass = '.' + rowSettings.source; |
webmaster@1
|
675 var sourceElement = $(sourceClass, sourceRow).get(0); |
webmaster@1
|
676 switch (rowSettings.action) { |
webmaster@1
|
677 case 'depth': |
webmaster@1
|
678 // Get the depth of the target row. |
webmaster@1
|
679 targetElement.value = $('.indentation', $(sourceElement).parents('tr:first')).size(); |
webmaster@1
|
680 break; |
webmaster@1
|
681 case 'match': |
webmaster@1
|
682 // Update the value. |
webmaster@1
|
683 targetElement.value = sourceElement.value; |
webmaster@1
|
684 break; |
webmaster@1
|
685 case 'order': |
webmaster@1
|
686 var siblings = this.rowObject.findSiblings(rowSettings); |
webmaster@1
|
687 if ($(targetElement).is('select')) { |
webmaster@1
|
688 // Get a list of acceptable values. |
webmaster@1
|
689 var values = new Array(); |
webmaster@1
|
690 $('option', targetElement).each(function() { |
webmaster@1
|
691 values.push(this.value); |
webmaster@1
|
692 }); |
webmaster@1
|
693 var maxVal = values[values.length - 1]; |
webmaster@1
|
694 // Populate the values in the siblings. |
webmaster@1
|
695 $(targetClass, siblings).each(function() { |
webmaster@1
|
696 // If there are more items than possible values, assign the maximum value to the row. |
webmaster@1
|
697 if (values.length > 0) { |
webmaster@1
|
698 this.value = values.shift(); |
webmaster@1
|
699 } |
webmaster@1
|
700 else { |
webmaster@1
|
701 this.value = maxVal; |
webmaster@1
|
702 } |
webmaster@1
|
703 }); |
webmaster@1
|
704 } |
webmaster@1
|
705 else { |
webmaster@1
|
706 // Assume a numeric input field. |
webmaster@1
|
707 var weight = parseInt($(targetClass, siblings[0]).val()) || 0; |
webmaster@1
|
708 $(targetClass, siblings).each(function() { |
webmaster@1
|
709 this.value = weight; |
webmaster@1
|
710 weight++; |
webmaster@1
|
711 }); |
webmaster@1
|
712 } |
webmaster@1
|
713 break; |
webmaster@1
|
714 } |
webmaster@1
|
715 } |
webmaster@1
|
716 }; |
webmaster@1
|
717 |
webmaster@1
|
718 /** |
webmaster@1
|
719 * Copy all special tableDrag classes from one row's form elements to a |
webmaster@1
|
720 * different one, removing any special classes that the destination row |
webmaster@1
|
721 * may have had. |
webmaster@1
|
722 */ |
webmaster@1
|
723 Drupal.tableDrag.prototype.copyDragClasses = function(sourceRow, targetRow, group) { |
webmaster@1
|
724 var sourceElement = $('.' + group, sourceRow); |
webmaster@1
|
725 var targetElement = $('.' + group, targetRow); |
webmaster@1
|
726 if (sourceElement.length && targetElement.length) { |
webmaster@1
|
727 targetElement[0].className = sourceElement[0].className; |
webmaster@1
|
728 } |
webmaster@1
|
729 }; |
webmaster@1
|
730 |
webmaster@1
|
731 Drupal.tableDrag.prototype.checkScroll = function(cursorY) { |
webmaster@1
|
732 var de = document.documentElement; |
webmaster@1
|
733 var b = document.body; |
webmaster@1
|
734 |
webmaster@1
|
735 var windowHeight = this.windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth != 0 ? de.clientHeight : b.offsetHeight); |
webmaster@1
|
736 var scrollY = this.scrollY = (document.all ? (!de.scrollTop ? b.scrollTop : de.scrollTop) : (window.pageYOffset ? window.pageYOffset : window.scrollY)); |
webmaster@1
|
737 var trigger = this.scrollSettings.trigger; |
webmaster@1
|
738 var delta = 0; |
webmaster@1
|
739 |
webmaster@1
|
740 // Return a scroll speed relative to the edge of the screen. |
webmaster@1
|
741 if (cursorY - scrollY > windowHeight - trigger) { |
webmaster@1
|
742 delta = trigger / (windowHeight + scrollY - cursorY); |
webmaster@1
|
743 delta = (delta > 0 && delta < trigger) ? delta : trigger; |
webmaster@1
|
744 return delta * this.scrollSettings.amount; |
webmaster@1
|
745 } |
webmaster@1
|
746 else if (cursorY - scrollY < trigger) { |
webmaster@1
|
747 delta = trigger / (cursorY - scrollY); |
webmaster@1
|
748 delta = (delta > 0 && delta < trigger) ? delta : trigger; |
webmaster@1
|
749 return -delta * this.scrollSettings.amount; |
webmaster@1
|
750 } |
webmaster@1
|
751 }; |
webmaster@1
|
752 |
webmaster@1
|
753 Drupal.tableDrag.prototype.setScroll = function(scrollAmount) { |
webmaster@1
|
754 var self = this; |
webmaster@1
|
755 |
webmaster@1
|
756 this.scrollInterval = setInterval(function() { |
webmaster@1
|
757 // Update the scroll values stored in the object. |
webmaster@1
|
758 self.checkScroll(self.currentMouseCoords.y); |
webmaster@1
|
759 var aboveTable = self.scrollY > self.table.topY; |
webmaster@1
|
760 var belowTable = self.scrollY + self.windowHeight < self.table.bottomY; |
webmaster@1
|
761 if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) { |
webmaster@1
|
762 window.scrollBy(0, scrollAmount); |
webmaster@1
|
763 } |
webmaster@1
|
764 }, this.scrollSettings.interval); |
webmaster@1
|
765 }; |
webmaster@1
|
766 |
webmaster@1
|
767 Drupal.tableDrag.prototype.restripeTable = function() { |
webmaster@1
|
768 // :even and :odd are reversed because jquery counts from 0 and |
webmaster@1
|
769 // we count from 1, so we're out of sync. |
webmaster@1
|
770 $('tr.draggable', this.table) |
webmaster@1
|
771 .filter(':odd').filter('.odd') |
webmaster@1
|
772 .removeClass('odd').addClass('even') |
webmaster@1
|
773 .end().end() |
webmaster@1
|
774 .filter(':even').filter('.even') |
webmaster@1
|
775 .removeClass('even').addClass('odd'); |
webmaster@1
|
776 }; |
webmaster@1
|
777 |
webmaster@1
|
778 /** |
webmaster@1
|
779 * Stub function. Allows a custom handler when a row begins dragging. |
webmaster@1
|
780 */ |
webmaster@1
|
781 Drupal.tableDrag.prototype.onDrag = function() { |
webmaster@1
|
782 return null; |
webmaster@1
|
783 }; |
webmaster@1
|
784 |
webmaster@1
|
785 /** |
webmaster@1
|
786 * Stub function. Allows a custom handler when a row is dropped. |
webmaster@1
|
787 */ |
webmaster@1
|
788 Drupal.tableDrag.prototype.onDrop = function() { |
webmaster@1
|
789 return null; |
webmaster@1
|
790 }; |
webmaster@1
|
791 |
webmaster@1
|
792 /** |
webmaster@1
|
793 * Constructor to make a new object to manipulate a table row. |
webmaster@1
|
794 * |
webmaster@1
|
795 * @param tableRow |
webmaster@1
|
796 * The DOM element for the table row we will be manipulating. |
webmaster@1
|
797 * @param method |
webmaster@1
|
798 * The method in which this row is being moved. Either 'keyboard' or 'mouse'. |
webmaster@1
|
799 * @param indentEnabled |
webmaster@1
|
800 * Whether the containing table uses indentations. Used for optimizations. |
webmaster@1
|
801 * @param maxDepth |
webmaster@1
|
802 * The maximum amount of indentations this row may contain. |
webmaster@1
|
803 * @param addClasses |
webmaster@1
|
804 * Whether we want to add classes to this row to indicate child relationships. |
webmaster@1
|
805 */ |
webmaster@1
|
806 Drupal.tableDrag.prototype.row = function(tableRow, method, indentEnabled, maxDepth, addClasses) { |
webmaster@1
|
807 this.element = tableRow; |
webmaster@1
|
808 this.method = method; |
webmaster@1
|
809 this.group = new Array(tableRow); |
webmaster@1
|
810 this.groupDepth = $('.indentation', tableRow).size(); |
webmaster@1
|
811 this.changed = false; |
webmaster@1
|
812 this.table = $(tableRow).parents('table:first').get(0); |
webmaster@1
|
813 this.indentEnabled = indentEnabled; |
webmaster@1
|
814 this.maxDepth = maxDepth; |
webmaster@1
|
815 this.direction = ''; // Direction the row is being moved. |
webmaster@1
|
816 |
webmaster@1
|
817 if (this.indentEnabled) { |
webmaster@1
|
818 this.indents = $('.indentation', tableRow).size(); |
webmaster@1
|
819 this.children = this.findChildren(addClasses); |
webmaster@1
|
820 this.group = $.merge(this.group, this.children); |
webmaster@1
|
821 // Find the depth of this entire group. |
webmaster@1
|
822 for (var n = 0; n < this.group.length; n++) { |
webmaster@1
|
823 this.groupDepth = Math.max($('.indentation', this.group[n]).size(), this.groupDepth); |
webmaster@1
|
824 } |
webmaster@1
|
825 } |
webmaster@1
|
826 }; |
webmaster@1
|
827 |
webmaster@1
|
828 /** |
webmaster@1
|
829 * Find all children of rowObject by indentation. |
webmaster@1
|
830 * |
webmaster@1
|
831 * @param addClasses |
webmaster@1
|
832 * Whether we want to add classes to this row to indicate child relationships. |
webmaster@1
|
833 */ |
webmaster@1
|
834 Drupal.tableDrag.prototype.row.prototype.findChildren = function(addClasses) { |
webmaster@1
|
835 var parentIndentation = this.indents; |
webmaster@1
|
836 var currentRow = $(this.element, this.table).next('tr.draggable'); |
webmaster@1
|
837 var rows = new Array(); |
webmaster@1
|
838 var child = 0; |
webmaster@1
|
839 while (currentRow.length) { |
webmaster@1
|
840 var rowIndentation = $('.indentation', currentRow).length; |
webmaster@1
|
841 // A greater indentation indicates this is a child. |
webmaster@1
|
842 if (rowIndentation > parentIndentation) { |
webmaster@1
|
843 child++; |
webmaster@1
|
844 rows.push(currentRow[0]); |
webmaster@1
|
845 if (addClasses) { |
webmaster@1
|
846 $('.indentation', currentRow).each(function(indentNum) { |
webmaster@1
|
847 if (child == 1 && (indentNum == parentIndentation)) { |
webmaster@1
|
848 $(this).addClass('tree-child-first'); |
webmaster@1
|
849 } |
webmaster@1
|
850 if (indentNum == parentIndentation) { |
webmaster@1
|
851 $(this).addClass('tree-child'); |
webmaster@1
|
852 } |
webmaster@1
|
853 else if (indentNum > parentIndentation) { |
webmaster@1
|
854 $(this).addClass('tree-child-horizontal'); |
webmaster@1
|
855 } |
webmaster@1
|
856 }); |
webmaster@1
|
857 } |
webmaster@1
|
858 } |
webmaster@1
|
859 else { |
webmaster@1
|
860 break; |
webmaster@1
|
861 } |
webmaster@1
|
862 currentRow = currentRow.next('tr.draggable'); |
webmaster@1
|
863 } |
webmaster@1
|
864 if (addClasses && rows.length) { |
webmaster@1
|
865 $('.indentation:nth-child(' + (parentIndentation + 1) + ')', rows[rows.length - 1]).addClass('tree-child-last'); |
webmaster@1
|
866 } |
webmaster@1
|
867 return rows; |
webmaster@1
|
868 }; |
webmaster@1
|
869 |
webmaster@1
|
870 /** |
webmaster@1
|
871 * Ensure that two rows are allowed to be swapped. |
webmaster@1
|
872 * |
webmaster@1
|
873 * @param row |
webmaster@1
|
874 * DOM object for the row being considered for swapping. |
webmaster@1
|
875 */ |
webmaster@1
|
876 Drupal.tableDrag.prototype.row.prototype.isValidSwap = function(row) { |
webmaster@1
|
877 if (this.indentEnabled) { |
webmaster@1
|
878 var prevRow, nextRow; |
webmaster@1
|
879 if (this.direction == 'down') { |
webmaster@1
|
880 prevRow = row; |
webmaster@1
|
881 nextRow = $(row).next('tr').get(0); |
webmaster@1
|
882 } |
webmaster@1
|
883 else { |
webmaster@1
|
884 prevRow = $(row).prev('tr').get(0); |
webmaster@1
|
885 nextRow = row; |
webmaster@1
|
886 } |
webmaster@1
|
887 this.interval = this.validIndentInterval(prevRow, nextRow); |
webmaster@1
|
888 |
webmaster@1
|
889 // We have an invalid swap if the valid indentations interval is empty. |
webmaster@1
|
890 if (this.interval.min > this.interval.max) { |
webmaster@1
|
891 return false; |
webmaster@1
|
892 } |
webmaster@1
|
893 } |
webmaster@1
|
894 |
webmaster@1
|
895 // Do not let an un-draggable first row have anything put before it. |
webmaster@1
|
896 if (this.table.tBodies[0].rows[0] == row && $(row).is(':not(.draggable)')) { |
webmaster@1
|
897 return false; |
webmaster@1
|
898 } |
webmaster@1
|
899 |
webmaster@1
|
900 return true; |
webmaster@1
|
901 }; |
webmaster@1
|
902 |
webmaster@1
|
903 /** |
webmaster@1
|
904 * Perform the swap between two rows. |
webmaster@1
|
905 * |
webmaster@1
|
906 * @param position |
webmaster@1
|
907 * Whether the swap will occur 'before' or 'after' the given row. |
webmaster@1
|
908 * @param row |
webmaster@1
|
909 * DOM element what will be swapped with the row group. |
webmaster@1
|
910 */ |
webmaster@1
|
911 Drupal.tableDrag.prototype.row.prototype.swap = function(position, row) { |
webmaster@1
|
912 $(row)[position](this.group); |
webmaster@1
|
913 this.changed = true; |
webmaster@1
|
914 this.onSwap(row); |
webmaster@1
|
915 }; |
webmaster@1
|
916 |
webmaster@1
|
917 /** |
webmaster@1
|
918 * Determine the valid indentations interval for the row at a given position |
webmaster@1
|
919 * in the table. |
webmaster@1
|
920 * |
webmaster@1
|
921 * @param prevRow |
webmaster@1
|
922 * DOM object for the row before the tested position |
webmaster@1
|
923 * (or null for first position in the table). |
webmaster@1
|
924 * @param nextRow |
webmaster@1
|
925 * DOM object for the row after the tested position |
webmaster@1
|
926 * (or null for last position in the table). |
webmaster@1
|
927 */ |
webmaster@1
|
928 Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) { |
webmaster@1
|
929 var minIndent, maxIndent; |
webmaster@1
|
930 |
webmaster@1
|
931 // Minimum indentation: |
webmaster@1
|
932 // Do not orphan the next row. |
webmaster@1
|
933 minIndent = nextRow ? $('.indentation', nextRow).size() : 0; |
webmaster@1
|
934 |
webmaster@1
|
935 // Maximum indentation: |
webmaster@1
|
936 if (!prevRow || $(this.element).is('.tabledrag-root')) { |
webmaster@1
|
937 // Do not indent the first row in the table or 'root' rows.. |
webmaster@1
|
938 maxIndent = 0; |
webmaster@1
|
939 } |
webmaster@1
|
940 else { |
webmaster@1
|
941 // Do not go deeper than as a child of the previous row. |
webmaster@1
|
942 maxIndent = $('.indentation', prevRow).size() + ($(prevRow).is('.tabledrag-leaf') ? 0 : 1); |
webmaster@1
|
943 // Limit by the maximum allowed depth for the table. |
webmaster@1
|
944 if (this.maxDepth) { |
webmaster@1
|
945 maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents)); |
webmaster@1
|
946 } |
webmaster@1
|
947 } |
webmaster@1
|
948 |
webmaster@1
|
949 return {'min':minIndent, 'max':maxIndent}; |
webmaster@1
|
950 } |
webmaster@1
|
951 |
webmaster@1
|
952 /** |
webmaster@1
|
953 * Indent a row within the legal bounds of the table. |
webmaster@1
|
954 * |
webmaster@1
|
955 * @param indentDiff |
webmaster@1
|
956 * The number of additional indentations proposed for the row (can be |
webmaster@1
|
957 * positive or negative). This number will be adjusted to nearest valid |
webmaster@1
|
958 * indentation level for the row. |
webmaster@1
|
959 */ |
webmaster@1
|
960 Drupal.tableDrag.prototype.row.prototype.indent = function(indentDiff) { |
webmaster@1
|
961 // Determine the valid indentations interval if not available yet. |
webmaster@1
|
962 if (!this.interval) { |
webmaster@1
|
963 prevRow = $(this.element).prev('tr').get(0); |
webmaster@1
|
964 nextRow = $(this.group).filter(':last').next('tr').get(0); |
webmaster@1
|
965 this.interval = this.validIndentInterval(prevRow, nextRow); |
webmaster@1
|
966 } |
webmaster@1
|
967 |
webmaster@1
|
968 // Adjust to the nearest valid indentation. |
webmaster@1
|
969 var indent = this.indents + indentDiff; |
webmaster@1
|
970 indent = Math.max(indent, this.interval.min); |
webmaster@1
|
971 indent = Math.min(indent, this.interval.max); |
webmaster@1
|
972 indentDiff = indent - this.indents; |
webmaster@1
|
973 |
webmaster@1
|
974 for (var n = 1; n <= Math.abs(indentDiff); n++) { |
webmaster@1
|
975 // Add or remove indentations. |
webmaster@1
|
976 if (indentDiff < 0) { |
webmaster@1
|
977 $('.indentation:first', this.group).remove(); |
webmaster@1
|
978 this.indents--; |
webmaster@1
|
979 } |
webmaster@1
|
980 else { |
webmaster@1
|
981 $('td:first', this.group).prepend(Drupal.theme('tableDragIndentation')); |
webmaster@1
|
982 this.indents++; |
webmaster@1
|
983 } |
webmaster@1
|
984 } |
webmaster@1
|
985 if (indentDiff) { |
webmaster@1
|
986 // Update indentation for this row. |
webmaster@1
|
987 this.changed = true; |
webmaster@1
|
988 this.groupDepth += indentDiff; |
webmaster@1
|
989 this.onIndent(); |
webmaster@1
|
990 } |
webmaster@1
|
991 |
webmaster@1
|
992 return indentDiff; |
webmaster@1
|
993 }; |
webmaster@1
|
994 |
webmaster@1
|
995 /** |
webmaster@1
|
996 * Find all siblings for a row, either according to its subgroup or indentation. |
webmaster@1
|
997 * Note that the passed in row is included in the list of siblings. |
webmaster@1
|
998 * |
webmaster@1
|
999 * @param settings |
webmaster@1
|
1000 * The field settings we're using to identify what constitutes a sibling. |
webmaster@1
|
1001 */ |
webmaster@1
|
1002 Drupal.tableDrag.prototype.row.prototype.findSiblings = function(rowSettings) { |
webmaster@1
|
1003 var siblings = new Array(); |
webmaster@1
|
1004 var directions = new Array('prev', 'next'); |
webmaster@1
|
1005 var rowIndentation = this.indents; |
webmaster@1
|
1006 for (var d in directions) { |
webmaster@1
|
1007 var checkRow = $(this.element)[directions[d]](); |
webmaster@1
|
1008 while (checkRow.length) { |
webmaster@1
|
1009 // Check that the sibling contains a similar target field. |
webmaster@1
|
1010 if ($('.' + rowSettings.target, checkRow)) { |
webmaster@1
|
1011 // Either add immediately if this is a flat table, or check to ensure |
webmaster@1
|
1012 // that this row has the same level of indentaiton. |
webmaster@1
|
1013 if (this.indentEnabled) { |
webmaster@1
|
1014 var checkRowIndentation = $('.indentation', checkRow).length |
webmaster@1
|
1015 } |
webmaster@1
|
1016 |
webmaster@1
|
1017 if (!(this.indentEnabled) || (checkRowIndentation == rowIndentation)) { |
webmaster@1
|
1018 siblings.push(checkRow[0]); |
webmaster@1
|
1019 } |
webmaster@1
|
1020 else if (checkRowIndentation < rowIndentation) { |
webmaster@1
|
1021 // No need to keep looking for siblings when we get to a parent. |
webmaster@1
|
1022 break; |
webmaster@1
|
1023 } |
webmaster@1
|
1024 } |
webmaster@1
|
1025 else { |
webmaster@1
|
1026 break; |
webmaster@1
|
1027 } |
webmaster@1
|
1028 checkRow = $(checkRow)[directions[d]](); |
webmaster@1
|
1029 } |
webmaster@1
|
1030 // Since siblings are added in reverse order for previous, reverse the |
webmaster@1
|
1031 // completed list of previous siblings. Add the current row and continue. |
webmaster@1
|
1032 if (directions[d] == 'prev') { |
webmaster@1
|
1033 siblings.reverse(); |
webmaster@1
|
1034 siblings.push(this.element); |
webmaster@1
|
1035 } |
webmaster@1
|
1036 } |
webmaster@1
|
1037 return siblings; |
webmaster@1
|
1038 }; |
webmaster@1
|
1039 |
webmaster@1
|
1040 /** |
webmaster@1
|
1041 * Remove indentation helper classes from the current row group. |
webmaster@1
|
1042 */ |
webmaster@1
|
1043 Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function() { |
webmaster@1
|
1044 for (n in this.children) { |
webmaster@1
|
1045 $('.indentation', this.children[n]) |
webmaster@1
|
1046 .removeClass('tree-child') |
webmaster@1
|
1047 .removeClass('tree-child-first') |
webmaster@1
|
1048 .removeClass('tree-child-last') |
webmaster@1
|
1049 .removeClass('tree-child-horizontal'); |
webmaster@1
|
1050 } |
webmaster@1
|
1051 }; |
webmaster@1
|
1052 |
webmaster@1
|
1053 /** |
webmaster@1
|
1054 * Add an asterisk or other marker to the changed row. |
webmaster@1
|
1055 */ |
webmaster@1
|
1056 Drupal.tableDrag.prototype.row.prototype.markChanged = function() { |
webmaster@1
|
1057 var marker = Drupal.theme('tableDragChangedMarker'); |
webmaster@1
|
1058 var cell = $('td:first', this.element); |
webmaster@1
|
1059 if ($('span.tabledrag-changed', cell).length == 0) { |
webmaster@1
|
1060 cell.append(marker); |
webmaster@1
|
1061 } |
webmaster@1
|
1062 }; |
webmaster@1
|
1063 |
webmaster@1
|
1064 /** |
webmaster@1
|
1065 * Stub function. Allows a custom handler when a row is indented. |
webmaster@1
|
1066 */ |
webmaster@1
|
1067 Drupal.tableDrag.prototype.row.prototype.onIndent = function() { |
webmaster@1
|
1068 return null; |
webmaster@1
|
1069 }; |
webmaster@1
|
1070 |
webmaster@1
|
1071 /** |
webmaster@1
|
1072 * Stub function. Allows a custom handler when a row is swapped. |
webmaster@1
|
1073 */ |
webmaster@1
|
1074 Drupal.tableDrag.prototype.row.prototype.onSwap = function(swappedRow) { |
webmaster@1
|
1075 return null; |
webmaster@1
|
1076 }; |
webmaster@1
|
1077 |
webmaster@1
|
1078 Drupal.theme.prototype.tableDragChangedMarker = function () { |
webmaster@1
|
1079 return '<span class="warning tabledrag-changed">*</span>'; |
webmaster@1
|
1080 }; |
webmaster@1
|
1081 |
webmaster@1
|
1082 Drupal.theme.prototype.tableDragIndentation = function () { |
webmaster@1
|
1083 return '<div class="indentation"> </div>'; |
webmaster@1
|
1084 }; |
webmaster@1
|
1085 |
webmaster@1
|
1086 Drupal.theme.prototype.tableDragChangedWarning = function () { |
webmaster@1
|
1087 return '<div class="warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t("Changes made in this table will not be saved until the form is submitted.") + '</div>'; |
webmaster@1
|
1088 }; |