]> git.0d.be Git - panikweb.git/blob - panikweb_templates/static/js/audioPlayer.js
Debug player, playlist wasn't saved anymore
[panikweb.git] / panikweb_templates / static / js / audioPlayer.js
1 (function($) {
2         var thePlaylist;
3         $.widget( "panik.playlist", {
4                 /*
5                         sound = {
6                                 source :{
7                                         ogg:"oggURL",
8                                         mp3:"mp3URL"
9                                 },
10                                 emission: "episode.slug",
11                                 episode: "episode.slug",
12                                 id:""
13                         }
14                 */
15                 options: {
16                         playlist: [],
17                         html5audioOptions:{controls:true},
18                         classes: "",
19                         itemClasses: "",
20                         controlContainer: $('<div>'),
21                         playlistContainer: $('<ol>'),
22                         onLoad: function(){},
23                         onAdd: function(){},
24                         onPlay: function(){},
25                         onUpdate: function(){},
26                 },
27                 _create: function() {
28                 // Initialization logic here
29                         thePlaylist = this;     
30                         this.isActive = false;
31                         this.isLastAdd = false;
32                         this.controlButtons = []
33                         this.debugContainer = $('<pre>').hide();
34                         this.controlContainer = this.options.controlContainer;
35                         this.playlistContainer = this.options.playlistContainer;
36                         this.element.addClass(this.options.classes);    
37                         this.element.append(this.controlContainer);     
38                         this.element.append(this.playlistContainer);
39                         this.element.append(this.debugContainer);
40                         this.playlist = this.options.playlist;
41                         this.buildPlaylistControls();
42                         this.loadPlaylist();
43                         this._update();
44                         this.options.onLoad(this);
45                 },
46
47                 _setOption: function( key, value ) {
48                         this.options[ key ] = value;
49                         this._update();
50                 },
51                 _update: function() {   
52                         this.playlist = [];     
53                         this.playlistContainer.find('audio').each(function(){
54                                 thePlaylist.playlist.push(thePlaylist.jsonifyAudio($(this)));
55                         });
56                         this.debugContainer.text(
57                                 JSON.stringify(this.playlist, null, '\t')
58                         );
59                         this.savePlaylist();
60                         this.options.onUpdate(this);
61                         return this.playlist;
62                 },
63                 _reset: function() {    
64                         this.isActive =false;
65                         this.stopSounds();
66                         this.playlistContainer.find('*').remove();
67                         this._update();
68                 },
69                 savePlaylist: function(){
70                         var JSONPlaylist = JSON.stringify(this.playlist, null, '\t');
71                         localStorage['playlist'] = JSON.stringify(this.playlist, null, '\t');
72                 },
73                 loadPlaylist: function(){
74                         this.playlist = localStorage['playlist']?JSON.parse(localStorage['playlist']):this.playlist;
75                         $.each(this.playlist,function(k,v){
76                                 thePlaylist.playlistContainer.append(thePlaylist._htmlifyJsonSound(v));
77                         });
78                         return this.playlist;
79                 },
80                 // Transform HTML5 <audio> to simple JSON object.
81                 jsonifyAudio: function(audio) {
82                         var sound = {
83                                 source :{},
84                                 title: audio.attr('title'),
85                                 id:audio.attr('id'),
86                                 url:audio.attr('data-url'),
87                                 focus:audio.attr('data-player-focus')
88                         };
89                         audio.children('source').each(function(){
90                                 sound.source[$(this).attr('type')] = $(this).attr('src');
91                         });
92                         return sound;
93                 },
94                 // Play next sound
95                 setFocus: function(element) {
96                         this.isActive = element;
97                         this.playlistContainer.find('li').each(function(){
98                                 $(this).removeClass('active');
99                                 $(this).find('audio').removeAttr('data-player-focus');
100                         });
101                         this.isActive.addClass('active').find('audio').attr('data-player-focus',true);
102                         this._update();
103                 },
104                 // Transform JSON sound object to HTML container for playlist.
105                 _htmlifyJsonSound: function(sound) {
106                         var container = $('<li>').attr('data-origin',sound.id);
107                         var audio = $('<audio>',this.options.html5audioOptions)
108                                 .attr('title',sound.title).hide()
109                                 .attr('data-url',sound.url);
110                         $.each(sound.source,function(k,v){
111                                 var source = $('<source>',{src:v,type:k});
112                                 audio.append(source);
113                         });
114                         audio.on('play',function(){
115                                 thePlaylist.setFocus(container);
116                                 container.addClass('playing');
117                                 playpause.addClass('icon-pause').removeClass('icon-play');
118                                 thePlaylist.controlButtons['playpause'].removeClass('icon-play').addClass('icon-pause');
119                                 thePlaylist.afterPlay();
120                         }).on('pause',function(){
121                                 $(this).removeClass('playing');
122                                 playpause.addClass('icon-play').removeClass('icon-pause');
123                                 thePlaylist.controlButtons['playpause'].removeClass('icon-pause').addClass('icon-play');
124                         }).on('stop',function(){
125                                 $(this).trigger('pause');
126                                 if($(this)[0].currentTime){$(this)[0].currentTime = 0;}
127                         }).on("ended", function(){
128                                 thePlaylist.playNext();
129                         }).on('beforePause',function(){
130                                 return this;
131                         }).on('beforePlay',function(){
132                                 thePlaylist.pauseSounds();
133                                 return this;
134                         });
135                         var controls = $('<span>',{class:'soundControls controls'});
136                         var link = $('<a>',{href:sound.url,class:'button icon-external-link'});
137                         var html5 = $('<button>',{title:"Display HTML5 audio",class:'icon-html5',click:function(){
138                                 audio.toggle();
139                         }}).hide();
140                         var remove = $('<button>',{title:"Remove from list",class:'icon-remove',click:function(){
141                                 container.remove();
142                                 thePlaylist._update();
143                         }});
144                         var stop = $('<button>',{title:"Stop",class:'icon-stop',click:function(){
145                                 audio.trigger('stop');
146                         }}).hide();
147                         var playpause = $('<button>',{title:"Play/Pause",class:'icon-play',click:function(){
148                                 
149                                   if (audio[0].paused == false) { audio.trigger('pause');
150                                   } else {
151                                         audio.trigger('beforePlay').trigger('play');
152                                 }
153                         }});
154                         controls.append(playpause).append(stop).append(remove).append(html5);
155                         var title = $('<a>',{title:"More information",href:sound.url,class:"button title",html:sound.title});
156                         container.append(controls).append(title).append(audio);
157                         if(sound.focus){thePlaylist.setFocus(container);}
158                         return container;
159                 },
160                 // Create a public method.
161                 registerAudio: function(audio,success) {
162                         var audioObj = this.jsonifyAudio(audio);
163                         var htmlAudio = this._htmlifyJsonSound(audioObj);
164                         this.playlistContainer.append(htmlAudio);
165                         this.isLastAdd = htmlAudio;
166                         if(!this.isActive){this.setFocus(this.isLastAdd);}
167                         this.options.onAdd(this);
168                         if(success){success();}
169                         this._update();
170                 },
171                 // Play next sound
172                 bindControl: function(control,audio,element,options) {
173                         element.addClass('loading');
174                         audioID = audio.attr('id');                     
175                         //TODO for controls in page content
176                 },
177                 // Play next sound
178                 registerControl: function(name,options) {
179                         this.controlButtons[name] = $('<button>',options);
180                         this.controlContainer.append(this.controlButtons[name]);
181                 },
182                 // Build controls
183                 buildPlaylistControls: function() {
184                         this.controlContainer.empty();
185                         /*
186                         this.registerControl('toggleList',{class:"icon-list",click:     function(){ 
187                                 thePlaylist.playlistContainer.toggle();
188                         }});
189                         this.registerControl('clearList',{class:"icon-trash",click:     function(){ 
190                                 thePlaylist.playlistContainer.empty();
191                                 thePlaylist._update();
192                         }});
193                         */
194                         this.registerControl('previous',{class:"icon-step-backward",click:function(){
195                                 thePlaylist.playPrevious();
196                         }});
197                         this.registerControl('stop',{class:"icon-stop",click:   function(){ 
198                                 thePlaylist.stopSounds();
199                         }});
200                         this.registerControl('playpause',{class:"icon-play playPause",click:    function(){ 
201                                 thePlaylist.playPauseList();
202                         }});
203                         this.registerControl('next',{class:"icon-step-forward",click:   function(){ 
204                                 thePlaylist.playNext();
205                         }});
206                         return true;
207                 },
208                 // Play next sound
209                 afterPlay: function() {
210                         this.options.onPlay(this);
211                 },
212                 // Play next sound
213                 beforePlay: function() {
214                         this.stopSounds();
215                 },
216                 // Play next sound
217                 getActive: function() {
218                         if(!this.isActive){
219                                 this.isActive = this.playlistContainer.children('li').first();                          
220                         }
221                         return this.isActive;
222                 },
223                 // Play next sound
224                 playPauseList: function() {
225                         if(this.controlButtons['playpause'].hasClass('icon-play')){
226                                 this.getActive().find('audio').trigger('beforePlay').trigger('play');
227                         }else{
228                                 this.getActive().find('audio').trigger('pause');        
229                         }
230                         return true;
231                 },
232                 // Play next sound
233                 playPrevious: function() {
234                         this.getActive().prev().find('audio').trigger('beforePlay').trigger('play');
235                         return true;
236                 },
237                 // Play next sound
238                 playNext: function() {
239                         this.getActive().next().find('audio').trigger('beforePlay').trigger('play');
240                         return true;
241                 },
242                 // Play next sound
243                 playFirst: function() {
244                         this.playlistContainer.find('audio').first().trigger('beforePlay').trigger('play');
245                         return true;
246                 },
247                 // Play next sound
248                 playLast: function() {
249                         this.playlistContainer.find('audio').last().trigger('beforePlay').trigger('play');
250                         return true;
251                 },
252                 // Pause all sounds.
253                 pauseSounds: function() {
254                         this.playlistContainer.find('audio').each(function(){$(this).trigger('pause');});       
255                 },
256                 // stop all sounds.
257                 stopSounds: function() {
258                         this.playlistContainer.find('audio').each(function(){$(this).trigger('stop');});        
259                 },
260         });
261 })(jQuery);
262