Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / js / ajax.js @ 64ad485a

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 = { 'url': response.url, 'event': 'submit', 'progress': { 'type': 'throbber' } };
29
        var $form = $(this);
30
        var id = $form.attr('id');
31
        Drupal.ajax[id] = new Drupal.ajax(id, this, element_settings);
32
        Drupal.ajax[id].form = $form;
33
      });
34
    }
35
    Drupal.viewsUi.resizeModal();
36
  };
37

    
38
  Drupal.ajax.prototype.commands.viewsDismissForm = function (ajax, response, status) {
39
    Drupal.ajax.prototype.commands.viewsSetForm({}, {'title': '', 'output': Drupal.settings.views.ajax.defaultForm});
40
    $(Drupal.settings.views.ajax.popup).dialog('close');
41
  }
42

    
43
  Drupal.ajax.prototype.commands.viewsHilite = function (ajax, response, status) {
44
    $('.hilited').removeClass('hilited');
45
    $(response.selector).addClass('hilited');
46
  };
47

    
48
  Drupal.ajax.prototype.commands.viewsAddTab = function (ajax, response, status) {
49
    var id = '#views-tab-' + response.id;
50
    $('#views-tabset').viewsAddTab(id, response.title, 0);
51
    $(id).html(response.body).addClass('views-tab');
52

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

    
57
    Drupal.attachBehaviors(id);
58
    var instance = $.viewsUi.tabs.instances[$('#views-tabset').get(0).UI_TABS_UUID];
59
    $('#views-tabset').viewsClickTab(instance.$tabs.length);
60
  };
61

    
62
  Drupal.ajax.prototype.commands.viewsShowButtons = function (ajax, response, status) {
63
    $('div.views-edit-view div.form-actions').removeClass('js-hide');
64
    $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
65
  };
66

    
67
  Drupal.ajax.prototype.commands.viewsTriggerPreview = function (ajax, response, status) {
68
    if ($('input#edit-displays-live-preview').is(':checked')) {
69
      $('#preview-submit').trigger('click');
70
    }
71
  };
72

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

    
86
    $('h1.page-title').text(response.title);
87
    $('h1#overlay-title').text(response.title);
88
  };
89

    
90
  /**
91
   * Get rid of irritating tabledrag messages
92
   */
93
  Drupal.theme.tableDragChangedWarning = function () {
94
    return [];
95
  }
96

    
97
  /**
98
   * Trigger preview when the "live preview" checkbox is checked.
99
   */
100
  Drupal.behaviors.livePreview = {
101
    attach: function (context) {
102
      $('input#edit-displays-live-preview', context).once('views-ajax-processed').click(function() {
103
        if ($(this).is(':checked')) {
104
          $('#preview-submit').click();
105
        }
106
      });
107
    }
108
  }
109

    
110
  /**
111
   * Sync preview display.
112
   */
113
  Drupal.behaviors.syncPreviewDisplay = {
114
    attach: function (context) {
115
      $("#views-tabset a").once('views-ajax-processed').click(function() {
116
        var href = $(this).attr('href');
117
        // Cut of #views-tabset.
118
        var display_id = href.substr(11);
119
        // Set the form element.
120
        $("#views-live-preview #preview-display-id").val(display_id);
121
      }).addClass('views-ajax-processed');
122
    }
123
  }
124

    
125
  Drupal.behaviors.viewsAjax = {
126
    collapseReplaced: false,
127
    attach: function (context, settings) {
128
      if (!settings.views) {
129
        return;
130
      }
131
      // Create a jQuery UI dialog, but leave it closed.
132
      var dialog_area = $(settings.views.ajax.popup, context);
133
      dialog_area.dialog({
134
        'autoOpen': false,
135
        'dialogClass': 'views-ui-dialog',
136
        'modal': true,
137
        'position': 'center',
138
        'resizable': false,
139
        'width': 750
140
      });
141

    
142
      var base_element_settings = {
143
        'event': 'click',
144
        'progress': { 'type': 'throbber' }
145
      };
146
      // Bind AJAX behaviors to all items showing the class.
147
      $('a.views-ajax-link', context).once('views-ajax-processed').each(function () {
148
        var element_settings = base_element_settings;
149
        // Set the URL to go to the anchor.
150
        if ($(this).attr('href')) {
151
          element_settings.url = $(this).attr('href');
152
        }
153
        var base = $(this).attr('id');
154
        Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
155
      });
156

    
157
      $('div#views-live-preview a')
158
        .once('views-ajax-processed').each(function () {
159
        // We don't bind to links without a URL.
160
        if (!$(this).attr('href')) {
161
          return true;
162
        }
163

    
164
        var element_settings = base_element_settings;
165
        // Set the URL to go to the anchor.
166
        element_settings.url = $(this).attr('href');
167
        if (Drupal.Views.getPath(element_settings.url).substring(0, 21) != 'admin/structure/views') {
168
          return true;
169
        }
170

    
171
        element_settings.wrapper = 'views-live-preview';
172
        element_settings.method = 'html';
173
        var base = $(this).attr('id');
174
        Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
175
      });
176

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

    
194
        element_settings.wrapper = 'views-live-preview';
195
        element_settings.method = 'html';
196
        element_settings.event = 'click';
197

    
198
        var base = $(this).attr('id');
199
        Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
200
      });
201

    
202
      if (!this.collapseReplaced && Drupal.collapseScrollIntoView) {
203
        this.collapseReplaced = true;
204
        Drupal.collapseScrollIntoView = function (node) {
205
          for (var $parent = $(node); $parent.get(0) != document && $parent.size() != 0; $parent = $parent.parent()) {
206
            if ($parent.css('overflow') == 'scroll' || $parent.css('overflow') == 'auto') {
207
              if (Drupal.viewsUi.resizeModal) {
208
                // If the modal is already at the max height, don't bother with
209
                // this since the only reason to do it is to grow the modal.
210
                if ($('.views-ui-dialog').height() < parseInt($(window).height() * .8)) {
211
                  Drupal.viewsUi.resizeModal('', true);
212
                }
213
              }
214
              return;
215
            }
216
          }
217

    
218
          var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
219
          var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
220
          var posY = $(node).offset().top;
221
          var fudge = 55;
222
          if (posY + node.offsetHeight + fudge > h + offset) {
223
            if (node.offsetHeight > h) {
224
              window.scrollTo(0, posY);
225
            }
226
            else {
227
              window.scrollTo(0, posY + node.offsetHeight - h + fudge);
228
            }
229
          }
230
        };
231
      }
232
    }
233
  };
234

    
235
})(jQuery);