PageSourceSearch

https://neuroabilities.org/core/modules/views/js/ajax_view.js?sdg3vi

js neuroabilities.org collected 2026-09-28 06:41:22 UTC 5,638 bytes, 161 lines download raw bytes

1/**
2 * @file
3 * Handles AJAX fetching of views, including filter submission and response.
4 */
5(function ($) {
6
7/**
8 * Attaches the AJAX behavior to Views exposed filter forms and key View links.
9 */
10Backdrop.behaviors.ViewsAjaxView = {};
11Backdrop.behaviors.ViewsAjaxView.attach = function() {
12  if (Backdrop.settings && Backdrop.settings.views && Backdrop.settings.views.ajaxViews) {
13    $.each(Backdrop.settings.views.ajaxViews, function(i, settings) {
14      Backdrop.views.instances[i] = new Backdrop.views.ajaxView(settings);
15    });
16  }
17};
18/**
19 * Removes configuration and state from the page when a view is removed.
20 */
21  Backdrop.behaviors.ViewsAjaxView.detach = function(context) {
22  if (Backdrop.settings && Backdrop.settings.views && Backdrop.settings.views.ajaxViews) {
23    $.each(Backdrop.settings.views.ajaxViews, function(i, settings) {
24      var $removedView = $('.view-dom-id-' + settings.view_dom_id, context);
25      if ($removedView.length) {
26        delete Backdrop.settings.views.ajaxViews[i];
27        delete Backdrop.views.instances[i];
28      }
29    });
30  }
31}
32
33Backdrop.views = {};
34Backdrop.views.instances = {};
35
36/**
37 * Javascript object for a certain view.
38 */
39Backdrop.views.ajaxView = function(settings) {
40  var selector = '.view-dom-id-' + settings.view_dom_id;
41  this.$view = $(selector);
42
43  // Retrieve the path to use for views' ajax.
44  var ajax_path = Backdrop.settings.views.ajax_path;
45
46  // If there are multiple views this might've ended up showing up multiple times.
47  if (ajax_path.constructor.toString().indexOf("Array") != -1) {
48    ajax_path = ajax_path[0];
49  }
50
51  // Check if there are any GET parameters to send to views.
52  var queryString = window.location.search || '';
53  if (queryString !== '') {
54    // Remove the question mark and Backdrop path component if any.
55    queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
56    if (queryString !== '') {
57      // If there is a '?' in ajax_path, clean url are on and & should be used to add parameters.
58      queryString = ((/\?/.test(ajax_path)) ? '&' : '?') + queryString;
59    }
60  }
61
62  this.element_settings = {
63    url: ajax_path + queryString,
64    submit: settings,
65    setClick: true,
66    event: 'click',
67    selector: selector,
68    progress: { type: 'throbber' }
69  };
70
71  this.settings = settings;
72
73  // Add the ajax to exposed forms.
74  this.$exposed_form = $('form#views-exposed-form-'+ settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-'));
75  this.$exposed_form.once(jQuery.proxy(this.attachExposedFormAjax, this));
76
77  // Add the ajax to pagers.
78  this.$view
79    // Don't attach to nested views. Doing so would attach multiple behaviors
80    // to a given element.
81    .filter(jQuery.proxy(this.filterNestedViews, this))
82    .once(jQuery.proxy(this.attachPagerAjax, this));
83
84  // In order to trigger a refresh, use the following code:
85  // @code
86  // jQuery('.view-name').trigger('RefreshView');
87  // @endcode
88  //
89  // Add a trigger to update this view specifically.
90  var self_settings = this.element_settings;
91  self_settings.event = 'RefreshView';
92  this.refreshViewAjax = new Backdrop.ajax(this.selector, this.$view[0], self_settings);
93};
94
95Backdrop.views.ajaxView.prototype.attachExposedFormAjax = function() {
96  var button = $('input[type=submit], button[type=submit], input[type=image]', this.$exposed_form);
97  button = button[0];
98
99  this.exposedFormAjax = new Backdrop.ajax($(button).attr('id'), button, this.element_settings);
100};
101
102Backdrop.views.ajaxView.prototype.filterNestedViews= function() {
103  // If there is at least one parent with a view class, this view
104  // is nested (e.g., an attachment). Bail.
105  return !this.$view.parents('.view').length;
106};
107
108/**
109 * Attach the ajax behavior to each link.
110 */
111Backdrop.views.ajaxView.prototype.attachPagerAjax = function() {
112  this.$view.find('ul.pager > li > a, th.views-field a, .attachment .views-summary a')
113  .each(jQuery.proxy(this.attachPagerLinkAjax, this));
114};
115
116/**
117 * Attach the ajax behavior to a singe link.
118 */
119Backdrop.views.ajaxView.prototype.attachPagerLinkAjax = function(id, link) {
120  var $link = $(link);
121  var viewData = {};
122  var href = $link.attr('href');
123  // Construct an object using the settings defaults and then overriding
124  // with data specific to the link.
125  $.extend(
126    viewData,
127    this.settings,
128    Backdrop.Views.parseQueryString(href),
129    // Extract argument data from the URL.
130    Backdrop.Views.parseViewArgs(href, this.settings.view_base_path)
131  );
132
133  // For anchor tags, these will go to the target of the anchor rather
134  // than the usual location.
135  $.extend(viewData, Backdrop.Views.parseViewArgs(href, this.settings.view_base_path));
136
137  this.element_settings.submit = viewData;
138  this.pagerAjax = new Backdrop.ajax(false, link, this.element_settings);
139};
140
141Backdrop.ajax.prototype.commands.viewsScrollTop = function (ajax, response) {
142  // Scroll to the top of the view. This will allow users
143  // to browse newly loaded content after e.g. clicking a pager
144  // link.
145  var offset = $(response.selector).offset();
146
147  // We can't guarantee that the scrollable object should be
148  // the body, as the view could be embedded in something
149  // more complex such as a modal popup. Recurse up the DOM
150  // and scroll the first element that has a non-zero top.
151  var scrollTarget = response.selector;
152  while ($(scrollTarget).scrollTop() == 0 && $(scrollTarget).parent()) {
153    scrollTarget = $(scrollTarget).parent();
154  }
155  // Only scroll upward
156  if (offset.top - 10 < $(scrollTarget).scrollTop()) {
157    $(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
158  }
159};
160
161})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.