3 * Some basic behaviors and utility functions for Views.
6 (function ($, Drupal, drupalSettings) {
16 * Helper function to parse a querystring.
18 * @param {string} query
19 * The querystring to parse.
22 * A map of query parameters.
24 Drupal.Views.parseQueryString = function (query) {
26 var pos = query.indexOf('?');
28 query = query.substring(pos + 1);
31 var pairs = query.split('&');
32 for (var i = 0; i < pairs.length; i++) {
33 pair = pairs[i].split('=');
34 // Ignore the 'q' path argument, if present.
35 if (pair[0] !== 'q' && pair[1]) {
36 args[decodeURIComponent(pair[0].replace(/\+/g, ' '))] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
43 * Helper function to return a view's arguments based on a path.
45 * @param {string} href
47 * @param {string} viewPath
48 * The views path to check.
51 * An object containing `view_args` and `view_path`.
53 Drupal.Views.parseViewArgs = function (href, viewPath) {
55 var path = Drupal.Views.getPath(href);
56 // Get viewPath url without baseUrl portion.
57 var viewHref = Drupal.url(viewPath).substring(drupalSettings.path.baseUrl.length);
58 // Ensure we have a correct path.
59 if (viewHref && path.substring(0, viewHref.length + 1) === viewHref + '/') {
60 returnObj.view_args = decodeURIComponent(path.substring(viewHref.length + 1, path.length));
61 returnObj.view_path = path;
67 * Strip off the protocol plus domain from an href.
69 * @param {string} href
73 * The href without the protocol and domain.
75 Drupal.Views.pathPortion = function (href) {
76 // Remove e.g. http://example.com if present.
77 var protocol = window.location.protocol;
78 if (href.substring(0, protocol.length) === protocol) {
79 // 2 is the length of the '//' that normally follows the protocol.
80 href = href.substring(href.indexOf('/', protocol.length + 2));
86 * Return the Drupal path portion of an href.
88 * @param {string} href
94 Drupal.Views.getPath = function (href) {
95 href = Drupal.Views.pathPortion(href);
96 href = href.substring(drupalSettings.path.baseUrl.length, href.length);
97 // 3 is the length of the '?q=' added to the url without clean urls.
98 if (href.substring(0, 3) === '?q=') {
99 href = href.substring(3, href.length);
101 var chars = ['#', '?', '&'];
102 for (var i = 0; i < chars.length; i++) {
103 if (href.indexOf(chars[i]) > -1) {
104 href = href.substr(0, href.indexOf(chars[i]));
110 })(jQuery, Drupal, drupalSettings);