Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / js / ajax.js @ 13c3c9b4

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
    if (response.changed) {
65
      $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
66
    }
67
  };
68

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
237
})(jQuery);