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