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