{% set entity = key is defined ? entity~key : entity %}
{% set lowerEntity = entity|lower %}
<script type="text/javascript">
var ${{ lowerEntity }}CollectionHolder;
// setup an "add a {{ lowerEntity }}" link
{% if addIcon is defined %}
var $add{{ entity }}Link = $('<a href="#" class="add_{{ lowerEntity }}_link addChild c-orange-h "><div class="d-flex align-items-center justify-content-center"><i class="fas fs-16 mar-11 fa-file-plus "></i> Ajouter une pièce jointe </div><div class="c-333333 fs-12 fw-500 infoUpload">Fichiers acceptés : jpg, png, pdf | Taille max : 4Mo</div> </a>');
{% else %}
var $add{{ entity }}Link = $('<a href="#" class="add_{{ lowerEntity }}_link">Add a {{ dataAdd }}</a>');
{% endif %}
var $new{{ entity }}LinkLi = $('<li class="linkCollection"></li>').append($add{{ entity }}Link);
jQuery(document).ready(function () {
// Get the ul that holds the collection of {{ lowerEntity }}s
${{ lowerEntity }}CollectionHolder = $('ul.{{ lowerEntity }}s');
// add a delete link to all of the existing {{ lowerEntity }} form li elements
${{ lowerEntity }}CollectionHolder.find('li').each(function () {
add{{ entity }}FormDeleteLink($(this));
});
// add the "add a {{ lowerEntity }}" anchor and li to the {{ lowerEntity }}s ul
${{ lowerEntity }}CollectionHolder.append($new{{ entity }}LinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
${{ lowerEntity }}CollectionHolder.data('index', ${{ lowerEntity }}CollectionHolder.find(':input').length);
$add{{ entity }}Link.on('click', function (e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new {{ lowerEntity }} form (see next code block)
add{{ entity }}Form(${{ lowerEntity }}CollectionHolder, $new{{ entity }}LinkLi);
});
});
function add{{ entity }}Form(${{ lowerEntity }}CollectionHolder, $new{{ entity }}LinkLi) {
// Get the data-prototype explained earlier
var prototype = ${{ lowerEntity }}CollectionHolder.data('prototype');
// get the new index
var index = ${{ lowerEntity }}CollectionHolder.data('index');
var newForm = prototype;
// You need this only if you didn't set 'label' => false in your {{ lowerEntity }}s field in TaskType
// Replace '__name__label__' in the prototype's HTML to
// instead be a number based on how many items we have
// newForm = newForm.replace(/__name__label__/g, index);
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
newForm = newForm.replace(/__name__/g, index);
// increase the index with one for the next item
${{ lowerEntity }}CollectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a {{ lowerEntity }}" link li
{% if entity starts with 'Photo' %}
var $newFormLi = $('<li class="linkCollection"><div class="miniature"><div class="row row-eq-height rowShowPicture"><div class="col-sm-2"><div class="showPicture"></div></div><div class="col-sm-8"><div class="textPicture"></div></div><div class="col-sm-2"><div class="deletePicture"><img src="{{ asset("assets/img/deletePicture.png") }}" alt="delete" width="12"/></div></div></div></div></li>').append(newForm);
{% else %}
var $newFormLi = $('<li class="linkCollection"><div class="miniature"></div></li>').append(newForm);
{% endif %}
$new{{ entity }}LinkLi.before($newFormLi);
add{{ entity }}FormDeleteLink($newFormLi);
var event = new CustomEvent('newCollectionForm', {'detail': {'Entity': "{{ entity }}", "index": index}});
document.dispatchEvent(event);
}
function add{{ entity }}FormDeleteLink(${{ lowerEntity }}FormLi) {
{% if deleteIcon is defined %}
var $removeFormA = $('<a href="#" class="deleteIcon deleteChild"><i class="fa fa-close" aria-hidden="true"></i> {% if deleteData is defined %}{{ deleteData }} {% else %}Annuler la saisie{% endif %}</a>');
{% else %}
var $removeFormA = $('<a href="#">delete this {{ lowerEntity }}</a>');
{% endif %}
${{ lowerEntity }}FormLi.append($removeFormA);
$removeFormA.on('click', function (e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// remove the li for the {{ lowerEntity }} form
${{ lowerEntity }}FormLi.remove();
});
}
</script>