modifications sur le template default
[mw_sourceml] / app / out / default / js / actions / sources.js
1 $(document).ready(
2   function(){
3     init_show_xml_links();
4     init_players("*");
5     init_player_listener();
6   }
7 );
8
9 // -----------------------------------------------------------------
10 //                                                      player audio
11 //
12
13 var current_document = false;
14 var autoplay_next = false;
15
16 function init_players(target){
17   $(target == "*" ? "audio" : target + " audio").each
18   ( function()
19     { var audio_elt = $(this).get(0);
20       var CAN_PLAY = false;
21       var id_document = $(this).attr("id").substring(6);
22       var id_source = null;
23       if((k = id_document.indexOf("_")) != -1) id_source = id_document.substring(0, k);
24       if(id_source){
25         $(this).find("source").each(
26           function(){
27             if(audio_elt.canPlayType($(this).attr("type"))) CAN_PLAY = true;
28          }
29         );
30         if(CAN_PLAY){
31           audio_elt.addEventListener("ended", track_ended, false);
32           $("#player_" + id_document + " .play").click(function() { play(id_document); return false; });
33           $("#player_" + id_document + " .play").css("display", "inline");
34           $("#player_" + id_document + " .pause").click(function() { pause(); return false; });
35           $("#player_" + id_document + " .pause").css("display", "none");
36           $("#player_" + id_document + " .stop").click(function() { stop(); return false; });
37           $("#player_" + id_document + " .stop").css("display", "none");
38           $("#document_" + id_document + " .no_player").remove();
39         }
40       }
41       if(!CAN_PLAY) $("#player_" + id_document).remove();
42     }
43   );
44   $(target == "*" ? ".track" : target + " .track").each(
45     function(){
46       if($(this).find(".player").size()){
47         var id_source = $(this).attr("id").substring(6);
48         $(this).find(".player_progress").first().click(
49           function(e){
50             if((progress_width = $(this).width()) != 0){
51               play_source_from(id_source, (100 * (e.pageX - this.offsetLeft)) / progress_width);
52             }
53           }
54         );
55       }
56       else $(this).find(".player_progress").first().css("cursor", "default");
57     }
58   );
59 }
60
61 function init_player_listener(){
62   setInterval("player_listener_update()", 300);
63 }
64
65 function player_listener_update(){
66   if(
67        (current_document != false)
68     && (source_id = get_current_source_id())
69     && (current_audio = $("#audio_" + current_document).get(0))
70     && ($("#track_" + source_id + " .player_progress").size())
71   ){
72     $("#track_" + source_id + " .player_progress .position").not(
73       "#track_" + source_id + " .pistes .player_progress .position").not(
74       "#track_" + source_id + " .derivation .player_progress .position").css(
75         "width",
76         Math.round((100 * current_audio.currentTime) / current_audio.duration) + "%"
77     );
78   }
79 }
80
81 function get_current_source_id(){
82   if(current_document != false){
83     if($("#document_" + current_document).size()){
84       var source_document_id = $("#document_" + current_document).attr("id").substring(9);
85       if((k = source_document_id.indexOf("_")) != -1){
86         return source_document_id.substring(0, k);
87       }
88     }
89   }
90   return false;
91 }
92
93 function play_all(){
94   autoplay_next = play_first_source();
95 }
96
97 function play_first_source(){
98   var FOUND = false;
99   $(".track").not(".pistes .track").not(".derivation .track").each(
100     function(){
101       if(!FOUND){
102         $(this).find(".documents li").not(".pistes .documents li").not(".derivation .documents li").each(
103           function(){
104             if(!FOUND){
105               var source_document_id = $(this).attr("id").substring(9);
106               if(source_document_id.length > 0){
107                 if($("#player_" + source_document_id).size()){
108                   FOUND = true;
109                   play(source_document_id);
110                 }
111               }
112             }
113           }
114         );
115       }
116     }
117   );
118   return FOUND;
119 }
120
121 function play_next_source(){
122   var FOUND = false;
123   if(current_document != false){
124     if($("#document_" + current_document).size()){
125       var current_source_document_id = $("#document_" + current_document).attr("id");
126       var current_source_id = get_current_source_id();
127       if(current_source_id != false){
128         var CURRENT_FOUND = false;
129         $(".track").not(".pistes .track").not(".derivation .track").each(
130           function(){
131             if(!FOUND){
132               if(CURRENT_FOUND){
133                 $(this).find(".documents li").not(".pistes .documents li").not(".derivation .documents li").each(
134                   function(){
135                     if(!FOUND){
136                       var source_document_id = $(this).attr("id").substring(9);
137                       if(source_document_id.length > 0){
138                         if($("#player_" + source_document_id).size()){
139                           FOUND = true;
140                           play(source_document_id);
141                         }
142                       }
143                     }
144                   }
145                 );
146               }
147               else{
148                 if(current_source_id == $(this).attr("id").substring("6")){
149                   CURRENT_FOUND = true;
150                 }
151               }
152             }
153           }
154         );
155       }
156     }
157   }
158   return FOUND;
159 }
160
161 function play(id_document){
162   if(current_document == id_document){
163     if($("#audio_" + current_document).get(0).paused){
164       gui_state("playing");
165       $("#audio_" + current_document).get(0).play();
166     }
167   }
168   else{
169     if(current_document != false){
170       var audio_elt = $("#audio_" + current_document).get(0);
171       audio_elt.pause();
172       audio_elt.currentTime = 0;
173       gui_state("stoped");
174     }
175     current_document = id_document;
176     _play(0);
177   }
178 }
179
180 function play_source_from(id_source, position){
181   if(
182        (current_document != false)
183     && (current_source_id = get_current_source_id())
184     && (id_source == current_source_id)
185   ){
186     _play(position);
187   }
188   else{
189     stop();
190     var FOUND = false;
191     $("#track_" + id_source + " .documents li").each(
192       function(){
193         if(!FOUND){
194           var source_document_id = $(this).attr("id").substring(9);
195           if(source_document_id.length > 0){
196             if($("#player_" + source_document_id).size()){
197               FOUND = true;
198               current_document = source_document_id;
199               _play(position);
200             }
201           }
202         }
203       }
204     );
205   }
206 }
207
208 function _play(position){
209   if(current_document != false){
210     var audio_elt = $("#audio_" + current_document).get(0);
211     audio_elt.preload = "auto";
212     audio_elt.addEventListener("loadeddata", track_loadeddata, false);
213     audio_elt.addEventListener("canplaythrough", track_canplaythrough, false);
214     audio_elt.addEventListener("playing", track_playing, false);
215     gui_state("loading");
216     audio_elt.position = position;
217     audio_elt.load();
218   }
219 }
220
221 function track_loadeddata(event){
222   var audio_elt = event.target;
223   audio_elt.removeEventListener("loadeddata", track_loadeddata, false);
224   if(current_document != false){
225     audio_elt.currentTime = audio_elt.position ? (audio_elt.position * audio_elt.duration) / 100 : 0;
226   }
227 }
228
229 function track_canplaythrough(event){
230   var audio_elt = event.target;
231   audio_elt.removeEventListener("canplaythrough", track_canplaythrough, false);
232   audio_elt.play();
233 }
234
235 function track_playing(event){
236   var audio_elt = event.target;
237   audio_elt.removeEventListener("playing", track_playing, false);
238   gui_state("playing");
239 }
240
241 function pause(){
242   if(current_document != false){
243     var audio_elt = $("#audio_" + current_document).get(0);
244     audio_elt.pause();
245     gui_state("paused");
246   }
247 }
248
249 function stop(){
250   if(current_document != false){
251     var audio_elt = $("#audio_" + current_document).get(0);
252     audio_elt.pause();
253     audio_elt.currentTime = 0;
254     gui_state("stoped");
255     current_document = false;
256   }
257   autoplay_next = false;
258 }
259
260 function track_ended(){
261   gui_state("stoped");
262   var current_audio = $("#audio_" + current_document).get(0);
263   current_audio.pause();
264   if(current_audio.currentTime) current_audio.currentTime = 0;
265   if(autoplay_next) autoplay_next = play_next_source();
266 }
267
268 function gui_state(state){
269   gui_blur();
270   if(current_document != false){
271     var source_id = get_current_source_id();
272     $("#track_" + source_id).removeClass("loading_player");
273     if(state == "playing"){
274       $("#player_" + current_document).find(".play").css("display", "none");
275       $("#player_" + current_document).find(".pause").css("display", "inline");
276       $("#player_" + current_document).find(".stop").css("display", "inline");
277 //      $("#track_" + source_id).removeClass("track");
278       $("#track_" + source_id).addClass("playing_track");
279     }
280     else if(state == "paused"){
281       $("#player_" + current_document).find(".play").get(0).style.display = "inline";
282       $("#player_" + current_document).find(".pause").get(0).style.display = "none";
283       $("#player_" + current_document).find(".stop").get(0).style.display = "inline";
284 //      $("#track_" + source_id).removeClass("track");
285       $("#track_" + source_id).addClass("playing_track");
286     }
287     else if(state == "stoped"){
288       $("#player_" + current_document).find(".play").get(0).style.display = "inline";
289       $("#player_" + current_document).find(".pause").get(0).style.display = "none";
290       $("#player_" + current_document).find(".stop").get(0).style.display = "none";
291       $("#track_" + source_id).removeClass("playing_track");
292 //      $("#track_" + source_id).addClass("track");
293       $("#track_" + source_id + " .player_progress .position").not(
294         "#track_" + source_id + " .pistes .player_progress .position").not(
295         "#track_" + source_id + " .derivation .player_progress .position").css("width", "0%");
296     }
297     else if(state == "loading"){
298       $("#track_" + source_id).addClass("loading_player");
299     }
300   }
301 }
302
303 function gui_blur(){
304   if((current_document != false) && $("#player_" + current_document).size()){
305     $("#player_" + current_document).find(".play").get(0).blur();
306     $("#player_" + current_document).find(".pause").get(0).blur();
307     $("#player_" + current_document).find(".stop").get(0).blur();
308   }
309 }
310
311 // -----------------------------------------------------------------
312 //                                                        source xml
313 //
314
315 var loaded = false;
316 var show_xml_links = {};
317
318 function show_xml(id){
319   alert(xml_contents[id]);
320 }
321
322 function init_show_xml_links(){
323   for(var i in show_xml_links){
324     $("#show_xml_" + i).colorbox();
325   }
326   loaded = true;
327 }
328
329 function set_show_xml_links(i){
330   if(loaded) $("#show_xml_" + i).colorbox();
331   else show_xml_links[i] = true;
332 }
333
334 // -----------------------------------------------------------------
335 //                                             sources / derivations
336 //
337
338 function toggle_source_list(id_block){
339   var content = $("#source_list_" + id_block + " .pistes").html();
340   if(content.length > 0){
341     $("#source_list_" + id_block).slideUp(200);
342     $("#source_list_" + id_block + " .pistes").empty();
343     $("#toggle_sources_list_" + id_block + ".block_list_toggle").html("[+]");
344   }
345   else{
346     $("#source_list_" + id_block).slideDown(200);
347     $("#toggle_sources_list_" + id_block + ".block_list_toggle").html("[-]");
348     $("#source_list_" + id_block + " .pistes").html("<div class=\"loading\"><span>en chargement...</span></div>");
349     $.ajax({
350       url: mw_site_url + "index.php?e=content/sources/sources&id=" + id_block,
351       dataType: "html",
352       success: function(content){
353         $("#source_list_" + id_block + " .pistes").html(content);
354         init_players("#source_list_" + id_block + " .pistes");
355       }
356     });
357   }
358 }
359
360 function toggle_derivation_list(id_block){
361   var content = $("#derivation_list_" + id_block + " .derivation").html();
362   if(content.length > 0){
363     $("#derivation_list_" + id_block).slideUp(200);
364     $("#derivation_list_" + id_block + " .derivation").empty();
365     $("#toggle_derivation_list_" + id_block + ".block_list_toggle").html("[+]");
366   }
367   else{
368     $("#derivation_list_" + id_block).slideDown(200);
369     $("#toggle_derivation_list_" + id_block + ".block_list_toggle").html("[-]");
370     $("#derivation_list_" + id_block + " .derivation").html("<div class=\"loading\"><span>en chargement...</span></div>");
371     $.ajax({
372       url: mw_site_url + "index.php?e=content/sources/derivations&id=" + id_block,
373       dataType: "html",
374       success: function(content){
375         $("#derivation_list_" + id_block + " .derivation").html(content);
376         init_players("#derivation_list_" + id_block + " .derivation");
377       }
378     });
379   }
380 }