diff --git a/core/modules/views/js/ajax_view.js b/core/modules/views/js/ajax_view.js index 85975b5..cc1a654 100644 --- a/core/modules/views/js/ajax_view.js +++ b/core/modules/views/js/ajax_view.js @@ -18,11 +18,10 @@ Drupal.behaviors.ViewsAjaxView = {}; Drupal.behaviors.ViewsAjaxView.attach = function () { if (drupalSettings && drupalSettings.views && drupalSettings.views.ajaxViews) { - var ajaxViews = drupalSettings.views.ajaxViews; + var ajaxViews = Drupal.views.sortByNestingLevel(drupalSettings.views.ajaxViews); for (var i in ajaxViews) { - if (ajaxViews.hasOwnProperty(i)) { - Drupal.views.instances[i] = new Drupal.views.ajaxView(ajaxViews[i]); - } + var item = ajaxViews[i]; + Drupal.views.instances[item.key] = new Drupal.views.ajaxView(item.value); } } }; @@ -38,6 +37,34 @@ Drupal.views.instances = {}; /** + * Helper function to sort views by nesting level. + * + * @param {object} ajaxViews + * Object containing ajax view. + * @return {Array} + */ + Drupal.views.sortByNestingLevel = function (ajaxViews) { + var ajaxViewsArray = []; + + for (var i in ajaxViews) { + if (ajaxViews.hasOwnProperty(i)) { + ajaxViews[i].selector = '.js-view-dom-id-' + ajaxViews[i].view_dom_id; + + ajaxViewsArray.push({ + key: i, + value: ajaxViews[i], + nestingLevel: $(ajaxViews[i].selector).parents('.view').length + }); + } + } + + // Sort views by nesting level descending. The goal is to start with the innermost. + return ajaxViewsArray.sort(function (a, b) { + return b.nestingLevel - a.nestingLevel; + }); + } + + /** * Javascript object for a certain view. * * @constructor @@ -48,8 +75,7 @@ * The DOM id of the view. */ Drupal.views.ajaxView = function (settings) { - var selector = '.js-view-dom-id-' + settings.view_dom_id; - this.$view = $(selector); + this.$view = $(settings.selector); // Retrieve the path to use for views' ajax. var ajax_path = drupalSettings.views.ajax_path; @@ -77,7 +103,7 @@ submit: settings, setClick: true, event: 'click', - selector: selector, + selector: settings.selector, progress: {type: 'fullscreen'} }; @@ -88,11 +114,7 @@ this.$exposed_form.once('exposed-form').each($.proxy(this.attachExposedFormAjax, this)); // Add the ajax to pagers. - this.$view - // Don't attach to nested views. Doing so would attach multiple behaviors - // to a given element. - .filter($.proxy(this.filterNestedViews, this)) - .once('ajax-pager').each($.proxy(this.attachPagerAjax, this)); + this.$view.once('ajax-pager').each($.proxy(this.attachPagerAjax, this)); // Add a trigger to update this view specifically. In order to trigger a // refresh use the following code. @@ -126,20 +148,11 @@ }; /** - * @return {bool} - * If there is at least one parent with a view class return false. - */ - 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. - return !this.$view.parents('.view').length; - }; - - /** * Attach the ajax behavior to each link. */ Drupal.views.ajaxView.prototype.attachPagerAjax = function () { this.$view.find('ul.js-pager__items > li > a, th.views-field a, .attachment .views-summary a') + .once('attach-pager-ajax') .each($.proxy(this.attachPagerLinkAjax, this)); };