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