faq.js 5.3 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. if (faq_category_hide_qa_accordion) {
  63. $('div.faq-qa-header .faq-header').not($(this)).removeClass('faq-category-qa-visible');
  64. }
  65. $(this).toggleClass('faq-category-qa-visible');
  66. $('div.faq-qa-hide').not($(this).parent().next('div.faq-qa-hide')).addClass("collapsed");
  67. if (!faq_category_hide_qa_accordion) {
  68. $(this).parent().next('div.faq-qa-hide').slideToggle('fast', function() {
  69. $(this).parent().toggleClass('expanded');
  70. });
  71. }
  72. $(this).parent().next('div.faq-qa-hide').toggleClass("collapsed");
  73. // Scroll the page if the collapsed FAQ is not visible.
  74. // If we have the toolbar so the title may be hidden by the bar.
  75. var mainScrollTop = Math.max($('html', context).scrollTop(), $('body', context).scrollTop());
  76. // We compute mainScrollTop because the behaviour is different on FF, IE and CR
  77. if (mainScrollTop > $(this).offset().top) {
  78. $('html, body', context).animate({
  79. scrollTop: $(this).offset().top
  80. }, 1);
  81. }
  82. return false;
  83. });
  84. // Show expand all link.
  85. if (!faq_hide_qa_accordion && !faq_category_hide_qa_accordion) {
  86. $('#faq-expand-all', context).show();
  87. $('#faq-expand-all a.faq-expand-all-link', context).show();
  88. // Add collapse link click event.
  89. $('#faq-expand-all a.faq-collapse-all-link:not(.faq-processed)', context).addClass('faq-processed').click(function () {
  90. $(this).hide();
  91. $('#faq-expand-all a.faq-expand-all-link').show();
  92. $('div.faq-qa-hide').slideUp('slow', function() {
  93. $(this).removeClass('expanded');
  94. });
  95. $('div.faq-dd-hide-answer').slideUp('slow', function() {
  96. $(this).removeClass('expanded');
  97. });
  98. });
  99. // Add expand link click event.
  100. $('#faq-expand-all a.faq-expand-all-link:not(.faq-processed)', context).addClass('faq-processed').click(function () {
  101. $(this).hide();
  102. $('#faq-expand-all a.faq-collapse-all-link').show();
  103. $('div.faq-qa-hide').slideDown('slow', function() {
  104. $(this).addClass('expanded');
  105. });
  106. $('div.faq-dd-hide-answer').slideDown('slow', function() {
  107. $(this).addClass('expanded');
  108. });
  109. });
  110. }
  111. }
  112. }
  113. })(jQuery);