shortcut.admin.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (function ($) {
  2. /**
  3. * Handle the concept of a fixed number of slots.
  4. *
  5. * This behavior is dependent on the tableDrag behavior, since it uses the
  6. * objects initialized in that behavior to update the row.
  7. */
  8. Drupal.behaviors.shortcutDrag = {
  9. attach: function (context, settings) {
  10. if (Drupal.tableDrag) {
  11. var table = $('table#shortcuts'),
  12. visibleLength = 0,
  13. slots = 0,
  14. tableDrag = Drupal.tableDrag.shortcuts;
  15. $('> tbody > tr, > tr', table)
  16. .filter(':visible')
  17. .filter(':odd').filter('.odd')
  18. .removeClass('odd').addClass('even')
  19. .end().end()
  20. .filter(':even').filter('.even')
  21. .removeClass('even').addClass('odd')
  22. .end().end()
  23. .end()
  24. .filter('.shortcut-slot-empty').each(function(index) {
  25. if ($(this).is(':visible')) {
  26. visibleLength++;
  27. }
  28. slots++;
  29. });
  30. // Add a handler for when a row is swapped.
  31. tableDrag.row.prototype.onSwap = function (swappedRow) {
  32. var disabledIndex = $(table).find('tr').index($(table).find('tr.shortcut-status-disabled')) - slots - 2,
  33. count = 0;
  34. $(table).find('tr.shortcut-status-enabled').nextAll(':not(.shortcut-slot-empty)').each(function(index) {
  35. if (index < disabledIndex) {
  36. count++;
  37. }
  38. });
  39. var total = slots - count;
  40. if (total == -1) {
  41. var disabled = $(table).find('tr.shortcut-status-disabled');
  42. // To maintain the shortcut links limit, we need to move the last
  43. // element from the enabled section to the disabled section.
  44. var changedRow = disabled.prevAll(':not(.shortcut-slot-empty)').not($(this.element)).get(0);
  45. disabled.after(changedRow);
  46. if ($(changedRow).hasClass('draggable')) {
  47. // The dropped element will automatically be marked as changed by
  48. // the tableDrag system. However, the row that swapped with it
  49. // has moved to the "disabled" section, so we need to force its
  50. // status to be disabled and mark it also as changed.
  51. var changedRowObject = new tableDrag.row(changedRow, 'mouse', false, 0, true);
  52. changedRowObject.markChanged();
  53. tableDrag.rowStatusChange(changedRowObject);
  54. }
  55. }
  56. else if (total != visibleLength) {
  57. if (total > visibleLength) {
  58. // Less slots on screen than needed.
  59. $('.shortcut-slot-empty:hidden:last').show();
  60. visibleLength++;
  61. }
  62. else {
  63. // More slots on screen than needed.
  64. $('.shortcut-slot-empty:visible:last').hide();
  65. visibleLength--;
  66. }
  67. }
  68. };
  69. // Add a handler so when a row is dropped, update fields dropped into new regions.
  70. tableDrag.onDrop = function () {
  71. tableDrag.rowStatusChange(this.rowObject);
  72. return true;
  73. };
  74. tableDrag.rowStatusChange = function (rowObject) {
  75. // Use "status-message" row instead of "status" row because
  76. // "status-{status_name}-message" is less prone to regexp match errors.
  77. var statusRow = $(rowObject.element).prevAll('tr.shortcut-status').get(0);
  78. var statusName = statusRow.className.replace(/([^ ]+[ ]+)*shortcut-status-([^ ]+)([ ]+[^ ]+)*/, '$2');
  79. var statusField = $('select.shortcut-status-select', rowObject.element);
  80. statusField.val(statusName);
  81. };
  82. tableDrag.restripeTable = function () {
  83. // :even and :odd are reversed because jQuery counts from 0 and
  84. // we count from 1, so we're out of sync.
  85. // Match immediate children of the parent element to allow nesting.
  86. $('> tbody > tr:visible, > tr:visible', this.table)
  87. .filter(':odd').filter('.odd')
  88. .removeClass('odd').addClass('even')
  89. .end().end()
  90. .filter(':even').filter('.even')
  91. .removeClass('even').addClass('odd');
  92. };
  93. }
  94. }
  95. };
  96. /**
  97. * Make it so when you enter text into the "New set" textfield, the
  98. * corresponding radio button gets selected.
  99. */
  100. Drupal.behaviors.newSet = {
  101. attach: function (context, settings) {
  102. var selectDefault = function() {
  103. $(this).closest('form').find('.form-item-set .form-type-radio:last input').attr('checked', 'checked');
  104. };
  105. $('div.form-item-new input').focus(selectDefault).keyup(selectDefault);
  106. }
  107. };
  108. })(jQuery);