3 * Handles AJAX fetching of views, including filter submission and response.
6 (function ($, Drupal, drupalSettings) {
11 * Attaches the AJAX behavior to exposed filters forms and key View links.
13 * @type {Drupal~behavior}
15 * @prop {Drupal~behaviorAttach} attach
16 * Attaches ajaxView functionality to relevant elements.
18 Drupal.behaviors.ViewsAjaxView = {};
19 Drupal.behaviors.ViewsAjaxView.attach = function () {
20 if (drupalSettings && drupalSettings.views && drupalSettings.views.ajaxViews) {
21 var ajaxViews = drupalSettings.views.ajaxViews;
22 for (var i in ajaxViews) {
23 if (ajaxViews.hasOwnProperty(i)) {
24 Drupal.views.instances[i] = new Drupal.views.ajaxView(ajaxViews[i]);
36 * @type {object.<string, Drupal.views.ajaxView>}
38 Drupal.views.instances = {};
41 * Javascript object for a certain view.
45 * @param {object} settings
46 * Settings object for the ajax view.
47 * @param {string} settings.view_dom_id
48 * The DOM id of the view.
50 Drupal.views.ajaxView = function (settings) {
51 var selector = '.js-view-dom-id-' + settings.view_dom_id;
52 this.$view = $(selector);
54 // Retrieve the path to use for views' ajax.
55 var ajax_path = drupalSettings.views.ajax_path;
57 // If there are multiple views this might've ended up showing up multiple
59 if (ajax_path.constructor.toString().indexOf('Array') !== -1) {
60 ajax_path = ajax_path[0];
63 // Check if there are any GET parameters to send to views.
64 var queryString = window.location.search || '';
65 if (queryString !== '') {
66 // Remove the question mark and Drupal path component if any.
67 queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
68 if (queryString !== '') {
69 // If there is a '?' in ajax_path, clean url are on and & should be
70 // used to add parameters.
71 queryString = ((/\?/.test(ajax_path)) ? '&' : '?') + queryString;
75 this.element_settings = {
76 url: ajax_path + queryString,
81 progress: {type: 'fullscreen'}
84 this.settings = settings;
86 // Add the ajax to exposed forms.
87 this.$exposed_form = $('form#views-exposed-form-' + settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-'));
88 this.$exposed_form.once('exposed-form').each($.proxy(this.attachExposedFormAjax, this));
90 // Add the ajax to pagers.
92 // Don't attach to nested views. Doing so would attach multiple behaviors
93 // to a given element.
94 .filter($.proxy(this.filterNestedViews, this))
95 .once('ajax-pager').each($.proxy(this.attachPagerAjax, this));
97 // Add a trigger to update this view specifically. In order to trigger a
98 // refresh use the following code.
101 // $('.view-name').trigger('RefreshView');
103 var self_settings = $.extend({}, this.element_settings, {
104 event: 'RefreshView',
106 element: this.$view.get(0)
108 this.refreshViewAjax = Drupal.ajax(self_settings);
114 Drupal.views.ajaxView.prototype.attachExposedFormAjax = function () {
116 this.exposedFormAjax = [];
117 // Exclude the reset buttons so no AJAX behaviours are bound. Many things
118 // break during the form reset phase if using AJAX.
119 $('input[type=submit], input[type=image]', this.$exposed_form).not('[data-drupal-selector=edit-reset]').each(function (index) {
120 var self_settings = $.extend({}, that.element_settings, {
121 base: $(this).attr('id'),
124 that.exposedFormAjax[index] = Drupal.ajax(self_settings);
130 * If there is at least one parent with a view class return false.
132 Drupal.views.ajaxView.prototype.filterNestedViews = function () {
133 // If there is at least one parent with a view class, this view
134 // is nested (e.g., an attachment). Bail.
135 return !this.$view.parents('.view').length;
139 * Attach the ajax behavior to each link.
141 Drupal.views.ajaxView.prototype.attachPagerAjax = function () {
142 this.$view.find('ul.js-pager__items > li > a, th.views-field a, .attachment .views-summary a')
143 .each($.proxy(this.attachPagerLinkAjax, this));
147 * Attach the ajax behavior to a singe link.
149 * @param {string} [id]
150 * The ID of the link.
151 * @param {HTMLElement} link
154 Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function (id, link) {
157 var href = $link.attr('href');
158 // Construct an object using the settings defaults and then overriding
159 // with data specific to the link.
163 Drupal.Views.parseQueryString(href),
164 // Extract argument data from the URL.
165 Drupal.Views.parseViewArgs(href, this.settings.view_base_path)
168 var self_settings = $.extend({}, this.element_settings, {
173 this.pagerAjax = Drupal.ajax(self_settings);
177 * Views scroll to top ajax command.
179 * @param {Drupal.Ajax} [ajax]
180 * A {@link Drupal.ajax} object.
181 * @param {object} response
183 * @param {string} response.selector
186 Drupal.AjaxCommands.prototype.viewsScrollTop = function (ajax, response) {
187 // Scroll to the top of the view. This will allow users
188 // to browse newly loaded content after e.g. clicking a pager
190 var offset = $(response.selector).offset();
191 // We can't guarantee that the scrollable object should be
192 // the body, as the view could be embedded in something
193 // more complex such as a modal popup. Recurse up the DOM
194 // and scroll the first element that has a non-zero top.
195 var scrollTarget = response.selector;
196 while ($(scrollTarget).scrollTop() === 0 && $(scrollTarget).parent()) {
197 scrollTarget = $(scrollTarget).parent();
199 // Only scroll upward.
200 if (offset.top - 10 < $(scrollTarget).scrollTop()) {
201 $(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
205 })(jQuery, Drupal, drupalSettings);