faq.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function ($) {
  2. Drupal.behaviors.initFaqModule = {
  3. attach: function (context) {
  4. // Hide/show answer for a question.
  5. var faq_hide_qa_accordion = Drupal.settings.faq.faq_hide_qa_accordion;
  6. $('div.faq-dd-hide-answer', context).addClass("collapsible collapsed");
  7. if (!faq_hide_qa_accordion) {
  8. $('div.faq-dd-hide-answer:not(.faq-processed)', context).addClass('faq-processed').hide();
  9. }
  10. $('div.faq-dt-hide-answer:not(.faq-processed)', context).addClass('faq-processed').click(function() {
  11. if (faq_hide_qa_accordion) {
  12. $('div.faq-dt-hide-answer').not($(this)).removeClass('faq-qa-visible');
  13. }
  14. $(this).toggleClass('faq-qa-visible');
  15. $(this).parent().addClass('faq-viewed');
  16. $('div.faq-dd-hide-answer').not($(this).next('div.faq-dd-hide-answer')).addClass("collapsed");
  17. if (!faq_hide_qa_accordion) {
  18. $(this).next('div.faq-dd-hide-answer').slideToggle('fast', function() {
  19. $(this).parent().toggleClass('expanded');
  20. });
  21. }
  22. $(this).next('div.faq-dd-hide-answer').toggleClass("collapsed");
  23. // Change the fragment, too, for permalink/bookmark.
  24. // To keep the current page from scrolling, refs
  25. // http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling/1489802#1489802
  26. var hash = $(this).find('a').attr('id');
  27. var fx, node = $('#' + hash);
  28. if (node.length) {
  29. fx = $('<div></div>')
  30. .css({position: 'absolute', visibility: 'hidden', top: $(window).scrollTop() + 'px'})
  31. .attr('id', hash)
  32. .appendTo(document.body);
  33. node.attr('id', '');
  34. }
  35. document.location.hash = hash;
  36. if (node.length) {
  37. fx.remove();
  38. node.attr('id', hash);
  39. }
  40. // Scroll the page if the collapsed FAQ is not visible.
  41. // If we have the toolbar so the title may be hidden by the bar.
  42. var mainScrollTop = Math.max($('html', context).scrollTop(), $('body', context).scrollTop());
  43. // We compute mainScrollTop because the behaviour is different on FF, IE and CR
  44. if (mainScrollTop > $(this).offset().top) {
  45. $('html, body', context).animate({
  46. scrollTop: $(this).offset().top
  47. }, 1);
  48. }
  49. return false;
  50. });
  51. // Show any question identified by a fragment
  52. if (/^#\w+$/.test(document.location.hash)) {
  53. $('div.faq-dt-hide-answer ' + document.location.hash).parents('.faq-dt-hide-answer').triggerHandler('click');
  54. }
  55. // Hide/show q/a for a category.
  56. var faq_category_hide_qa_accordion = Drupal.settings.faq.faq_category_hide_qa_accordion;
  57. $('div.faq-qa-hide', context).addClass("collapsible collapsed");
  58. if (!faq_category_hide_qa_accordion) {
  59. $('div.faq-qa-hide', context).hide();
  60. }
  61. $('div.faq-qa-header .faq-header:not(.faq-processed)', context).addClass('faq-processed').click(function() {
  62. var $this = $(this);
  63. if (faq_category_hide_qa_accordion) {
  64. $('.faq-category-qa-visible').not($('.faq-category-qa-visible').closest('.faq-category-group').has($this).children('div.faq-qa-hide')).removeClass('faq-category-qa-visible');
  65. }
  66. $(this).toggleClass('faq-category-qa-visible');
  67. $('div.faq-qa-hide').not($this.parent().siblings('div.faq-qa-hide')).not($('div.faq-qa-hide').closest('.faq-category-group').has($this).children('div.faq-qa-hide')).addClass("collapsed");
  68. if (!faq_category_hide_qa_accordion) {
  69. $this.parent().siblings('div.faq-qa-hide').slideToggle('fast', function() {
  70. $(this).parent().toggleClass('expanded');
  71. });
  72. }
  73. $this.parent().siblings('div.faq-qa-hide').toggleClass("collapsed");
  74. // Scroll the page if the collapsed FAQ is not visible.
  75. // If we have the toolbar so the title may be hidden by the bar.
  76. var mainScrollTop = Math.max($('html', context).scrollTop(), $('body', context).scrollTop());
  77. // We compute mainScrollTop because the behaviour is different on FF, IE and CR
  78. if (mainScrollTop > $this.offset().top) {
  79. $('html, body', context).animate({
  80. scrollTop: $this.offset().top
  81. }, 1);
  82. }
  83. return false;
  84. });
  85. // Show expand all link.
  86. if (!faq_hide_qa_accordion && !faq_category_hide_qa_accordion) {
  87. $('#faq-expand-all', context).show();
  88. $('#faq-expand-all a.faq-expand-all-link', context).show();
  89. // Add collapse link click event.
  90. $('#faq-expand-all a.faq-collapse-all-link:not(.faq-processed)', context).addClass('faq-processed').click(function () {
  91. $(this).hide();
  92. $('#faq-expand-all a.faq-expand-all-link').show();
  93. $('div.faq-qa-hide').slideUp('slow', function() {
  94. $(this).removeClass('expanded');
  95. });
  96. $('div.faq-dd-hide-answer').slideUp('slow', function() {
  97. $(this).removeClass('expanded');
  98. });
  99. });
  100. // Add expand link click event.
  101. $('#faq-expand-all a.faq-expand-all-link:not(.faq-processed)', context).addClass('faq-processed').click(function () {
  102. $(this).hide();
  103. $('#faq-expand-all a.faq-collapse-all-link').show();
  104. $('div.faq-qa-hide').slideDown('slow', function() {
  105. $(this).addClass('expanded');
  106. });
  107. $('div.faq-dd-hide-answer').slideDown('slow', function() {
  108. $(this).addClass('expanded');
  109. });
  110. });
  111. }
  112. }
  113. };
  114. })(jQuery);