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