haste-server/static/application.js

189 lines
4.8 KiB
JavaScript
Raw Normal View History

2011-11-18 15:49:00 +00:00
///// represents a single document
2011-11-18 15:17:41 +00:00
2011-11-18 22:23:23 +00:00
var haste_document = function() {
2011-11-18 15:17:41 +00:00
this.locked = false;
};
2011-11-18 22:14:03 +00:00
// Get this document from the server and lock it here
2011-11-18 22:23:23 +00:00
haste_document.prototype.load = function(key, callback) {
2011-11-18 21:22:00 +00:00
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,
2011-11-18 21:25:18 +00:00
key: key,
2011-11-18 21:22:00 +00:00
language: high.language
});
2011-11-18 21:37:18 +00:00
},
error: function(err) {
callback(false);
2011-11-18 21:22:00 +00:00
}
});
};
2011-11-18 22:14:03 +00:00
// Save this document to the server and lock it here
2011-11-18 22:23:23 +00:00
haste_document.prototype.save = function(data, callback) {
2011-11-18 15:17:41 +00:00
if (this.locked) {
return false;
}
2011-11-18 20:18:38 +00:00
this.data = data;
var _this = this;
2011-11-18 15:49:00 +00:00
$.ajax('/documents', {
type: 'post',
data: data,
dataType: 'json',
success: function(res) {
2011-11-18 20:18:38 +00:00
_this.locked = true;
2011-11-18 15:49:00 +00:00
var high = hljs.highlightAuto(data);
callback({
value: high.value,
2011-11-18 21:25:18 +00:00
key: res.key,
2011-11-18 15:49:00 +00:00
language: high.language
});
}
});
2011-11-18 15:17:41 +00:00
};
2011-11-18 15:49:00 +00:00
///// represents the paste application
2011-11-18 22:23:23 +00:00
var haste = function(appName) {
2011-11-18 15:17:41 +00:00
this.appName = appName;
this.$textarea = $('textarea');
this.$box = $('#box');
this.$code = $('#box code');
this.configureShortcuts();
};
2011-11-18 21:37:18 +00:00
// TODO add key of commands
2011-11-18 15:17:41 +00:00
// Set the page title - include the appName
2011-11-18 22:23:23 +00:00
haste.prototype.setTitle = function(ext) {
2011-11-18 15:17:41 +00:00
var title = ext ? this.appName + ' - ' + ext : this.appName;
document.title = title;
};
// Remove the current document (if there is one)
// and set up for a new one
2011-11-18 22:23:23 +00:00
haste.prototype.newDocument = function(hideHistory) {
this.doc = new haste_document();
2011-11-18 15:17:41 +00:00
this.$box.hide();
2011-11-18 21:37:18 +00:00
if (!hideHistory) {
window.history.pushState(null, this.appName, '/');
}
2011-11-18 15:19:44 +00:00
this.setTitle();
2011-11-18 15:17:41 +00:00
this.$textarea.val('').show().focus();
}
2011-11-18 21:22:00 +00:00
// Load a document and show it
2011-11-18 22:23:23 +00:00
haste.prototype.loadDocument = function(key) {
2011-11-18 21:22:00 +00:00
var _this = this;
2011-11-18 22:23:23 +00:00
_this.doc = new haste_document();
2011-11-18 21:22:00 +00:00
_this.doc.load(key, function(ret) {
if (ret) {
_this.$code.html(ret.value);
2011-11-18 21:42:05 +00:00
var title = ret.key;
if (ret.language) {
title += ' - ' + ret.language;
}
_this.setTitle(title);
2011-11-18 21:22:00 +00:00
_this.$textarea.val('').hide();
_this.$box.show();
}
2011-11-18 21:37:18 +00:00
else {
_this.newDocument();
}
2011-11-18 21:22:00 +00:00
});
};
2011-11-18 20:18:38 +00:00
// Duplicate the current document - only if locked
2011-11-18 22:23:23 +00:00
haste.prototype.duplicateDocument = function() {
2011-11-18 20:18:38 +00:00
if (this.doc.locked) {
var currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
}
};
2011-11-18 15:17:41 +00:00
// Lock the current document
2011-11-18 22:23:23 +00:00
haste.prototype.lockDocument = function() {
2011-11-18 15:17:41 +00:00
var _this = this;
this.doc.save(this.$textarea.val(), function(ret) {
if (ret) {
_this.$code.html(ret.value);
2011-11-18 21:42:05 +00:00
var title = ret.key;
if (ret.language) {
title += ' - ' + ret.language;
}
_this.setTitle(title);
2011-11-18 21:25:18 +00:00
window.history.pushState(null, _this.appName + '-' + ret.key, '/' + ret.key);
2011-11-18 15:17:41 +00:00
_this.$textarea.val('').hide();
_this.$box.show();
}
});
};
// Configure keyboard shortcuts for the textarea
2011-11-18 22:23:23 +00:00
haste.prototype.configureShortcuts = function() {
2011-11-18 15:17:41 +00:00
var _this = this;
2011-11-18 22:20:33 +00:00
$('body').keyup(function(evt) {
2011-11-18 20:18:38 +00:00
// ^L or ^S for lock
if (evt.ctrlKey && (evt.keyCode === 76 || evt.keyCode === 83)) {
2011-11-18 22:20:33 +00:00
if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') {
evt.preventDefault();
_this.lockDocument();
}
2011-11-18 15:17:41 +00:00
}
2011-11-18 20:18:38 +00:00
// ^N for new document
2011-11-18 15:19:44 +00:00
else if (evt.ctrlKey && evt.keyCode === 78) {
2011-11-18 20:18:38 +00:00
evt.preventDefault();
2011-11-18 15:19:44 +00:00
_this.newDocument();
}
2011-11-18 20:18:38 +00:00
// ^D for duplicate - only when locked
else if (_this.doc.locked && evt.ctrlKey && evt.keyCode === 68) {
evt.preventDefault();
_this.duplicateDocument();
}
2011-11-18 15:17:41 +00:00
});
};
2011-11-18 15:49:00 +00:00
///// Tab behavior in the textarea - 2 spaces per tab
$(function() {
2011-11-18 15:17:41 +00:00
$('textarea').keydown(function(evt) {
if (evt.keyCode === 9) {
evt.preventDefault();
var myValue = ' ';
// http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery
// For browsers like Internet Explorer
if (document.selection) {
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
// Mozilla and Webkit
else if (this.selectionStart || this.selectionStart == '0') {
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos) + myValue +
this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
}
else {
this.value += myValue;
this.focus();
}
}
});
});