haste-server/static/application.js

396 lines
11 KiB
JavaScript
Raw Permalink Normal View History

2011-11-18 16:49:00 +01:00
///// represents a single document
2011-11-18 16:17:41 +01:00
2011-11-18 23:23:23 +01:00
var haste_document = function() {
2011-11-18 16:17:41 +01:00
this.locked = false;
};
// Escapes HTML tag characters
haste_document.prototype.htmlEscape = function(s) {
return s
.replace(/&/g, '&')
.replace(/>/g, '>')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;');
};
2011-11-18 23:14:03 +01:00
// Get this document from the server and lock it here
2011-11-23 02:29:46 +01:00
haste_document.prototype.load = function(key, callback, lang) {
2011-11-18 22:22:00 +01:00
var _this = this;
$.ajax('/documents/' + key, {
type: 'get',
dataType: 'json',
success: function(res) {
_this.locked = true;
2011-11-19 06:23:53 +01:00
_this.key = key;
2011-11-18 22:22:00 +01:00
_this.data = res.data;
try {
2011-11-29 16:44:05 +01:00
var high;
if (lang === 'txt') {
high = { value: _this.htmlEscape(res.data) };
2011-11-29 16:44:05 +01:00
}
else if (lang) {
high = hljs.highlight(lang, res.data);
}
else {
high = hljs.highlightAuto(res.data);
}
} catch(err) {
2011-11-28 06:54:24 +01:00
// failed highlight, fall back on auto
high = hljs.highlightAuto(res.data);
}
2011-11-18 22:22:00 +01:00
callback({
value: high.value,
2011-11-18 22:25:18 +01:00
key: key,
language: high.language || lang,
lineCount: res.data.split("\n").length
2011-11-18 22:22:00 +01:00
});
2011-11-18 22:37:18 +01:00
},
error: function(err) {
callback(false);
2011-11-18 22:22:00 +01:00
}
});
};
2011-11-18 23:14:03 +01:00
// Save this document to the server and lock it here
2011-11-18 23:23:23 +01:00
haste_document.prototype.save = function(data, callback) {
2011-11-18 16:17:41 +01:00
if (this.locked) {
return false;
}
2011-11-18 21:18:38 +01:00
this.data = data;
var _this = this;
2011-11-18 16:49:00 +01:00
$.ajax('/documents', {
type: 'post',
data: data,
dataType: 'json',
success: function(res) {
2011-11-18 21:18:38 +01:00
_this.locked = true;
2011-11-19 06:23:53 +01:00
_this.key = res.key;
2011-11-18 16:49:00 +01:00
var high = hljs.highlightAuto(data);
2011-12-19 18:44:12 +01:00
callback(null, {
2011-11-18 16:49:00 +01:00
value: high.value,
2011-11-18 22:25:18 +01:00
key: res.key,
language: high.language,
lineCount: data.split("\n").length
2011-11-18 16:49:00 +01:00
});
2011-12-19 18:44:12 +01:00
},
error: function(res) {
try {
callback($.parseJSON(res.responseText));
}
catch (e) {
callback({message: 'Something went wrong!'});
}
2011-11-18 16:49:00 +01:00
}
});
2011-11-18 16:17:41 +01:00
};
2011-11-18 16:49:00 +01:00
///// represents the paste application
2011-11-19 07:00:04 +01:00
var haste = function(appName, options) {
2011-11-18 16:17:41 +01:00
this.appName = appName;
this.$textarea = $('textarea');
this.$box = $('#box');
this.$code = $('#box code');
this.$linenos = $('#linenos');
2011-11-19 07:00:04 +01:00
this.options = options;
2011-11-21 15:56:33 +01:00
this.configureShortcuts();
2011-11-23 17:31:50 +01:00
this.configureButtons();
// If twitter is disabled, hide the button
if (!options.twitter) {
2011-11-28 15:56:06 +01:00
$('#box2 .twitter').hide();
2011-11-23 17:31:50 +01:00
}
2011-11-18 16:17:41 +01:00
};
// Set the page title - include the appName
2011-11-18 23:23:23 +01:00
haste.prototype.setTitle = function(ext) {
2011-11-18 16:17:41 +01:00
var title = ext ? this.appName + ' - ' + ext : this.appName;
document.title = title;
};
2011-12-19 18:44:12 +01:00
// Show a message box
haste.prototype.showMessage = function(msg, cls) {
var msgBox = $('<li class="'+(cls || 'info')+'">'+msg+'</li>');
$('#messages').prepend(msgBox);
setTimeout(function() {
msgBox.slideUp('fast', function() { $(this).remove(); });
}, 3000);
};
2011-11-19 06:30:14 +01:00
// Show the light key
haste.prototype.lightKey = function() {
2011-11-23 17:31:50 +01:00
this.configureKey(['new', 'save']);
2011-11-19 06:30:14 +01:00
};
// Show the full key
haste.prototype.fullKey = function() {
2012-01-11 05:06:44 +01:00
this.configureKey(['new', 'duplicate', 'twitter', 'raw']);
2011-11-23 17:31:50 +01:00
};
// Set the key up for certain things to be enabled
haste.prototype.configureKey = function(enable) {
var $this, i = 0;
2011-11-28 15:56:06 +01:00
$('#box2 .function').each(function() {
2011-11-23 17:31:50 +01:00
$this = $(this);
for (i = 0; i < enable.length; i++) {
if ($this.hasClass(enable[i])) {
$this.addClass('enabled');
return true;
}
}
$this.removeClass('enabled');
});
2011-11-19 06:30:14 +01:00
};
2011-11-18 16:17:41 +01:00
// Remove the current document (if there is one)
// and set up for a new one
2011-11-18 23:23:23 +01:00
haste.prototype.newDocument = function(hideHistory) {
2011-11-18 16:17:41 +01:00
this.$box.hide();
2011-11-19 06:53:38 +01:00
this.doc = new haste_document();
2011-11-18 22:37:18 +01:00
if (!hideHistory) {
window.history.pushState(null, this.appName, '/');
}
2011-11-18 16:19:44 +01:00
this.setTitle();
2011-11-19 06:30:14 +01:00
this.lightKey();
2011-11-21 16:01:33 +01:00
this.$textarea.val('').show('fast', function() {
2011-11-19 06:02:11 +01:00
this.focus();
});
this.removeLineNumbers();
2011-11-19 06:53:38 +01:00
};
2011-11-18 16:17:41 +01:00
2011-11-23 02:29:46 +01:00
// Map of common extensions
2011-11-29 16:39:12 +01:00
// Note: this list does not need to include anything that IS its extension,
// due to the behavior of lookupTypeByExtension and lookupExtensionByType
// Note: optimized for lookupTypeByExtension
2011-11-23 02:29:46 +01:00
haste.extensionMap = {
2011-11-29 16:39:12 +01:00
rb: 'ruby', py: 'python', pl: 'perl', php: 'php', scala: 'scala', go: 'go',
xml: 'xml', html: 'xml', htm: 'xml', css: 'css', js: 'javascript', vbs: 'vbscript',
lua: 'lua', pas: 'delphi', java: 'java', cpp: 'cpp', cc: 'cpp', m: 'objectivec',
vala: 'vala', cs: 'cs', sql: 'sql', sm: 'smalltalk', lisp: 'lisp', ini: 'ini',
diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell',
md: 'markdown', txt: '', coffee: 'coffee'
2011-11-23 02:29:46 +01:00
};
2011-11-29 16:39:12 +01:00
// Look up the extension preferred for a type
// If not found, return the type itself - which we'll place as the extension
haste.prototype.lookupExtensionByType = function(type) {
for (var key in haste.extensionMap) {
if (haste.extensionMap[key] === type) return key;
}
return type;
};
// Look up the type for a given extension
// If not found, return the extension - which we'll attempt to use as the type
haste.prototype.lookupTypeByExtension = function(ext) {
return haste.extensionMap[ext] || ext;
2011-11-23 02:29:46 +01:00
};
// Add line numbers to the document
// For the specified number of lines
haste.prototype.addLineNumbers = function(lineCount) {
var h = '';
for (var i = 0; i < lineCount; i++) {
h += (i + 1).toString() + '<br/>';
}
$('#linenos').html(h);
};
// Remove the line numbers
haste.prototype.removeLineNumbers = function() {
$('#linenos').html('&gt;');
};
2011-11-18 22:22:00 +01:00
// Load a document and show it
2011-11-18 23:23:23 +01:00
haste.prototype.loadDocument = function(key) {
2011-11-23 02:29:46 +01:00
// Split the key up
var parts = key.split('.', 2);
// Ask for what we want
2011-11-18 22:22:00 +01:00
var _this = this;
2011-11-18 23:23:23 +01:00
_this.doc = new haste_document();
2011-11-23 02:29:46 +01:00
_this.doc.load(parts[0], function(ret) {
2011-11-18 22:22:00 +01:00
if (ret) {
_this.$code.html(ret.value);
2011-11-29 16:39:12 +01:00
_this.setTitle(ret.key);
2011-11-19 06:30:14 +01:00
_this.fullKey();
2011-11-18 22:22:00 +01:00
_this.$textarea.val('').hide();
2011-11-21 15:56:33 +01:00
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
2011-11-18 22:22:00 +01:00
}
2011-11-18 22:37:18 +01:00
else {
_this.newDocument();
}
2011-11-29 16:39:12 +01:00
}, this.lookupTypeByExtension(parts[1]));
2011-11-18 22:22:00 +01:00
};
2011-11-18 21:18:38 +01:00
// Duplicate the current document - only if locked
2011-11-18 23:23:23 +01:00
haste.prototype.duplicateDocument = function() {
2011-11-18 21:18:38 +01:00
if (this.doc.locked) {
var currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
}
};
2011-11-18 16:17:41 +01:00
// Lock the current document
2011-11-18 23:23:23 +01:00
haste.prototype.lockDocument = function() {
2011-11-18 16:17:41 +01:00
var _this = this;
2011-12-19 18:44:12 +01:00
this.doc.save(this.$textarea.val(), function(err, ret) {
if (err) {
_this.showMessage(err.message, 'error');
}
else if (ret) {
2011-11-18 16:17:41 +01:00
_this.$code.html(ret.value);
2011-11-29 16:39:12 +01:00
_this.setTitle(ret.key);
var file = '/' + ret.key;
2011-11-18 22:42:05 +01:00
if (ret.language) {
2011-11-29 16:39:12 +01:00
file += '.' + _this.lookupExtensionByType(ret.language);
2011-11-18 22:42:05 +01:00
}
2011-11-29 16:39:12 +01:00
window.history.pushState(null, _this.appName + '-' + ret.key, file);
2011-11-27 21:34:09 +01:00
_this.fullKey();
2011-11-18 16:17:41 +01:00
_this.$textarea.val('').hide();
2011-11-21 15:56:33 +01:00
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
2011-11-18 16:17:41 +01:00
}
});
};
2011-11-23 17:31:50 +01:00
haste.prototype.configureButtons = function() {
var _this = this;
this.buttons = [
{
2011-11-28 15:56:06 +01:00
$where: $('#box2 .save'),
2011-11-23 17:31:50 +01:00
label: 'Save',
shortcutDescription: 'control + s',
shortcut: function(evt) {
2011-11-30 02:11:09 +01:00
return evt.ctrlKey && (evt.keyCode === 83);
2011-11-23 17:31:50 +01:00
},
action: function() {
if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') {
_this.lockDocument();
}
}
},
{
2011-11-28 15:56:06 +01:00
$where: $('#box2 .new'),
2011-11-23 17:31:50 +01:00
label: 'New',
shortcut: function(evt) {
return evt.ctrlKey && evt.keyCode === 78
},
shortcutDescription: 'control + n',
action: function() {
_this.newDocument(!_this.doc.key);
}
},
{
2011-11-28 15:56:06 +01:00
$where: $('#box2 .duplicate'),
2011-11-23 17:31:50 +01:00
label: 'Duplicate & Edit',
shortcut: function(evt) {
return _this.doc.locked && evt.ctrlKey && evt.keyCode === 68;
},
shortcutDescription: 'control + d',
action: function() {
_this.duplicateDocument();
}
},
2012-01-11 05:06:44 +01:00
{
$where: $('#box2 .raw'),
label: 'Just Text',
shortcut: function(evt) {
return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82;
},
shortcutDescription: 'control + shift + r',
action: function() {
2012-01-23 17:40:09 +01:00
window.location.href = '/raw/' + _this.doc.key;
2012-01-11 05:06:44 +01:00
}
},
2011-11-23 17:31:50 +01:00
{
2011-11-28 15:56:06 +01:00
$where: $('#box2 .twitter'),
2011-11-23 17:31:50 +01:00
label: 'Twitter',
shortcut: function(evt) {
return _this.options.twitter && _this.doc.locked && evt.ctrlKey && evt.keyCode == 84;
},
shortcutDescription: 'control + t',
action: function() {
2011-11-29 18:06:49 +01:00
window.open('https://twitter.com/share?url=' + encodeURI(window.location.href));
2011-11-23 17:31:50 +01:00
}
}
];
for (var i = 0; i < this.buttons.length; i++) {
this.configureButton(this.buttons[i]);
}
};
haste.prototype.configureButton = function(options) {
// Handle the click action
options.$where.click(function(evt) {
evt.preventDefault();
2011-11-28 19:02:48 +01:00
if (!options.clickDisabled && $(this).hasClass('enabled')) {
2011-11-23 17:31:50 +01:00
options.action();
}
});
// Show the label
options.$where.mouseenter(function(evt) {
2011-11-28 15:56:06 +01:00
$('#box3 .label').text(options.label);
$('#box3 .shortcut').text(options.shortcutDescription || '');
$('#box3').show();
2011-11-28 15:39:31 +01:00
$(this).append($('#pointer').remove().show());
2011-11-23 17:31:50 +01:00
});
// Hide the label
options.$where.mouseleave(function(evt) {
2011-11-28 15:56:06 +01:00
$('#box3').hide();
2011-11-28 15:39:31 +01:00
$('#pointer').hide();
2011-11-23 17:31:50 +01:00
});
};
2011-11-18 16:17:41 +01:00
// Configure keyboard shortcuts for the textarea
2011-11-18 23:23:23 +01:00
haste.prototype.configureShortcuts = function() {
2011-11-18 16:17:41 +01:00
var _this = this;
2011-11-21 15:56:33 +01:00
$(document.body).keydown(function(evt) {
2011-11-23 17:31:50 +01:00
var button;
for (var i = 0 ; i < _this.buttons.length; i++) {
button = _this.buttons[i];
if (button.shortcut && button.shortcut(evt)) {
2011-11-28 19:02:48 +01:00
evt.preventDefault();
2011-11-23 17:31:50 +01:00
button.action();
return;
2011-11-18 23:20:33 +01:00
}
2011-11-18 16:17:41 +01:00
}
});
};
2011-11-18 16:49:00 +01:00
///// Tab behavior in the textarea - 2 spaces per tab
$(function() {
2011-11-18 16:17:41 +01: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();
}
}
});
});