Browse Source

Merge pull request #470 from arnaud-lb/collection-add-delete

Add add/delete buttons to collections with allow_add/allow_delete
Thomas 13 years ago
parent
commit
e9cfb916b7

+ 21 - 1
Resources/public/base.js

@@ -1,8 +1,10 @@
 jQuery(document).ready(function() {
+    jQuery('html').removeClass('no-js');
     Admin.add_pretty_errors(document);
     Admin.add_collapsed_toggle();
     Admin.add_filters(document);
     Admin.set_object_field_value(document);
+    Admin.setup_collection_buttons(document);
 });
 
 var Admin = {
@@ -143,5 +145,23 @@ var Admin = {
                 }
             });
         });
+    },
+
+    setup_collection_buttons: function(subject) {
+
+        jQuery(subject).on('click', '.sonata-collection-add', function(event) {
+            Admin.stopEvent(event);
+
+            var container = jQuery(this).closest('[data-prototype]');
+            var proto = container.attr('data-prototype');
+            proto = proto.replace(/\$\$name\$\$/g, '');
+            jQuery(proto).insertBefore(jQuery(this).parent());
+        });
+
+        jQuery(subject).on('click', '.sonata-collection-delete', function(event) {
+            Admin.stopEvent(event);
+
+            jQuery(this).closest('.sonata-collection-row').remove();
+        });
     }
-}
+}

+ 19 - 1
Resources/public/css/layout.css

@@ -216,4 +216,22 @@ body.sonata-bc {
 .sonata-bc .container-fluid > .sidebar
 {
     top: auto;
-}
+}
+
+.sonata-collection-add, .sonata-collection-delete {
+    display: inline-block;
+    width: 16px;
+    height: 28px;
+    background: url(../famfamfam/add.png) no-repeat 50% 50%;
+}
+
+.sonata-collection-delete {
+    background-image: url(../famfamfam/delete.png);
+    float: left;
+    margin-top: 5px;
+}
+
+.no-js .sonata-collection-add, .no-js .sonata-collection-delete {
+    display: none;
+}
+

+ 31 - 1
Resources/views/Form/form_admin_fields.html.twig

@@ -107,4 +107,34 @@ file that was distributed with this source code.
             </div>
         </div>
     {% endif %}
-{% endblock field_row %}
+{% endblock field_row %}
+
+{% block collection_widget_row %}
+{% spaceless %}
+    <div class="sonata-collection-row">
+        {% if allow_delete %}
+            <a href="#" class="sonata-collection-delete"></a>
+        {% endif %}
+        {{ form_row(child) }}
+    </div>
+{% endspaceless %}
+{% endblock %}
+
+{% block collection_widget %}
+{% spaceless %}
+    {% if prototype is defined %}
+        {% set child = prototype %}
+        {% set attr = attr|merge({'data-prototype': block('collection_widget_row') }) %}
+    {% endif %}
+    <div {{ block('widget_container_attributes') }}>
+        {{ form_errors(form) }}
+        {% for child in form %}
+            {{ block('collection_widget_row') }}
+        {% endfor %}
+        {{ form_rest(form) }}
+        {% if allow_add %}
+            <div><a href="#" class="sonata-collection-add"></a></div>
+        {% endif %}
+    </div>
+{% endspaceless %}
+{% endblock collection_widget %}

+ 1 - 1
Resources/views/standard_layout.html.twig

@@ -18,7 +18,7 @@ file that was distributed with this source code.
 {% set _title        = block('title') %}
 {% set _breadcrumb   = block('breadcrumb') %}
 <!DOCTYPE html>
-<html>
+<html class="no-js">
     <head>
         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />