Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / js / ajax_view.es6.js
1 /**
2  * @file
3  * Handles AJAX fetching of views, including filter submission and response.
4  */
5
6 (function ($, Drupal, drupalSettings) {
7   /**
8    * Attaches the AJAX behavior to exposed filters forms and key View links.
9    *
10    * @type {Drupal~behavior}
11    *
12    * @prop {Drupal~behaviorAttach} attach
13    *   Attaches ajaxView functionality to relevant elements.
14    */
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]);
22         }
23       }
24     }
25   };
26
27   /**
28    * @namespace
29    */
30   Drupal.views = {};
31
32   /**
33    * @type {object.<string, Drupal.views.ajaxView>}
34    */
35   Drupal.views.instances = {};
36
37   /**
38    * Javascript object for a certain view.
39    *
40    * @constructor
41    *
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.
46    */
47   Drupal.views.ajaxView = function (settings) {
48     const selector = `.js-view-dom-id-${settings.view_dom_id}`;
49     this.$view = $(selector);
50
51     // Retrieve the path to use for views' ajax.
52     let ajax_path = drupalSettings.views.ajax_path;
53
54     // If there are multiple views this might've ended up showing up multiple
55     // times.
56     if (ajax_path.constructor.toString().indexOf('Array') !== -1) {
57       ajax_path = ajax_path[0];
58     }
59
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;
69       }
70     }
71
72     this.element_settings = {
73       url: ajax_path + queryString,
74       submit: settings,
75       setClick: true,
76       event: 'click',
77       selector,
78       progress: { type: 'fullscreen' },
79     };
80
81     this.settings = settings;
82
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));
86
87     // Add the ajax to pagers.
88     this.$view
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));
93
94     // Add a trigger to update this view specifically. In order to trigger a
95     // refresh use the following code.
96     //
97     // @code
98     // $('.view-name').trigger('RefreshView');
99     // @endcode
100     const self_settings = $.extend({}, this.element_settings, {
101       event: 'RefreshView',
102       base: this.selector,
103       element: this.$view.get(0),
104     });
105     this.refreshViewAjax = Drupal.ajax(self_settings);
106   };
107
108   /**
109    * @method
110    */
111   Drupal.views.ajaxView.prototype.attachExposedFormAjax = function () {
112     const that = this;
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'),
119         element: this,
120       });
121       that.exposedFormAjax[index] = Drupal.ajax(self_settings);
122     });
123   };
124
125   /**
126    * @return {bool}
127    *   If there is at least one parent with a view class return false.
128    */
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;
133   };
134
135   /**
136    * Attach the ajax behavior to each link.
137    */
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));
141   };
142
143   /**
144    * Attach the ajax behavior to a singe link.
145    *
146    * @param {string} [id]
147    *   The ID of the link.
148    * @param {HTMLElement} link
149    *   The link element.
150    */
151   Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function (id, link) {
152     const $link = $(link);
153     const viewData = {};
154     const href = $link.attr('href');
155     // Construct an object using the settings defaults and then overriding
156     // with data specific to the link.
157     $.extend(
158       viewData,
159       this.settings,
160       Drupal.Views.parseQueryString(href),
161       // Extract argument data from the URL.
162       Drupal.Views.parseViewArgs(href, this.settings.view_base_path),
163     );
164
165     const self_settings = $.extend({}, this.element_settings, {
166       submit: viewData,
167       base: false,
168       element: link,
169     });
170     this.pagerAjax = Drupal.ajax(self_settings);
171   };
172
173   /**
174    * Views scroll to top ajax command.
175    *
176    * @param {Drupal.Ajax} [ajax]
177    *   A {@link Drupal.ajax} object.
178    * @param {object} response
179    *   Ajax response.
180    * @param {string} response.selector
181    *   Selector to use.
182    */
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
186     // link.
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();
195     }
196     // Only scroll upward.
197     if (offset.top - 10 < $(scrollTarget).scrollTop()) {
198       $(scrollTarget).animate({ scrollTop: (offset.top - 10) }, 500);
199     }
200   };
201 }(jQuery, Drupal, drupalSettings));