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]) {
33 args[decodeURIComponent(pair[0].replace(/\+/g, ' '))] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
40 * Helper function to return a view's arguments based on a path.
42 * @param {string} href
44 * @param {string} viewPath
45 * The views path to check.
48 * An object containing `view_args` and `view_path`.
50 Drupal.Views.parseViewArgs = function (href, viewPath) {
52 const path = Drupal.Views.getPath(href);
53 // Get viewPath url without baseUrl portion.
54 const viewHref = Drupal.url(viewPath).substring(drupalSettings.path.baseUrl.length);
55 // Ensure we have a correct path.
56 if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) {
57 returnObj.view_args = decodeURIComponent(path.substring(viewHref.length + 1, path.length));
58 returnObj.view_path = path;
64 * Strip off the protocol plus domain from an href.
66 * @param {string} href
70 * The href without the protocol and domain.
72 Drupal.Views.pathPortion = function (href) {
73 // Remove e.g. http://example.com if present.
74 const protocol = window.location.protocol;
75 if (href.substring(0, protocol.length) === protocol) {
76 // 2 is the length of the '//' that normally follows the protocol.
77 href = href.substring(href.indexOf('/', protocol.length + 2));
83 * Return the Drupal path portion of an href.
85 * @param {string} href
91 Drupal.Views.getPath = function (href) {
92 href = Drupal.Views.pathPortion(href);
93 href = href.substring(drupalSettings.path.baseUrl.length, href.length);
94 // 3 is the length of the '?q=' added to the url without clean urls.
95 if (href.substring(0, 3) === '?q=') {
96 href = href.substring(3, href.length);
98 const chars = ['#', '?', '&'];
99 for (let i = 0; i < chars.length; i++) {
100 if (href.indexOf(chars[i]) > -1) {
101 href = href.substr(0, href.indexOf(chars[i]));
106 }(jQuery, Drupal, drupalSettings));