3 * Handles AJAX fetching of views, including filter submission and response.
8 * Attaches the AJAX behavior to Views exposed filter forms and key View links.
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
);
20 Drupal
.views
.instances
= {};
23 * Javascript object for a certain view.
25 Drupal
.views
.ajaxView = function(settings
) {
26 var selector
= '.view-dom-id-' + settings
.view_dom_id
;
27 this.$view
= $(selector
);
29 // Retrieve the path to use for views' ajax.
30 var ajax_path
= Drupal
.settings
.views
.ajax_path
;
32 // If there are multiple views this might've ended up showing up multiple times.
33 if (ajax_path
.constructor.toString().indexOf("Array") != -1) {
34 ajax_path
= ajax_path
[0];
37 // Check if there are any GET parameters to send to views.
38 var queryString
= window
.location
.search
|| '';
39 if (queryString
!== '') {
40 // Remove the question mark and Drupal path component if any.
41 var queryString
= queryString
.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
42 if (queryString
!== '') {
43 // If there is a '?' in ajax_path, clean url are on and & should be used to add parameters.
44 queryString
= ((/\?/.test(ajax_path
)) ? '&' : '?') + queryString
;
48 this.element_settings
= {
49 url
: ajax_path
+ queryString
,
54 progress
: { type
: 'throbber' }
57 this.settings
= settings
;
59 // Add the ajax to exposed forms.
60 this.$exposed_form
= $('#views-exposed-form-'+ settings
.view_name
.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_
/g
, '-'));
61 this.$exposed_form
.once(jQuery
.proxy(this.attachExposedFormAjax
, this));
63 // Add the ajax to pagers.
65 // Don't attach to nested views. Doing so would attach multiple behaviors
66 // to a given element.
67 .filter(jQuery
.proxy(this.filterNestedViews
, this))
68 .once(jQuery
.proxy(this.attachPagerAjax
, this));
70 // Add a trigger to update this view specifically. In order to trigger a
71 // refresh use the following code.
74 // jQuery('.view-name').trigger('RefreshView');
76 // Add a trigger to update this view specifically.
77 var self_settings
= this.element_settings
;
78 self_settings
.event
= 'RefreshView';
79 this.refreshViewAjax
= new Drupal
.ajax(this.selector
, this.$view
, self_settings
);
82 Drupal
.views
.ajaxView
.prototype.attachExposedFormAjax = function() {
83 var button
= $('input[type=submit], button[type=submit], input[type=image]', this.$exposed_form
);
86 this.exposedFormAjax
= new Drupal
.ajax($(button
).attr('id'), button
, this.element_settings
);
89 Drupal
.views
.ajaxView
.prototype.filterNestedViews= function() {
90 // If there is at least one parent with a view class, this view
91 // is nested (e.g., an attachment). Bail.
92 return !this.$view
.parents('.view').size();
96 * Attach the ajax behavior to each link.
98 Drupal
.views
.ajaxView
.prototype.attachPagerAjax = function() {
99 this.$view
.find('ul.pager > li > a, th.views-field a, .attachment .views-summary a')
100 .each(jQuery
.proxy(this.attachPagerLinkAjax
, this));
104 * Attach the ajax behavior to a singe link.
106 Drupal
.views
.ajaxView
.prototype.attachPagerLinkAjax = function(id
, link
) {
109 var href
= $link
.attr('href');
110 // Construct an object using the settings defaults and then overriding
111 // with data specific to the link.
115 Drupal
.Views
.parseQueryString(href
),
116 // Extract argument data from the URL.
117 Drupal
.Views
.parseViewArgs(href
, this.settings
.view_base_path
)
120 // For anchor tags, these will go to the target of the anchor rather
121 // than the usual location.
122 $.extend(viewData
, Drupal
.Views
.parseViewArgs(href
, this.settings
.view_base_path
));
124 this.element_settings
.submit
= viewData
;
125 this.pagerAjax
= new Drupal
.ajax(false, $link
, this.element_settings
);
128 Drupal
.ajax
.prototype.commands
.viewsScrollTop = function (ajax
, response
, status
) {
129 // Scroll to the top of the view. This will allow users
130 // to browse newly loaded content after e.g. clicking a pager
132 var offset
= $(response
.selector
).offset();
133 // We can't guarantee that the scrollable object should be
134 // the body, as the view could be embedded in something
135 // more complex such as a modal popup. Recurse up the DOM
136 // and scroll the first element that has a non-zero top.
137 var scrollTarget
= response
.selector
;
138 while ($(scrollTarget
).scrollTop() == 0 && $(scrollTarget
).parent()) {
139 scrollTarget
= $(scrollTarget
).parent();
141 // Only scroll upward
142 if (offset
.top
- 10 < $(scrollTarget
).scrollTop()) {
143 $(scrollTarget
).animate({scrollTop
: (offset
.top
- 10)}, 500);