base.js 2.8 KB

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