Projet

Général

Profil

Paste
Télécharger (7,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_group / field_group.js @ e9f59589

1

    
2
(function($) {
3

    
4
/**
5
 * Drupal FieldGroup object.
6
 */
7
Drupal.FieldGroup = Drupal.FieldGroup || {};
8
Drupal.FieldGroup.Effects = Drupal.FieldGroup.Effects || {};
9
Drupal.FieldGroup.groupWithfocus = null;
10

    
11
Drupal.FieldGroup.setGroupWithfocus = function(element) {
12
  element.css({display: 'block'});
13
  Drupal.FieldGroup.groupWithfocus = element;
14
}
15

    
16
/**
17
 * Implements Drupal.FieldGroup.processHook().
18
 */
19
Drupal.FieldGroup.Effects.processFieldset = {
20
  execute: function (context, settings, type) {
21
    if (type == 'form') {
22
      // Add required fields mark to any fieldsets containing required fields
23
      $('fieldset.fieldset', context).once('fieldgroup-effects', function(i) {
24
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
25
          $('legend span.fieldset-legend', $(this)).eq(0).append(' ').append($('.form-required').eq(0).clone());
26
        }
27
        if ($('.error', $(this)).length) {
28
          $('legend span.fieldset-legend', $(this)).eq(0).addClass('error');
29
          Drupal.FieldGroup.setGroupWithfocus($(this));
30
        }
31
      });
32
    }
33
  }
34
}
35

    
36
/**
37
 * Implements Drupal.FieldGroup.processHook().
38
 */
39
Drupal.FieldGroup.Effects.processAccordion = {
40
  execute: function (context, settings, type) {
41
    $('div.field-group-accordion-wrapper', context).once('fieldgroup-effects', function () {
42
      var wrapper = $(this);
43

    
44
      wrapper.accordion({
45
        autoHeight: false,
46
        active: '.field-group-accordion-active',
47
        collapsible: true,
48
        changestart: function(event, ui) {
49
          if ($(this).hasClass('effect-none')) {
50
            ui.options.animated = false;
51
          }
52
          else {
53
            ui.options.animated = 'slide';
54
          }
55
        }
56
      });
57

    
58
      if (type == 'form') {
59

    
60
        var $firstErrorItem = false;
61

    
62
        // Add required fields mark to any element containing required fields
63
        wrapper.find('div.field-group-accordion-item').each(function(i) {
64

    
65
          if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
66
            $('h3.ui-accordion-header a').eq(i).append(' ').append($('.form-required').eq(0).clone());
67
          }
68
          if ($('.error', $(this)).length) {
69
            // Save first error item, for focussing it.
70
            if (!$firstErrorItem) {
71
              $firstErrorItem = $(this).parent().accordion("activate" , i);
72
            }
73
            $('h3.ui-accordion-header').eq(i).addClass('error');
74
          }
75
        });
76

    
77
        // Save first error item, for focussing it.
78
        if (!$firstErrorItem) {
79
          $('.ui-accordion-content-active', $firstErrorItem).css({height: 'auto', width: 'auto', display: 'block'});
80
        }
81

    
82
      }
83
    });
84
  }
85
}
86

    
87
/**
88
 * Implements Drupal.FieldGroup.processHook().
89
 */
90
Drupal.FieldGroup.Effects.processHtabs = {
91
  execute: function (context, settings, type) {
92
    if (type == 'form') {
93
      // Add required fields mark to any element containing required fields
94
      $('fieldset.horizontal-tabs-pane', context).once('fieldgroup-effects', function(i) {
95
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
96
          $(this).data('horizontalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after(' ');
97
        }
98
        if ($('.error', $(this)).length) {
99
          $(this).data('horizontalTab').link.parent().addClass('error');
100
          Drupal.FieldGroup.setGroupWithfocus($(this));
101
          $(this).data('horizontalTab').focus();
102
        }
103
      });
104
    }
105
  }
106
}
107

    
108
/**
109
 * Implements Drupal.FieldGroup.processHook().
110
 */
111
Drupal.FieldGroup.Effects.processTabs = {
112
  execute: function (context, settings, type) {
113
    if (type == 'form') {
114
      // Add required fields mark to any fieldsets containing required fields
115
      $('fieldset.vertical-tabs-pane', context).once('fieldgroup-effects', function(i) {
116
        if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
117
          $(this).data('verticalTab').link.find('strong:first').after($('.form-required').eq(0).clone()).after(' ');
118
        }
119
        if ($('.error', $(this)).length) {
120
          $(this).data('verticalTab').link.parent().addClass('error');
121
          Drupal.FieldGroup.setGroupWithfocus($(this));
122
          $(this).data('verticalTab').focus();
123
        }
124
      });
125
    }
126
  }
127
}
128

    
129
/**
130
 * Implements Drupal.FieldGroup.processHook().
131
 *
132
 * TODO clean this up meaning check if this is really
133
 *      necessary.
134
 */
135
Drupal.FieldGroup.Effects.processDiv = {
136
  execute: function (context, settings, type) {
137

    
138
    $('div.collapsible', context).once('fieldgroup-effects', function() {
139
      var $wrapper = $(this);
140

    
141
      // Turn the legend into a clickable link, but retain span.field-group-format-toggler
142
      // for CSS positioning.
143

    
144
      var $toggler = $('span.field-group-format-toggler:first', $wrapper);
145
      var $link = $('<a class="field-group-format-title" href="#"></a>');
146
      $link.prepend($toggler.contents());
147

    
148
      // Add required field markers if needed
149
      if ($(this).is('.required-fields') && $(this).find('.form-required').length > 0) {
150
        $link.append(' ').append($('.form-required').eq(0).clone());
151
      }
152

    
153
      $link.appendTo($toggler);
154

    
155
      // .wrapInner() does not retain bound events.
156
      $link.click(function () {
157
        var wrapper = $wrapper.get(0);
158
        // Don't animate multiple times.
159
        if (!wrapper.animating) {
160
          wrapper.animating = true;
161
          var speed = $wrapper.hasClass('speed-fast') ? 300 : 1000;
162
          if ($wrapper.hasClass('effect-none') && $wrapper.hasClass('speed-none')) {
163
            $('> .field-group-format-wrapper', wrapper).toggle();
164
          }
165
          else if ($wrapper.hasClass('effect-blind')) {
166
            $('> .field-group-format-wrapper', wrapper).toggle('blind', {}, speed);
167
          }
168
          else {
169
            $('> .field-group-format-wrapper', wrapper).toggle(speed);
170
          }
171
          wrapper.animating = false;
172
        }
173
        $wrapper.toggleClass('collapsed');
174
        return false;
175
      });
176

    
177
    });
178
  }
179
};
180

    
181
/**
182
 * Behaviors.
183
 */
184
Drupal.behaviors.fieldGroup = {
185
  attach: function (context, settings) {
186
    settings.field_group = settings.field_group || Drupal.settings.field_group;
187
    if (settings.field_group == undefined) {
188
      return;
189
    }
190

    
191
    // Execute all of them.
192
    $.each(Drupal.FieldGroup.Effects, function (func) {
193
      // We check for a wrapper function in Drupal.field_group as
194
      // alternative for dynamic string function calls.
195
      var type = func.toLowerCase().replace("process", "");
196
      if (settings.field_group[type] != undefined && $.isFunction(this.execute)) {
197
        this.execute(context, settings, settings.field_group[type]);
198
      }
199
    });
200

    
201
    // Fixes css for fieldgroups under vertical tabs.
202
    $('.fieldset-wrapper .fieldset > legend').css({display: 'block'});
203
    $('.vertical-tabs fieldset.fieldset').addClass('default-fallback');
204

    
205

    
206
    // Add a new ID to each fieldset.
207
    $('.group-wrapper fieldset').each(function() {
208
      // Tats bad, but we have to keep the actual id to prevent layouts to break.
209
      var fieldgorupID = 'field_group-' + $(this).attr('id') + ' ' + $(this).attr('id');
210
      $(this).attr('id', fieldgorupID);
211
    })
212
    // Set the hash in url to remember last userselection.
213
    $('.group-wrapper ul li').each(function() {
214
      var fieldGroupNavigationListIndex = $(this).index();
215
      $(this).children('a').click(function() {
216
        var fieldset = $('.group-wrapper fieldset').get(fieldGroupNavigationListIndex);
217
        // Grab the first id, holding the wanted hashurl.
218
        var hashUrl = $(fieldset).attr('id').replace(/^field_group-/, '').split(' ')[0];
219
        window.location.hash = hashUrl;
220
      });
221
    });
222
  }
223
};
224

    
225
})(jQuery);