Added possibility to remove on file in the recovery

This commit is contained in:
benweet 2013-11-03 11:31:45 +00:00
parent 766a56b457
commit 4a2d80d5e9

View File

@ -17,27 +17,68 @@
deps: ['main'] deps: ['main']
}; };
setTimeout(function() { setTimeout(function() {
var failure = false;
try { try {
// Try to use StackEdit dependencies to save a file // Try to use StackEdit dependencies to save a file
var utils = require('utils'); var utils = require('utils');
utils.saveAs(JSON.stringify(localStorage), "StackEdit local storage.json"); utils.saveAs(JSON.stringify(localStorage), "StackEdit local storage.json");
$('.download-success').removeClass('hide');
} }
catch (e) { catch (e) {
// Try something safer // Try something safer
document.getElementsByTagName('textarea')[0].value = JSON.stringify(localStorage); document.getElementsByTagName('textarea')[0].value = JSON.stringify(localStorage);
var eltList = document.querySelectorAll('.download-failure'); failure = true;
for (var i = 0; i < eltList.length; ++i) {
var elt = eltList[i];
elt.className = elt.className.replace(/ hide/, '');
}
} }
listFiles();
// Show finish messages
var eltList = document.querySelectorAll(failure ? '.download-failure' : '.download-success');
for (var i = 0; i < eltList.length; ++i) {
var elt = eltList[i];
show(elt);
}
}, 5000); }, 5000);
function show(elt) {
elt.className = elt.className.replace(/ hide/, '');
}
function listFiles() {
// List files
var fileListElt = document.querySelector('.file-list');
fileListElt.innerHTML = '';
var fileIndexList = (localStorage['file.list'] || '').split(';');
for (var i = 0; i < fileIndexList.length; ++i) {
var fileIndex = fileIndexList[i];
if(!fileIndex) continue;
var fileTitle = localStorage[fileIndex + '.title'];
var divElt = document.createElement('div');
divElt.innerHTML = [
'<a href="javascript:removeFile(\'',
fileIndex,
'\')" class="icon-trash"></a> ',
fileTitle,
].join('');
fileListElt.appendChild(divElt);
}
}
function removeFile(fileIndex) {
var ok = confirm('Are you sure you want to remove "' + localStorage[fileIndex + '.title'] + '"?');
if(ok) {
localStorage['file.list'] = localStorage['file.list'].replace(';' + fileIndex + ';', ';');
for (var key in localStorage){
key.indexOf(fileIndex) === 0 && localStorage.removeItem(key);
}
listFiles();
}
}
function clearLocalStorage() { function clearLocalStorage() {
var ok = confirm('This will remove all your documents and settings. Are you sure?'); var ok = confirm('This will remove all your documents and settings. Are you sure?');
ok && localStorage.clear(); if(ok) {
localStorage.clear();
listFiles();
}
} }
</script> </script>
<script src="res-min/require.js"></script> <script src="res-min/require.js"></script>
@ -77,8 +118,11 @@
<p> <p>
<textarea class="download-failure hide form-control" rows="10"></textarea> <textarea class="download-failure hide form-control" rows="10"></textarea>
</p> </p>
<p class="download-success download-failure hide">To clear the local storage <p class="download-success download-failure hide">To remove one document from the local storage
<a href="#" onclick="clearLocalStorage()">click here</a>.</p> <a href="javascript:void(0)" onclick="show(document.querySelector('.file-list'))">click here</a>.</p>
<p class="file-list hide"></p>
<p class="download-success download-failure hide">To fully clear the local storage
<a href="javascript:void(0)" onclick="clearLocalStorage()">click here</a>.</p>
<p class="download-success download-failure hide">To reopen StackEdit <p class="download-success download-failure hide">To reopen StackEdit
<a href="https://stackedit.io/">click here</a>.</p> <a href="https://stackedit.io/">click here</a>.</p>
</div> </div>