From bf37f0dc2c87b6e1c8b036914f406bc9de6a4763 Mon Sep 17 00:00:00 2001 From: Antonio Junior Date: Wed, 28 Jan 2015 14:55:25 -0200 Subject: [PATCH 1/3] Creating a method to multiply form. --- source/assets/javascripts/locastyle.js | 1 + .../javascripts/locastyle/_duplicate_form.js | 65 +++++++++++++++++++ .../javascripts/locastyle/_initializer.js | 1 + 3 files changed, 67 insertions(+) create mode 100644 source/assets/javascripts/locastyle/_duplicate_form.js diff --git a/source/assets/javascripts/locastyle.js b/source/assets/javascripts/locastyle.js index 8d7ea25f..3b91b316 100644 --- a/source/assets/javascripts/locastyle.js +++ b/source/assets/javascripts/locastyle.js @@ -5,6 +5,7 @@ //= require locastyle/_hopscotch //= require locastyle/_guided_tour //= require locastyle/_equal_height +//= require locastyle/_duplicate_form //= require locastyle/_password_strength //= require locastyle/_step_form //= require locastyle/_sum_values diff --git a/source/assets/javascripts/locastyle/_duplicate_form.js b/source/assets/javascripts/locastyle/_duplicate_form.js new file mode 100644 index 00000000..747cd86e --- /dev/null +++ b/source/assets/javascripts/locastyle/_duplicate_form.js @@ -0,0 +1,65 @@ +Locastyle.prototype.duplicateForm = (function() { + 'use strict'; + + function init() { + multiplyForm(); + verifyFormNumbers(); + addValueToDataForm(); + removeForm(); + } + + var config = { + selectors: { + form: '[data-form]', + parent: '[data-form-parent]' + }, + classes: { + hide: 'dNone' + }, + actions: { + duplicate: '[data-duplicate-form]', + remove: '[data-remove-form]' + } + }; + + function multiplyForm() { + $(config.actions.duplicate).on('click', function(el) { + el.preventDefault(); + var cloneLast = $(config.selectors.form).last().clone(); + cloneLast.appendTo(config.selectors.parent); + + addValueToDataForm(); + verifyFormNumbers(); + removeForm(); + }); + } + + function addValueToDataForm() { + $(config.selectors.form).each(function(i, el) { + $(el).attr('data-form', i); + }); + } + + function verifyFormNumbers() { + if ($(config.selectors.form).length > 1) { + $(config.actions.remove).removeClass(config.classes.hide); + } else { + $(config.actions.remove).addClass(config.classes.hide); + } + } + + function removeForm() { + $(config.actions.remove).on('click', function(el) { + el.preventDefault(); + if($(config.selectors.form).length > 1) { + $(this).parent().parent().remove(); + } + verifyFormNumbers(); + }); + } + + return { + init: init + }; + +}()); diff --git a/source/assets/javascripts/locastyle/_initializer.js b/source/assets/javascripts/locastyle/_initializer.js index e2d804b7..663d257a 100644 --- a/source/assets/javascripts/locastyle/_initializer.js +++ b/source/assets/javascripts/locastyle/_initializer.js @@ -5,4 +5,5 @@ $(window).load(function() { locastyle.passwordStrength.init(); locastyle.sumValues.init(); locastyle.equalHeight.init(); + locastyle.duplicateForm.init(); }); From c94f729076e0d1d5366d5bbc7b7d2f70873a8207 Mon Sep 17 00:00:00 2001 From: Antonio Junior Date: Wed, 28 Jan 2015 15:08:49 -0200 Subject: [PATCH 2/3] Improvement parent remove. --- source/assets/javascripts/locastyle/_duplicate_form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/assets/javascripts/locastyle/_duplicate_form.js b/source/assets/javascripts/locastyle/_duplicate_form.js index 747cd86e..7db0b734 100644 --- a/source/assets/javascripts/locastyle/_duplicate_form.js +++ b/source/assets/javascripts/locastyle/_duplicate_form.js @@ -52,7 +52,7 @@ Locastyle.prototype.duplicateForm = (function() { $(config.actions.remove).on('click', function(el) { el.preventDefault(); if($(config.selectors.form).length > 1) { - $(this).parent().parent().remove(); + $(this).parents().eq(1).remove(); } verifyFormNumbers(); }); From e9acd045ac06f0d66a1bfb39a78e31f41f7cce37 Mon Sep 17 00:00:00 2001 From: Antonio Junior Date: Wed, 28 Jan 2015 15:40:22 -0200 Subject: [PATCH 3/3] Creating a documentation multiply fields on form. --- .../formularios/multiplicando-campos.html.erb | 63 +++++++++++++++++++ source/partials/_sidebar-formularios.erb | 1 + 2 files changed, 64 insertions(+) create mode 100644 source/manual/formularios/multiplicando-campos.html.erb diff --git a/source/manual/formularios/multiplicando-campos.html.erb b/source/manual/formularios/multiplicando-campos.html.erb new file mode 100644 index 00000000..b8bebaff --- /dev/null +++ b/source/manual/formularios/multiplicando-campos.html.erb @@ -0,0 +1,63 @@ +--- +title: Formulários - Multiplicando Campos +menu_page: sidebar-formularios.erb +--- + +
+
+

Multiplicando Formulários

+

Como multiplicar campos dentro de um formulários

+
+ +
+
+
+
+
+
+ + +
+
+ + +
+
+ +
+
+
+
+ Adicionar +
+
+ +
+<form action="#" class="form">
+  <div class="row">
+    <div data-form-parent class="dInlineBlock">
+      <div class="dInlineBlock" data-form>
+        <div class="control-group span3">
+          <label for="type" class="dNone">Select</label>
+          <select>
+            <option value="1">Opção 1</option>
+            <option value="1">Opção 2</option>
+          </select>
+        </div>
+        <div class="control-group span6">
+          <label class="dNone">Campo</label>
+          <input type="text" class="span6" placeholder="Campo">
+        </div>
+        <div class="control-group span2">
+          <a href="#" class="btn ico-hifen dNone" data-remove-form></a>
+        </div>
+      </div>
+    </div>
+  </div>
+  <a href="#" class="btn" data-duplicate-form>Adicionar</a>
+</form>
+
+
\ No newline at end of file diff --git a/source/partials/_sidebar-formularios.erb b/source/partials/_sidebar-formularios.erb index d2471c1e..c4f49d16 100644 --- a/source/partials/_sidebar-formularios.erb +++ b/source/partials/_sidebar-formularios.erb @@ -9,5 +9,6 @@
  • <%= link_to "Customizando os selects", "#{base_url}/manual/formularios/selects" %>
  • <%= link_to "Medidor de senha", "#{base_url}/manual/formularios/medidor-senhas" %>
  • <%= link_to "Alternando campos", "#{base_url}/manual/formularios/alternando-campos" %>
  • +
  • <%= link_to "Multiplicando Campos", "#{base_url}/manual/formularios/multiplicando-campos" %>