1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Handle push state entirely

This commit is contained in:
John Crepezzi 2011-11-18 16:22:00 -05:00
parent 4f00d3c71a
commit 6597dba36d
4 changed files with 56 additions and 9 deletions

View file

@ -41,4 +41,6 @@ DocumentHandler.prototype.handlePost = function(request, response) {
}); });
}; };
// TODO block modifying
module.exports = DocumentHandler; module.exports = DocumentHandler;

View file

@ -25,6 +25,7 @@ StaticHandler.contentTypeFor = function(ext) {
// Handle a request, and serve back the asset if it exists // Handle a request, and serve back the asset if it exists
StaticHandler.prototype.handle = function(incPath, response) { StaticHandler.prototype.handle = function(incPath, response) {
var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath); var filePath = this.basePath + (incPath == '/' ? this.defaultPath : incPath);
var _this = this;
path.exists(filePath, function(exists) { path.exists(filePath, function(exists) {
if (exists) { if (exists) {
fs.readFile(filePath, function(error, content) { fs.readFile(filePath, function(error, content) {
@ -41,9 +42,11 @@ StaticHandler.prototype.handle = function(incPath, response) {
}); });
} }
else { else {
winston.warn('file not found', { path: filePath }); // TODO 404 if not match regex
response.writeHead(404, { 'content-type': 'application/json' }); //winston.warn('file not found', { path: filePath });
response.end(JSON.stringify({ message: 'file not found' })); //response.writeHead(404, { 'content-type': 'application/json' });
//response.end(JSON.stringify({ message: 'file not found' }));
_this.handle('/', response);
} }
}); });
}; };

View file

@ -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) { heist_document.prototype.save = function(data, callback) {
if (this.locked) { if (this.locked) {
@ -60,10 +82,23 @@ heist.prototype.newDocument = function(ext) {
this.doc = new heist_document(); this.doc = new heist_document();
this.$box.hide(); this.$box.hide();
this.setTitle(); this.setTitle();
window.history.pushState(null, this.appName, '/');
this.$textarea.val('').show().focus(); 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 // Duplicate the current document - only if locked
heist.prototype.duplicateDocument = function() { heist.prototype.duplicateDocument = function() {
if (this.doc.locked) { if (this.doc.locked) {
@ -79,7 +114,7 @@ heist.prototype.lockDocument = function() {
this.doc.save(this.$textarea.val(), function(ret) { this.doc.save(this.$textarea.val(), function(ret) {
if (ret) { if (ret) {
_this.$code.html(ret.value); _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); window.history.pushState(null, _this.appName + '-' + ret.uuid, '/' + ret.uuid);
_this.$textarea.val('').hide(); _this.$textarea.val('').hide();
_this.$box.show(); _this.$box.show();
@ -112,11 +147,8 @@ heist.prototype.configureShortcuts = function() {
// TODO handle not found gracefully // TODO handle not found gracefully
// TODO refuse to lock empty documents // 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 ///// Tab behavior in the textarea - 2 spaces per tab
$(function() { $(function() {
$('textarea').keydown(function(evt) { $('textarea').keydown(function(evt) {

View file

@ -39,9 +39,19 @@
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
// Set up
var app = new heist('heist'); var app = new heist('heist');
app.newDocument();
$('textarea').focus(); $('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> </script>