base.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @file
  3. * Some basic behaviors and utility functions for Views.
  4. */
  5. (function ($) {
  6. Drupal.Views = {};
  7. /**
  8. * JQuery UI tabs, Views integration component.
  9. */
  10. Drupal.behaviors.viewsTabs = {
  11. attach: function (context) {
  12. if ($.viewsUi && $.viewsUi.tabs) {
  13. $('#views-tabset').once('views-processed').viewsTabs({
  14. selectedClass: 'active'
  15. });
  16. }
  17. $('a.views-remove-link').once('views-processed').click(function(event) {
  18. var id = $(this).attr('id').replace('views-remove-link-', '');
  19. $('#views-row-' + id).hide();
  20. $('#views-removed-' + id).attr('checked', true);
  21. event.preventDefault();
  22. });
  23. /**
  24. * Here is to handle display deletion
  25. * (checking in the hidden checkbox and hiding out the row).
  26. */
  27. $('a.display-remove-link')
  28. .addClass('display-processed')
  29. .click(function() {
  30. var id = $(this).attr('id').replace('display-remove-link-', '');
  31. $('#display-row-' + id).hide();
  32. $('#display-removed-' + id).attr('checked', true);
  33. return false;
  34. });
  35. }
  36. };
  37. /**
  38. * Helper function to parse a querystring.
  39. */
  40. Drupal.Views.parseQueryString = function (query) {
  41. var args = {};
  42. var pos = query.indexOf('?');
  43. if (pos != -1) {
  44. query = query.substring(pos + 1);
  45. }
  46. var pairs = query.split('&');
  47. for (var i in pairs) {
  48. if (typeof(pairs[i]) == 'string') {
  49. var pair = pairs[i].split('=');
  50. // Ignore the 'q' path argument, if present.
  51. if (pair[0] != 'q' && pair[1]) {
  52. args[decodeURIComponent(pair[0].replace(/\+/g, ' '))] = decodeURIComponent(pair[1].replace(/\+/g, ' '));
  53. }
  54. }
  55. }
  56. return args;
  57. };
  58. /**
  59. * Helper function to return a view's arguments based on a path.
  60. */
  61. Drupal.Views.parseViewArgs = function (href, viewPath) {
  62. // Provide language prefix.
  63. if (Drupal.settings.pathPrefix) {
  64. var viewPath = Drupal.settings.pathPrefix + viewPath;
  65. }
  66. var returnObj = {};
  67. var path = Drupal.Views.getPath(href);
  68. // Ensure we have a correct path.
  69. if (viewPath && path.substring(0, viewPath.length + 1) == viewPath + '/') {
  70. var args = decodeURIComponent(path.substring(viewPath.length + 1, path.length));
  71. returnObj.view_args = args;
  72. returnObj.view_path = path;
  73. }
  74. return returnObj;
  75. };
  76. /**
  77. * Strip off the protocol plus domain from an href.
  78. */
  79. Drupal.Views.pathPortion = function (href) {
  80. // Remove e.g. http://example.com if present.
  81. var protocol = window.location.protocol;
  82. if (href.substring(0, protocol.length) == protocol) {
  83. // 2 is the length of the '//' that normally follows the protocol.
  84. href = href.substring(href.indexOf('/', protocol.length + 2));
  85. }
  86. return href;
  87. };
  88. /**
  89. * Return the Drupal path portion of an href.
  90. */
  91. Drupal.Views.getPath = function (href) {
  92. href = Drupal.Views.pathPortion(href);
  93. href = href.substring(Drupal.settings.basePath.length, href.length);
  94. // 3 is the length of the '?q=' added to the url without clean urls.
  95. if (href.substring(0, 3) == '?q=') {
  96. href = href.substring(3, href.length);
  97. }
  98. var chars = ['#', '?', '&'];
  99. for (var i in chars) {
  100. if (href.indexOf(chars[i]) > -1) {
  101. href = href.substr(0, href.indexOf(chars[i]));
  102. }
  103. }
  104. return href;
  105. };
  106. })(jQuery);