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; font-family: sans-serif;
} }
#info-filename { #file-title {
float:right; float:right;
padding: 10px;
}
#file-title-input {
display: none;
} }
#wmd-input, #wmd-preview { #wmd-input, #wmd-preview {

View File

@ -39,16 +39,27 @@
<body> <body>
<div id="navbar" class="navbar navbar-fixed-top"> <div id="navbar" class="navbar navbar-fixed-top">
<div class="navbar-inner"> <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"> <ul class="nav pull-right">
<li class="divider-vertical"></li> <li class="navbar-form pull-left">
<li id="menu" class="dropdown"><a class="dropdown-toggle" <input id="file-title-input" type="text" class="span3" placeholder="File title" />
data-toggle="dropdown" href="#"><img src="img/stackedit-16.png"/> Menu</a> </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"> <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></li>
</ul> </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>
</div> </div>
<textarea id="wmd-input"></textarea> <textarea id="wmd-input"></textarea>

View File

@ -7,38 +7,104 @@ var fileManager = (function($) {
var fileManager = {}; var fileManager = {};
fileManager.init = function() { fileManager.init = function() {
if (localStorage.fileSystem) { fileManager.selectFile();
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");
}
window.setInterval(function() { window.setInterval(function() {
fileManager.saveFile(); fileManager.saveFile();
}, 5000); }, 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.createFile = function(filename) { fileManager.selectFile = function() {
this.fileSystem[filename] = "blah blah"; // If file system does not exist
this.selectFile(filename); 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.selectFile = function(filename) { fileManager.createFile = function(title) {
this.currentFile = filename; if(!title) {
this.content = this.fileSystem[this.currentFile]; title = "New file";
$("#wmd-input").val(this.content); }
$("#info-filename").text(filename); 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.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() { fileManager.saveFile = function() {
this.content = $("#wmd-input").val(); var content = $("#wmd-input").val();
this.fileSystem[this.currentFile] = this.content; var fileIndex = localStorage["file.current"];
localStorage.fileSystem = JSON.stringify(this.fileSystem); localStorage[fileIndex + ".content"] = content;
localStorage.currentFile = this.currentFile;
//insertFile(this.currentFile, this.content); //insertFile(this.currentFile, this.content);
}; };