Chris@0: /** Chris@0: * @file Chris@0: * Some basic behaviors and utility functions for Views. Chris@0: */ Chris@0: Chris@17: (function($, Drupal, drupalSettings) { Chris@0: /** Chris@0: * @namespace Chris@0: */ Chris@0: Drupal.Views = {}; Chris@0: Chris@0: /** Chris@0: * Helper function to parse a querystring. Chris@0: * Chris@0: * @param {string} query Chris@0: * The querystring to parse. Chris@0: * Chris@0: * @return {object} Chris@0: * A map of query parameters. Chris@0: */ Chris@17: Drupal.Views.parseQueryString = function(query) { Chris@0: const args = {}; Chris@0: const pos = query.indexOf('?'); Chris@0: if (pos !== -1) { Chris@0: query = query.substring(pos + 1); Chris@0: } Chris@0: let pair; Chris@0: const pairs = query.split('&'); Chris@0: for (let i = 0; i < pairs.length; i++) { Chris@0: pair = pairs[i].split('='); Chris@0: // Ignore the 'q' path argument, if present. Chris@0: if (pair[0] !== 'q' && pair[1]) { Chris@17: args[ Chris@17: decodeURIComponent(pair[0].replace(/\+/g, ' ')) Chris@17: ] = decodeURIComponent(pair[1].replace(/\+/g, ' ')); Chris@0: } Chris@0: } Chris@0: return args; Chris@0: }; Chris@0: Chris@0: /** Chris@0: * Helper function to return a view's arguments based on a path. Chris@0: * Chris@0: * @param {string} href Chris@0: * The href to check. Chris@0: * @param {string} viewPath Chris@0: * The views path to check. Chris@0: * Chris@0: * @return {object} Chris@0: * An object containing `view_args` and `view_path`. Chris@0: */ Chris@17: Drupal.Views.parseViewArgs = function(href, viewPath) { Chris@0: const returnObj = {}; Chris@0: const path = Drupal.Views.getPath(href); Chris@0: // Get viewPath url without baseUrl portion. Chris@17: const viewHref = Drupal.url(viewPath).substring( Chris@17: drupalSettings.path.baseUrl.length, Chris@17: ); Chris@0: // Ensure we have a correct path. Chris@0: if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) { Chris@17: returnObj.view_args = decodeURIComponent( Chris@17: path.substring(viewHref.length + 1, path.length), Chris@17: ); Chris@0: returnObj.view_path = path; Chris@0: } Chris@0: return returnObj; Chris@0: }; Chris@0: Chris@0: /** Chris@0: * Strip off the protocol plus domain from an href. Chris@0: * Chris@0: * @param {string} href Chris@0: * The href to strip. Chris@0: * Chris@0: * @return {string} Chris@0: * The href without the protocol and domain. Chris@0: */ Chris@17: Drupal.Views.pathPortion = function(href) { Chris@0: // Remove e.g. http://example.com if present. Chris@0: const protocol = window.location.protocol; Chris@0: if (href.substring(0, protocol.length) === protocol) { Chris@0: // 2 is the length of the '//' that normally follows the protocol. Chris@0: href = href.substring(href.indexOf('/', protocol.length + 2)); Chris@0: } Chris@0: return href; Chris@0: }; Chris@0: Chris@0: /** Chris@0: * Return the Drupal path portion of an href. Chris@0: * Chris@0: * @param {string} href Chris@0: * The href to check. Chris@0: * Chris@0: * @return {string} Chris@0: * An internal path. Chris@0: */ Chris@17: Drupal.Views.getPath = function(href) { Chris@0: href = Drupal.Views.pathPortion(href); Chris@0: href = href.substring(drupalSettings.path.baseUrl.length, href.length); Chris@0: // 3 is the length of the '?q=' added to the url without clean urls. Chris@0: if (href.substring(0, 3) === '?q=') { Chris@0: href = href.substring(3, href.length); Chris@0: } Chris@0: const chars = ['#', '?', '&']; Chris@0: for (let i = 0; i < chars.length; i++) { Chris@0: if (href.indexOf(chars[i]) > -1) { Chris@0: href = href.substr(0, href.indexOf(chars[i])); Chris@0: } Chris@0: } Chris@0: return href; Chris@0: }; Chris@17: })(jQuery, Drupal, drupalSettings);