Projet

Général

Profil

Paste
Télécharger (5,71 ko) Statistiques
| Branche: | Révision:

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

1
/**
2
 * @file
3
 * Handles AJAX fetching of views, including filter submission and response.
4
 */
5
(function ($) {
6

    
7
  /**
8
   * Attaches the AJAX behavior to exposed filter forms and key views links.
9
   */
10
  Drupal.behaviors.ViewsAjaxView = {};
11
  Drupal.behaviors.ViewsAjaxView.attach = function() {
12
    if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
13
      $.each(Drupal.settings.views.ajaxViews, function(i, settings) {
14
        Drupal.views.instances[i] = new Drupal.views.ajaxView(settings);
15
      });
16
    }
17
  };
18

    
19
  Drupal.views = {};
20
  Drupal.views.instances = {};
21

    
22
  /**
23
   * JavaScript object for a certain view.
24
   */
25
  Drupal.views.ajaxView = function(settings) {
26
    var selector = '.view-dom-id-' + settings.view_dom_id;
27
    this.$view = $(selector);
28

    
29
    // Retrieve the path to use for views' ajax.
30
    var ajax_path = Drupal.settings.views.ajax_path;
31

    
32
    // If there are multiple views this might've ended up showing up multiple
33
    // times.
34
    if (ajax_path.constructor.toString().indexOf("Array") != -1) {
35
      ajax_path = ajax_path[0];
36
    }
37

    
38
    // Check if there are any GET parameters to send to views.
39
    var queryString = window.location.search || '';
40
    if (queryString !== '') {
41
      // Remove the question mark and Drupal path component if any.
42
      var queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
43
      if (queryString !== '') {
44
        // If there is a '?' in ajax_path, clean url are on and & should be
45
        // used to add parameters.
46
        queryString = ((/\?/.test(ajax_path)) ? '&' : '?') + queryString;
47
      }
48
    }
49

    
50
    this.element_settings = {
51
      url: ajax_path + queryString,
52
      submit: settings,
53
      setClick: true,
54
      event: 'click',
55
      selector: selector,
56
      progress: {
57
        type: 'throbber'
58
      }
59
    };
60

    
61
    this.settings = settings;
62

    
63
    // Add the ajax to exposed forms.
64
    this.$exposed_form = $('#views-exposed-form-' + settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-'));
65
    this.$exposed_form.once(jQuery.proxy(this.attachExposedFormAjax, this));
66

    
67
    // Store Drupal.ajax objects here for all pager links.
68
    this.links = [];
69

    
70
    // Add the ajax to pagers.
71
    this.$view
72
      .once(jQuery.proxy(this.attachPagerAjax, this));
73

    
74
    // Add a trigger to update this view specifically. In order to trigger a
75
    // refresh use the following code.
76
    //
77
    // @code
78
    // jQuery('.view-name').trigger('RefreshView');
79
    // @endcode
80
    // Add a trigger to update this view specifically.
81
    var self_settings = this.element_settings;
82
    self_settings.event = 'RefreshView';
83
    var self = this;
84
    this.$view.once('refresh', function () {
85
      self.refreshViewAjax = new Drupal.ajax(self.selector, self.$view, self_settings);
86
    });
87
  };
88

    
89
  Drupal.views.ajaxView.prototype.attachExposedFormAjax = function() {
90
    var button = $('input[type=submit], button[type=submit], input[type=image]', this.$exposed_form);
91
    button = button[0];
92

    
93
    // Call the autocomplete submit before doing AJAX.
94
    $(button).click(function () {
95
      if (Drupal.autocompleteSubmit) {
96
        Drupal.autocompleteSubmit();
97
      }
98
    });
99

    
100
    this.exposedFormAjax = new Drupal.ajax($(button).attr('id'), button, this.element_settings);
101
  };
102

    
103
  /**
104
   * Attach the ajax behavior to each link.
105
   */
106
  Drupal.views.ajaxView.prototype.attachPagerAjax = function() {
107
    this.$view.find('ul.pager > li > a, ol.pager > li > a, th.views-field a, .attachment .views-summary a')
108
      .each(jQuery.proxy(this.attachPagerLinkAjax, this));
109
  };
110

    
111
  /**
112
   * Attach the ajax behavior to a single link.
113
   */
114
  Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function(id, link) {
115
    var $link = $(link);
116
    var viewData = {};
117
    var href = $link.attr('href');
118
    // Don't attach to pagers inside nested views.
119
    if ($link.closest('.view')[0] !== this.$view[0] &&
120
      $link.closest('.view').parent().hasClass('attachment') === false) {
121
      return;
122
    }
123

    
124
    // Provide a default page if none has been set. This must be done
125
    // prior to merging with settings to avoid accidentally using the
126
    // page landed on instead of page 1.
127
    if (typeof(viewData.page) === 'undefined') {
128
      viewData.page = 0;
129
    }
130

    
131
    // Construct an object using the settings defaults and then overriding
132
    // with data specific to the link.
133
    $.extend(
134
      viewData,
135
      this.settings,
136
      Drupal.Views.parseQueryString(href),
137
      // Extract argument data from the URL.
138
      Drupal.Views.parseViewArgs(href, this.settings.view_base_path)
139
    );
140

    
141
    // For anchor tags, these will go to the target of the anchor rather
142
    // than the usual location.
143
    $.extend(viewData, Drupal.Views.parseViewArgs(href, this.settings.view_base_path));
144

    
145
    this.element_settings.submit = viewData;
146
    this.pagerAjax = new Drupal.ajax(false, $link, this.element_settings);
147
    this.links.push(this.pagerAjax);
148
  };
149

    
150
  Drupal.ajax.prototype.commands.viewsScrollTop = function (ajax, response, status) {
151
    // Scroll to the top of the view. This will allow users
152
    // to browse newly loaded content after e.g. clicking a pager
153
    // link.
154
    var offset = $(response.selector).offset();
155
    // We can't guarantee that the scrollable object should be
156
    // the body, as the view could be embedded in something
157
    // more complex such as a modal popup. Recurse up the DOM
158
    // and scroll the first element that has a non-zero top.
159
    var scrollTarget = response.selector;
160
    while ($(scrollTarget).scrollTop() == 0 && $(scrollTarget).parent()) {
161
      scrollTarget = $(scrollTarget).parent();
162
    }
163
    // Only scroll upward.
164
    if (offset.top - 10 < $(scrollTarget).scrollTop()) {
165
      $(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
166
    }
167
  };
168

    
169
})(jQuery);