templates/commons/collectionType.html.twig line 1

Open in your IDE?
  1. {% set entity = key is defined ? entity~key : entity %}
  2. {% set lowerEntity = entity|lower %}
  3. <script type="text/javascript">
  4.     var ${{ lowerEntity }}CollectionHolder;
  5.     // setup an "add a {{ lowerEntity }}" link
  6.     {% if addIcon is defined %}
  7.         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>');
  8.     {% else %}
  9.         var $add{{ entity }}Link = $('<a href="#" class="add_{{ lowerEntity }}_link">Add a {{ dataAdd }}</a>');
  10.     {% endif %}
  11.         var $new{{ entity }}LinkLi = $('<li class="linkCollection"></li>').append($add{{ entity }}Link);
  12.         jQuery(document).ready(function () {
  13.             // Get the ul that holds the collection of {{ lowerEntity }}s
  14.             ${{ lowerEntity }}CollectionHolder = $('ul.{{ lowerEntity }}s');
  15.             // add a delete link to all of the existing {{ lowerEntity }} form li elements
  16.             ${{ lowerEntity }}CollectionHolder.find('li').each(function () {
  17.                 add{{ entity }}FormDeleteLink($(this));
  18.             });
  19.             // add the "add a {{ lowerEntity }}" anchor and li to the {{ lowerEntity }}s ul
  20.             ${{ lowerEntity }}CollectionHolder.append($new{{ entity }}LinkLi);
  21.             // count the current form inputs we have (e.g. 2), use that as the new
  22.             // index when inserting a new item (e.g. 2)
  23.             ${{ lowerEntity }}CollectionHolder.data('index', ${{ lowerEntity }}CollectionHolder.find(':input').length);
  24.             $add{{ entity }}Link.on('click', function (e) {
  25.                 // prevent the link from creating a "#" on the URL
  26.                 e.preventDefault();
  27.                 // add a new {{ lowerEntity }} form (see next code block)
  28.                 add{{ entity }}Form(${{ lowerEntity }}CollectionHolder, $new{{ entity }}LinkLi);
  29.             });
  30.         });
  31.         function add{{ entity }}Form(${{ lowerEntity }}CollectionHolder, $new{{ entity }}LinkLi) {
  32.             // Get the data-prototype explained earlier
  33.             var prototype = ${{ lowerEntity }}CollectionHolder.data('prototype');
  34.             // get the new index
  35.             var index = ${{ lowerEntity }}CollectionHolder.data('index');
  36.             var newForm = prototype;
  37.             // You need this only if you didn't set 'label' => false in your {{ lowerEntity }}s field in TaskType
  38.             // Replace '__name__label__' in the prototype's HTML to
  39.             // instead be a number based on how many items we have
  40.             // newForm = newForm.replace(/__name__label__/g, index);
  41.             // Replace '__name__' in the prototype's HTML to
  42.             // instead be a number based on how many items we have
  43.             newForm = newForm.replace(/__name__/g, index);
  44.             // increase the index with one for the next item
  45.             ${{ lowerEntity }}CollectionHolder.data('index', index + 1);
  46.             // Display the form in the page in an li, before the "Add a {{ lowerEntity }}" link li
  47.     {% if entity starts with 'Photo' %}
  48.             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);
  49.     {% else %}
  50.             var $newFormLi = $('<li class="linkCollection"><div class="miniature"></div></li>').append(newForm);
  51.     {% endif %}
  52.             $new{{ entity }}LinkLi.before($newFormLi);
  53.             add{{ entity }}FormDeleteLink($newFormLi);
  54.             var event = new CustomEvent('newCollectionForm', {'detail': {'Entity': "{{ entity }}", "index": index}});
  55.             document.dispatchEvent(event);
  56.         }
  57.         function add{{ entity }}FormDeleteLink(${{ lowerEntity }}FormLi) {
  58.     {% if deleteIcon is defined %}
  59.             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>');
  60.     {% else %}
  61.             var $removeFormA = $('<a href="#">delete this {{ lowerEntity }}</a>');
  62.     {% endif %}
  63.             ${{ lowerEntity }}FormLi.append($removeFormA);
  64.             $removeFormA.on('click', function (e) {
  65.                 // prevent the link from creating a "#" on the URL
  66.                 e.preventDefault();
  67.                 // remove the li for the {{ lowerEntity }} form
  68.                 ${{ lowerEntity }}FormLi.remove();
  69.             });
  70.         }
  71. </script>