frontend.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. (function($) {
  2. PPFAQModule = function( settings )
  3. {
  4. this.settings = settings;
  5. this.nodeClass = '.fl-node-' + settings.id;
  6. this._init();
  7. };
  8. PPFAQModule.prototype = {
  9. settings : {},
  10. nodeClass : '',
  11. clicked : false,
  12. _init: function()
  13. {
  14. //$( this.nodeClass + ' .pp-faq-button' ).css( 'height', $( this.nodeClass + ' .pp-faq-button' ).outerHeight() + 'px' );
  15. var button = $( this.nodeClass + ' .pp-faq-button' );
  16. button.off( 'click' ).on( 'click', this._buttonClick.bind( this ) );
  17. button.on( 'keypress', this._buttonClick.bind( this ) );
  18. button.on( 'focus', this._focusIn.bind( this ) );
  19. button.on( 'focusout', this._focusOut.bind( this ) );
  20. this._openDefaultItem();
  21. this._hashChange();
  22. $( window ).on( 'hashchange', this._hashChange.bind( this ) );
  23. },
  24. _hashChange: function()
  25. {
  26. if ( location.hash && $(location.hash).length > 0 ) {
  27. var element = $( location.hash + '.pp-faq-item' );
  28. if ( element && element.length > 0 ) {
  29. location.href = '#';
  30. $( 'html, body' ).animate({
  31. scrollTop: element.offset().top - 120
  32. }, 0, function() {
  33. if ( ! element.hasClass( 'pp-faq-item-active' ) ) {
  34. element.find( '.pp-faq-button' ).trigger( 'click' );
  35. }
  36. });
  37. }
  38. }
  39. },
  40. _buttonClick: function( e )
  41. {
  42. // Click or keyboard (enter or spacebar) input?
  43. if ( ! this._validClick(e) ) {
  44. return;
  45. }
  46. // Prevent scrolling when the spacebar is pressed
  47. e.preventDefault();
  48. e.stopPropagation();
  49. var button = $( e.target ).closest( '.pp-faq-button' ),
  50. faq = button.closest( '.pp-faq' ),
  51. item = button.closest( '.pp-faq-item' ),
  52. allContent = faq.find( '.pp-faq-content' ),
  53. content = button.siblings( '.pp-faq-content' ),
  54. self = this;
  55. if ( 1 == this.settings.defaultItem ) {
  56. allContent = faq.find('.pp-faq-item:not(:first-child) .pp-faq-content');
  57. }
  58. if ( faq.hasClass( 'pp-faq-collapse' ) && allContent.length ) {
  59. faq.find( '.pp-faq-item-active' ).removeClass( 'pp-faq-item-active' );
  60. faq.find( '.pp-faq-item-active .pp-faq-button' ).attr('aria-expanded', 'false');
  61. faq.find( '.pp-faq-item-active .pp-faq-button' ).attr('aria-hidden', 'true');
  62. button.attr('aria-expanded', 'false');
  63. button.attr('aria-hidden', 'true');
  64. allContent.slideUp( 'normal' );
  65. }
  66. // if ( this.settings.responsiveCollapse && window.innerWidth <= 768 && ! this.clicked ) {
  67. // this.clicked = true;
  68. // return;
  69. // }
  70. if ( content.is( ':hidden' ) ) {
  71. button.attr('aria-expanded', 'true');
  72. item.addClass( 'pp-faq-item-active' );
  73. content.slideDown( 'normal', function() {
  74. self._slideDownComplete( this );
  75. $(this).attr( 'aria-hidden', 'false' );
  76. });
  77. }
  78. else {
  79. button.attr('aria-expanded', 'false');
  80. item.removeClass( 'pp-faq-item-active' );
  81. content.slideUp( 'normal', function() {
  82. self._slideUpComplete( this );
  83. $(this).attr( 'aria-hidden', 'true' );
  84. });
  85. }
  86. },
  87. _slideUpComplete: function(target)
  88. {
  89. var content = $( target ),
  90. faq = content.closest( '.pp-faq' );
  91. faq.trigger( 'fl-builder.pp-faq-toggle-complete' );
  92. },
  93. _slideDownComplete: function(target)
  94. {
  95. var content = $( target ),
  96. faq = content.closest( '.pp-faq' ),
  97. item = content.parent(),
  98. win = $( window );
  99. // Gallery module support.
  100. FLBuilderLayout.refreshGalleries( content );
  101. // Content Grid module support.
  102. if ( 'undefined' !== typeof $.fn.isotope ) {
  103. content.find( '.pp-content-post-grid.pp-masonry-active' ).isotope( 'layout' );
  104. var highestBox = 0;
  105. var contentHeight = 0;
  106. content.find( '.pp-equal-height .pp-content-post' ).css( 'height', '' ).each(function(){
  107. if ( $(this).height() > highestBox ) {
  108. highestBox = $( this ).height();
  109. contentHeight = $( this ).find( '.pp-content-post-data' ).outerHeight();
  110. }
  111. });
  112. $( this.nodeClass ).find( '.pp-equal-height .pp-content-post' ).height(highestBox);
  113. }
  114. if ( item.offset().top < win.scrollTop() + 100 ) {
  115. if ( ! this.clicked ) {
  116. $( 'html, body' ).animate({
  117. scrollTop: item.offset().top - 100
  118. }, 500, 'swing' );
  119. }
  120. }
  121. this.clicked = false;
  122. faq.trigger( 'fl-builder.pp-faq-toggle-complete' );
  123. $( document ).trigger( 'pp-faq-toggle-complete', [ faq ] );
  124. },
  125. _openDefaultItem: function()
  126. {
  127. if ( this.settings.responsiveCollapse && window.innerWidth <= 768 ) {
  128. return;
  129. }
  130. if ( 'all' == this.settings.defaultItem ) {
  131. $( this.nodeClass + ' .pp-faq-button' ).trigger( 'click' );
  132. } else {
  133. var item = $.isNumeric( this.settings.defaultItem ) ? ( this.settings.defaultItem - 1 ) : null;
  134. if ( item !== null ) {
  135. this.clicked = true;
  136. if ( ! this.settings.isBuilderActive ) {
  137. $( this.nodeClass + ' .pp-faq-item' ).eq( item ).not('.pp-faq-item-active').find( '.pp-faq-button' ).trigger( 'click' );
  138. } else {
  139. $( this.nodeClass + ' .pp-faq-item' ).eq( item ).addClass('pp-faq-item-active').find( '.pp-faq-content' ).show();
  140. }
  141. }
  142. }
  143. },
  144. _focusIn: function( e )
  145. {
  146. var button = $( e.target ).closest('.pp-faq-button');
  147. button.attr('aria-selected', 'true');
  148. },
  149. _focusOut: function( e )
  150. {
  151. var button = $( e.target ).closest('.pp-faq-button');
  152. button.attr('aria-selected', 'false');
  153. },
  154. _validClick: function(e)
  155. {
  156. return (e.which == 1 || e.which == 13 || e.which == 32 || e.which == undefined) ? true : false;
  157. }
  158. };
  159. })(jQuery);