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