xref: /template/writr/script.js (revision 9da07a3e01be99da0b28126f3f17501a67739636)
1/* DOKUWIKI:include js/skip-link-focus-fix.js */
2
3jQuery(document).ready(function() {
4    /*
5     * Click to toggle sidebar.
6     */
7    function toggleSidebar() {
8        jQuery( '#writr__sidebar' ).on( 'click', '#writr__sidebar-toggle', function( e ) {
9            e.preventDefault();
10            jQuery( 'html, body' ).scrollTop( 0 );
11            jQuery( this ).toggleClass( 'open' );
12            jQuery( 'body' ).toggleClass( 'sidebar-closed' );
13            jQuery( '#writr__secondary' ).resize();
14        } );
15    }
16
17    /**
18     * Handles toggling the navigation menu for small screens.
19     */
20    function toggleNavigation() {
21        var $container = jQuery('#writr__site-navigation');
22        if (!$container.length) return;
23        var $button = jQuery('.menu-toggle', $container);
24        if (!$button.length) return;
25        var $menu = jQuery('ul', $container);
26        if (!$menu.length) {
27            $menu.hide();
28            return;
29        }
30        $button.click(function(){
31            $container.toggleClass('toggled');
32        });
33    }
34
35    /*
36     * A function to enable/disable a dropdown submenu.
37     */
38    function toggleSubmenu() {
39        jQuery( '.main-navigation .node > div > a' ).append( '<span class="dropdown-icon" />' );
40        jQuery( '#writr__site-navigation' ).on( 'click', '.dropdown-icon', function( e ) {
41            e.preventDefault();
42            jQuery( this ).toggleClass( 'open' );
43            if ( jQuery( this ).hasClass( 'open' ) ) {
44                jQuery( this ).parent().parent().next( 'ul' ).show();
45            } else {
46                jQuery( this ).parent().parent().next( 'ul' ).hide();
47            }
48        } );
49    }
50
51    /*
52     * Close TOC by default
53     */
54    function closeToc() {
55        var $toc = jQuery('#dw__toc .toggle');
56        if($toc.length) {
57            $toc[0].setState(-1);
58        }
59    }
60
61    /*
62     * Change search submit input to submit button to make it easier to style
63     * @deprecated since Detritus
64     */
65    function changeSearchInput() {
66        var $searchForm = jQuery('.search-form > form > div');
67        var $searchButton = jQuery('input[type="submit"]', $searchForm).detach();
68        var title = $searchButton.attr('title');
69        var value = $searchButton.val();
70        $searchForm.append('<button type="submit" title="'+title+'">'+value+'</button>');
71    }
72
73    /*
74     * Enable add new page dropdown
75     */
76    function enableAddNewPage() {
77        jQuery('.action.AddNewPage').click(function(event) {
78            event.preventDefault();
79            jQuery('.addnewpage').toggle(0,function(){
80                // set aria-expanded attribute based on visibility
81                jQuery(this).attr('aria-expanded', jQuery(this).is(':visible'));
82            });
83        });
84
85        jQuery(document).click(function(event) {
86            if (!jQuery(event.target).closest('.action.AddNewPage, .addnewpage').length) {
87                jQuery('.addnewpage').hide();
88            }
89        });
90    }
91
92    /*
93     * Enable translation dropdown
94     */
95    function enableTranslation() {
96        jQuery('.action.Translation').click(function(event) {
97            event.preventDefault();
98            jQuery('.plugin_translation').toggle(0,function(){
99                // set aria-expanded attribute based on visibility
100                jQuery(this).attr('aria-expanded', jQuery(this).is(':visible'));
101            });
102        });
103
104        jQuery(document).click(function(event) {
105            if (!jQuery(event.target).closest('.action.Translation, .plugin_translation').length) {
106                jQuery('.plugin_translation').hide();
107            }
108        });
109    }
110
111    jQuery(function(){
112        toggleSidebar();
113        toggleNavigation();
114        toggleSubmenu();
115        closeToc();
116        changeSearchInput();
117        enableAddNewPage();
118        enableTranslation();
119    });
120});
121