Browse Source

Evolution #90: Ajaxsification des actions

bastien 13 years ago
parent
commit
af3dfd39de

+ 23 - 2
src/Muzich/CoreBundle/Controller/CoreController.php View File

200
 
200
 
201
       if ($this->getRequest()->isXmlHttpRequest())
201
       if ($this->getRequest()->isXmlHttpRequest())
202
       {
202
       {
203
-
203
+        // Récupération du li
204
+        $html = $this->render('MuzichCoreBundle:SearchElement:li.element.html.twig', array(
205
+          'element'     => $element,
206
+          'class_color' => 'odd'
207
+        ))->getContent();
208
+        
209
+        return $this->jsonResponse(array(
210
+          'status' => 'success',
211
+          'html'   => $html
212
+        ));
204
       }
213
       }
205
       else
214
       else
206
       {
215
       {
212
     {
221
     {
213
       if ($this->getRequest()->isXmlHttpRequest())
222
       if ($this->getRequest()->isXmlHttpRequest())
214
       {
223
       {
215
-        return $this->jsonResponse(array(true));
224
+        // Récupération des erreurs
225
+        $validator = $this->container->get('validator');
226
+        $errorList = $validator->validate($form);
227
+
228
+        foreach ($errorList as $error)
229
+        {
230
+          $errors[] = $error->getMessage();
231
+        }
232
+        
233
+        return $this->jsonResponse(array(
234
+          'status' => 'error',
235
+          'errors' => $errors
236
+        ));
216
       }
237
       }
217
       else
238
       else
218
       {
239
       {

+ 5 - 0
src/Muzich/CoreBundle/Resources/views/SearchElement/li.element.html.twig View File

1
+<li class="element {{ class_color }}" id="element_{{ element.id }}">
2
+             
3
+  {% include "MuzichCoreBundle:SearchElement:element.html.twig" %}
4
+
5
+</li>

+ 2 - 1
src/Muzich/HomeBundle/Resources/views/Home/index.html.twig View File

10
       &lt;&lt; {{ 'home.add_element_box.close'|trans({}, 'navigationui') }}
10
       &lt;&lt; {{ 'home.add_element_box.close'|trans({}, 'navigationui') }}
11
     </a>
11
     </a>
12
     
12
     
13
-    <form name="{{ add_form_name }}" action="{{ path('element_add') }}" method="post" {{ form_enctype(add_form) }}>
13
+    <form novalidate name="{{ add_form_name }}" action="{{ path('element_add') }}" method="post" {{ form_enctype(add_form) }}>
14
 
14
 
15
       {% include "MuzichCoreBundle:Element:form.add.html.twig" with { 
15
       {% include "MuzichCoreBundle:Element:form.add.html.twig" with { 
16
         'form'          : add_form, 
16
         'form'          : add_form, 
18
       } %}
18
       } %}
19
 
19
 
20
       <input type="submit" class="button" />
20
       <input type="submit" class="button" />
21
+      <img class="tag_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading" />
21
     </form>
22
     </form>
22
   </div>
23
   </div>
23
 
24
 

+ 3 - 3
src/Muzich/HomeBundle/Resources/views/Show/showGroup.html.twig View File

29
   
29
   
30
     <div id="element_add_box" style="display: none;">
30
     <div id="element_add_box" style="display: none;">
31
   
31
   
32
-      <a href="#" id="element_add_close_link">
32
+      <a href="#" id="element_add_close_link" class="button">
33
         &lt;&lt; {{ 'group.add_element_box.close'|trans({}, 'navigationui') }}
33
         &lt;&lt; {{ 'group.add_element_box.close'|trans({}, 'navigationui') }}
34
       </a>
34
       </a>
35
       
35
       
36
-      <form name="{{ add_form_name }}" action="{{ path('element_add', {'group_slug' : group.slug}) }}" method="post" {{ form_enctype(add_form) }}>
36
+      <form novalidate name="{{ add_form_name }}" action="{{ path('element_add', {'group_slug' : group.slug}) }}" method="post" {{ form_enctype(add_form) }}>
37
 
37
 
38
         {% include "MuzichCoreBundle:Element:form.add.html.twig" with { 'form': add_form, 'form_name': add_form_name } %}
38
         {% include "MuzichCoreBundle:Element:form.add.html.twig" with { 'form': add_form, 'form_name': add_form_name } %}
39
 
39
 
42
       
42
       
43
     </div>
43
     </div>
44
   
44
   
45
-    <a href="#" id="element_add_link">
45
+    <a href="#" id="element_add_link" class="button">
46
       {{ 'group.add_element'|trans({}, 'navigationui') }} &gt;&gt;
46
       {{ 'group.add_element'|trans({}, 'navigationui') }} &gt;&gt;
47
     </a>
47
     </a>
48
     <br />
48
     <br />

+ 37 - 1
web/bundles/muzichcore/js/muzich.js View File

666
       divSelect.find('select').val('network_personal');
666
       divSelect.find('select').val('network_personal');
667
     }
667
     }
668
   });
668
   });
669
+
670
+  // Ajout d'un element
671
+  
672
+  
673
+  $('form[name="add"] input[type="submit"]').live('click', function(){
674
+    $('form[name="add"]').find('img.tag_loader').show();
675
+  });
676
+  $('form[name="add"]').ajaxForm(function(response) {
677
+    $('form[name="add"] img.tag_loader').hide();
678
+    if (response.status == 'success')
679
+    {
680
+      $('form[name="add"]').find('ul.error_list').remove();
681
+      $('ul.elements').prepend(response.html);
682
+      $('form[name="add"] input[type="text"]').val('');
683
+      $('div#element_add_box').slideUp();
684
+      $('a#element_add_link').show();
685
+      if ($('form[name="search"]').length)
686
+      {
687
+        $('form[name="search"]').slideDown();
688
+      }
689
+    }
690
+    else if (response.status == 'error')
691
+    {
692
+      $('form[name="add"]').find('ul.error_list').remove();
693
+      ul_errors = $('<ul>').addClass('error_list');
694
+      
695
+      for (i in response.errors)
696
+      {
697
+        ul_errors.append($('<li>').append(response.errors[i]));
698
+      }
699
+      
700
+      $('form[name="add"]').prepend(ul_errors);
701
+    }
702
+    
703
+    return false;
704
+  });
669
    
705
    
670
- });
706
+ });