Projet

Général

Profil

Paste
Télécharger (8,92 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / js / ajax.js @ 4003efde

1
/**
2
 * @file
3
 * Handles AJAX submission and response in Views UI.
4
 */
5
(function ($) {
6

    
7
  Drupal.ajax.prototype.commands.viewsSetForm = function (ajax, response, status) {
8
    var ajax_title = Drupal.settings.views.ajax.title;
9
    var ajax_body = Drupal.settings.views.ajax.id;
10
    var ajax_popup = Drupal.settings.views.ajax.popup;
11
    $(ajax_title).html(response.title);
12
    $(ajax_body).html(response.output);
13
    $(ajax_popup).dialog('open');
14
    Drupal.attachBehaviors($(ajax_popup), ajax.settings);
15
    if (response.url) {
16
      // Identify the button that was clicked so that .ajaxSubmit() can use it.
17
      // We need to do this for both .click() and .mousedown() since JavaScript
18
      // code might trigger either behavior.
19
      var $submit_buttons = $('input[type=submit], button', ajax_body);
20
      $submit_buttons.click(function(event) {
21
        this.form.clk = this;
22
      });
23
      $submit_buttons.mousedown(function(event) {
24
        this.form.clk = this;
25
      });
26

    
27
      $('form', ajax_body).once('views-ajax-submit-processed').each(function() {
28
        var element_settings = {
29
          'url': response.url,
30
          'event': 'submit',
31
          'progress': {
32
            'type': 'throbber'
33
          }
34
        };
35
        var $form = $(this);
36
        var id = $form.attr('id');
37
        Drupal.ajax[id] = new Drupal.ajax(id, this, element_settings);
38
        Drupal.ajax[id].form = $form;
39
      });
40
    }
41
    Drupal.viewsUi.resizeModal();
42
  };
43

    
44
  Drupal.ajax.prototype.commands.viewsDismissForm = function (ajax, response, status) {
45
    Drupal.ajax.prototype.commands.viewsSetForm({}, {'title': '', 'output': Drupal.settings.views.ajax.defaultForm});
46
    $(Drupal.settings.views.ajax.popup).dialog('close');
47
  }
48

    
49
  Drupal.ajax.prototype.commands.viewsHilite = function (ajax, response, status) {
50
    $('.hilited').removeClass('hilited');
51
    $(response.selector).addClass('hilited');
52
  };
53

    
54
  Drupal.ajax.prototype.commands.viewsAddTab = function (ajax, response, status) {
55
    var id = '#views-tab-' + response.id;
56
    $('#views-tabset').viewsAddTab(id, response.title, 0);
57
    $(id).html(response.body).addClass('views-tab');
58

    
59
    // Update the preview widget to preview the new tab.
60
    var display_id = id.replace('#views-tab-', '');
61
    $("#preview-display-id").append('<option selected="selected" value="' + display_id + '">' + response.title + '</option>');
62

    
63
    Drupal.attachBehaviors(id);
64
    var instance = $.viewsUi.tabs.instances[$('#views-tabset').get(0).UI_TABS_UUID];
65
    $('#views-tabset').viewsClickTab(instance.$tabs.length);
66
  };
67

    
68
  Drupal.ajax.prototype.commands.viewsShowButtons = function (ajax, response, status) {
69
    $('div.views-edit-view div.form-actions').removeClass('js-hide');
70
    if (response.changed) {
71
      $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
72
    }
73
  };
74

    
75
  Drupal.ajax.prototype.commands.viewsTriggerPreview = function (ajax, response, status) {
76
    if ($('input#edit-displays-live-preview').is(':checked')) {
77
      $('#preview-submit').trigger('click');
78
    }
79
  };
80

    
81
  Drupal.ajax.prototype.commands.viewsReplaceTitle = function (ajax, response, status) {
82
    // In case we're in the overlay, get a reference to the underlying window.
83
    var doc = parent.document;
84
    // For the <title> element, make a best-effort attempt to replace the page
85
    // title and leave the site name alone. If the theme doesn't use the site
86
    // name in the <title> element, this will fail.
87
    var oldTitle = doc.title;
88
    // Escape the site name, in case it has special characters in it, so we can
89
    // use it in our regex.
90
    var escapedSiteName = response.siteName.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
91
    var re = new RegExp('.+ (.) ' + escapedSiteName);
92
    doc.title = oldTitle.replace(re, response.title + ' $1 ' + response.siteName);
93

    
94
    $('h1.page-title').text(response.title);
95
    $('h1#overlay-title').text(response.title);
96
  };
97

    
98
  /**
99
   * Get rid of irritating tabledrag messages.
100
   */
101
  Drupal.theme.tableDragChangedWarning = function () {
102
    return [];
103
  }
104

    
105
  /**
106
   * Trigger preview when the "live preview" checkbox is checked.
107
   */
108
  Drupal.behaviors.livePreview = {
109
    attach: function (context) {
110
      $('input#edit-displays-live-preview', context).once('views-ajax-processed').click(function() {
111
        if ($(this).is(':checked')) {
112
          $('#preview-submit').click();
113
        }
114
      });
115
    }
116
  }
117

    
118
  /**
119
   * Sync preview display.
120
   */
121
  Drupal.behaviors.syncPreviewDisplay = {
122
    attach: function (context) {
123
      $("#views-tabset a").once('views-ajax-processed').click(function() {
124
        var href = $(this).attr('href');
125
        // Cut of #views-tabset.
126
        var display_id = href.substr(11);
127
        // Set the form element.
128
        $("#views-live-preview #preview-display-id").val(display_id);
129
      }).addClass('views-ajax-processed');
130
    }
131
  }
132

    
133
  Drupal.behaviors.viewsAjax = {
134
    collapseReplaced: false,
135
    attach: function (context, settings) {
136
      if (!settings.views) {
137
        return;
138
      }
139
      // Create a jQuery UI dialog, but leave it closed.
140
      var dialog_area = $(settings.views.ajax.popup, context);
141
      dialog_area.dialog({
142
        'autoOpen': false,
143
        'dialogClass': 'views-ui-dialog',
144
        'modal': true,
145
        'position': 'center',
146
        'resizable': false,
147
        'width': 750
148
      });
149

    
150
      var base_element_settings = {
151
        'event': 'click',
152
        'progress': {
153
          'type': 'throbber'
154
        }
155
      };
156
      // Bind AJAX behaviors to all items showing the class.
157
      $('a.views-ajax-link', context).once('views-ajax-processed').each(function () {
158
        var element_settings = base_element_settings;
159
        // Set the URL to go to the anchor.
160
        if ($(this).attr('href')) {
161
          element_settings.url = $(this).attr('href');
162
        }
163
        var base = $(this).attr('id');
164
        Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
165
      });
166

    
167
      $('div#views-live-preview a')
168
        .once('views-ajax-processed').each(function () {
169
          // We don't bind to links without a URL.
170
          if (!$(this).attr('href')) {
171
            return true;
172
          }
173

    
174
          var element_settings = base_element_settings;
175
          // Set the URL to go to the anchor.
176
          element_settings.url = $(this).attr('href');
177
          if (Drupal.Views.getPath(element_settings.url).substring(0, 21) != 'admin/structure/views') {
178
            return true;
179
          }
180

    
181
          element_settings.wrapper = 'views-live-preview';
182
          element_settings.method = 'html';
183
          var base = $(this).attr('id');
184
          Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
185
        });
186

    
187
      // Within a live preview, make exposed widget form buttons re-trigger the
188
      // Preview button.
189
      // @todo Revisit this after fixing Views UI to display a Preview outside
190
      //   of the main Edit form.
191
      $('div#views-live-preview input[type=submit]')
192
        .once('views-ajax-processed').each(function(event) {
193
          $(this).click(function () {
194
            this.form.clk = this;
195
            return true;
196
          });
197
          var element_settings = base_element_settings;
198
          // Set the URL to go to the anchor.
199
          element_settings.url = $(this.form).attr('action');
200
          if (Drupal.Views.getPath(element_settings.url).substring(0, 21) != 'admin/structure/views') {
201
            return true;
202
          }
203

    
204
          element_settings.wrapper = 'views-live-preview';
205
          element_settings.method = 'html';
206
          element_settings.event = 'click';
207

    
208
          var base = $(this).attr('id');
209
          Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
210
        });
211

    
212
      if (!this.collapseReplaced && Drupal.collapseScrollIntoView) {
213
        this.collapseReplaced = true;
214
        Drupal.collapseScrollIntoView = function (node) {
215
          for (var $parent = $(node); $parent.get(0) != document && $parent.length != 0; $parent = $parent.parent()) {
216
            if ($parent.css('overflow') == 'scroll' || $parent.css('overflow') == 'auto') {
217
              if (Drupal.viewsUi.resizeModal) {
218
                // If the modal is already at the max height, don't bother with
219
                // this since the only reason to do it is to grow the modal.
220
                if ($('.views-ui-dialog').height() < parseInt($(window).height() * .8)) {
221
                  Drupal.viewsUi.resizeModal('', true);
222
                }
223
              }
224
              return;
225
            }
226
          }
227

    
228
          var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
229
          var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
230
          var posY = $(node).offset().top;
231
          var fudge = 55;
232
          if (posY + node.offsetHeight + fudge > h + offset) {
233
            if (node.offsetHeight > h) {
234
              window.scrollTo(0, posY);
235
            }
236
            else {
237
              window.scrollTo(0, posY + node.offsetHeight - h + fudge);
238
            }
239
          }
240
        };
241
      }
242
    }
243
  };
244

    
245
})(jQuery);