3 * Some basic behaviors and utility functions for Views.
6 (function($, Drupal, drupalSettings) {
13 * Helper function to parse a querystring.
15 * @param {string} query
16 * The querystring to parse.
19 * A map of query parameters.
21 Drupal.Views.parseQueryString = function(query) {
23 const pos = query.indexOf('?');
25 query = query.substring(pos + 1);
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' && pair[1]) {
34 decodeURIComponent(pair[0].replace(/\+/g, ' '))
35 ] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
42 * Helper function to return a view's arguments based on a path.
44 * @param {string} href
46 * @param {string} viewPath
47 * The views path to check.
50 * An object containing `view_args` and `view_path`.
52 Drupal.Views.parseViewArgs = function(href, viewPath) {
54 const path = Drupal.Views.getPath(href);
55 // Get viewPath url without baseUrl portion.
56 const viewHref = Drupal.url(viewPath).substring(
57 drupalSettings.path.baseUrl.length,
59 // Ensure we have a correct path.
60 if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) {
61 returnObj.view_args = decodeURIComponent(
62 path.substring(viewHref.length + 1, path.length),
64 returnObj.view_path = path;
70 * Strip off the protocol plus domain from an href.
72 * @param {string} href
76 * The href without the protocol and domain.
78 Drupal.Views.pathPortion = function(href) {
79 // Remove e.g. http://example.com if present.
80 const protocol = window.location.protocol;
81 if (href.substring(0, protocol.length) === protocol) {
82 // 2 is the length of the '//' that normally follows the protocol.
83 href = href.substring(href.indexOf('/', protocol.length + 2));
89 * Return the Drupal path portion of an href.
91 * @param {string} href
97 Drupal.Views.getPath = function(href) {
98 href = Drupal.Views.pathPortion(href);
99 href = href.substring(drupalSettings.path.baseUrl.length, href.length);
100 // 3 is the length of the '?q=' added to the url without clean urls.
101 if (href.substring(0, 3) === '?q=') {
102 href = href.substring(3, href.length);
104 const chars = ['#', '?', '&'];
105 for (let i = 0; i < chars.length; i++) {
106 if (href.indexOf(chars[i]) > -1) {
107 href = href.substr(0, href.indexOf(chars[i]));
112 })(jQuery, Drupal, drupalSettings);