Projet

Général

Profil

Paste
Télécharger (2,63 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / profile / profile.js @ db2d93dd

1
(function ($) {
2

    
3
/**
4
 * Add functionality to the profile drag and drop table.
5
 *
6
 * This behavior is dependent on the tableDrag behavior, since it uses the
7
 * objects initialized in that behavior to update the row. It shows and hides
8
 * a warning message when removing the last field from a profile category.
9
 */
10
Drupal.behaviors.profileDrag = {
11
  attach: function (context, settings) {
12
    var table = $('#profile-fields');
13
    var tableDrag = Drupal.tableDrag['profile-fields']; // Get the profile tableDrag object.
14

    
15
    // Add a handler for when a row is swapped, update empty categories.
16
    tableDrag.row.prototype.onSwap = function (swappedRow) {
17
      var rowObject = this;
18
      $('tr.category-message', table).each(function () {
19
        // If the dragged row is in this category, but above the message row, swap it down one space.
20
        if ($(this).prev('tr').get(0) == rowObject.element) {
21
          // Prevent a recursion problem when using the keyboard to move rows up.
22
          if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
23
            rowObject.swap('after', this);
24
          }
25
        }
26
        // This category has become empty
27
        if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').length == 0) {
28
          $(this).removeClass('category-populated').addClass('category-empty');
29
        }
30
        // This category has become populated.
31
        else if ($(this).is('.category-empty')) {
32
          $(this).removeClass('category-empty').addClass('category-populated');
33
        }
34
      });
35
    };
36

    
37
    // Add a handler so when a row is dropped, update fields dropped into new categories.
38
    tableDrag.onDrop = function () {
39
      dragObject = this;
40
      if ($(dragObject.rowObject.element).prev('tr').is('.category-message')) {
41
        var categoryRow = $(dragObject.rowObject.element).prev('tr').get(0);
42
        var categoryNum = categoryRow.className.replace(/([^ ]+[ ]+)*category-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
43
        var categoryField = $('select.profile-category', dragObject.rowObject.element);
44
        var weightField = $('select.profile-weight', dragObject.rowObject.element);
45
        var oldcategoryNum = weightField[0].className.replace(/([^ ]+[ ]+)*profile-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
46

    
47
        if (!categoryField.is('.profile-category-' + categoryNum)) {
48
          categoryField.removeClass('profile-category-' + oldcategoryNum).addClass('profile-category-' + categoryNum);
49
          weightField.removeClass('profile-weight-' + oldcategoryNum).addClass('profile-weight-' + categoryNum);
50

    
51
          categoryField.val(categoryField[0].options[categoryNum].value);
52
        }
53
      }
54
    };
55
  }
56
};
57

    
58
})(jQuery);