readmore.js
123 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 | * forEach method |
| 14 | * thanks https://toddmotto.com/ditch-the-array-foreach-call-nodelist-hack/ |
| 15 | */ |
| 16 | var forEach = function (array, callback, scope) { |
| 17 | for (var i = 0; i < array.length; i++) { |
| 18 | callback.call(scope, i, array[i]); // passes back stuff we need |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | /* |
| 23 | * Do stuff at the end of animation. |
| 24 | */ |
| 25 | var onAnimationEnd = function (event) { |
| 26 | if (event.type === 'animationend' && event.animationName === 'fadeOutUp') { |
| 27 | // Add `hidden` attribute |
| 28 | event.target.setAttribute('hidden', 'true'); |
| 29 | |
| 30 | // Show read-more link |
| 31 | event.target.parentElement.querySelector('.readmore-toggle').style.display = 'inline'; |
| 32 | |
| 33 | // Show ellipsis |
| 34 | var ellipsis = event.target.parentElement.querySelector('.ellipsis'); |
| 35 | if (ellipsis) { |
| 36 | ellipsis.style.display = 'inline'; |
| 37 | } |
| 38 | |
| 39 | fireCustomEvent(); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | var fireCustomEvent = function () { |
| 44 | window.dispatchEvent(new Event('toggleFullContent')); |
| 45 | }; |
| 46 | |
| 47 | // Only in modern browsers. |
| 48 | if ('querySelector' in document && 'addEventListener' in window) { |
| 49 | |
| 50 | // Listen for an animation. |
| 51 | var fullTextWrappers = document.querySelectorAll('.readmore-content'); |
| 52 | |
| 53 | forEach(fullTextWrappers, function (index, fullTextWrapper) { |
| 54 | fullTextWrapper.addEventListener('transitionend', onAnimationEnd); |
| 55 | fullTextWrapper.addEventListener('animationend', onAnimationEnd); |
| 56 | }); |
| 57 | |
| 58 | // Listen to each button. |
| 59 | var toggleButtons = document.querySelectorAll('.readmore-toggle'); |
| 60 | |
| 61 | forEach(toggleButtons, function (index, toggleButton) { |
| 62 | |
| 63 | toggleButton.addEventListener('click', function () { |
| 64 | |
| 65 | var fullTextWrapper = this.parentElement.querySelector('.readmore-content'); |
| 66 | if (!fullTextWrapper) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | var ellipsis = this.parentElement.querySelector('.ellipsis'); |
| 71 | var toggleButtonText = this.querySelector('.readmore-text'); |
| 72 | |
| 73 | // change attributes and text if full text is shown/hidden |
| 74 | if (fullTextWrapper.hasAttribute('hidden')) { |
| 75 | |
| 76 | // show |
| 77 | |
| 78 | // 1. remove hidden attribute so we can animate it |
| 79 | fullTextWrapper.removeAttribute('hidden'); |
| 80 | |
| 81 | // 2. update toggle link |
| 82 | // change text (may be blank) |
| 83 | toggleButtonText.innerText = toggleButtonText.dataset.lessText; |
| 84 | toggleButton.setAttribute('aria-expanded', true); |
| 85 | if( ellipsis ) { |
| 86 | ellipsis.style.display = 'none'; |
| 87 | } |
| 88 | |
| 89 | // 3. animate it |
| 90 | fullTextWrapper.classList.add('fadeInDown'); |
| 91 | fullTextWrapper.classList.remove('fadeOutUp'); |
| 92 | fullTextWrapper.classList.remove('faster'); |
| 93 | |
| 94 | fireCustomEvent(); |
| 95 | |
| 96 | } else { |
| 97 | |
| 98 | // hide |
| 99 | |
| 100 | // 1. update toggle link |
| 101 | // hide link during transition |
| 102 | toggleButton.style.display = 'none' |
| 103 | toggleButton.setAttribute('aria-expanded', false); |
| 104 | // change link text (may be blank) |
| 105 | toggleButtonText.innerText = toggleButtonText.dataset.moreText; |
| 106 | |
| 107 | // 2. animate it |
| 108 | fullTextWrapper.classList.add('fadeOutUp'); |
| 109 | fullTextWrapper.classList.add('faster'); |
| 110 | fullTextWrapper.classList.remove('fadeInDown'); |
| 111 | |
| 112 | // 3. do stuff at end of animation (the event listener above) |
| 113 | |
| 114 | } |
| 115 | |
| 116 | }); |
| 117 | |
| 118 | }); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | })(); |
| 123 |