haste-server/static/application.js

343 lines
9.3 KiB
JavaScript
Raw 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;
};
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: res.data };
}
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
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);
callback({
value: high.value,
2011-11-18 22:25:18 +01:00
key: res.key,
2011-11-18 16:49:00 +01:00
language: high.language
});
}
});
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;
2011-11-19 06:23:53 +01:00
this.baseUrl = window.location.href; // since this is loaded first
2011-11-18 16:17:41 +01:00
this.$textarea = $('textarea');
this.$box = $('#box');
this.$code = $('#box code');
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-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() {
2011-11-23 17:31:50 +01:00
this.configureKey(['new', 'duplicate', 'twitter', 'link']);
};
// 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();
});
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',
2011-11-29 16:44:05 +01:00
md: 'markdown', txt: ''
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
};
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);
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);
}
if (window.location.path != file) {
window.history.pushState(null, _this.appName + '-' + ret.key, file);
2011-11-18 22:42:05 +01:00
}
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();
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;
this.doc.save(this.$textarea.val(), function(ret) {
if (ret) {
_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();
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();
}
},
{
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();
}
}
});
});