| @@ -15,8 +15,40 @@ | ||
| 15 | 15 | ); |
| 16 | 16 | |
| 17 | 17 | let nestedCategoriesToggle = $(".betterdocs-nested-category-title"); |
| 18 | 18 | |
| 19 | + // When "Lazy Load Sidebar Docs & Subcategories" is on, non-active | |
| 20 | + // categories render an empty body (data-bd-lazy="1") and the docs are | |
| 21 | + // fetched on click. That fetch normally lives in category-toggler.js, | |
| 22 | + // which binds on window.load and does NOT run inside the Elementor editor | |
| 23 | + // preview — so replicate it here. The AJAX returns the category's full | |
| 24 | + // doc list, so all docs display (no cap). No-ops for non-lazy bodies. | |
| 25 | + function loadLazyBody($body) { | |
| 26 | + if (!$body.length || $body.attr("data-bd-lazy") !== "1") return; | |
| 27 | + if ($body.attr("data-bd-loaded") === "1" || $body.attr("data-bd-loading") === "1") return; | |
| 28 | + var cfg = window.betterdocsCategoryGridConfig || {}; | |
| 29 | + // The config object is not reliably printed into the Elementor editor | |
| 30 | + // preview, so fall back to the PHP-injected admin-ajax URL. Without this | |
| 31 | + // the editor fetch bailed on `!cfg.ajax_url` and no docs loaded, even | |
| 32 | + // though the front end (where the config IS present) worked fine. | |
| 33 | + var ajaxUrl = cfg.ajax_url || <?php echo wp_json_encode( esc_url_raw( admin_url( 'admin-ajax.php' ) ) ); ?>; | |
| 34 | + var lazyAction = cfg.lazy_load_action || "betterdocs_lazy_category_body"; | |
| 35 | + if (!ajaxUrl) return; | |
| 36 | + var category_icon = $body.closest(".betterdocs-sidebar-layout-7").length ? "folder" : ""; | |
| 37 | + $body.attr("data-bd-loading", "1"); | |
| 38 | + $.post(ajaxUrl, { | |
| 39 | + action: lazyAction, | |
| 40 | + term_id: $body.attr("data-bd-term-id"), | |
| 41 | + category_icon: category_icon | |
| 42 | + }).done(function (resp) { | |
| 43 | + if (resp && resp.success && resp.data && typeof resp.data.html === "string") { | |
| 44 | + $body.html(resp.data.html).attr("data-bd-loaded", "1").removeAttr("data-bd-lazy"); | |
| 45 | + } | |
| 46 | + }).always(function () { | |
| 47 | + $body.removeAttr("data-bd-loading"); | |
| 48 | + }); | |
| 49 | + } | |
| 50 | + | |
| 19 | 51 | function categoryAccordion() { |
| 20 | 52 | let $parentThis = this; |
| 21 | 53 | currentActiveCat |
| 22 | 54 | .addClass("show") |
| @@ -30,9 +62,16 @@ | ||
| 30 | 62 | e.preventDefault(); |
| 31 | 63 | let $parentCat = jQuery(e.target).closest( |
| 32 | 64 | ".betterdocs-single-category-wrapper" |
| 33 | 65 | ); |
| 34 | - $parentCat.find(".betterdocs-body").slideToggle(); | |
| 66 | + let $body = $parentCat | |
| 67 | + .children(".betterdocs-single-category-inner") | |
| 68 | + .children(".betterdocs-body"); | |
| 69 | + if (!$body.length) { | |
| 70 | + $body = $parentCat.find(".betterdocs-body").first(); | |
| 71 | + } | |
| 72 | + loadLazyBody($body); | |
| 73 | + $body.slideToggle(); | |
| 35 | 74 | $parentCat |
| 36 | 75 | .addClass("active") |
| 37 | 76 | .toggleClass("show") |
| 38 | 77 | .siblings() |