+

Permit to add multiple things in scase at the same time

Benjamin Renard [2017-09-17 10:18:45]
Permit to add multiple things in scase at the same time
Filename
public_html/inc/main.js
public_html/index.html
diff --git a/public_html/inc/main.js b/public_html/inc/main.js
index b370de9..1bfda74 100644
--- a/public_html/inc/main.js
+++ b/public_html/inc/main.js
@@ -294,28 +294,47 @@ on_li_click=function(event) {
 on_li_add_click=function(event) {
   var li=$(this);
   var cat=li.parent().data('cat');
-        var modal=$('#add_thing_modal');
+  var modal=$('#add_thing_modal');
   modal.data('cat',cat);
   modal.modal('show');
 }

 on_valid_add_thing_modal=function (e) {
   e.preventDefault();
-  var label=$('#add_thing_label')[0].value;
-  if (label=='') {
-    alert("Vous devez saisir le nom de l'élément !");
-    return;
-  }
+
   var modal=$('#add_thing_modal');
   var scase=scases.byName($('#cats').data('scase'));
   if (scase) {
     var cat=scase.cats.byName(modal.data('cat'));
     if (cat) {
-      if (cat.byLabel(label)) {
-        alert("Cet élément existe déjà !");
+      var labels=[];
+      var error=false;
+      $('input.add_thing_label').each(function(idx,input) {
+        var val=$(input).val();
+        if (val && val!='') {
+          if (labels.indexOf(val)>-1) {
+            alert("Deux élements ne peuvent porter le même nom !");
+            error=true;
+            return;
+          }
+          if (cat.byLabel(val)) {
+            alert("L'élément '"+val+"' existe déjà !");
+            error=true;
+            return;
+          }
+          labels.push(val);
+        }
+      });
+      if (error) {
         return;
       }
-      cat.newThing(label);
+      if (labels.length==0) {
+        alert("Vous devez saisir au moins un nom d'élément !");
+        return;
+      }
+      for (idx in labels) {
+        cat.newThing(labels[idx]);
+      }
       scases.save();
       show_scase(scase,cat.name);
     }
@@ -324,13 +343,25 @@ on_valid_add_thing_modal=function (e) {
 }

 on_show_add_thing_modal=function () {
-  $('#add_thing_label').focus();
+  $('input.add_thing_label_other').parent().remove();
+  $('input.add_thing_label').val('');
+  $('input.add_thing_label').first().focus();
 }

 on_close_add_thing_modal=function () {
   $('#add_thing_modal form')[0].reset();
 }

+on_add_thing_label_focus=function(event) {
+  if ($('input.add_thing_label').last()[0] == event.target) {
+    var new_input_group=$('<div class="form-group"></div>');
+    var new_input=$('<input type="text" class="form-control add_thing_label add_thing_label_other" class="form-control" placeholder="Encore un ?"/>');
+    new_input_group.append(new_input);
+    new_input.bind('focus', on_add_thing_label_focus);
+    $(event.target).parent().after(new_input_group);
+  }
+}
+

 /***********************
  * Rename thing
@@ -642,6 +673,7 @@ $( document ).ready( function() {

   $('#back_to_scases').bind('click',on_back_to_scases_btn_click);

+  $('input.add_thing_label').bind('focus',on_add_thing_label_focus);
   $('#add_thing_submit').bind('click',on_valid_add_thing_modal);
   $("#add_thing_modal").on('shown.bs.modal',on_show_add_thing_modal);
   $("#add_thing_modal").on('hidden.bs.modal',on_close_add_thing_modal);
diff --git a/public_html/index.html b/public_html/index.html
index e0ef584..ceea5a0 100644
--- a/public_html/index.html
+++ b/public_html/index.html
@@ -209,9 +209,12 @@ div.panel-heading, li.list-group-item, a {
         <h4 class="modal-title">Ajouter un élément</h4>
       </div>
       <div class="modal-body">
-        <form class="form-horizontal" role="form">
+        <form role="form">
+          <div class="form-group">
+            <input type='text' class='form-control add_thing_label' class="form-control" placeholder="Nom de l'élément"/>
+          </div>
           <div class="form-group">
-            <input type='text' id='add_thing_label' class="form-control" placeholder="Nom de l'élément"/>
+            <input type='text' class='form-control add_thing_label' class="form-control" placeholder="Un autre ?"/>
           </div>
         </form>
       </div>
ViewGit