nodeformcols.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Move a field in the fields table from one region to another via select list.
  3. *
  4. * This behavior is dependent on the tableDrag behavior, since it uses the
  5. * objects initialized in that behavior to update the row.
  6. */
  7. (function($) {
  8. Drupal.behaviors.fieldDrag = {};
  9. Drupal.behaviors.fieldDrag.attach = function(context) {
  10. var table = $('table#fields');
  11. var tableDrag = Drupal.tableDrag.fields; // Get the fields tableDrag object.
  12. // Add a handler for when a row is swapped, update empty regions.
  13. tableDrag.row.prototype.onSwap = function(swappedRow) {
  14. checkEmptyRegions(table, this);
  15. };
  16. // Add a handler so when a row is dropped, update fields dropped into new regions.
  17. tableDrag.onDrop = function() {
  18. dragObject = this;
  19. if ($(dragObject.rowObject.element).prev('tr').is('.region-message')) {
  20. var regionRow = $(dragObject.rowObject.element).prev('tr').get(0);
  21. var regionName = regionRow.className.replace(/([^ ]+[ ]+)*region-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
  22. var regionField = $('select.field-region-select', dragObject.rowObject.element);
  23. var weightField = $('input.field-weight', dragObject.rowObject.element);
  24. var oldRegionName = weightField[0].className.replace(/([^ ]+[ ]+)*field-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
  25. if (!regionField.is('.field-region-'+ regionName)) {
  26. regionField.removeClass('field-region-' + oldRegionName).addClass('field-region-' + regionName);
  27. weightField.removeClass('field-weight-' + oldRegionName).addClass('field-weight-' + regionName);
  28. regionField.val(regionName);
  29. }
  30. }
  31. };
  32. // Add the behavior to each region select list.
  33. $('select.field-region-select:not(.fieldregionselect-processed)', context).each(function() {
  34. $(this).change(function(event) {
  35. // Make our new row and select field.
  36. var row = $(this).parents('tr:first');
  37. var select = $(this);
  38. tableDrag.rowObject = new tableDrag.row(row);
  39. // Find the correct region and insert the row as the first in the region.
  40. $('tr.region-message', table).each(function() {
  41. if ($(this).is('.region-' + select[0].value + '-message')) {
  42. // Add the new row and remove the old one.
  43. $(this).after(row);
  44. // Manually update weights and restripe.
  45. tableDrag.updateFields(row.get(0));
  46. tableDrag.rowObject.changed = true;
  47. if (tableDrag.oldRowElement) {
  48. $(tableDrag.oldRowElement).removeClass('drag-previous');
  49. }
  50. tableDrag.oldRowElement = row.get(0);
  51. tableDrag.restripeTable();
  52. tableDrag.rowObject.markChanged();
  53. tableDrag.oldRowElement = row;
  54. $(row).addClass('drag-previous');
  55. }
  56. });
  57. // Modify empty regions with added or removed fields.
  58. checkEmptyRegions(table, row);
  59. // Remove focus from selectbox.
  60. select.get(0).blur();
  61. });
  62. $(this).addClass('fieldregionselect-processed');
  63. });
  64. var checkEmptyRegions = function(table, rowObject) {
  65. $('tr.region-message', table).each(function() {
  66. // If the dragged row is in this region, but above the message row, swap it down one space.
  67. if ($(this).prev('tr').get(0) == rowObject.element) {
  68. // Prevent a recursion problem when using the keyboard to move rows up.
  69. if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
  70. rowObject.swap('after', this);
  71. }
  72. }
  73. // This region has become empty
  74. if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').size() == 0) {
  75. $(this).removeClass('region-populated').addClass('region-empty');
  76. }
  77. // This region has become populated.
  78. else if ($(this).is('.region-empty')) {
  79. $(this).removeClass('region-empty').addClass('region-populated');
  80. }
  81. });
  82. };
  83. };
  84. } (jQuery));