base.es6.js 3.0 KB

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