Browse Source

Anomalie #450: Hardclick autoplay

Sevajol Bastien 12 years ago
parent
commit
30dfb4c679

+ 1 - 0
src/Muzich/CoreBundle/Resources/views/layout.html.twig View File

97
         <img src="{{ asset('/bundles/muzichcore/img/1353505874_button_next.png') }}" alt="next" />
97
         <img src="{{ asset('/bundles/muzichcore/img/1353505874_button_next.png') }}" alt="next" />
98
       </a>
98
       </a>
99
       
99
       
100
+    <img id="autoplay_element_loader" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loader" />
100
     <ul id="autoplay_element"><li class="element" id="autoplay_element_container"></li></ul>
101
     <ul id="autoplay_element"><li class="element" id="autoplay_element_container"></li></ul>
101
     <div id="autoplay_player_container">
102
     <div id="autoplay_player_container">
102
       <div id="autoplay_player"></div>
103
       <div id="autoplay_player"></div>

+ 96 - 32
web/bundles/muzichcore/js/autoplay.js View File

17
   var autoplay_player_id     = "autoplay_player_id";
17
   var autoplay_player_id     = "autoplay_player_id";
18
   // étape de lecture, on commence naturellement a 0
18
   // étape de lecture, on commence naturellement a 0
19
   var autoplay_step = 0;
19
   var autoplay_step = 0;
20
+  // On utilise cette variable pour savoir quel est l'élélement choisis en 
21
+  // dernier (cas du brute click)
22
+  var autoplay_last_element_id = null;
23
+  // Pour savoir si on est en bout de ligne
24
+  var autoplay_no_more = false;
20
   
25
   
21
   // En cas de click sur un bouton de lecture
26
   // En cas de click sur un bouton de lecture
22
   $('a#autoplay_launch').click(function(){
27
   $('a#autoplay_launch').click(function(){
35
       {
40
       {
36
         // On récupère la liste d'élèments
41
         // On récupère la liste d'élèments
37
         autoplay_list = response.data;
42
         autoplay_list = response.data;
43
+        autoplay_last_element_id = autoplay_list[0].element_id;
38
         autoplay_run(0);
44
         autoplay_run(0);
39
       }
45
       }
40
       
46
       
42
     return false;
48
     return false;
43
   });
49
   });
44
   
50
   
45
-  // Lancement de l'élèment suivant
46
-  function autoplay_run(step)
51
+  function autoplay_load_element(element_id, timeouted)
47
   {
52
   {
48
-    // En premier lieu on réinitialise le lecteur en détruisant le dom qui a
49
-    // pu être créé par la lecture précedente.
50
-    $('div#'+autoplay_player_div_id+'_container').html('<div id="'+autoplay_player_div_id+'"></div>');
51
-    $('#autoplay_noelements_text').hide();
52
-    $('li#autoplay_element_container').html('');
53
-    $('img#autoplay_loader').show();
54
-    
55
-    
56
-    ////// TMP to check
57
-    if (autoplay_player_soundcloud)
53
+    // On vérifie ici que c'est le dernier élement demandé, ca evite de multiples
54
+    // requetes en cas de brute click sur les flèches.
55
+    if (!timeouted)
58
     {
56
     {
59
-      autoplay_player_soundcloud.pause();
57
+      $('#autoplay_element_loader').show();
58
+      $('li#autoplay_element_container').html('');
59
+      setTimeout(autoplay_load_element, 1000, element_id, true);
60
     }
60
     }
61
-    $('div#autoplay_player_soundcloud').hide();
62
-    
63
-    if (autoplay_list.length)
61
+    else
64
     {
62
     {
65
-    
66
-      if (array_key_exists(step, autoplay_list))
63
+      if (autoplay_last_element_id == element_id)
67
       {
64
       {
68
-        
69
-        // Récupération du dom d'un élement
70
-        $.getJSON(url_element_dom_get_one_autoplay+'/'+autoplay_list[step].element_id, function(response) {
65
+        $.getJSON(url_element_dom_get_one_autoplay+'/'+element_id, function(response) {
66
+          
67
+          $('#autoplay_element_loader').hide();
68
+      
71
           if (response.status == 'mustbeconnected')
69
           if (response.status == 'mustbeconnected')
72
           {
70
           {
73
             $(location).attr('href', url_index);
71
             $(location).attr('href', url_index);
74
           }
72
           }
75
-          
76
-          if (response.status == 'success')
73
+
74
+          if (response.status == 'success' && autoplay_last_element_id == element_id)
77
           {
75
           {
78
-            // On récupère la liste d'élèments
79
-            $('li#autoplay_element_container').html(response.data);
80
-            
81
-            // Youtube case
82
-            if (autoplay_list[step].element_type == 'youtube.com' || autoplay_list[step].element_type == 'youtu.be')
76
+            if (!autoplay_no_more)
83
             {
77
             {
84
-              youtube_create_player(autoplay_list[step].element_ref_id);
78
+              // On récupère la liste d'élèments
79
+              $('li#autoplay_element_container').html(response.data);
85
             }
80
             }
86
-            
87
-            if (autoplay_list[step].element_type == 'soundcloud.com')
81
+            else
88
             {
82
             {
89
-              soundcloud_create_player(autoplay_list[step].element_ref_id, autoplay_list[step].element_normalized_url);
83
+              $('li#autoplay_element_container').html('');
90
             }
84
             }
91
           }
85
           }
92
 
86
 
93
         });
87
         });
88
+      }
89
+      
90
+    }
91
+  }
92
+  
93
+  // Lancement de l'élèment suivant
94
+  function autoplay_run(step)
95
+  {
96
+    // En premier lieu on réinitialise le lecteur en détruisant le dom qui a
97
+    // pu être créé par la lecture précedente.
98
+    autoplay_clean_for_player_creation();
99
+    
100
+    // Pause des lecteurs potentiels
101
+    autoplay_pause_all_players();
102
+    
103
+    if (autoplay_list.length)
104
+    {
105
+    
106
+      if (array_key_exists(step, autoplay_list))
107
+      {
94
         
108
         
109
+        // Youtube case
110
+        if (autoplay_list[step].element_type == 'youtube.com' || autoplay_list[step].element_type == 'youtu.be')
111
+        {
112
+          autoplay_load_element(autoplay_list[step].element_id, false);
113
+          youtube_create_player(autoplay_list[step].element_ref_id);
114
+        }
115
+
116
+        if (autoplay_list[step].element_type == 'soundcloud.com')
117
+        {
118
+          autoplay_load_element(autoplay_list[step].element_id, false);
119
+          soundcloud_create_player(autoplay_list[step].element_ref_id, autoplay_list[step].element_normalized_url);
120
+        }
95
         
121
         
96
       }
122
       }
97
     
123
     
102
     }
128
     }
103
   }
129
   }
104
   
130
   
131
+  function autoplay_clean_for_player_creation(clean_element)
132
+  {
133
+    autoplay_pause_all_players();
134
+    
135
+    $('div#'+autoplay_player_div_id+'_container').html('<div id="'+autoplay_player_div_id+'"></div>');
136
+    $('#autoplay_noelements_text').hide();
137
+    if (clean_element)
138
+    {
139
+      $('li#autoplay_element_container').html('');
140
+    }
141
+    $('img#autoplay_loader').show();
142
+  }
143
+  
144
+  function autoplay_pause_all_players()
145
+  {
146
+    soundcloud_stop_player();
147
+  }
148
+  
105
   function autoplay_display_nomore()
149
   function autoplay_display_nomore()
106
   {
150
   {
151
+    autoplay_no_more = true;
107
     $('div#'+autoplay_player_div_id+'_container').html('<div id="'+autoplay_player_div_id+'"></div>');
152
     $('div#'+autoplay_player_div_id+'_container').html('<div id="'+autoplay_player_div_id+'"></div>');
108
     $('li#autoplay_element_container').html('');
153
     $('li#autoplay_element_container').html('');
109
     $('#autoplay_noelements_text').show();
154
     $('#autoplay_noelements_text').show();
118
   // Avancer d'un élelement dans la liste
163
   // Avancer d'un élelement dans la liste
119
   function autoplay_next()
164
   function autoplay_next()
120
   {
165
   {
166
+    autoplay_no_more = false;
121
     autoplay_step++;
167
     autoplay_step++;
122
     if (array_key_exists(autoplay_step, autoplay_list))
168
     if (array_key_exists(autoplay_step, autoplay_list))
123
     {
169
     {
170
+      autoplay_last_element_id = autoplay_list[autoplay_step].element_id;
124
       autoplay_run(autoplay_step);
171
       autoplay_run(autoplay_step);
125
     }
172
     }
126
     else
173
     else
133
   // Reculer d'un élement dans la liste
180
   // Reculer d'un élement dans la liste
134
   function autoplay_previous()
181
   function autoplay_previous()
135
   {
182
   {
183
+    autoplay_no_more = false;
136
     autoplay_step--;
184
     autoplay_step--;
137
     if (array_key_exists(autoplay_step, autoplay_list))
185
     if (array_key_exists(autoplay_step, autoplay_list))
138
     {
186
     {
187
+      autoplay_last_element_id = autoplay_list[autoplay_step].element_id;
139
       autoplay_run(autoplay_step);
188
       autoplay_run(autoplay_step);
140
     }
189
     }
141
     else
190
     else
173
   // Création du lecteur FLASH youtube
222
   // Création du lecteur FLASH youtube
174
   function youtube_create_player(ref_id)
223
   function youtube_create_player(ref_id)
175
   {
224
   {
225
+    // Préparation du terrain
226
+    autoplay_clean_for_player_creation();
227
+    
176
     var playerapiid = "ytplayerapiid";
228
     var playerapiid = "ytplayerapiid";
177
     var params = {allowScriptAccess: "always"};
229
     var params = {allowScriptAccess: "always"};
178
     var atts = {id: autoplay_player_id};
230
     var atts = {id: autoplay_player_id};
229
   function soundcloud_create_player(ref_id, ref_url)
281
   function soundcloud_create_player(ref_id, ref_url)
230
   {
282
   {
231
     
283
     
284
+    // Préparation du terrain
285
+    autoplay_clean_for_player_creation();
286
+    
232
     // Variable dans lequelle on met l'index dela track lu précedamment
287
     // Variable dans lequelle on met l'index dela track lu précedamment
233
     var index_track_previous = 0;
288
     var index_track_previous = 0;
234
      
289
      
310
          
365
          
311
   }
366
   }
312
   
367
   
368
+  function soundcloud_stop_player()
369
+  {
370
+    if (autoplay_player_soundcloud)
371
+    {
372
+      autoplay_player_soundcloud.pause();
373
+    }
374
+    $('div#autoplay_player_soundcloud').hide();
375
+  }
376
+  
313
 });
377
 });