From b202638b29c21d2e24eea1f61111d48522f2c2a0 Mon Sep 17 00:00:00 2001 From: lazyants Date: Thu, 22 Aug 2013 10:35:26 +0200 Subject: [PATCH] makes possible to use multiple embedded forms --- Resources/public/js/embedded_form.js | 68 ++++++++++++++++++++++++++++ Resources/public/js/embeded_form.js | 64 -------------------------- 2 files changed, 68 insertions(+), 64 deletions(-) create mode 100644 Resources/public/js/embedded_form.js delete mode 100644 Resources/public/js/embeded_form.js diff --git a/Resources/public/js/embedded_form.js b/Resources/public/js/embedded_form.js new file mode 100644 index 0000000..b4fd97a --- /dev/null +++ b/Resources/public/js/embedded_form.js @@ -0,0 +1,68 @@ +function embeddedForm(id) { + this.addEmbeddedForm = function addEmbeddedForm(collectionHolder, $newLinkLi) { + // Get the data-prototype explained earlier + var prototype = collectionHolder.data('prototype'); + + // get the new index + var index = collectionHolder.data('index'); + + // Replace '__name__' in the prototype's HTML to + // instead be a number based on how many items we have + var newForm = prototype.replace(/__name__/g, index); + + // increase the index with one for the next item + collectionHolder.data('index', index + 1); + + // Display the form in the page in an li, before the "Add" link li + var $newFormLi = $('
  • ').append(newForm); + $newLinkLi.before($newFormLi); + + // add a delete link to the new form + addEmbededFormDeleteLink($newFormLi); + }; + + this.addEmbeddedFormDeleteLink = function addEmbeddedFormDeleteLink($embededFormLi) { + removeId = 'remove_' + $embededFormLi.children().eq(0).attr('id'); + var $removeFormA = $('
    '); + $embededFormLi.append($removeFormA); + + $removeFormA.on('click', function (e) { + // prevent the link from creating a "#" on the URL + e.preventDefault(); + + // remove the li for the embeded form + $embededFormLi.remove(); + }); + }; + + this.id = id; + + // Get the ul that holds the collection of embeded + var collectionHolder = $('#' + this.id); + + // setup an "add" link + var $addLink = $('Add'); + var $newLinkLi = $('
  • ').append($addLink); + + var embeddedForm = this; + + // add a delete link to all of the existing embeded form li elements + collectionHolder.find('li').each(function () { + embeddedForm.addEmbeddedFormDeleteLink($(this)); + }); + + // add the "add" anchor and li to the embeded ul + collectionHolder.append($newLinkLi); + + // count the current form inputs we have (e.g. 2), use that as the new + // index when inserting a new item (e.g. 2) + collectionHolder.data('index', collectionHolder.find(':input').length); + + $addLink.on('click', function (e) { + // prevent the link from creating a "#" on the URL + e.preventDefault(); + + // add a new embeded form (see next code block) + embeddedForm.addEmbeddedForm(collectionHolder, $newLinkLi); + }); +} \ No newline at end of file diff --git a/Resources/public/js/embeded_form.js b/Resources/public/js/embeded_form.js deleted file mode 100644 index fdac2f7..0000000 --- a/Resources/public/js/embeded_form.js +++ /dev/null @@ -1,64 +0,0 @@ -jQuery(document).ready(function () { - // Get the ul that holds the collection of embeded - var collectionHolder = $('ul.embeded'); - - // setup an "add" link - var $addLink = $('Add'); - var $newLinkLi = $('
  • ').append($addLink); - - // add a delete link to all of the existing embeded form li elements - collectionHolder.find('li').each(function () { - addEmbededFormDeleteLink($(this)); - }); - - // add the "add" anchor and li to the embeded ul - collectionHolder.append($newLinkLi); - - // count the current form inputs we have (e.g. 2), use that as the new - // index when inserting a new item (e.g. 2) - collectionHolder.data('index', collectionHolder.find(':input').length); - - $addLink.on('click', function (e) { - // prevent the link from creating a "#" on the URL - e.preventDefault(); - - // add a new embeded form (see next code block) - addEmbededForm(collectionHolder, $newLinkLi); - }); -}); - -function addEmbededForm(collectionHolder, $newLinkLi) { - // Get the data-prototype explained earlier - var prototype = collectionHolder.data('prototype'); - - // get the new index - var index = collectionHolder.data('index'); - - // Replace '__name__' in the prototype's HTML to - // instead be a number based on how many items we have - var newForm = prototype.replace(/__name__/g, index); - - // increase the index with one for the next item - collectionHolder.data('index', index + 1); - - // Display the form in the page in an li, before the "Add" link li - var $newFormLi = $('
  • ').append(newForm); - $newLinkLi.before($newFormLi); - - // add a delete link to the new form - addEmbededFormDeleteLink($newFormLi); -} - -function addEmbededFormDeleteLink($embededFormLi) { - removeId = 'remove_' + $embededFormLi.children().eq(0).attr('id'); - var $removeFormA = $('
    '); - $embededFormLi.append($removeFormA); - - $removeFormA.on('click', function (e) { - // prevent the link from creating a "#" on the URL - e.preventDefault(); - - // remove the li for the embeded form - $embededFormLi.remove(); - }); -} \ No newline at end of file