Drupal investigation

base.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Get viewPath url without baseUrl portion.
  52. var viewHref = Drupal.url(viewPath).substring(drupalSettings.path.baseUrl.length);
  53. // Ensure we have a correct path.
  54. if (viewHref && path.substring(0, viewHref.length + 1) === viewHref + '/') {
  55. returnObj.view_args = decodeURIComponent(path.substring(viewHref.length + 1, path.length));
  56. returnObj.view_path = path;
  57. }
  58. return returnObj;
  59. };
  60. /**
  61. * Strip off the protocol plus domain from an href.
  62. *
  63. * @param {string} href
  64. * The href to strip.
  65. *
  66. * @return {string}
  67. * The href without the protocol and domain.
  68. */
  69. Drupal.Views.pathPortion = function (href) {
  70. // Remove e.g. http://example.com if present.
  71. var protocol = window.location.protocol;
  72. if (href.substring(0, protocol.length) === protocol) {
  73. // 2 is the length of the '//' that normally follows the protocol.
  74. href = href.substring(href.indexOf('/', protocol.length + 2));
  75. }
  76. return href;
  77. };
  78. /**
  79. * Return the Drupal path portion of an href.
  80. *
  81. * @param {string} href
  82. * The href to check.
  83. *
  84. * @return {string}
  85. * An internal path.
  86. */
  87. Drupal.Views.getPath = function (href) {
  88. href = Drupal.Views.pathPortion(href);
  89. href = href.substring(drupalSettings.path.baseUrl.length, href.length);
  90. // 3 is the length of the '?q=' added to the url without clean urls.
  91. if (href.substring(0, 3) === '?q=') {
  92. href = href.substring(3, href.length);
  93. }
  94. var chars = ['#', '?', '&'];
  95. for (var i = 0; i < chars.length; i++) {
  96. if (href.indexOf(chars[i]) > -1) {
  97. href = href.substr(0, href.indexOf(chars[i]));
  98. }
  99. }
  100. return href;
  101. };
  102. })(jQuery, Drupal, drupalSettings);