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