Skip to content

Commit

Permalink
further improves collections, fixes naming issues
Browse files Browse the repository at this point in the history
The class that marks collection childs for removal was renamed to more descriptive 'remove-on-collection-add' (from 'no-data') and now applies to any child element (not justt direct children).
Collection items' ID is now decided by trying all numbers before one "slot" is free. This should fix all issues with items overwriting one another (inspired by Bootstrap's collection code: https://github.com/braincrafted/bootstrap-bundle/blob/v2.1.2/Resources/js/bc-bootstrap-collection.js#L42-L50).
  • Loading branch information
Amunak committed May 29, 2015
1 parent 4f16d42 commit 5536402
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/Sylius/Bundle/WebBundle/Resources/public/js/form-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
$(document).on('click', 'a[data-collection-button="add"]', function(e) {
e.preventDefault();
var collectionContainer = $('#' + $(this).data('collection'));
collectionContainer.children('.no-data').remove();
var isArray = collectionContainer.data('is-php-array');
collectionContainer.find('.remove-on-collection-add').remove();
var prototype = $('#' + $(this).data('prototype')).data('prototype');
var item = prototype.replace(/__name__/g, collectionContainer.children().length + (undefined == isArray ? 0 : 1));

// Check if an element with this ID already exists.
// If it does, increase the count by one and try again
var id = prototype.match(/input.*?id="(.*?)"/);
var count = 0;
while ($('#' + id[1].replace(/__name__/g, count)).length > 0) {
count++;
}

var item = prototype.replace(/__name__/g, count);
collectionContainer.append(item);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</tr>
</thead>
<tbody id="sylius-assortment-product-pricing-ranks" class="collection-container"
data-prototype="{{ _self.volume_based_prototype(form)|e }}" data-is-php-array="true">
data-prototype="{{ _self.volume_based_prototype(form)|e }}">
{% for rank in form.masterVariant.pricingConfiguration.children %}
<tr class="sylius-assortment-product-pricing-ranks-item">
<td>{{ form_widget(rank.children.min) }}</td>
Expand All @@ -39,7 +39,7 @@
<td>{{ _self.volume_based_delete() }}</td>
</tr>
{% else %}
<tr class="no-data">
<tr class="remove-on-collection-add">
<td colspan="4">{{ "sylius.pricing.volume_based.no_results"|trans }}</td>
</tr>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</tr>
</thead>
<tbody id="sylius-assortment-product-pricing-ranks" class="collection-container"
data-prototype="{{ _self.volume_based_prototype(form)|e }}" data-is-php-array="true">
data-prototype="{{ _self.volume_based_prototype(form)|e }}">
{% for rank in form.pricingConfiguration.children %}
<tr class="sylius-assortment-product-pricing-ranks-item">
<td>{{ form_widget(rank.children.min) }}</td>
Expand All @@ -39,7 +39,7 @@
<td>{{ _self.volume_based_delete() }}</td>
</tr>
{% else %}
<tr class="no-data">
<tr class="remove-on-collection-add">
<td colspan="4">{{ "sylius.pricing.volume_based.no_results"|trans }}</td>
</tr>
{% endfor %}
Expand Down

0 comments on commit 5536402

Please sign in to comment.