profile.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. (function ($) {
  2. /**
  3. * Add functionality to the profile drag and drop table.
  4. *
  5. * This behavior is dependent on the tableDrag behavior, since it uses the
  6. * objects initialized in that behavior to update the row. It shows and hides
  7. * a warning message when removing the last field from a profile category.
  8. */
  9. Drupal.behaviors.profileDrag = {
  10. attach: function (context, settings) {
  11. var table = $('#profile-fields');
  12. var tableDrag = Drupal.tableDrag['profile-fields']; // Get the profile tableDrag object.
  13. // Add a handler for when a row is swapped, update empty categories.
  14. tableDrag.row.prototype.onSwap = function (swappedRow) {
  15. var rowObject = this;
  16. $('tr.category-message', table).each(function () {
  17. // If the dragged row is in this category, but above the message row, swap it down one space.
  18. if ($(this).prev('tr').get(0) == rowObject.element) {
  19. // Prevent a recursion problem when using the keyboard to move rows up.
  20. if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
  21. rowObject.swap('after', this);
  22. }
  23. }
  24. // This category has become empty
  25. if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').length == 0) {
  26. $(this).removeClass('category-populated').addClass('category-empty');
  27. }
  28. // This category has become populated.
  29. else if ($(this).is('.category-empty')) {
  30. $(this).removeClass('category-empty').addClass('category-populated');
  31. }
  32. });
  33. };
  34. // Add a handler so when a row is dropped, update fields dropped into new categories.
  35. tableDrag.onDrop = function () {
  36. dragObject = this;
  37. if ($(dragObject.rowObject.element).prev('tr').is('.category-message')) {
  38. var categoryRow = $(dragObject.rowObject.element).prev('tr').get(0);
  39. var categoryNum = categoryRow.className.replace(/([^ ]+[ ]+)*category-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
  40. var categoryField = $('select.profile-category', dragObject.rowObject.element);
  41. var weightField = $('select.profile-weight', dragObject.rowObject.element);
  42. var oldcategoryNum = weightField[0].className.replace(/([^ ]+[ ]+)*profile-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
  43. if (!categoryField.is('.profile-category-' + categoryNum)) {
  44. categoryField.removeClass('profile-category-' + oldcategoryNum).addClass('profile-category-' + categoryNum);
  45. weightField.removeClass('profile-weight-' + oldcategoryNum).addClass('profile-weight-' + categoryNum);
  46. categoryField.val(categoryField[0].options[categoryNum].value);
  47. }
  48. }
  49. };
  50. }
  51. };
  52. })(jQuery);