|
10 | 10 | * @author Gorka Laucirica <gorka.lauzirika@gmail.com> |
11 | 11 | */ |
12 | 12 |
|
13 | | -'use strict'; |
14 | | - |
15 | 13 | import {onDomReady} from 'lin3s-event-bus'; |
16 | | - |
17 | 14 | import $ from 'jquery'; |
18 | 15 |
|
19 | | -let $form, $collectionHolder; |
20 | | - |
21 | | -function addFormType($aCollectionHolder) { |
22 | | - let |
23 | | - prototype = $aCollectionHolder.attr('data-prototype'), |
24 | | - prototypeName = $aCollectionHolder.attr('data-prototype-name'), |
25 | | - regExp = new RegExp(prototypeName === undefined ? '__name__' : prototypeName, 'g'), |
26 | | - index = $aCollectionHolder.find(':input').length, |
27 | | - newForm = prototype.replace(regExp, index); |
28 | | - |
29 | | - $(newForm).appendTo($aCollectionHolder); |
30 | | - $aCollectionHolder.data('index', index + 1); |
31 | | -} |
32 | | - |
33 | | -function onReady() { |
34 | | - $form = $('form'); |
35 | | - |
36 | | - $('.form__collection-item-toggle').click(function () { |
37 | | - $(this).parents('.form__collection-item').toggleClass('form__collection-item--hidden') |
38 | | - }); |
39 | | - |
40 | | - $form.on('click', '.js-collection-add', function () { |
41 | | - $collectionHolder = $(this).closest('.form__collection').find('.form__collection-items').first(); |
42 | | - addFormType($collectionHolder); |
43 | | - |
44 | | - return false; |
45 | | - }); |
46 | | - |
47 | | - $form.on('click', '.js-collection-remove', function () { |
48 | | - $(this).closest('.form__collection-item').remove(); |
49 | | - |
50 | | - return false; |
51 | | - }); |
52 | | -} |
| 16 | +const |
| 17 | + addFormType = ($aCollectionHolder) => { |
| 18 | + let |
| 19 | + prototype = $aCollectionHolder.attr('data-prototype'), |
| 20 | + prototypeName = $aCollectionHolder.attr('data-prototype-name'), |
| 21 | + regExp = new RegExp(prototypeName === undefined ? '__name__' : prototypeName, 'g'), |
| 22 | + index = $aCollectionHolder.find(':input').length, |
| 23 | + newForm = prototype.replace(regExp, index); |
| 24 | + |
| 25 | + $(newForm).appendTo($aCollectionHolder); |
| 26 | + $aCollectionHolder.data('index', index + 1); |
| 27 | + }, |
| 28 | + onReady = () => { |
| 29 | + const |
| 30 | + $formCollectionAddButtons = $('.js-collection-add'), |
| 31 | + $formCollectionRemoveButtons = $('.js-collection-remove'), |
| 32 | + $formToggles = $('.form__collection-item-toggle'); |
| 33 | + |
| 34 | + Array.from($formCollectionAddButtons).forEach(formCollectionAddButton => { |
| 35 | + const $formCollectionAddButton = $(formCollectionAddButton); |
| 36 | + |
| 37 | + $formCollectionAddButton.on('click', () => { |
| 38 | + const $collectionHolder = $formCollectionAddButton |
| 39 | + .closest('.form__collection').find('.form__collection-items').first(); |
| 40 | + addFormType($collectionHolder); |
| 41 | + |
| 42 | + return false; |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + Array.from($formCollectionRemoveButtons).forEach(formCollectionRemoveButton => { |
| 47 | + const $formCollectionRemoveButton = $(formCollectionRemoveButton); |
| 48 | + $formCollectionRemoveButton.closest('.form__collection-item').remove(); |
| 49 | + |
| 50 | + return false; |
| 51 | + }); |
| 52 | + |
| 53 | + Array.from($formToggles).forEach(formToggle => { |
| 54 | + const $formToggle = $(formToggle); |
| 55 | + |
| 56 | + $formToggle.on('click', function () { |
| 57 | + $formToggle.closest('.form__collection-item').toggleClass('form__collection-item--hidden') |
| 58 | + }); |
| 59 | + }); |
| 60 | + }; |
53 | 61 |
|
54 | 62 | onDomReady(onReady); |
0 commit comments