fix for paralells
[wolnelektury.git] / src / wolnelektury / static / js / book_text / menu.js
1 (function($){$(function(){
2
3
4 function hide_menu_boxes() {
5     /* Closes any open menu boxes. */
6     $("#menu .active").each(function() {
7         $(this).removeClass("active");
8         $("#" + $(this).attr("data-box")).hide();
9         if ($(this).hasClass('dropdown')) {
10             $($(this).attr('href')).slideUp('fast');
11         }
12     });
13     $("#box-underlay").hide();
14 }
15
16 function release_menu() {
17     /* Exits the menu. It only really disappears on small screens. */
18     hide_menu_boxes();
19     $("body").removeClass("menu-showed");
20 }
21
22 /* Show menu */
23 $('#menu-toggle-on').click(function(e) {
24     e.preventDefault();
25     var body = $("body");
26     /* Just stop hiding the menu. This way, after narrowing the browser,
27      * menu will still disappear normally. */
28     body.removeClass("menu-hidden");
29     /* Menu still not visible? Really open it then. */
30     if (!$("#menu").is(":visible")) {
31         body.addClass("menu-showed");
32     }
33     _paq.push(['trackEvent', 'html', 'menu-on']);
34 });
35
36 /* Hide menu */
37 $('#menu-toggle-off').click(function(e) {
38     e.preventDefault();
39     /* Just release the menu. This way, after widening the browser,
40      * menu will still appear normally. */
41     release_menu();
42     /* Menu still visible after releasing it? Really hide it then. */
43     if ($("#menu").is(":visible")) {
44         $("body").addClass("menu-hidden");
45     }
46     _paq.push(['trackEvent', 'html', 'menu-off']);
47 });
48
49
50 /* Exit menu by clicking anywhere else. */
51 $("#box-underlay").click(release_menu);
52
53
54 /* Toggle hidden box on click. */
55 $("#menu a").each(function() {
56     var boxid = $(this).attr("data-box");
57     if (boxid) {
58         $("#" + boxid).hide();
59
60         $(this).click(function(e) {
61             e.preventDefault();
62             var showing = $(this).hasClass("active");
63             hide_menu_boxes();
64             if (!showing) {
65                 $("body").addClass("menu-showed");
66                 $(this).addClass("active");
67                 $("#box-underlay").show();
68                 $("#" + boxid).show();
69             }
70         });
71         _paq.push(['trackEvent', 'html', boxid]);
72     }
73     else if ($(this).hasClass('dropdown')) {
74         $(this).click(function(e) {
75             e.preventDefault();
76             var showing = $(this).hasClass("active");
77             hide_menu_boxes();
78             if (!showing) {
79                 $("body").addClass("menu-showed");
80                 $("#sponsors:not(:hidden)").fadeOut();
81                 $(this).addClass("active");
82                 $($(this).attr('href')).slideDown('fast');
83             }
84         });
85     }
86     else if (!$(this).hasClass('button')) {
87         $(this).click(release_menu);
88     }
89 });
90
91
92 /* Show menu item for other versions of text. 
93  * It's only present if there are any. */
94 $("#menu-other").show();
95
96
97     function insertOtherText(text) {
98         let tree = $(text);
99         let lang = tree.attr('lang') || 'pl';
100         
101         // toc?
102         // themes?
103
104         let cursor = $(".main-text-body #book-text").children().first();
105         // wstawiamy przed kursorem
106         lastTarget = '';
107         tree.children().each((i, e) => {
108             let $e = $(e);
109
110             if ($e.hasClass('anchor')) return;
111             if ($e.hasClass('numeracja')) return;
112             if ($e.attr('id') == 'toc') return;
113             if ($e.attr('id') == 'nota_red') return;
114             if ($e.attr('id') == 'themes') return;
115             if ($e.attr('name') && $e.attr('name').startsWith('sec')) return;
116             
117             if ($e.hasClass('target')) {
118                 let target = $e.attr('name');
119
120                 while (lastTarget != target) {
121                     let nc = cursor.next();
122                     if (!nc.length) {
123                         break;
124                     }
125                     cursor = nc;
126                     lastTarget = cursor.attr('name');
127                 }
128
129                 while (true) {
130                     let nc = cursor.next();
131                     if (!nc.length) {
132                         break;
133                     }
134                     cursor = nc;
135                     lastTarget = cursor.attr('name');
136                     if (lastTarget) break;
137                 }
138                 
139             } else {
140                 let d = $('<div class="other">');
141                 d.attr('lang', lang);
142                 d.append(e);
143                 d.insertBefore(cursor);
144             }
145         });
146     }
147     
148 /* Load other version of text. */
149 $(".display-other").click(function(e) {
150     e.preventDefault();
151     release_menu();
152
153     $(".other").remove();
154     $("body").addClass('with-other-text');
155
156     $.ajax($(this).attr('data-other'), {
157         success: function(text) {
158             insertOtherText(text);
159             $("#other-text-waiter").hide();
160             loaded_text($(".other"));
161         }
162     });
163     _paq.push(['trackEvent', 'html', 'other-text']);
164 });
165
166
167
168     
169
170 /* Remove other version of text. */
171 $(".other-text-close").click(function(e) {
172     release_menu();
173     e.preventDefault();
174     $(".other").remove();
175     $("body").removeClass('with-other-text');
176     _paq.push(['trackEvent', 'html', 'other-text-close']);
177 });
178
179
180 /* Release menu after clicking inside TOC. */
181     $("#toc a").click(function(){
182         release_menu();
183         _paq.push(['trackEvent', 'html', 'toc-item']);
184     });
185
186
187 if ($('#nota_red').length > 0) {
188     $("#menu-nota_red").show();
189 }
190
191 /* Show themes menu item, if there are any. */
192 if ($('#themes li').length > 0) {
193     $("#menu-themes").show();
194 }
195
196 function loaded_text(text) {
197     /* Attach events to elements inside book texts here.
198      * This way they'll work for the other text when it's loaded. */
199
200     $(".theme-begin", text).click(function(e) {
201         e.preventDefault();
202         if ($(this).css("overflow") == "hidden" || $(this).hasClass('showing')) {
203             $(this).toggleClass("showing");
204         }
205     });
206
207 }
208 loaded_text("#book-text");
209
210
211 })})(jQuery);