Handle push state entirely
This commit is contained in:
parent
4f00d3c71a
commit
6597dba36d
@ -41,4 +41,6 @@ DocumentHandler.prototype.handlePost = function(request, response) {
|
||||
});
|
||||
};
|
||||
|
||||
// TODO block modifying
|
||||
|
||||
module.exports = DocumentHandler;
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -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) {
|
||||
|
@ -39,9 +39,19 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
$(function() {
|
||||
// Set up
|
||||
var app = new heist('heist');
|
||||
app.newDocument();
|
||||
$('textarea').focus();
|
||||
// Handle pops
|
||||
window.onpopstate = function(evt) {
|
||||
var path = evt.target.location.pathname;
|
||||
if (path === '/') {
|
||||
app.newDocument();
|
||||
}
|
||||
else {
|
||||
app.loadDocument(path.substring(1, path.length));
|
||||
}
|
||||
};
|
||||
});
|
||||
</script>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user