Projet

Général

Profil

Paste
Télécharger (4,31 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / checkout / commerce_checkout_admin.js @ 9d13637e

1
(function ($) {
2

    
3
/**
4
 * Add functionality to the checkout panes tabledrag enhanced table.
5
 *
6
 * This code is almost an exact copy of the code used for the block region and
7
 * weight settings form.
8
 */
9
Drupal.behaviors.paneDrag = {
10
  attach: function (context, settings) {
11
    // tableDrag is required for this behavior.
12
    if (typeof Drupal.tableDrag == 'undefined' || typeof Drupal.tableDrag.panes == 'undefined') {
13
      return;
14
    }
15

    
16
    var table = $('table#panes');
17
    var tableDrag = Drupal.tableDrag.panes; // Get the blocks tableDrag object.
18

    
19
    // Add a handler for when a row is swapped, update empty regions.
20
    tableDrag.row.prototype.onSwap = function (swappedRow) {
21
      checkEmptyPages(table, this);
22
    };
23

    
24
    // A custom message for the panes page specifically.
25
    Drupal.theme.tableDragChangedWarning = function () {
26
      return '<div class="messages warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t("Changes to the checkout panes will not be saved until the <em>Save configuration</em> button is clicked.") + '</div>';
27
    };
28

    
29
    // Add a handler so when a row is dropped, update fields dropped into new regions.
30
    tableDrag.onDrop = function() {
31
      dragObject = this;
32

    
33
      var pageRow = $(dragObject.rowObject.element).prev('tr').get(0);
34
      var pageName = pageRow.className.replace(/([^ ]+[ ]+)*page-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
35
      var pageField = $('select.checkout-pane-page', dragObject.rowObject.element);
36

    
37
      if ($(dragObject.rowObject.element).prev('tr').is('.page-message')) {
38
        var weightField = $('select.checkout-pane-weight', dragObject.rowObject.element);
39
        var oldPageName = weightField[0].className.replace(/([^ ]+[ ]+)*checkout-pane-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
40

    
41
        if (!pageField.is('.checkout-pane-page-'+ pageName)) {
42
          pageField.removeClass('checkout-pane-page-' + oldPageName).addClass('checkout-pane-page-' + pageName);
43
          weightField.removeClass('checkout-pane-weight-' + oldPageName).addClass('checkout-pane-weight-' + pageName);
44
          pageField.val(pageName);
45
        }
46
      }
47
    };
48

    
49
    // Add the behavior to each region select list.
50
    $('select.checkout-pane-page', context).once('checkout-pane-page', function () {
51
      $(this).change(function (event) {
52
        // Make our new row and select field.
53
        var row = $(this).parents('tr:first');
54
        var select = $(this);
55
        tableDrag.rowObject = new tableDrag.row(row);
56

    
57
        // Find the correct region and insert the row as the first in the region.
58
        $('tr.page-message', table).each(function () {
59
          if ($(this).is('.page-' + select[0].value + '-message')) {
60
            // Add the new row and remove the old one.
61
            $(this).after(row);
62
            // Manually update weights and restripe.
63
            tableDrag.updateFields(row.get(0));
64
            tableDrag.rowObject.changed = true;
65
            if (tableDrag.oldRowElement) {
66
              $(tableDrag.oldRowElement).removeClass('drag-previous');
67
            }
68
            tableDrag.oldRowElement = row.get(0);
69
            tableDrag.restripeTable();
70
            tableDrag.rowObject.markChanged();
71
            tableDrag.oldRowElement = row;
72
            $(row).addClass('drag-previous');
73
          }
74
        });
75

    
76
        // Modify empty regions with added or removed fields.
77
        checkEmptyPages(table, row);
78
        // Remove focus from selectbox.
79
        select.get(0).blur();
80
      });
81
    });
82

    
83
    var checkEmptyPages = function(table, rowObject) {
84
      $('tr.page-message', table).each(function() {
85
        // If the dragged row is in this region, but above the message row, swap it down one space.
86
        if ($(this).prev('tr').get(0) == rowObject.element) {
87
          // Prevent a recursion problem when using the keyboard to move rows up.
88
          if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
89
            rowObject.swap('after', this);
90
          }
91
        }
92
        // This region has become empty
93
        if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').length == 0) {
94
          $(this).removeClass('page-populated').addClass('page-empty');
95
        }
96
        // This region has become populated.
97
        else if ($(this).is('.page-empty')) {
98
          $(this).removeClass('page-empty').addClass('page-populated');
99
        }
100
      });
101
    };
102
  }
103
};
104

    
105
})(jQuery);