3 * Handles AJAX fetching of views, including filter submission and response.
6 (function ($, Drupal, drupalSettings) {
8 * Attaches the AJAX behavior to exposed filters forms and key View links.
10 * @type {Drupal~behavior}
12 * @prop {Drupal~behaviorAttach} attach
13 * Attaches ajaxView functionality to relevant elements.
15 Drupal.behaviors.ViewsAjaxView = {};
16 Drupal.behaviors.ViewsAjaxView.attach = function () {
17 if (drupalSettings && drupalSettings.views && drupalSettings.views.ajaxViews) {
18 const ajaxViews = drupalSettings.views.ajaxViews;
19 for (const i in ajaxViews) {
20 if (ajaxViews.hasOwnProperty(i)) {
21 Drupal.views.instances[i] = new Drupal.views.ajaxView(ajaxViews[i]);
33 * @type {object.<string, Drupal.views.ajaxView>}
35 Drupal.views.instances = {};
38 * Javascript object for a certain view.
42 * @param {object} settings
43 * Settings object for the ajax view.
44 * @param {string} settings.view_dom_id
45 * The DOM id of the view.
47 Drupal.views.ajaxView = function (settings) {
48 const selector = `.js-view-dom-id-${settings.view_dom_id}`;
49 this.$view = $(selector);
51 // Retrieve the path to use for views' ajax.
52 let ajax_path = drupalSettings.views.ajax_path;
54 // If there are multiple views this might've ended up showing up multiple
56 if (ajax_path.constructor.toString().indexOf('Array') !== -1) {
57 ajax_path = ajax_path[0];
60 // Check if there are any GET parameters to send to views.
61 let queryString = window.location.search || '';
62 if (queryString !== '') {
63 // Remove the question mark and Drupal path component if any.
64 queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
65 if (queryString !== '') {
66 // If there is a '?' in ajax_path, clean url are on and & should be
67 // used to add parameters.
68 queryString = ((/\?/.test(ajax_path)) ? '&' : '?') + queryString;
72 this.element_settings = {
73 url: ajax_path + queryString,
78 progress: { type: 'fullscreen' },
81 this.settings = settings;
83 // Add the ajax to exposed forms.
84 this.$exposed_form = $(`form#views-exposed-form-${settings.view_name.replace(/_/g, '-')}-${settings.view_display_id.replace(/_/g, '-')}`);
85 this.$exposed_form.once('exposed-form').each($.proxy(this.attachExposedFormAjax, this));
87 // Add the ajax to pagers.
89 // Don't attach to nested views. Doing so would attach multiple behaviors
90 // to a given element.
91 .filter($.proxy(this.filterNestedViews, this))
92 .once('ajax-pager').each($.proxy(this.attachPagerAjax, this));
94 // Add a trigger to update this view specifically. In order to trigger a
95 // refresh use the following code.
98 // $('.view-name').trigger('RefreshView');
100 const self_settings = $.extend({}, this.element_settings, {
101 event: 'RefreshView',
103 element: this.$view.get(0),
105 this.refreshViewAjax = Drupal.ajax(self_settings);
111 Drupal.views.ajaxView.prototype.attachExposedFormAjax = function () {
113 this.exposedFormAjax = [];
114 // Exclude the reset buttons so no AJAX behaviours are bound. Many things
115 // break during the form reset phase if using AJAX.
116 $('input[type=submit], input[type=image]', this.$exposed_form).not('[data-drupal-selector=edit-reset]').each(function (index) {
117 const self_settings = $.extend({}, that.element_settings, {
118 base: $(this).attr('id'),
121 that.exposedFormAjax[index] = Drupal.ajax(self_settings);
127 * If there is at least one parent with a view class return false.
129 Drupal.views.ajaxView.prototype.filterNestedViews = function () {
130 // If there is at least one parent with a view class, this view
131 // is nested (e.g., an attachment). Bail.
132 return !this.$view.parents('.view').length;
136 * Attach the ajax behavior to each link.
138 Drupal.views.ajaxView.prototype.attachPagerAjax = function () {
139 this.$view.find('ul.js-pager__items > li > a, th.views-field a, .attachment .views-summary a')
140 .each($.proxy(this.attachPagerLinkAjax, this));
144 * Attach the ajax behavior to a singe link.
146 * @param {string} [id]
147 * The ID of the link.
148 * @param {HTMLElement} link
151 Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function (id, link) {
152 const $link = $(link);
154 const href = $link.attr('href');
155 // Construct an object using the settings defaults and then overriding
156 // with data specific to the link.
160 Drupal.Views.parseQueryString(href),
161 // Extract argument data from the URL.
162 Drupal.Views.parseViewArgs(href, this.settings.view_base_path),
165 const self_settings = $.extend({}, this.element_settings, {
170 this.pagerAjax = Drupal.ajax(self_settings);
174 * Views scroll to top ajax command.
176 * @param {Drupal.Ajax} [ajax]
177 * A {@link Drupal.ajax} object.
178 * @param {object} response
180 * @param {string} response.selector
183 Drupal.AjaxCommands.prototype.viewsScrollTop = function (ajax, response) {
184 // Scroll to the top of the view. This will allow users
185 // to browse newly loaded content after e.g. clicking a pager
187 const offset = $(response.selector).offset();
188 // We can't guarantee that the scrollable object should be
189 // the body, as the view could be embedded in something
190 // more complex such as a modal popup. Recurse up the DOM
191 // and scroll the first element that has a non-zero top.
192 let scrollTarget = response.selector;
193 while ($(scrollTarget).scrollTop() === 0 && $(scrollTarget).parent()) {
194 scrollTarget = $(scrollTarget).parent();
196 // Only scroll upward.
197 if (offset.top - 10 < $(scrollTarget).scrollTop()) {
198 $(scrollTarget).animate({ scrollTop: (offset.top - 10) }, 500);
201 }(jQuery, Drupal, drupalSettings));