From 6597dba36d3680e21faa237e1df5503fe3f64df6 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 18 Nov 2011 16:22:00 -0500 Subject: [PATCH] Handle push state entirely --- lib/document_handler.js | 2 ++ lib/static_handler.js | 9 ++++++--- static/application.js | 42 ++++++++++++++++++++++++++++++++++++----- static/index.html | 12 +++++++++++- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/lib/document_handler.js b/lib/document_handler.js index cb4331d..742e910 100644 --- a/lib/document_handler.js +++ b/lib/document_handler.js @@ -41,4 +41,6 @@ DocumentHandler.prototype.handlePost = function(request, response) { }); }; +// TODO block modifying + module.exports = DocumentHandler; diff --git a/lib/static_handler.js b/lib/static_handler.js index 95b0e57..6a90dfb 100644 --- a/lib/static_handler.js +++ b/lib/static_handler.js @@ -25,6 +25,7 @@ StaticHandler.contentTypeFor = function(ext) { // Handle a request, and serve back the asset if it exists StaticHandler.prototype.handle = function(incPath, response) { var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); + var _this = this; path.exists(filePath, function(exists) { if (exists) { fs.readFile(filePath, function(error, content) { @@ -41,9 +42,11 @@ StaticHandler.prototype.handle = function(incPath, response) { }); } else { - winston.warn('file not found', { path: filePath }); - response.writeHead(404, { 'content-type': 'application/json' }); - response.end(JSON.stringify({ message: 'file not found' })); + // TODO 404 if not match regex + //winston.warn('file not found', { path: filePath }); + //response.writeHead(404, { 'content-type': 'application/json' }); + //response.end(JSON.stringify({ message: 'file not found' })); + _this.handle('/', response); } }); }; diff --git a/static/application.js b/static/application.js index e16c3a6..9e5b95a 100644 --- a/static/application.js +++ b/static/application.js @@ -6,6 +6,28 @@ var heist_document = function() { }; +heist_document.prototype.load = function(key, callback) { + + var _this = this; + + $.ajax('/documents/' + key, { + type: 'get', + dataType: 'json', + + success: function(res) { + _this.locked = true; + _this.data = res.data; + var high = hljs.highlightAuto(res.data); + callback({ + value: high.value, + uuid: key, + language: high.language + }); + } + }); + +}; + heist_document.prototype.save = function(data, callback) { if (this.locked) { @@ -60,10 +82,23 @@ heist.prototype.newDocument = function(ext) { this.doc = new heist_document(); this.$box.hide(); this.setTitle(); - window.history.pushState(null, this.appName, '/'); this.$textarea.val('').show().focus(); } +// Load a document and show it +heist.prototype.loadDocument = function(key) { + var _this = this; + _this.doc = new heist_document(); + _this.doc.load(key, function(ret) { + if (ret) { + _this.$code.html(ret.value); + _this.setTitle(ret.language ? ret.language : 'unknown'); + _this.$textarea.val('').hide(); + _this.$box.show(); + } + }); +}; + // Duplicate the current document - only if locked heist.prototype.duplicateDocument = function() { if (this.doc.locked) { @@ -79,7 +114,7 @@ heist.prototype.lockDocument = function() { this.doc.save(this.$textarea.val(), function(ret) { if (ret) { _this.$code.html(ret.value); - _this.setTitle(ret.language + '-' + ret.uuid); + _this.setTitle(ret.language ? ret.language : 'unknown'); window.history.pushState(null, _this.appName + '-' + ret.uuid, '/' + ret.uuid); _this.$textarea.val('').hide(); _this.$box.show(); @@ -112,11 +147,8 @@ heist.prototype.configureShortcuts = function() { // TODO handle not found gracefully // TODO refuse to lock empty documents -// TODO support for browsers without pushstate -// TODO support for push state navigation ///// Tab behavior in the textarea - 2 spaces per tab - $(function() { $('textarea').keydown(function(evt) { diff --git a/static/index.html b/static/index.html index 323f274..46b4eb7 100644 --- a/static/index.html +++ b/static/index.html @@ -39,9 +39,19 @@