New file system management

This commit is contained in:
benweet 2013-03-24 23:21:55 +00:00
parent f75925f3ae
commit b5e8ec98c5
3 changed files with 113 additions and 31 deletions

View File

@ -3,8 +3,13 @@ body {
font-family: sans-serif;
}
#info-filename {
#file-title {
float:right;
padding: 10px;
}
#file-title-input {
display: none;
}
#wmd-input, #wmd-preview {

View File

@ -39,19 +39,30 @@
<body>
<div id="navbar" class="navbar navbar-fixed-top">
<div class="navbar-inner">
<a id="wmd-button-bar" class="nav"></a>
<div id="wmd-button-bar" class="nav"></div>
<ul class="nav pull-right">
<li class="divider-vertical"></li>
<li id="menu" class="dropdown"><a class="dropdown-toggle"
data-toggle="dropdown" href="#"><img src="img/stackedit-16.png"/> Menu</a>
<li class="navbar-form pull-left">
<input id="file-title-input" type="text" class="span3" placeholder="File title" />
</li>
<li class="dropdown"><a class="dropdown-toggle"
data-toggle="dropdown" href="#"><b class="caret"></b></a>
<ul id="file-selector" class="dropdown-menu">
</ul></li>
<li class="divider-vertical"></li>
<li class="dropdown"><a class="dropdown-toggle"
data-toggle="dropdown" href="#"><img src="img/stackedit-16.png" />
Menu <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a id="drive-link" href="javascript:void(0);">Link with Google Drive</a></li>
<li><a id="new-file" href="javascript:void(0);"><i class="icon-file"></i> New file</a></li>
<li><a id="remove-file" href="javascript:void(0);"><i class="icon-trash"></i> Remove file</a></li>
<li><a id="drive-link" href="javascript:void(0);"><i class="icon-magnet"></i> Link
with Google Drive</a></li>
</ul></li>
</ul>
<a class="brand pull-right" id="info-filename"></a>
<a class="brand pull-right" id="file-title" href="javascript:void(0);"></a>
</div>
</div>
<textarea id="wmd-input"></textarea>
<div id="wmd-preview" class="well"></div>
</body>
</html>
</html>

View File

@ -7,38 +7,104 @@ var fileManager = (function($) {
var fileManager = {};
fileManager.init = function() {
if (localStorage.fileSystem) {
this.fileSystem = JSON.parse(localStorage.fileSystem);
if (localStorage.currentFile)
this.selectFile(localStorage.currentFile);
else
this.selectFile(Object.keys(this.fileSystem)[0]);
} else {
this.fileSystem = {};
this.createFile("New file");
}
fileManager.selectFile();
window.setInterval(function() {
fileManager.saveFile();
}, 5000);
$("#new-file").click(function() {
fileManager.saveFile();
fileManager.createFile();
fileManager.selectFile();
});
$("#file-title").click(function() {
$(this).hide();
$("#file-title-input").show().focus();
});
$("#file-title-input").blur(function() {
var title = $.trim($(this).val());
if(title) {
fileIndex = localStorage["file.current"];
localStorage[fileIndex + ".title"] = title;
}
$(this).hide();
$("#file-title").show();
fileManager.selectFile();
});
};
fileManager.selectFile = function() {
// If file system does not exist
if(!localStorage["file.count"]) {
localStorage.clear();
localStorage["file.count"] = 0;
}
this.updateFileTitleList();
// If no file create one
if(this.fileTitleList.length == 0) {
this.createFile();
this.updateFileTitleList();
}
// If no default file take first one
if(!localStorage["file.current"]) {
var fileCount = parseInt(localStorage["file.count"]);
for(var i=0; i<fileCount; i++) {
var fileIndex = "file." + i;
if(localStorage[fileIndex + ".title"]) {
localStorage["file.current"] = fileIndex;
break;
}
}
}
// Update the editor and the file title
var fileIndex = localStorage["file.current"];
$("#wmd-input").val(localStorage[fileIndex + ".content"]);
var title = localStorage[fileIndex + ".title"];
$("#file-title").text(title);
$("#file-title-input").val(title);
};
fileManager.createFile = function(filename) {
this.fileSystem[filename] = "blah blah";
this.selectFile(filename);
fileManager.createFile = function(title) {
if(!title) {
title = "New file";
}
var fileIndex = "file." + parseInt(localStorage["file.count"]);
localStorage[fileIndex + ".title"] = title;
localStorage[fileIndex + ".content"] = "";
localStorage["file.count"] = parseInt(localStorage["file.count"]) + 1;
localStorage["file.current"] = fileIndex;
};
fileManager.selectFile = function(filename) {
this.currentFile = filename;
this.content = this.fileSystem[this.currentFile];
$("#wmd-input").val(this.content);
$("#info-filename").text(filename);
fileManager.updateFileTitleList = function() {
var fileCount = parseInt(localStorage["file.count"]);
this.fileTitleList = [];
$("#file-selector").empty();
for(var i=0; i<fileCount; i++) {
var fileIndex = "file." + i;
var title = localStorage[fileIndex + ".title"];
if(title) {
this.fileTitleList[i] = title;
var a = $("<a>").text(title);
var li = $("<li>").append(a);
if(fileIndex == localStorage["file.current"]) {
li.addClass("disabled");
}
else {
a.click((function(fileIndex) {
return function() {
localStorage["file.current"] = fileIndex;
fileManager.selectFile();
};
})(fileIndex));
}
$("#file-selector").append(li);
}
}
};
fileManager.saveFile = function() {
this.content = $("#wmd-input").val();
this.fileSystem[this.currentFile] = this.content;
localStorage.fileSystem = JSON.stringify(this.fileSystem);
localStorage.currentFile = this.currentFile;
var content = $("#wmd-input").val();
var fileIndex = localStorage["file.current"];
localStorage[fileIndex + ".content"] = content;
//insertFile(this.currentFile, this.content);
};