+

Add backup/restore data feature

Benjamin Renard [2017-10-24 23:31:23]
Add backup/restore data feature
Filename
public_html/inc/main.js
public_html/inc/mysc_objects.js
public_html/index.html
diff --git a/public_html/inc/main.js b/public_html/inc/main.js
index 31e7917..5272329 100644
--- a/public_html/inc/main.js
+++ b/public_html/inc/main.js
@@ -871,6 +871,58 @@ on_confirm_clear_local_data=function(data) {
   location.reload();
 }

+/*******************************
+ * Import/Export local data
+ *******************************/
+export_local_data=function() {
+  navbar_collapse_hide();
+  $('#export_local_data').attr('href','data:application/json;base64,' + btoa(JSON.stringify(scases.export())));
+}
+
+import_local_data=function() {
+  navbar_collapse_hide();
+  var input=$('<input type="file" accept="application/json">');
+  input.css('display', 'none');
+  input.bind('change',{'input': input},function(e) {
+    pleaseWaitShow();
+    var input=e.data.input;
+    var file=input.prop('files')[0];
+    if (file) {
+      var reader = new FileReader();
+      $(reader).bind('load',function(e) {
+        if ($.type(e.target.result)=='string') {
+          if (e.target.result.startsWith('data:application/json;base64,')) {
+            try {
+              json_data=atob(e.target.result.replace('data:application/json;base64,',''));
+              data=JSON.parse(json_data);
+              pleaseWaitHide();
+              myconfirm('Etes-vous sûre de vouloir écraser vos données locales par celle issues de ce fichier ?',function() {
+                scases.save();
+                var backData=localStorage.scases;
+                localStorage.scases=json_data;
+                scases=new SCaseList();
+                scases.loadFromLocalStorage(backData);
+                show_scases();
+              });
+            }
+            catch (e) {
+              alert('Impossible de décodé le fichier.');
+              pleaseWaitHide();
+            }
+          }
+          else {
+            alert('Fichier invalide.');
+            pleaseWaitHide();
+          }
+        }
+      });
+      reader.readAsDataURL(file);
+    }
+  });
+  $('body').append(input);
+  input[0].click();
+}
+
 /*********************
  * Activate
  *********************/
@@ -888,6 +940,8 @@ $( document ).ready( function() {
   }

   $('#clear_local_data').bind('click',clear_local_data);
+  $('#import_local_data').bind('click',import_local_data);
+  $('#export_local_data').bind('click',export_local_data);

   $('#add_scase_btn').bind('click',on_add_scase_btn_click);
   $('#add_scase_submit').bind('click',on_valid_add_scase_modal);
diff --git a/public_html/inc/mysc_objects.js b/public_html/inc/mysc_objects.js
index 906d7f1..ec79ee5 100644
--- a/public_html/inc/mysc_objects.js
+++ b/public_html/inc/mysc_objects.js
@@ -35,7 +35,7 @@ function SCaseList() {
     }
   }

-  this.loadFromLocalStorage=function() {
+  this.loadFromLocalStorage=function(backData) {
     if (jQuery.type(localStorage.scases)!='undefined') {
       try {
         var data=JSON.parse(localStorage.scases);
@@ -50,12 +50,19 @@ function SCaseList() {
             delete this[el];
           }
         }
-        myconfirm('Erreur en chargeant les données locales. On les purges ?',
-          function(data) {
-            delete localStorage.scases;
-            location.reload();
-          }
-        );
+        if (jQuery.type(backData)!='undefined') {
+          alert('Erreur en chargeant les données. Restauration des données précédentes');
+          localStorage.scases=backData;
+          return this.loadFromLocalStorage();
+        }
+        else {
+          myconfirm('Erreur en chargeant les données locales. On les purges ?',
+            function(data) {
+              delete localStorage.scases;
+              location.reload();
+            }
+          );
+        }
       }
     }
     else {
diff --git a/public_html/index.html b/public_html/index.html
index 544919e..eec6cf9 100644
--- a/public_html/index.html
+++ b/public_html/index.html
@@ -51,7 +51,15 @@
                 <li><a href="#scase_trash" id="scase_trash_btn"><span class="glyphicon glyphicon-trash"></span> Voir la corbeille de la valise</a></li>
               </ul>
             </li>
-            <li><a id='clear_local_data'><span class='glyphicon glyphicon-trash'></span> Purger les données locales</a></li>
+            <li class="dropdown">
+              <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class='glyphicon glyphicon-hdd'></span> Gérer vos données <b class="caret"></b></a>
+              <ul class="dropdown-menu">
+                <li><a id='export_local_data' href='#' download='mysc_export.json'><span class='glyphicon glyphicon-save'></span> Sauvegarder vos données</a></li>
+                <li><a id='import_local_data' href='#' download='mysc_export.json'><span class='glyphicon glyphicon-open'></span> Restaurer vos données</a></li>
+                <li class="divider"></li>
+                <li><a id='clear_local_data'><span class='glyphicon glyphicon-trash'></span> Purger les données locales</a></li>
+              </ul>
+            </li>
             <li><a id='update_app'><span class='glyphicon glyphicon-refresh'></span> Mise à jour de l'application</a></li>
           </ul>
         </div>
ViewGit