]> git.0d.be Git - panikweb.git/blob - panikweb_templates/static/js/specifics.js
js: remove unnecessary call to window.onpopstate()
[panikweb.git] / panikweb_templates / static / js / specifics.js
1 var urlParams;
2 (window.onpopstate = function () {
3     var match,
4         pl     = /\+/g,  // Regex for replacing addition symbol with a space
5         search = /([^&=]+)=?([^&]*)/g,
6         decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
7         query  = window.location.search.substring(1);
8
9     urlParams = {};
10     while (match = search.exec(query))
11        urlParams[decode(match[1])] = decode(match[2]);
12 })();
13
14 $(function() {
15
16         doLog = function(aTextToLog, type){
17                 var aLog = $('<div>',{class:"log "+type,html:aTextToLog});
18                 aLog.hide().prependTo($log).show('fast').delay(3000).hide('fast', function() { 
19                         $(this).remove(); 
20                 });
21         }
22         var $main = $("#Changing");
23         var $metaNav = $("#metaNav");
24         var $log = $("#userLog");
25         tooltip_options = {
26                 position: {
27                         my: "center bottom-5",
28                         at: "center top",
29                         using: function( position, feedback ) {
30                                 $( this ).css( position );
31                                 $( "<div>" )
32                                 .addClass( "arrow" )
33                                 .addClass( feedback.vertical )
34                                 .addClass( feedback.horizontal )
35                                 .appendTo( this );
36                         }
37                 }
38         };
39
40         $('.newsRoll').tooltip(tooltip_options);
41         $('#metaNav').tooltip(tooltip_options);
42
43         $('body').css({'min-height':$(window).height() + $('#Commons').height()});
44         /****************************************************/
45         /**** AJAX UTILITIES FOR REQUESTS ****/
46         /****************************************************/
47         String.prototype.decodeHTML = function() {
48                 return $("<div>", {html: "" + this}).html();
49         };
50         var loadPage_request = null;
51         afterLoad = function(html, textStatus, XMLHttpRequest) {
52                 loadPage_request = null;
53                 if (textStatus == "error") {
54                         doLog('Sorry! And error occur when loading page content','error');
55                 }
56                 new_content = $($.parseHTML(html)).find('#Changing>*');
57                 $main.hide().empty().append(new_content).show();
58
59                 new_menu = $($.parseHTML(html)).find('#metaNav>*');
60                 $metaNav.empty().append(new_menu);
61
62                 var newTitle = html?html.match(/<title>(.*?)<\/title>/):'';
63                 if(newTitle){document.title = newTitle[1].trim().decodeHTML();}
64
65                 /*
66                 Quite UGLY but needed for styling the whole body with ID
67                 Feel free to correct and find a better way
68                 According to this link the probles is that $(html).filter('body').Attr('id') will not work!
69                 http://www.devnetwork.net/viewtopic.php?f=13&t=117065
70                 */
71                 if(sectionName = $(html).find('[data-section]').attr('data-section')){
72                         $('body').attr('id',sectionName);
73                 }else{
74                         var bodyID = html.match(/<body id="(.*?)">/);   
75                         if(bodyID){$('body').attr('id',bodyID[1].trim());}      
76                 }
77                 $("#Changing").css({'min-height':$(window).height()});
78                 $.scrollTo('#Changing',1000,{offset:-$('#metaNav').height()+2});
79                 init();
80
81                 if (typeof (Piwik) == 'object') {
82                         piwikTracker = Piwik.getAsyncTracker();
83                         if (typeof (piwikTracker.trackPageView) == 'function') {
84                                 piwikTracker.setDocumentTitle(document.title);
85                                 piwikTracker.setCustomUrl(window.location.href);
86                                 piwikTracker.trackPageView();
87                         }
88                 }
89
90         };
91
92         function afterLoadError(xhr, text, error) {
93                 afterLoad(xhr.responseText, 'error', xhr);
94         };
95
96         $(window).on("popstate", function(e) {
97                 if (e.originalEvent.state !== null) {loadPage(location.href, false);}
98         });
99
100         loadPage = function(href, push_state) {
101                 if (push_state !== false) {
102                         history.pushState({}, '', href);
103                 }
104                 if (loadPage_request !== null) {
105                         loadPage_request.abort();
106                 }
107                 loadPage_request = $.ajax({
108                         url: href,
109                         success: afterLoad,
110                         error: afterLoadError,
111                         dataType: 'html'});
112         };
113         $.fn.ajaxifyClick = function(params) {
114                 this.each(function() {
115                         $(this).unbind('click');
116                         $(this).bind('click',function(e){
117                                 var href = $(this).attr("href");
118                                 if (e.which == 2) {
119                                         window.open(href, '_blank');
120                                         return false;
121                                 }
122                                 $(this).addClass('loading');
123                                 /* this checks the link points to a local document, be
124                                  * it because it's just a path, or because the server
125                                  * part points to the same domain */
126                                 if (!href) {
127                                         doLog('No href attributes, unable to load content','error');
128                                         $("#All a, #All area").removeClass('loading');
129                                         return false;
130                                 }else if (!$(this).attr('target') && (
131                                                 href.indexOf(document.domain) > -1 ||href.indexOf(':') === -1 || href.indexOf(':') > 5
132                                         )) {
133                                         loadPage(href);
134                                         return false;
135                                 }else{
136                                         $(this).attr('target','_blanck');
137                                         $("#All a, #All area").removeClass('loading');
138                                         return true;
139                                 }
140                         });
141                 });
142         };
143         /****************************************************/
144         /**** AUDIOPLAYER ****/
145         /****************************************************/
146
147         var timer = null, 
148         interval = 5000;
149         var ticker_interval = null;
150         /*
151         //TODO: mini icon version for player, (playpause only)
152         $('#togglePlayer').on('click',function(e) {
153                 e.preventDefault();
154                 if($(this).is('.icon-double-angle-left')){
155                         $("#player").fadeOut('fast',function(){
156                                 $("#player-container").toggleClass('minimized');                        
157                                 $('#togglePlayer').toggleClass('icon-double-angle-left icon-double-angle-right');
158                         });                     
159                 }else{
160                         $("#player").fadeIn('fast',function(){
161                                 $("#player-container").toggleClass('minimized');                        
162                                 $('#togglePlayer').toggleClass('icon-double-angle-left icon-double-angle-right');
163                         });     
164                 }
165                 return false;
166         });
167         */
168         $('#WhatsOnAir').on('load',function(){
169                 var WhatsOnAir = $(this);
170                 $.getJSON('/onair.json', function(onair) {
171                         WhatsOnAir.fadeOut();
172                         if(onair.data.episode || onair.data.emission){
173                                 var onairContainer = $('<span>');
174                                 if(onair.data.emission){
175                                         $('<a>',{href:onair.data.emission.url,html:onair.data.emission.title}).appendTo(onairContainer).ajaxifyClick();
176                                 }
177                                 if(onair.data.episode){
178                                         $('<span> - </span>').appendTo(onairContainer);
179                                         $('<a>',{href:onair.data.episode.url,html:onair.data.episode.title}).appendTo(onairContainer).ajaxifyClick();
180                                 }
181                                 WhatsOnAir.empty().append(onairContainer);
182                         } else if (onair.data.nonstop) {
183                                 result = onair.data.nonstop.title;
184                                 WhatsOnAir.html('<span>' + result + '</span>');
185                         }
186                         else{WhatsOnAir.html('<span>Unknown (Probably Non-Stop)</span>');}
187                         WhatsOnAir.fadeIn();
188                 });
189         }).trigger('load');
190         $('#RefreshWhatsOnAir').on('activate',function(e){
191                 $(this).addClass('spinning');
192                 $('#WhatsOnAir').addClass('active');
193                 $('#WhatsOnAir').trigger('load');
194                 timer = setInterval( "$('#WhatsOnAir').trigger('load');", interval);
195         }).on('deactivate',function(e){
196                 $(this).removeClass('spinning');
197                 $('#WhatsOnAir').removeClass('active');
198                 clearInterval(timer);
199         }).on('click',function(e){
200                 $(this).toggleClass('spinning');
201                 if($(this).is('.spinning')){
202                         $(this).trigger('activate');                                            
203                 }else{$(this).trigger('deactivate');}
204                 return false;
205         });
206         $("#DirectStreamPanikControler").on('click',function(e) {
207                 e.preventDefault();
208                 var stream = $('#DirectStreamPanik').get(0);
209                 if (stream.paused == false){
210                         stream.pause();
211                 }else{
212                         stream.play();
213                 }
214                 return false;
215         });
216         $('#DirectStreamPanik').on('play',function(){
217                 $('audio:not(#DirectStreamPanik)').each(function(){this.pause();});
218                 $('#streamSymbol').removeClass('icon-volume-up').addClass('icon-pause');
219                 $('#RefreshWhatsOnAir').trigger('activate');
220         }).on('pause',function(){
221                 //$('audio:not(#DirectStreamPanik)').each(function(){this.pause();});
222                 $('#streamSymbol').addClass('icon-volume-up').removeClass('icon-pause');
223                 $('#RefreshWhatsOnAir').trigger('deactivate');
224         });
225         if($('#player-container').offset()){
226                 var topPosition = 0;
227                 topPosition = $('#player-container').offset().top;
228                 $(window).bind('scroll load',function (event) {
229                         //$('#player-container').removeClass('fixed');
230                         var y = $(this).scrollTop() + $('#metaNav').height() - $('#Player').height() + 10;
231                         if (topPosition!== 0 && y >= topPosition) {
232                                 $('#Player').css({'min-height':$('#Player').height()});
233                                 $('#player-container').addClass('fixed').removeClass('normal');
234                         } else {
235                                 $('#Player').css({'min-height':'auto'});        
236                                 $('#player-container').removeClass('fixed').addClass('normal');
237                         }
238                 });
239         }
240
241         var $localList = $('#localList').playlist({
242                 controlContainer: $('<div>',{class:"playListControls"}).sortable(),
243                 playlistContainer: $('<ol>',{id:"myPlaylist",class:"custom"}).sortable(),
244                 onLoad:function(self){
245                         $('#toggleList').on('click',function(){ 
246                                 self.playlistContainer.toggleClass('deploy');
247                         });
248                         $('#emptyList').on('click',function(){ 
249                                 self._reset();
250                         });
251
252                         if(self.isActive){
253                                 self.playlistContainer.scrollTo(self.isActive, 800 );
254                                 self.isActive.find('audio').attr('preload',"preload")
255                         }
256                         self.controlButtons['playpause'].addClass('resymbol');
257                 },
258                 onPlay:function(self){
259                         $('#DirectStreamPanik')[0].pause();
260                         self.playlistContainer.scrollTo(self.isActive, 800 );
261                 },
262                 onAdd:function(self){
263                         //self.isLastAdd[0].scrollIntoView();
264                         self.isLastAdd.find('a').ajaxifyClick();
265                         self.playlistContainer.scrollTo(self.isLastAdd, 800).delay(1000).scrollTo(self.isActive, 800 ).clearQueue();
266
267                 },
268                 onUpdate:function(self){
269                         //doLog(JSON.stringify(self.playlist, null, '\t'));     
270                         if(self.playlist.length >= 1){
271                                 self.element.show();
272                                 $('#Player').addClass('withPlaylist').removeClass('withoutPlaylist');
273                         }else{
274                                 self.element.hide();
275                                 $('#Player').removeClass('withPlaylist').addClass('withoutPlaylist');
276                         }
277                 }
278         });
279
280         init = function() {
281                 $("#All a, #All area").removeClass('loading');
282                 $("#All a, #All area").ajaxifyClick();
283                 $("#search-form").unbind('submit').on('submit', function(event) {
284                         event.preventDefault();
285                         $(this).addClass('loading');
286                         console.log('action:', $(this).attr('action') + '?' + $(this).serialize());
287                         loadPage($(this).attr('action') + '?' + $(this).serialize());
288                 });
289                 $(".tabs").each(function() {
290                         var self = $(this);
291                         var about= $($(this).attr("data-tab-about"));
292                         var current = $(this).find("[data-tab].active")[0];
293                         var dftShowSelector = current?".active":":first";
294                         var activeTab = $(this).find("[data-tab]"+dftShowSelector+"").addClass("active");
295                         $(this).find("[data-tab]").each(function() {
296                             $(this).on('click load',function (e) {  
297                                 e.preventDefault();
298                                 self.find(".active").removeClass("active");  
299                                 $(this).addClass("active");  
300                                 about.find("[data-tabbed]").hide();  
301                                 $($(this).attr("data-tab")).fadeIn();  
302                 
303                             });  
304                         });  
305                         activeTab.trigger('load');
306                 });
307                 $('[data-player-action]').on('click',function(){
308                         var audio = $('#'+$(this).attr('data-player-audio'));
309                         if($(this).attr('data-player-action') == "registerAudio"){
310                                 $localList.playlist("registerAudio",audio,doLog(audio.attr('title')+' has been added to your playlist.','ok'));
311                         }else if($(this).attr('data-player-action') == "playAudio"){
312                                 $localList.playlist("registerAudio",audio,doLog(audio.attr('title')+' will play soon.','ok'));
313                                 $localList.playlist("playLast");
314                         }else{
315                                 $localList.playlist($(this).attr('data-player-action'));
316                         }
317                 });
318                 $('[data-player-control]').each(function(){
319                         var audio = $('#'+$(this).attr('data-player-audio'));
320                         $localList.playlist("bindControl",$(this).attr('data-player-control'),audio,$(this));
321                 });
322
323                 $('[data-highlight]').on('check',function(){
324                         $($(this).attr('data-about')).find($(this).attr('data-highlight')).addClass('highlighted').removeClass('normal');
325                 }).on('uncheck',function(){
326                         $($(this).attr('data-about')).find($(this).attr('data-highlight')).removeClass('highlighted').addClass('normal');
327                 }).on('click',function(){
328                         $(this).toggleClass('icon-check icon-check-empty');
329                         if($(this).hasClass('icon-check')){$(this).trigger('check');
330                         }else{  $(this).trigger('uncheck');}
331                 });
332                 $('[data-highlight].icon-check-empty').each(function(){
333                         $(this).trigger('uncheck');
334                 });
335                 $('[data-toggle]').on('check',function(){
336                         /* make sure all other unchecked items are hidden */
337                         $('[data-toggle].icon-check-empty').each(function() {
338                                 $($(this).attr('data-about')).find($(this).attr('data-toggle')).hide();
339                         });
340                         $($(this).attr('data-about')).find($(this).attr('data-toggle')).show();
341                 }).on('uncheck',function(){
342                         $($(this).attr('data-about')).find($(this).attr('data-toggle')).hide();
343                         if ($('[data-toggle].icon-check').length == 0) {
344                                 /* special case the situation where all toggles
345                                  * are unchecked, as we want that to mean
346                                  * "everything", not "nothing".
347                                  */
348                                 $('[data-toggle].icon-check-empty').each(function() {
349                                         $($(this).attr('data-about')).find($(this).attr('data-toggle')).show();
350                                 });
351                         }
352                 }).on('click',function(){
353                         $(this).toggleClass('icon-check icon-check-empty');
354                         if($(this).hasClass('icon-check')){$(this).trigger('check');
355                         }else{  $(this).trigger('uncheck');}
356                 });
357                 $('[data-toggle].icon-check-empty').each(function(){
358                         $(this).trigger('uncheck');
359                 });
360
361                 initial_enabled_toggles = {};
362                 if (typeof(urlParams.q) == 'string') {
363                         urlParams.q.split('|').forEach(function(a) { initial_enabled_toggles[a] = 1; })
364                 }
365                 $('[data-toggle]').each(function() {
366                         if ($(this).data('toggle').substring(1) in initial_enabled_toggles) {
367                                 $(this).trigger('click');
368                         }
369                 });
370
371                 if ($('input#id_q').val() == '') {
372                         $('input#id_q').focus();
373                 }
374
375                 $('#ticker li:not(:first)');
376                 if (ticker_interval) clearInterval(ticker_interval);
377                 function tick(){
378                     $('#ticker li:first').animate({'opacity':0}, 200, function () {
379                         $(this).appendTo($('#ticker')).css('opacity', 1);
380                     });
381                 }
382                 $("#roller button").on('click',function(e){
383                     clearInterval(ticker_interval);
384                     e.preventDefault();
385                     $($(this).attr('data-about')).prependTo('#ticker');
386                     return false;
387                 });
388                 ticker_interval = setInterval(function(){tick();  }, 20000);/**/
389
390         }
391         init();
392 });