danielebarchiesi@0
|
1
|
danielebarchiesi@0
|
2 (function ($) {
|
danielebarchiesi@0
|
3
|
danielebarchiesi@0
|
4 /**
|
danielebarchiesi@0
|
5 * This script transforms a set of fieldsets into a stack of vertical
|
danielebarchiesi@0
|
6 * tabs. Another tab pane can be selected by clicking on the respective
|
danielebarchiesi@0
|
7 * tab.
|
danielebarchiesi@0
|
8 *
|
danielebarchiesi@0
|
9 * Each tab may have a summary which can be updated by another
|
danielebarchiesi@0
|
10 * script. For that to work, each fieldset has an associated
|
danielebarchiesi@0
|
11 * 'verticalTabCallback' (with jQuery.data() attached to the fieldset),
|
danielebarchiesi@0
|
12 * which is called every time the user performs an update to a form
|
danielebarchiesi@0
|
13 * element inside the tab pane.
|
danielebarchiesi@0
|
14 */
|
danielebarchiesi@0
|
15 Drupal.behaviors.verticalTabs = {
|
danielebarchiesi@0
|
16 attach: function (context) {
|
danielebarchiesi@0
|
17 $('.vertical-tabs-panes', context).once('vertical-tabs', function () {
|
danielebarchiesi@0
|
18 var focusID = $(':hidden.vertical-tabs-active-tab', this).val();
|
danielebarchiesi@0
|
19 var tab_focus;
|
danielebarchiesi@0
|
20
|
danielebarchiesi@0
|
21 // Check if there are some fieldsets that can be converted to vertical-tabs
|
danielebarchiesi@0
|
22 var $fieldsets = $('> fieldset', this);
|
danielebarchiesi@0
|
23 if ($fieldsets.length == 0) {
|
danielebarchiesi@0
|
24 return;
|
danielebarchiesi@0
|
25 }
|
danielebarchiesi@0
|
26
|
danielebarchiesi@0
|
27 // Create the tab column.
|
danielebarchiesi@0
|
28 var tab_list = $('<ul class="vertical-tabs-list"></ul>');
|
danielebarchiesi@0
|
29 $(this).wrap('<div class="vertical-tabs clearfix"></div>').before(tab_list);
|
danielebarchiesi@0
|
30
|
danielebarchiesi@0
|
31 // Transform each fieldset into a tab.
|
danielebarchiesi@0
|
32 $fieldsets.each(function () {
|
danielebarchiesi@0
|
33 var vertical_tab = new Drupal.verticalTab({
|
danielebarchiesi@0
|
34 title: $('> legend', this).text(),
|
danielebarchiesi@0
|
35 fieldset: $(this)
|
danielebarchiesi@0
|
36 });
|
danielebarchiesi@0
|
37 tab_list.append(vertical_tab.item);
|
danielebarchiesi@0
|
38 $(this)
|
danielebarchiesi@0
|
39 .removeClass('collapsible collapsed')
|
danielebarchiesi@0
|
40 .addClass('vertical-tabs-pane')
|
danielebarchiesi@0
|
41 .data('verticalTab', vertical_tab);
|
danielebarchiesi@0
|
42 if (this.id == focusID) {
|
danielebarchiesi@0
|
43 tab_focus = $(this);
|
danielebarchiesi@0
|
44 }
|
danielebarchiesi@0
|
45 });
|
danielebarchiesi@0
|
46
|
danielebarchiesi@0
|
47 $('> li:first', tab_list).addClass('first');
|
danielebarchiesi@0
|
48 $('> li:last', tab_list).addClass('last');
|
danielebarchiesi@0
|
49
|
danielebarchiesi@0
|
50 if (!tab_focus) {
|
danielebarchiesi@0
|
51 // If the current URL has a fragment and one of the tabs contains an
|
danielebarchiesi@0
|
52 // element that matches the URL fragment, activate that tab.
|
danielebarchiesi@0
|
53 if (window.location.hash && $(this).find(window.location.hash).length) {
|
danielebarchiesi@0
|
54 tab_focus = $(this).find(window.location.hash).closest('.vertical-tabs-pane');
|
danielebarchiesi@0
|
55 }
|
danielebarchiesi@0
|
56 else {
|
danielebarchiesi@0
|
57 tab_focus = $('> .vertical-tabs-pane:first', this);
|
danielebarchiesi@0
|
58 }
|
danielebarchiesi@0
|
59 }
|
danielebarchiesi@0
|
60 if (tab_focus.length) {
|
danielebarchiesi@0
|
61 tab_focus.data('verticalTab').focus();
|
danielebarchiesi@0
|
62 }
|
danielebarchiesi@0
|
63 });
|
danielebarchiesi@0
|
64 }
|
danielebarchiesi@0
|
65 };
|
danielebarchiesi@0
|
66
|
danielebarchiesi@0
|
67 /**
|
danielebarchiesi@0
|
68 * The vertical tab object represents a single tab within a tab group.
|
danielebarchiesi@0
|
69 *
|
danielebarchiesi@0
|
70 * @param settings
|
danielebarchiesi@0
|
71 * An object with the following keys:
|
danielebarchiesi@0
|
72 * - title: The name of the tab.
|
danielebarchiesi@0
|
73 * - fieldset: The jQuery object of the fieldset that is the tab pane.
|
danielebarchiesi@0
|
74 */
|
danielebarchiesi@0
|
75 Drupal.verticalTab = function (settings) {
|
danielebarchiesi@0
|
76 var self = this;
|
danielebarchiesi@0
|
77 $.extend(this, settings, Drupal.theme('verticalTab', settings));
|
danielebarchiesi@0
|
78
|
danielebarchiesi@0
|
79 this.link.click(function () {
|
danielebarchiesi@0
|
80 self.focus();
|
danielebarchiesi@0
|
81 return false;
|
danielebarchiesi@0
|
82 });
|
danielebarchiesi@0
|
83
|
danielebarchiesi@0
|
84 // Keyboard events added:
|
danielebarchiesi@0
|
85 // Pressing the Enter key will open the tab pane.
|
danielebarchiesi@0
|
86 this.link.keydown(function(event) {
|
danielebarchiesi@0
|
87 if (event.keyCode == 13) {
|
danielebarchiesi@0
|
88 self.focus();
|
danielebarchiesi@0
|
89 // Set focus on the first input field of the visible fieldset/tab pane.
|
danielebarchiesi@0
|
90 $("fieldset.vertical-tabs-pane :input:visible:enabled:first").focus();
|
danielebarchiesi@0
|
91 return false;
|
danielebarchiesi@0
|
92 }
|
danielebarchiesi@0
|
93 });
|
danielebarchiesi@0
|
94
|
danielebarchiesi@0
|
95 this.fieldset
|
danielebarchiesi@0
|
96 .bind('summaryUpdated', function () {
|
danielebarchiesi@0
|
97 self.updateSummary();
|
danielebarchiesi@0
|
98 })
|
danielebarchiesi@0
|
99 .trigger('summaryUpdated');
|
danielebarchiesi@0
|
100 };
|
danielebarchiesi@0
|
101
|
danielebarchiesi@0
|
102 Drupal.verticalTab.prototype = {
|
danielebarchiesi@0
|
103 /**
|
danielebarchiesi@0
|
104 * Displays the tab's content pane.
|
danielebarchiesi@0
|
105 */
|
danielebarchiesi@0
|
106 focus: function () {
|
danielebarchiesi@0
|
107 this.fieldset
|
danielebarchiesi@0
|
108 .siblings('fieldset.vertical-tabs-pane')
|
danielebarchiesi@0
|
109 .each(function () {
|
danielebarchiesi@0
|
110 var tab = $(this).data('verticalTab');
|
danielebarchiesi@0
|
111 tab.fieldset.hide();
|
danielebarchiesi@0
|
112 tab.item.removeClass('selected');
|
danielebarchiesi@0
|
113 })
|
danielebarchiesi@0
|
114 .end()
|
danielebarchiesi@0
|
115 .show()
|
danielebarchiesi@0
|
116 .siblings(':hidden.vertical-tabs-active-tab')
|
danielebarchiesi@0
|
117 .val(this.fieldset.attr('id'));
|
danielebarchiesi@0
|
118 this.item.addClass('selected');
|
danielebarchiesi@0
|
119 // Mark the active tab for screen readers.
|
danielebarchiesi@0
|
120 $('#active-vertical-tab').remove();
|
danielebarchiesi@0
|
121 this.link.append('<span id="active-vertical-tab" class="element-invisible">' + Drupal.t('(active tab)') + '</span>');
|
danielebarchiesi@0
|
122 },
|
danielebarchiesi@0
|
123
|
danielebarchiesi@0
|
124 /**
|
danielebarchiesi@0
|
125 * Updates the tab's summary.
|
danielebarchiesi@0
|
126 */
|
danielebarchiesi@0
|
127 updateSummary: function () {
|
danielebarchiesi@0
|
128 this.summary.html(this.fieldset.drupalGetSummary());
|
danielebarchiesi@0
|
129 },
|
danielebarchiesi@0
|
130
|
danielebarchiesi@0
|
131 /**
|
danielebarchiesi@0
|
132 * Shows a vertical tab pane.
|
danielebarchiesi@0
|
133 */
|
danielebarchiesi@0
|
134 tabShow: function () {
|
danielebarchiesi@0
|
135 // Display the tab.
|
danielebarchiesi@0
|
136 this.item.show();
|
danielebarchiesi@0
|
137 // Update .first marker for items. We need recurse from parent to retain the
|
danielebarchiesi@0
|
138 // actual DOM element order as jQuery implements sortOrder, but not as public
|
danielebarchiesi@0
|
139 // method.
|
danielebarchiesi@0
|
140 this.item.parent().children('.vertical-tab-button').removeClass('first')
|
danielebarchiesi@0
|
141 .filter(':visible:first').addClass('first');
|
danielebarchiesi@0
|
142 // Display the fieldset.
|
danielebarchiesi@0
|
143 this.fieldset.removeClass('vertical-tab-hidden').show();
|
danielebarchiesi@0
|
144 // Focus this tab.
|
danielebarchiesi@0
|
145 this.focus();
|
danielebarchiesi@0
|
146 return this;
|
danielebarchiesi@0
|
147 },
|
danielebarchiesi@0
|
148
|
danielebarchiesi@0
|
149 /**
|
danielebarchiesi@0
|
150 * Hides a vertical tab pane.
|
danielebarchiesi@0
|
151 */
|
danielebarchiesi@0
|
152 tabHide: function () {
|
danielebarchiesi@0
|
153 // Hide this tab.
|
danielebarchiesi@0
|
154 this.item.hide();
|
danielebarchiesi@0
|
155 // Update .first marker for items. We need recurse from parent to retain the
|
danielebarchiesi@0
|
156 // actual DOM element order as jQuery implements sortOrder, but not as public
|
danielebarchiesi@0
|
157 // method.
|
danielebarchiesi@0
|
158 this.item.parent().children('.vertical-tab-button').removeClass('first')
|
danielebarchiesi@0
|
159 .filter(':visible:first').addClass('first');
|
danielebarchiesi@0
|
160 // Hide the fieldset.
|
danielebarchiesi@0
|
161 this.fieldset.addClass('vertical-tab-hidden').hide();
|
danielebarchiesi@0
|
162 // Focus the first visible tab (if there is one).
|
danielebarchiesi@0
|
163 var $firstTab = this.fieldset.siblings('.vertical-tabs-pane:not(.vertical-tab-hidden):first');
|
danielebarchiesi@0
|
164 if ($firstTab.length) {
|
danielebarchiesi@0
|
165 $firstTab.data('verticalTab').focus();
|
danielebarchiesi@0
|
166 }
|
danielebarchiesi@0
|
167 return this;
|
danielebarchiesi@0
|
168 }
|
danielebarchiesi@0
|
169 };
|
danielebarchiesi@0
|
170
|
danielebarchiesi@0
|
171 /**
|
danielebarchiesi@0
|
172 * Theme function for a vertical tab.
|
danielebarchiesi@0
|
173 *
|
danielebarchiesi@0
|
174 * @param settings
|
danielebarchiesi@0
|
175 * An object with the following keys:
|
danielebarchiesi@0
|
176 * - title: The name of the tab.
|
danielebarchiesi@0
|
177 * @return
|
danielebarchiesi@0
|
178 * This function has to return an object with at least these keys:
|
danielebarchiesi@0
|
179 * - item: The root tab jQuery element
|
danielebarchiesi@0
|
180 * - link: The anchor tag that acts as the clickable area of the tab
|
danielebarchiesi@0
|
181 * (jQuery version)
|
danielebarchiesi@0
|
182 * - summary: The jQuery element that contains the tab summary
|
danielebarchiesi@0
|
183 */
|
danielebarchiesi@0
|
184 Drupal.theme.prototype.verticalTab = function (settings) {
|
danielebarchiesi@0
|
185 var tab = {};
|
danielebarchiesi@0
|
186 tab.item = $('<li class="vertical-tab-button" tabindex="-1"></li>')
|
danielebarchiesi@0
|
187 .append(tab.link = $('<a href="#"></a>')
|
danielebarchiesi@0
|
188 .append(tab.title = $('<strong></strong>').text(settings.title))
|
danielebarchiesi@0
|
189 .append(tab.summary = $('<span class="summary"></span>')
|
danielebarchiesi@0
|
190 )
|
danielebarchiesi@0
|
191 );
|
danielebarchiesi@0
|
192 return tab;
|
danielebarchiesi@0
|
193 };
|
danielebarchiesi@0
|
194
|
danielebarchiesi@0
|
195 })(jQuery);
|