1/** 2 * @file 3 * Some basic behaviors and utility functions for Views. 4 */ 5 6(function ($, Drupal, drupalSettings) { 7 /** 8 * @namespace 9 */ 10 Drupal.Views = {}; 11 12 /** 13 * Helper function to parse a querystring. 14 * 15 * @param {string} query 16 * The querystring to parse. 17 * 18 * @return {object} 19 * A map of query parameters. 20 */ 21 Drupal.Views.parseQueryString = function (query) { 22 const args = {}; 23 const pos = query.indexOf('?'); 24 if (pos !== -1) { 25 query = query.substring(pos + 1); 26 } 27 let pair; 28 const pairs = query.split('&'); 29 for (let i = 0; i < pairs.length; i++) { 30 pair = pairs[i].split('='); 31 // Ignore the 'q' path argument, if present. 32 if (pair[0] !== 'q') { 33 if (pair[1]) { 34 args[decodeURIComponent(pair[0].replace(/\+/g, ' '))] = 35 decodeURIComponent(pair[1].replace(/\+/g, ' ')); 36 } else { 37 args[decodeURIComponent(pair[0].replace(/\+/g, ' '))] = ''; 38 } 39 } 40 } 41 return args; 42 }; 43 44 /** 45 * Helper function to return a view's arguments based on a path. 46 * 47 * @param {string} href 48 * The href to check. 49 * @param {string} viewPath 50 * The views path to check. 51 * 52 * @return {object} 53 * An object containing `view_args` and `view_path`. 54 */ 55 Drupal.Views.parseViewArgs = function (href, viewPath) { 56 const returnObj = {}; 57 const path = Drupal.Views.getPath(href); 58 // Get viewPath URL without baseUrl portion. 59 const viewHref = Drupal.url(viewPath).substring( 60 drupalSettings.path.baseUrl.length, 61 ); 62 // Ensure we have a correct path. 63 if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) { 64 returnObj.view_args = decodeURIComponent( 65 path.substring(viewHref.length + 1, path.length), 66 ); 67 returnObj.view_path = path; 68 } 69 return returnObj; 70 }; 71 72 /** 73 * Strip off the protocol plus domain from an href. 74 * 75 * @param {string} href 76 * The href to strip. 77 * 78 * @return {string} 79 * The href without the protocol and domain. 80 */ 81 Drupal.Views.pathPortion = function (href) { 82 // Remove e.g. http://example.com if present. 83 const protocol = window.location.protocol; 84 if (href.substring(0, protocol.length) === protocol) { 85 // 2 is the length of the '//' that normally follows the protocol. 86 href = href.substring(href.indexOf('/', protocol.length + 2)); 87 } 88 return href; 89 }; 90 91 /** 92 * Return the Drupal path portion of an href. 93 * 94 * @param {string} href 95 * The href to check. 96 * 97 * @return {string} 98 * An internal path. 99 */ 100 Drupal.Views.getPath = function (href) { 101 href = Drupal.Views.pathPortion(href); 102 href = href.substring(drupalSettings.path.baseUrl.length, href.length); 103 // 3 is the length of the '?q=' added to the URL without clean URLs. 104 if (href.substring(0, 3) === '?q=') { 105 href = href.substring(3, href.length); 106 } 107 const chars = ['#', '?', '&']; 108 for (let i = 0; i < chars.length; i++) { 109 if (href.indexOf(chars[i]) > -1) { 110 href = href.substr(0, href.indexOf(chars[i])); 111 } 112 } 113 return href; 114 }; 115})(jQuery, Drupal, drupalSettings);
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.