diff --git a/js/ajax_view.js b/js/ajax_view.js index ce6cc52..7ef4969 100644 --- a/js/ajax_view.js +++ b/js/ajax_view.js @@ -11,7 +11,10 @@ Drupal.behaviors.ViewsAjaxView = {}; Drupal.behaviors.ViewsAjaxView.attach = function() { if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) { $.each(Drupal.settings.views.ajaxViews, function(i, settings) { - Drupal.views.instances[i] = new Drupal.views.ajaxView(settings); + var ajaxView = new Drupal.views.ajaxView(settings); + // Ensure the ajax handling is kept in a global unique object even if this + // was called multiple times for the same view. + Drupal.views.instances[i] = $.extend({}, Drupal.views.instances[i], ajaxView); }); } }; @@ -60,12 +63,12 @@ Drupal.views.ajaxView = function(settings) { this.$exposed_form = $('#views-exposed-form-'+ settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-')); this.$exposed_form.once(jQuery.proxy(this.attachExposedFormAjax, this)); - // Add the ajax to pagers. + // Add the ajax to the different elements. this.$view // Don't attach to nested views. Doing so would attach multiple behaviors // to a given element. .filter(jQuery.proxy(this.filterNestedViews, this)) - .once(jQuery.proxy(this.attachPagerAjax, this)); + .once(jQuery.proxy(this.attachNonNestedAjax, this)); // Add a trigger to update this view specifically. In order to trigger a // refresh use the following code. @@ -86,6 +89,25 @@ Drupal.views.ajaxView.prototype.attachExposedFormAjax = function() { this.exposedFormAjax = new Drupal.ajax($(button).attr('id'), button, this.element_settings); }; +/** + * Attach the ajax behavior. + */ +Drupal.views.ajaxView.prototype.attachNonNestedAjax = function(index, elem) { + // Add ajax to the pager. + this.attachPagerAjax(); + // Add a trigger to update this view specifically. + this.attachRefreshViewAjax(index, elem); +}; + +/** + * Attach the ajax refresh behavior. + */ +Drupal.views.ajaxView.prototype.attachRefreshViewAjax = function(index, elem) { + var self_settings = this.element_settings; + self_settings.event = 'RefreshView'; + this.refreshViewAjax = new Drupal.ajax(this.$view, elem, self_settings); +}; + Drupal.views.ajaxView.prototype.filterNestedViews= function() { // If there is at least one parent with a view class, this view // is nested (e.g., an attachment). Bail. @@ -97,7 +119,7 @@ Drupal.views.ajaxView.prototype.filterNestedViews= function() { */ Drupal.views.ajaxView.prototype.attachPagerAjax = function() { this.$view.find('ul.pager > li > a, th.views-field a, .attachment .views-summary a') - .each(jQuery.proxy(this.attachPagerLinkAjax, this)); + .once(jQuery.proxy(this.attachPagerLinkAjax, this)); }; /** @@ -139,7 +161,7 @@ Drupal.ajax.prototype.commands.viewsScrollTop = function (ajax, response, status scrollTarget = $(scrollTarget).parent(); } // Only scroll upward - if (offset.top - 10 < $(scrollTarget).scrollTop()) { + if (offset && offset.top - 10 < $(scrollTarget).scrollTop()) { $(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500); } };