Permalink
Browse files

Refactorizado formulario de añadir

  • Loading branch information...
1 parent b79abb7 commit de77d661a00e4e7609997475dd0beb74a5e967fb @ioachim ioachim committed Nov 12, 2011
Showing with 144 additions and 42 deletions.
  1. +47 −11 app/application/views/add_modelo.php
  2. +97 −31 app/script/dynaform.js
@@ -5,13 +5,13 @@
</head>
<body>
<script type="text/javascript" src="<?php echo base_url('script/jquery-1.7.min.js') ?>"></script>
- <script type="text/javascript" src="<?php echo base_url('script/dynaform.js') ?>">
+ <script type="text/javascript" src="<?php echo base_url('script/dynaform.js') ?>"></script>
+
- </script>
<form action="<?php echo base_url('modelo') ?>" method="post">
<h1>
Agregar Modelo</h1>
- <input type="submit" title="hai" >
+ <input type="submit" name="send">
<p>
<label>
nombre
@@ -33,18 +33,54 @@
Contribuciones <a href="#" id="add_contrib">+</a> </h2>
<script type="text/javascript">
-
-
- $('#add_contrib').click(function (ev) {
- var count = $('#contrib .contrib').length;
-
- $('#contrib').append(gen_contribfields(count));
- });
-
+ $('#add_contrib').click(add_contribfields);
</script>
+
<div id="contrib">
</div>
</form>
+
+ <div style="display: none" id="prototype">
+
+
+ <fieldset class="contrib"><legend>contribución</legend><p>
+ <label>nombre<input type="text" name="nombre" /></label>
+ <label>template<input type="text" name="template" /></label></p>
+ <fieldset class="metadata">
+ <legend>metadata <a href="#">+</a></legend>
+
+ </fieldset>
+
+ <fieldset class="ref">
+ <legend>referencias <a href="#">+</a></legend>
+ </fieldset>
+ </fieldset>
+
+
+ <div class="metadata">
+ <label>nombre<input type="text" name="nombre" /></label>
+ <label>tipo
+ <select name="tipo">
+ <option value="text">texto</option>
+ <option value="number">número</option>
+ <option value="datetime">fecha/hora</option>
+ <option value="url">url</option>
+ <option value="file">archivo</option>
+ </select>
+ </label>
+ </div>
+
+ <fieldset class="ref">
+ <label>nombre <input type="text" name="nombre"></label>
+ <label>tipo destino <input type="text" name="tipo_dest"></label>
+ <label>template <input type="text" name="template"></label>
+ <fieldset class="metadata">
+ <legend>metadata <a href="#">+</a></legend>
+ </fieldset>
+ </fieldset>
+
+ </div>
+
</body>
</html>
View
@@ -1,42 +1,108 @@
-function gen_contribfields(count) {
- var id = 'contrib['+count+']';
+re_field_id = RegExp('-(\\w+)|(\\w+)$', 'g');
- var container = $('<fieldset class="contrib"><legend>contribución</legend><p> \
- <label>nombre<input type="text" name="' + id + '[name]" /></label> \
- <label>template<input type="text" name="' + id + '][template]" /></label></p> \
- </fieldset>');
+function add_contribfields()
+{
+ var id = 'contrib-' + $("#contrib .contrib").length;
+ var container = $("#prototype > .contrib").clone();
+ container.attr('id', id);
+ $.map(container.find('input'), function(e, i)
+ {
+ e = $(e);
+ var input_id = id + '-' + e.attr('name');
+ e.attr('id', input_id);
+ e.attr('name', input_id.replace(re_field_id,'[$1]'));
+ }
+ );
+
+ var meta_list = container.children('fieldset.metadata');
+ var meta_list_id = id + '-metadata';
+ var add_meta = meta_list.find('legend a');
+
+ meta_list.attr('id', meta_list_id);
- var meta_container = $(document.createElement('fieldset'));
- var meta_adder = meta_container.append($('<legend>metadata</legend>')
- .append('<a href="#">+</a>').click(
- function (ev) {
- c = $(ev.target).parent().parent().find("div").length;
- metafield = gen_metadatafields(id, c);
- meta_container.append(metafield);
- }));
+ add_meta.click(
+ get_addmeta_function(meta_list)
+ );
+
+
+ var ref_list = container.children('fieldset.ref');
+ var ref_list_id = id + '-ref';
+ var add_ref = ref_list.find('legend a');
+
+ ref_list.attr('id', ref_list_id);
+
+ add_ref.click(
+ get_addref_function(ref_list)
+ );
+
+ $('#contrib').append(container);
+
+ return false;
+
+}
+
+function get_addmeta_function(meta_list)
+{
+ var meta_list_id = meta_list.attr('id');
+ return function() {
+ var meta_count = meta_list.children('.metadata').length;
+ var meta_id = meta_list_id + '-' + meta_count;
+ var meta = $("#prototype > .metadata").clone();
+ meta.attr('id', meta_id);
- container.append(meta_container);
-
- return container;
+ $.map(meta.find('input,select'), function(e, i)
+ {
+ e = $(e);
+ var input_id = meta_id + '-' + e.attr('name');
+ e.attr('id', input_id);
+ e.attr('name', input_id.replace(re_field_id,'[$1]'));
+ });
+
+ meta_list.append(meta);
+
+ return false;
+ }
}
-function gen_metadatafields(parent_id, count) {
-
-return $('<div> \
- <label> \
- <input type="text" name="' + parent_id + '[metadata][' + count + '][name]" /></label> \
- <label>tipo \
- <select name="' + parent_id + '[metadata][' + count + '][type]"> \
- <option value="text">texto</option> \
- <option value="number">número</option> \
- <option value="datetime">fecha/hora</option> \
- <option value="url">url</option> \
- <option value="file">archivo</option> \
- </select></label> \
-</div>');
+function get_addref_function(ref_list)
+{
+ var ref_list_id = ref_list.attr('id');
+
+ return function() {
+ var ref_count = ref_list.children('.ref').length;
+ var ref_id = ref_list_id + '-' + ref_count;
+ var ref = $("#prototype > .ref").clone();
+
+ ref.attr('id', ref_id);
+
+ $.map(ref.find('input,select'), function(e, i)
+ {
+ e = $(e);
+ var input_id = ref_id + '-' + e.attr('name');
+ e.attr('id', input_id);
+ e.attr('name', input_id.replace(re_field_id,'[$1]'));
+ });
+
+
+ var meta_list = ref.children('fieldset.metadata');
+ var meta_list_id = ref_id + '-metadata';
+ var add_meta = meta_list.find('legend a');
+
+ meta_list.attr('id', meta_list_id);
+
+ console.log(meta_list);
+
+ add_meta.click(
+ get_addmeta_function(meta_list)
+ );
+
+ ref_list.append(ref);
+
+ return false;
+ }
}

0 comments on commit de77d66

Please sign in to comment.