From b5e8ec98c556c223401c27dc1821f0394a8ae438 Mon Sep 17 00:00:00 2001 From: benweet Date: Sun, 24 Mar 2013 23:21:55 +0000 Subject: [PATCH] New file system management --- css/main.css | 7 +++- index.html | 25 ++++++++---- js/main.js | 112 ++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 113 insertions(+), 31 deletions(-) diff --git a/css/main.css b/css/main.css index 27fc03fb..1660bc9e 100644 --- a/css/main.css +++ b/css/main.css @@ -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 { diff --git a/index.html b/index.html index 771f118f..cceedb32 100644 --- a/index.html +++ b/index.html @@ -39,19 +39,30 @@
- + diff --git a/js/main.js b/js/main.js index 0431fb09..d1397c4c 100644 --- a/js/main.js +++ b/js/main.js @@ -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").text(title); + var 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); };