← All changes
|
includes/widgets/Woo_Archive_Description/script.js
+42
-23
51.1.64
→
51.1.83
View file →
| @@ -1,34 +1,53 @@ | ||
| 1 | 1 | /** |
| 2 | - * Woo Archive Description widget behavior. | |
| 2 | + * Woo Archive Description widget behaviour. | |
| 3 | 3 | * |
| 4 | - * This widget is static; behavior is handled by WooCommerce/theme templates. | |
| 5 | - * We keep an Elementor hook to ensure correct initialization in the editor. | |
| 4 | + * Expands a trimmed description when the "Read more" control is used. The | |
| 5 | + * markup ships both the trimmed and the full text; this only swaps which one | |
| 6 | + * is visible. | |
| 6 | 7 | */ |
| 7 | -(function ($) { | |
| 8 | - "use strict"; | |
| 8 | +(function () { | |
| 9 | + 'use strict'; | |
| 9 | 10 | |
| 10 | - /** | |
| 11 | - * Initialize widget instance. | |
| 12 | - * | |
| 13 | - * @param {Object} $scope Elementor scope. | |
| 14 | - * @returns {void} | |
| 15 | - */ | |
| 16 | - const initWooArchiveDescription = ($scope) => { | |
| 17 | - void $scope; | |
| 18 | - }; | |
| 11 | + const BOUND = 'kaArchiveDescriptionBound'; | |
| 19 | 12 | |
| 20 | - $(window).on("elementor/frontend/init", function () { | |
| 21 | - elementorFrontend.hooks.addAction( | |
| 22 | - "frontend/element_ready/woo_archive_description.default", | |
| 23 | - function ($scope) { | |
| 24 | - initWooArchiveDescription($scope); | |
| 25 | - } | |
| 26 | - ); | |
| 27 | - }); | |
| 28 | -})(jQuery); | |
| 13 | + const bind = (wrap) => { | |
| 14 | + if (!wrap || !wrap.dataset || wrap.dataset[BOUND] === '1') { | |
| 15 | + return; | |
| 16 | + } | |
| 29 | 17 | |
| 18 | + const button = wrap.querySelector('.ka-woo-archive-description__readmore'); | |
| 19 | + const short = wrap.querySelector('.ka-woo-archive-description__short'); | |
| 20 | + const full = wrap.querySelector('.ka-woo-archive-description__full'); | |
| 21 | + if (!button || !short || !full) { | |
| 22 | + return; | |
| 23 | + } | |
| 30 | 24 | |
| 25 | + wrap.dataset[BOUND] = '1'; | |
| 26 | + const moreLabel = button.textContent; | |
| 27 | + const lessLabel = button.dataset.lessText || ''; | |
| 31 | 28 | |
| 29 | + button.addEventListener('click', () => { | |
| 30 | + const expanded = button.getAttribute('aria-expanded') === 'true'; | |
| 31 | + short.hidden = !expanded; | |
| 32 | + full.hidden = expanded; | |
| 33 | + button.setAttribute('aria-expanded', expanded ? 'false' : 'true'); | |
| 34 | + if (lessLabel) { | |
| 35 | + button.textContent = expanded ? moreLabel : lessLabel; | |
| 36 | + } | |
| 37 | + }); | |
| 38 | + }; | |
| 32 | 39 | |
| 40 | + const init = (scope) => { | |
| 41 | + const root = scope && scope.querySelectorAll ? scope : document; | |
| 42 | + root.querySelectorAll('[data-ka-expandable="1"]').forEach(bind); | |
| 43 | + }; | |
| 33 | 44 | |
| 45 | + document.addEventListener('DOMContentLoaded', () => init(document)); | |
| 34 | 46 | |
| 47 | + if (window.elementorFrontend && window.elementorFrontend.hooks) { | |
| 48 | + window.elementorFrontend.hooks.addAction( | |
| 49 | + 'frontend/element_ready/woo_archive_description.default', | |
| 50 | + (scope) => init(scope && scope[0] ? scope[0] : document) | |
| 51 | + ); | |
| 52 | + } | |
| 53 | +})(); | |