readmore.js
137 lines
| 1 | /** |
| 2 | * readmore.js |
| 3 | * |
| 4 | * @param toggleButtonText |
| 5 | * @param toggleButtonText.dataset |
| 6 | * @param toggleButtonText.dataset.moreText |
| 7 | * @param toggleButtonText.dataset.lessText |
| 8 | */ |
| 9 | |
| 10 | (function () { |
| 11 | |
| 12 | /* |
| 13 | * Do stuff at the end of animation. |
| 14 | */ |
| 15 | var onAnimationEnd = function (event) { |
| 16 | if (event.type === 'animationend' && event.animationName === 'fadeOutUp') { |
| 17 | |
| 18 | // Add `hidden` attribute |
| 19 | event.target.setAttribute('hidden', 'true'); |
| 20 | event.target.style.display = 'none'; |
| 21 | |
| 22 | // Show read-more link |
| 23 | event.target.parentElement.parentElement.querySelector('.readmore-toggle').style.display = 'inline'; |
| 24 | |
| 25 | // Show ellipsis |
| 26 | var ellipsis = event.target.parentElement.querySelector('.ellipsis'); |
| 27 | if (ellipsis) { |
| 28 | ellipsis.style.display = 'inline'; |
| 29 | } |
| 30 | |
| 31 | fireCustomEvent(); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | var fireCustomEvent = function () { |
| 36 | window.dispatchEvent(new Event('toggleFullContent')); |
| 37 | }; |
| 38 | |
| 39 | // Only in modern browsers. |
| 40 | if ('querySelector' in document && 'addEventListener' in window) { |
| 41 | |
| 42 | // Listen for an animation. |
| 43 | document.addEventListener('transitionend', function (event) { |
| 44 | if (event.target.matches('.readmore-content')) { |
| 45 | onAnimationEnd(event); |
| 46 | } |
| 47 | }, false); |
| 48 | |
| 49 | document.addEventListener('animationend', function (event) { |
| 50 | if (event.target.matches('.readmore-content')) { |
| 51 | onAnimationEnd(event); |
| 52 | } |
| 53 | }, false); |
| 54 | |
| 55 | // Listen to each readmore link. |
| 56 | document.addEventListener('click', function (event) { |
| 57 | |
| 58 | |
| 59 | if (!event.target.matches('.readmore-text')) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | var toggleButtonText = event.target; |
| 64 | var theContainer = jQuery( event.target ).parents( '.wpmtst-testimonial-content' ); |
| 65 | var toggleButton = toggleButtonText.parentElement; |
| 66 | var excerptWrapper = jQuery( theContainer ).find( '.readmore-excerpt' ); |
| 67 | |
| 68 | var fullTextWrapper = jQuery( theContainer ).find( '.readmore-content' )[0]; |
| 69 | var ellipsis = jQuery( theContainer ).find( '.ellipsis' )[0]; |
| 70 | var allHtml = false; |
| 71 | |
| 72 | if ( excerptWrapper.hasClass( 'all-html' ) ) { |
| 73 | allHtml = true; |
| 74 | } |
| 75 | |
| 76 | if ( !fullTextWrapper ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // change attributes and text if full text is shown/hidden |
| 81 | if (fullTextWrapper.hasAttribute('hidden')) { |
| 82 | // show |
| 83 | // 1. remove hidden attribute so we can animate it |
| 84 | fullTextWrapper.removeAttribute('hidden'); |
| 85 | fullTextWrapper.style.display = 'inline'; |
| 86 | |
| 87 | // 2. update toggle link |
| 88 | // change text (may be blank) |
| 89 | toggleButtonText.innerText = toggleButtonText.dataset.lessText; |
| 90 | toggleButton.setAttribute('aria-expanded', true); |
| 91 | if (ellipsis) { |
| 92 | ellipsis.style.display = 'none'; |
| 93 | } |
| 94 | |
| 95 | // 3. animate it |
| 96 | fullTextWrapper.classList.add( 'fadeInDown' ); |
| 97 | fullTextWrapper.classList.remove( 'fadeOutUp' ); |
| 98 | fullTextWrapper.classList.remove( 'faster' ); |
| 99 | |
| 100 | excerptWrapper.hide(); |
| 101 | |
| 102 | fireCustomEvent(); |
| 103 | |
| 104 | } else { |
| 105 | // hide |
| 106 | // 1. update toggle link |
| 107 | |
| 108 | fullTextWrapper.style.display = 'none'; |
| 109 | fullTextWrapper.setAttribute('hidden', true); |
| 110 | |
| 111 | toggleButton.setAttribute( 'aria-expanded', false ); |
| 112 | // change link text (may be blank) |
| 113 | toggleButtonText.innerText = toggleButtonText.dataset.moreText; |
| 114 | |
| 115 | // 2. animate it |
| 116 | fullTextWrapper.classList.add( 'fadeOutUp' ); |
| 117 | fullTextWrapper.classList.add( 'faster' ); |
| 118 | fullTextWrapper.classList.remove( 'fadeInDown' ); |
| 119 | |
| 120 | excerptWrapper.show(); |
| 121 | // 3. Add back the elipsis if needed. |
| 122 | if (ellipsis) { |
| 123 | ellipsis.style.display = 'inline'; |
| 124 | } |
| 125 | |
| 126 | // 4. do stuff at end of animation (the event listener above) |
| 127 | fireCustomEvent(); |
| 128 | } |
| 129 | |
| 130 | }, false); |
| 131 | |
| 132 | |
| 133 | |
| 134 | } |
| 135 | |
| 136 | })(); |
| 137 |