| @@ -38,9 +38,34 @@ | ||
| 38 | 38 | // Don't show ToC for password-protected posts until password is provided |
| 39 | 39 | return ''; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | + if ( '1' == $this->attributes['collapsible_on_mobile'] && false === has_action( 'wp_footer', [ __CLASS__, 'print_collapsible_toggle' ] ) ) { | |
| 43 | + add_action( 'wp_footer', [ __CLASS__, 'print_collapsible_toggle' ], 5 ); | |
| 44 | + } | |
| 45 | + | |
| 42 | 46 | $this->views( 'shortcodes/toc' ); |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * "Collapsible on small devices" is toggled by betterdocs.js, which only | |
| 51 | + * docs pages load. On other pages (the shortcode, block or widget placed | |
| 52 | + * on a page) the title did nothing and the list stayed hidden on phones, | |
| 53 | + * so add the same toggle there. The CSS limits it to small screens. | |
| 54 | + */ | |
| 55 | + public static function print_collapsible_toggle() { | |
| 56 | + if ( wp_script_is( 'betterdocs', 'enqueued' ) ) { | |
| 57 | + return; | |
| 58 | + } | |
| 59 | + | |
| 60 | + $js = <<<'JS' | |
| 61 | +document.addEventListener('click',function(e){var t=e.target.closest?e.target.closest('.betterdocs-toc.collapsible-sm .toc-title'):null;if(!t)return;e.preventDefault(); | |
| 62 | +function s(el){el.style.display=getComputedStyle(el).display==='none'?'block':'none';} | |
| 63 | +t.querySelectorAll('.angle-icon').forEach(s);var l=t.nextElementSibling;if(l&&l.classList.contains('toc-list'))s(l);}); | |
| 64 | +JS; | |
| 65 | + wp_register_script( 'betterdocs-toc-collapsible', false, [], BETTERDOCS_VERSION, true ); | |
| 66 | + wp_add_inline_script( 'betterdocs-toc-collapsible', $js ); | |
| 67 | + wp_enqueue_script( 'betterdocs-toc-collapsible' ); | |
| 43 | 68 | } |
| 44 | 69 | |
| 45 | 70 | public function view_params() { |
| 46 | 71 | return [ |