Projet

Général

Profil

Paste
Télécharger (5,99 ko) Statistiques
| Branche: | Révision:

root / htmltest / misc / vertical-tabs.js @ 85ad3d82

1

    
2
(function ($) {
3

    
4
/**
5
 * This script transforms a set of fieldsets into a stack of vertical
6
 * tabs. Another tab pane can be selected by clicking on the respective
7
 * tab.
8
 *
9
 * Each tab may have a summary which can be updated by another
10
 * script. For that to work, each fieldset has an associated
11
 * 'verticalTabCallback' (with jQuery.data() attached to the fieldset),
12
 * which is called every time the user performs an update to a form
13
 * element inside the tab pane.
14
 */
15
Drupal.behaviors.verticalTabs = {
16
  attach: function (context) {
17
    $('.vertical-tabs-panes', context).once('vertical-tabs', function () {
18
      var focusID = $(':hidden.vertical-tabs-active-tab', this).val();
19
      var tab_focus;
20

    
21
      // Check if there are some fieldsets that can be converted to vertical-tabs
22
      var $fieldsets = $('> fieldset', this);
23
      if ($fieldsets.length == 0) {
24
        return;
25
      }
26

    
27
      // Create the tab column.
28
      var tab_list = $('<ul class="vertical-tabs-list"></ul>');
29
      $(this).wrap('<div class="vertical-tabs clearfix"></div>').before(tab_list);
30

    
31
      // Transform each fieldset into a tab.
32
      $fieldsets.each(function () {
33
        var vertical_tab = new Drupal.verticalTab({
34
          title: $('> legend', this).text(),
35
          fieldset: $(this)
36
        });
37
        tab_list.append(vertical_tab.item);
38
        $(this)
39
          .removeClass('collapsible collapsed')
40
          .addClass('vertical-tabs-pane')
41
          .data('verticalTab', vertical_tab);
42
        if (this.id == focusID) {
43
          tab_focus = $(this);
44
        }
45
      });
46

    
47
      $('> li:first', tab_list).addClass('first');
48
      $('> li:last', tab_list).addClass('last');
49

    
50
      if (!tab_focus) {
51
        // If the current URL has a fragment and one of the tabs contains an
52
        // element that matches the URL fragment, activate that tab.
53
        if (window.location.hash && $(this).find(window.location.hash).length) {
54
          tab_focus = $(this).find(window.location.hash).closest('.vertical-tabs-pane');
55
        }
56
        else {
57
          tab_focus = $('> .vertical-tabs-pane:first', this);
58
        }
59
      }
60
      if (tab_focus.length) {
61
        tab_focus.data('verticalTab').focus();
62
      }
63
    });
64
  }
65
};
66

    
67
/**
68
 * The vertical tab object represents a single tab within a tab group.
69
 *
70
 * @param settings
71
 *   An object with the following keys:
72
 *   - title: The name of the tab.
73
 *   - fieldset: The jQuery object of the fieldset that is the tab pane.
74
 */
75
Drupal.verticalTab = function (settings) {
76
  var self = this;
77
  $.extend(this, settings, Drupal.theme('verticalTab', settings));
78

    
79
  this.link.click(function () {
80
    self.focus();
81
    return false;
82
  });
83

    
84
  // Keyboard events added:
85
  // Pressing the Enter key will open the tab pane.
86
  this.link.keydown(function(event) {
87
    if (event.keyCode == 13) {
88
      self.focus();
89
      // Set focus on the first input field of the visible fieldset/tab pane.
90
      $("fieldset.vertical-tabs-pane :input:visible:enabled:first").focus();
91
      return false;
92
    }
93
  });
94

    
95
  this.fieldset
96
    .bind('summaryUpdated', function () {
97
      self.updateSummary();
98
    })
99
    .trigger('summaryUpdated');
100
};
101

    
102
Drupal.verticalTab.prototype = {
103
  /**
104
   * Displays the tab's content pane.
105
   */
106
  focus: function () {
107
    this.fieldset
108
      .siblings('fieldset.vertical-tabs-pane')
109
        .each(function () {
110
          var tab = $(this).data('verticalTab');
111
          tab.fieldset.hide();
112
          tab.item.removeClass('selected');
113
        })
114
        .end()
115
      .show()
116
      .siblings(':hidden.vertical-tabs-active-tab')
117
        .val(this.fieldset.attr('id'));
118
    this.item.addClass('selected');
119
    // Mark the active tab for screen readers.
120
    $('#active-vertical-tab').remove();
121
    this.link.append('<span id="active-vertical-tab" class="element-invisible">' + Drupal.t('(active tab)') + '</span>');
122
  },
123

    
124
  /**
125
   * Updates the tab's summary.
126
   */
127
  updateSummary: function () {
128
    this.summary.html(this.fieldset.drupalGetSummary());
129
  },
130

    
131
  /**
132
   * Shows a vertical tab pane.
133
   */
134
  tabShow: function () {
135
    // Display the tab.
136
    this.item.show();
137
    // Update .first marker for items. We need recurse from parent to retain the
138
    // actual DOM element order as jQuery implements sortOrder, but not as public
139
    // method.
140
    this.item.parent().children('.vertical-tab-button').removeClass('first')
141
      .filter(':visible:first').addClass('first');
142
    // Display the fieldset.
143
    this.fieldset.removeClass('vertical-tab-hidden').show();
144
    // Focus this tab.
145
    this.focus();
146
    return this;
147
  },
148

    
149
  /**
150
   * Hides a vertical tab pane.
151
   */
152
  tabHide: function () {
153
    // Hide this tab.
154
    this.item.hide();
155
    // Update .first marker for items. We need recurse from parent to retain the
156
    // actual DOM element order as jQuery implements sortOrder, but not as public
157
    // method.
158
    this.item.parent().children('.vertical-tab-button').removeClass('first')
159
      .filter(':visible:first').addClass('first');
160
    // Hide the fieldset.
161
    this.fieldset.addClass('vertical-tab-hidden').hide();
162
    // Focus the first visible tab (if there is one).
163
    var $firstTab = this.fieldset.siblings('.vertical-tabs-pane:not(.vertical-tab-hidden):first');
164
    if ($firstTab.length) {
165
      $firstTab.data('verticalTab').focus();
166
    }
167
    return this;
168
  }
169
};
170

    
171
/**
172
 * Theme function for a vertical tab.
173
 *
174
 * @param settings
175
 *   An object with the following keys:
176
 *   - title: The name of the tab.
177
 * @return
178
 *   This function has to return an object with at least these keys:
179
 *   - item: The root tab jQuery element
180
 *   - link: The anchor tag that acts as the clickable area of the tab
181
 *       (jQuery version)
182
 *   - summary: The jQuery element that contains the tab summary
183
 */
184
Drupal.theme.prototype.verticalTab = function (settings) {
185
  var tab = {};
186
  tab.item = $('<li class="vertical-tab-button" tabindex="-1"></li>')
187
    .append(tab.link = $('<a href="#"></a>')
188
      .append(tab.title = $('<strong></strong>').text(settings.title))
189
      .append(tab.summary = $('<span class="summary"></span>')
190
    )
191
  );
192
  return tab;
193
};
194

    
195
})(jQuery);