1/**
2 *
3 * DokuWiki Twenty Fifteen Template
4 *
5 * @link     https://github.com/lainme/dokuwiki-theme-twentyfifteen
6 * @author   Anika Henke <anika@selfthinker.org> (upstream)
7 * @author   WordPress.org & Automattic.com (upstream)
8 * @author   lainme <lainme993@gmail.com>
9 * @license  GPLv2 (http://www.gnu.org/licenses/gpl.html)
10 */
11
12(function($) {
13    var top = false, bottom = false, topOffset = 0, lastWindowPos = 0;
14    var secondary, sidebar, button;
15
16    function chattr() {
17        if (955 > $(window).width()) {
18            button.attr('aria-expanded', 'false');
19            button.attr('aria-controls', 'secondary');
20            secondary.attr('aria-expanded', 'false');
21        } else {
22            button.removeAttr('aria-expanded');
23            button.removeAttr('aria-controls');
24            secondary.removeAttr('aria-expanded');
25        }
26    }
27
28    function resize() {
29        if (955 > $(window).width()) {
30            top = bottom = false;
31            sidebar.removeAttr('style');
32        }
33    }
34
35    function scroll() {
36        var windowPos     = $(window).scrollTop();
37        var windowWidth   = $(window).width();
38        var windowHeight  = $(window).height();
39        var bodyHeight    = $(document.body).height();
40        var sidebarHeight = sidebar.height();
41
42        if (955 > windowWidth) {
43            return;
44        }
45
46        if (sidebarHeight > windowHeight) {
47            if (windowPos > lastWindowPos) {
48                if (top) {
49                    top = false;
50                    topOffset = (sidebar.offset().top > 0) ? sidebar.offset().top : 0;
51                    sidebar.attr('style', 'top: ' + topOffset + 'px;');
52                } else if (! bottom && windowPos + windowHeight > sidebarHeight + sidebar.offset().top && sidebarHeight < bodyHeight) {
53                    bottom = true;
54                    sidebar.attr('style', 'position: fixed; bottom: 0;');
55                }
56            } else if (windowPos < lastWindowPos) {
57                if (bottom) {
58                    bottom = false;
59                    topOffset = (sidebar.offset().top > 0) ? sidebar.offset().top : 0;
60                    sidebar.attr('style', 'top: ' + topOffset + 'px;');
61                } else if (! top && windowPos < sidebar.offset().top) {
62                    top = true;
63                    sidebar.attr('style', 'position: fixed;');
64                }
65            } else {
66                top = bottom = false;
67                topOffset = (sidebar.offset().top > 0) ? sidebar.offset().top : 0;
68                sidebar.attr('style', 'top: ' + topOffset + 'px;');
69            }
70        } else if (! top) {
71            top = true;
72            sidebar.attr('style', 'position: fixed;');
73        }
74        lastWindowPos = windowPos;
75    }
76
77    function changeLayout() {
78        secondary = $('.secondary');
79        sidebar   = $('.sidebar');
80        button    = $('.secondary-toggle');
81
82        $(window)
83            .on('scroll.twentyfifteen', scroll)
84            .on('load.twentyfifteen', chattr)
85            .on('resize.twentyfifteen', function() {
86                setTimeout(function() {resize(); scroll();}, 500);
87                chattr();
88            });
89
90        sidebar.on('click.twentyfifteen', 'button', function() {resize(); scroll();});
91    }
92
93    function changeMenu() {
94        var widgets = secondary.find('.widget-area');
95
96        if (! secondary.length || ! button.length) {
97            return;
98        }
99
100        if (! widgets.length) {
101            button.hide();
102            return;
103        }
104
105        button.on('click.twentyfifteen', function() {
106            secondary.toggleClass('toggled-on');
107            $(this).toggleClass('toggled-on');
108            if ($(this, secondary).hasClass('toggled-on')) {
109                $(this).attr('aria-expanded', 'true');
110                secondary.attr('aria-expanded', 'true');
111            } else {
112                $(this).attr('aria-expanded', 'false');
113                secondary.attr('aria-expanded', 'false');
114            }
115        } );
116    }
117
118    function changeToc() {
119        var $toc = $('#dw__toc .toggle');
120        if($toc.length) {
121            $toc[0].setState(-1);
122        }
123    }
124
125    $(document).ready(function() {
126        changeLayout();
127        changeMenu();
128        changeToc();
129    } );
130})(jQuery);
131