quicktabs.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. (function ($) {
  2. Drupal.settings.views = Drupal.settings.views || {'ajax_path': '/views/ajax'};
  3. Drupal.quicktabs = Drupal.quicktabs || {};
  4. Drupal.quicktabs.getQTName = function (el) {
  5. return el.id.substring(el.id.indexOf('-') +1);
  6. }
  7. Drupal.behaviors.quicktabs = {
  8. attach: function (context, settings) {
  9. $.extend(true, Drupal.settings, settings);
  10. $('.quicktabs-wrapper', context).once(function(){
  11. Drupal.quicktabs.prepare(this);
  12. });
  13. }
  14. }
  15. // Setting up the inital behaviours
  16. Drupal.quicktabs.prepare = function(el) {
  17. // el.id format: "quicktabs-$name"
  18. var qt_name = Drupal.quicktabs.getQTName(el);
  19. var $ul = $(el).find('ul.quicktabs-tabs:first');
  20. $ul.find('li a').each(function(i, element){
  21. element.myTabIndex = i;
  22. element.qt_name = qt_name;
  23. var tab = new Drupal.quicktabs.tab(element);
  24. var parent_li = $(element).parents('li').get(0);
  25. if ($(parent_li).hasClass('active')) {
  26. $(element).addClass('quicktabs-loaded');
  27. }
  28. $(element).once(function() {$(this).bind('click', {tab: tab}, Drupal.quicktabs.clickHandler);});
  29. });
  30. }
  31. Drupal.quicktabs.clickHandler = function(event) {
  32. var tab = event.data.tab;
  33. var element = this;
  34. // Set clicked tab to active.
  35. $(this).parents('li').siblings().removeClass('active');
  36. $(this).parents('li').addClass('active');
  37. // Hide all tabpages.
  38. tab.container.children().addClass('quicktabs-hide');
  39. if (!tab.tabpage.hasClass("quicktabs-tabpage")) {
  40. tab = new Drupal.quicktabs.tab(element);
  41. }
  42. tab.tabpage.removeClass('quicktabs-hide');
  43. return false;
  44. }
  45. // Constructor for an individual tab
  46. Drupal.quicktabs.tab = function (el) {
  47. this.element = el;
  48. this.tabIndex = el.myTabIndex;
  49. var qtKey = 'qt_' + el.qt_name;
  50. var i = 0;
  51. for (var key in Drupal.settings.quicktabs[qtKey].tabs) {
  52. if (i == this.tabIndex) {
  53. this.tabObj = Drupal.settings.quicktabs[qtKey].tabs[key];
  54. this.tabKey = key;
  55. }
  56. i++;
  57. }
  58. this.tabpage_id = 'quicktabs-tabpage-' + el.qt_name + '-' + this.tabKey;
  59. this.container = $('#quicktabs-container-' + el.qt_name);
  60. this.tabpage = this.container.find('#' + this.tabpage_id);
  61. }
  62. if (Drupal.ajax) {
  63. /**
  64. * Handle an event that triggers an AJAX response.
  65. *
  66. * We unfortunately need to override this function, which originally comes from
  67. * misc/ajax.js, in order to be able to cache loaded tabs, i.e. once a tab
  68. * content has loaded it should not need to be loaded again.
  69. *
  70. * I have removed all comments that were in the original core function, so that
  71. * the only comments inside this function relate to the Quicktabs modification
  72. * of it.
  73. */
  74. Drupal.ajax.prototype.eventResponse = function (element, event) {
  75. var ajax = this;
  76. if (ajax.ajaxing) {
  77. return false;
  78. }
  79. try {
  80. if (ajax.form) {
  81. if (ajax.setClick) {
  82. element.form.clk = element;
  83. }
  84. ajax.form.ajaxSubmit(ajax.options);
  85. }
  86. else {
  87. // Do not perform an ajax request for already loaded Quicktabs content.
  88. if (!$(element).hasClass('quicktabs-loaded')) {
  89. ajax.beforeSerialize(ajax.element, ajax.options);
  90. $.ajax(ajax.options);
  91. if ($(element).parents('ul').hasClass('quicktabs-tabs')) {
  92. $(element).addClass('quicktabs-loaded');
  93. }
  94. }
  95. }
  96. }
  97. catch (e) {
  98. ajax.ajaxing = false;
  99. alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message);
  100. }
  101. return false;
  102. };
  103. }
  104. })(jQuery);