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