Pārlūkot izejas kodu

Evolution #90: Ajaxsification des actions

bastien 12 gadus atpakaļ
vecāks
revīzija
af3dfd39de

+ 23 - 2
src/Muzich/CoreBundle/Controller/CoreController.php Parādīt failu

@@ -200,7 +200,16 @@ class CoreController extends Controller
200 200
 
201 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 214
       else
206 215
       {
@@ -212,7 +221,19 @@ class CoreController extends Controller
212 221
     {
213 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 238
       else
218 239
       {

+ 5 - 0
src/Muzich/CoreBundle/Resources/views/SearchElement/li.element.html.twig Parādīt failu

@@ -0,0 +1,5 @@
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 Parādīt failu

@@ -10,7 +10,7 @@
10 10
       &lt;&lt; {{ 'home.add_element_box.close'|trans({}, 'navigationui') }}
11 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 15
       {% include "MuzichCoreBundle:Element:form.add.html.twig" with { 
16 16
         'form'          : add_form, 
@@ -18,6 +18,7 @@
18 18
       } %}
19 19
 
20 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 22
     </form>
22 23
   </div>
23 24
 

+ 3 - 3
src/Muzich/HomeBundle/Resources/views/Show/showGroup.html.twig Parādīt failu

@@ -29,11 +29,11 @@
29 29
   
30 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 33
         &lt;&lt; {{ 'group.add_element_box.close'|trans({}, 'navigationui') }}
34 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 38
         {% include "MuzichCoreBundle:Element:form.add.html.twig" with { 'form': add_form, 'form_name': add_form_name } %}
39 39
 
@@ -42,7 +42,7 @@
42 42
       
43 43
     </div>
44 44
   
45
-    <a href="#" id="element_add_link">
45
+    <a href="#" id="element_add_link" class="button">
46 46
       {{ 'group.add_element'|trans({}, 'navigationui') }} &gt;&gt;
47 47
     </a>
48 48
     <br />

+ 37 - 1
web/bundles/muzichcore/js/muzich.js Parādīt failu

@@ -666,5 +666,41 @@ $(document).ready(function(){
666 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
+ });