Benjamin Renard commited on 2017-10-24 23:31:23
Showing 3 changed files, with 70 additions and 1 deletions.
... | ... |
@@ -871,6 +871,58 @@ on_confirm_clear_local_data=function(data) { |
871 | 871 |
location.reload(); |
872 | 872 |
} |
873 | 873 |
|
874 |
+/******************************* |
|
875 |
+ * Import/Export local data |
|
876 |
+ *******************************/ |
|
877 |
+export_local_data=function() { |
|
878 |
+ navbar_collapse_hide(); |
|
879 |
+ $('#export_local_data').attr('href','data:application/json;base64,' + btoa(JSON.stringify(scases.export()))); |
|
880 |
+} |
|
881 |
+ |
|
882 |
+import_local_data=function() { |
|
883 |
+ navbar_collapse_hide(); |
|
884 |
+ var input=$('<input type="file" accept="application/json">'); |
|
885 |
+ input.css('display', 'none'); |
|
886 |
+ input.bind('change',{'input': input},function(e) { |
|
887 |
+ pleaseWaitShow(); |
|
888 |
+ var input=e.data.input; |
|
889 |
+ var file=input.prop('files')[0]; |
|
890 |
+ if (file) { |
|
891 |
+ var reader = new FileReader(); |
|
892 |
+ $(reader).bind('load',function(e) { |
|
893 |
+ if ($.type(e.target.result)=='string') { |
|
894 |
+ if (e.target.result.startsWith('data:application/json;base64,')) { |
|
895 |
+ try { |
|
896 |
+ json_data=atob(e.target.result.replace('data:application/json;base64,','')); |
|
897 |
+ data=JSON.parse(json_data); |
|
898 |
+ pleaseWaitHide(); |
|
899 |
+ myconfirm('Etes-vous sûre de vouloir écraser vos données locales par celle issues de ce fichier ?',function() { |
|
900 |
+ scases.save(); |
|
901 |
+ var backData=localStorage.scases; |
|
902 |
+ localStorage.scases=json_data; |
|
903 |
+ scases=new SCaseList(); |
|
904 |
+ scases.loadFromLocalStorage(backData); |
|
905 |
+ show_scases(); |
|
906 |
+ }); |
|
907 |
+ } |
|
908 |
+ catch (e) { |
|
909 |
+ alert('Impossible de décodé le fichier.'); |
|
910 |
+ pleaseWaitHide(); |
|
911 |
+ } |
|
912 |
+ } |
|
913 |
+ else { |
|
914 |
+ alert('Fichier invalide.'); |
|
915 |
+ pleaseWaitHide(); |
|
916 |
+ } |
|
917 |
+ } |
|
918 |
+ }); |
|
919 |
+ reader.readAsDataURL(file); |
|
920 |
+ } |
|
921 |
+ }); |
|
922 |
+ $('body').append(input); |
|
923 |
+ input[0].click(); |
|
924 |
+} |
|
925 |
+ |
|
874 | 926 |
/********************* |
875 | 927 |
* Activate |
876 | 928 |
*********************/ |
... | ... |
@@ -888,6 +940,8 @@ $( document ).ready( function() { |
888 | 940 |
} |
889 | 941 |
|
890 | 942 |
$('#clear_local_data').bind('click',clear_local_data); |
943 |
+ $('#import_local_data').bind('click',import_local_data); |
|
944 |
+ $('#export_local_data').bind('click',export_local_data); |
|
891 | 945 |
|
892 | 946 |
$('#add_scase_btn').bind('click',on_add_scase_btn_click); |
893 | 947 |
$('#add_scase_submit').bind('click',on_valid_add_scase_modal); |
... | ... |
@@ -35,7 +35,7 @@ function SCaseList() { |
35 | 35 |
} |
36 | 36 |
} |
37 | 37 |
|
38 |
- this.loadFromLocalStorage=function() { |
|
38 |
+ this.loadFromLocalStorage=function(backData) { |
|
39 | 39 |
if (jQuery.type(localStorage.scases)!='undefined') { |
40 | 40 |
try { |
41 | 41 |
var data=JSON.parse(localStorage.scases); |
... | ... |
@@ -50,6 +50,12 @@ function SCaseList() { |
50 | 50 |
delete this[el]; |
51 | 51 |
} |
52 | 52 |
} |
53 |
+ if (jQuery.type(backData)!='undefined') { |
|
54 |
+ alert('Erreur en chargeant les données. Restauration des données précédentes'); |
|
55 |
+ localStorage.scases=backData; |
|
56 |
+ return this.loadFromLocalStorage(); |
|
57 |
+ } |
|
58 |
+ else { |
|
53 | 59 |
myconfirm('Erreur en chargeant les données locales. On les purges ?', |
54 | 60 |
function(data) { |
55 | 61 |
delete localStorage.scases; |
... | ... |
@@ -58,6 +64,7 @@ function SCaseList() { |
58 | 64 |
); |
59 | 65 |
} |
60 | 66 |
} |
67 |
+ } |
|
61 | 68 |
else { |
62 | 69 |
myconfirm("<h2>Bienvenu !</h2><p>Souhaitez-vous charger les données d'exemple ?</p>", |
63 | 70 |
function(scases) { |
... | ... |
@@ -51,7 +51,15 @@ |
51 | 51 |
<li><a href="#scase_trash" id="scase_trash_btn"><span class="glyphicon glyphicon-trash"></span> Voir la corbeille de la valise</a></li> |
52 | 52 |
</ul> |
53 | 53 |
</li> |
54 |
+ <li class="dropdown"> |
|
55 |
+ <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class='glyphicon glyphicon-hdd'></span> Gérer vos données <b class="caret"></b></a> |
|
56 |
+ <ul class="dropdown-menu"> |
|
57 |
+ <li><a id='export_local_data' href='#' download='mysc_export.json'><span class='glyphicon glyphicon-save'></span> Sauvegarder vos données</a></li> |
|
58 |
+ <li><a id='import_local_data' href='#' download='mysc_export.json'><span class='glyphicon glyphicon-open'></span> Restaurer vos données</a></li> |
|
59 |
+ <li class="divider"></li> |
|
54 | 60 |
<li><a id='clear_local_data'><span class='glyphicon glyphicon-trash'></span> Purger les données locales</a></li> |
61 |
+ </ul> |
|
62 |
+ </li> |
|
55 | 63 |
<li><a id='update_app'><span class='glyphicon glyphicon-refresh'></span> Mise à jour de l'application</a></li> |
56 | 64 |
</ul> |
57 | 65 |
</div> |
58 | 66 |