base.es6.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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[decodeURIComponent(pair[0].replace(/\+/g, ' '))] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
  32. }
  33. }
  34. return args;
  35. };
  36. /**
  37. * Helper function to return a view's arguments based on a path.
  38. *
  39. * @param {string} href
  40. * The href to check.
  41. * @param {string} viewPath
  42. * The views path to check.
  43. *
  44. * @return {object}
  45. * An object containing `view_args` and `view_path`.
  46. */
  47. Drupal.Views.parseViewArgs = function (href, viewPath) {
  48. const returnObj = {};
  49. const path = Drupal.Views.getPath(href);
  50. // Get viewPath url without baseUrl portion.
  51. const viewHref = Drupal.url(viewPath).substring(drupalSettings.path.baseUrl.length);
  52. // Ensure we have a correct path.
  53. if (viewHref && path.substring(0, viewHref.length + 1) === `${viewHref}/`) {
  54. returnObj.view_args = decodeURIComponent(path.substring(viewHref.length + 1, path.length));
  55. returnObj.view_path = path;
  56. }
  57. return returnObj;
  58. };
  59. /**
  60. * Strip off the protocol plus domain from an href.
  61. *
  62. * @param {string} href
  63. * The href to strip.
  64. *
  65. * @return {string}
  66. * The href without the protocol and domain.
  67. */
  68. Drupal.Views.pathPortion = function (href) {
  69. // Remove e.g. http://example.com if present.
  70. const protocol = window.location.protocol;
  71. if (href.substring(0, protocol.length) === protocol) {
  72. // 2 is the length of the '//' that normally follows the protocol.
  73. href = href.substring(href.indexOf('/', protocol.length + 2));
  74. }
  75. return href;
  76. };
  77. /**
  78. * Return the Drupal path portion of an href.
  79. *
  80. * @param {string} href
  81. * The href to check.
  82. *
  83. * @return {string}
  84. * An internal path.
  85. */
  86. Drupal.Views.getPath = function (href) {
  87. href = Drupal.Views.pathPortion(href);
  88. href = href.substring(drupalSettings.path.baseUrl.length, href.length);
  89. // 3 is the length of the '?q=' added to the url without clean urls.
  90. if (href.substring(0, 3) === '?q=') {
  91. href = href.substring(3, href.length);
  92. }
  93. const chars = ['#', '?', '&'];
  94. for (let i = 0; i < chars.length; i++) {
  95. if (href.indexOf(chars[i]) > -1) {
  96. href = href.substr(0, href.indexOf(chars[i]));
  97. }
  98. }
  99. return href;
  100. };
  101. }(jQuery, Drupal, drupalSettings));