haste-server/static/application.js

345 lines
9.2 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-23 01:29:46 +00:00
haste_document.prototype.load = function(key, callback, lang) {
2011-11-18 21:22:00 +00:00
var _this = this;
$.ajax('/documents/' + key, {
type: 'get',
dataType: 'json',
success: function(res) {
_this.locked = true;
2011-11-19 05:23:53 +00:00
_this.key = key;
2011-11-18 21:22:00 +00:00
_this.data = res.data;
try {
var high = lang ? hljs.highlight(lang, res.data) : hljs.highlightAuto(res.data);
} catch(err) {
2011-11-28 05:54:24 +00:00
// failed highlight, fall back on auto
high = hljs.highlightAuto(res.data);
}
2011-11-18 21:22:00 +00:00
callback({
value: high.value,
2011-11-18 21:25:18 +00:00
key: key,
language: high.language || lang
2011-11-18 21:22:00 +00:00
});
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-19 05:23:53 +00:00
_this.key = res.key;
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-19 06:00:04 +00:00
var haste = function(appName, options) {
2011-11-18 15:17:41 +00:00
this.appName = appName;
2011-11-19 05:23:53 +00:00
this.baseUrl = window.location.href; // since this is loaded first
2011-11-18 15:17:41 +00:00
this.$textarea = $('textarea');
this.$box = $('#box');
this.$code = $('#box code');
2011-11-19 06:00:04 +00:00
this.options = options;
2011-11-21 14:56:33 +00:00
this.configureShortcuts();
2011-11-23 16:31:50 +00:00
this.configureButtons();
// If twitter is disabled, hide the button
if (!options.twitter) {
$('#key .box2 .twitter').hide();
}
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;
};
2011-11-19 05:30:14 +00:00
// Show the light key
haste.prototype.lightKey = function() {
2011-11-23 16:31:50 +00:00
this.configureKey(['new', 'save']);
2011-11-27 20:34:09 +00:00
this.removeClip();
2011-11-19 05:30:14 +00:00
};
// Show the full key
haste.prototype.fullKey = function() {
2011-11-23 16:31:50 +00:00
this.configureKey(['new', 'duplicate', 'twitter', 'link']);
2011-11-27 20:34:09 +00:00
this.configureClip();
2011-11-23 16:31:50 +00:00
};
// Set the key up for certain things to be enabled
haste.prototype.configureKey = function(enable) {
var $this, i = 0;
$('#key .box2 .function').each(function() {
$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 05:30:14 +00:00
};
2011-11-18 15:17:41 +00:00
// 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) {
2011-11-18 15:17:41 +00:00
this.$box.hide();
2011-11-19 05:53:38 +00:00
this.doc = new haste_document();
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-19 05:30:14 +00:00
this.lightKey();
2011-11-21 15:01:33 +00:00
this.$textarea.val('').show('fast', function() {
2011-11-19 05:02:11 +00:00
this.focus();
});
2011-11-19 05:53:38 +00:00
};
2011-11-18 15:17:41 +00:00
2011-11-23 01:29:46 +00:00
// Map of common extensions
haste.extensionMap = {
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',
2011-11-28 04:39:09 +00:00
diff: 'diff', bash: 'bash', sh: 'bash', tex: 'tex', erl: 'erlang', hs: 'haskell',
md: 'markdown'
2011-11-23 01:29:46 +00:00
};
// Map an extension to a language
haste.prototype.lookupExtension = function(ext) {
var match = haste.extensionMap[ext];
return match; // if not found, will auto-detect
};
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-23 01:29:46 +00:00
// Split the key up
var parts = key.split('.', 2);
// Ask for what we want
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-23 01:29:46 +00:00
_this.doc.load(parts[0], function(ret) {
2011-11-18 21:22:00 +00:00
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-19 05:30:14 +00:00
_this.fullKey();
2011-11-18 21:22:00 +00:00
_this.$textarea.val('').hide();
2011-11-21 14:56:33 +00:00
_this.$box.show().focus();
2011-11-18 21:22:00 +00:00
}
2011-11-18 21:37:18 +00:00
else {
_this.newDocument();
}
2011-11-23 01:29:46 +00:00
}, this.lookupExtension(parts[1]));
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-27 20:34:09 +00:00
_this.fullKey();
2011-11-18 15:17:41 +00:00
_this.$textarea.val('').hide();
2011-11-21 14:56:33 +00:00
_this.$box.show().focus();
2011-11-18 15:17:41 +00:00
}
});
};
2011-11-27 20:34:09 +00:00
// set up a clip that will copy the current url
haste.prototype.configureClip = function() {
var clip = this.clipper = new ZeroClipboard.Client();
this.clipper.setHandCursor(true);
this.clipper.setCSSEffects(false);
// and then set and show
this.clipper.setText(window.location.href);
$('#key .box2 .link').html(this.clipper.getHTML(32, 37));
};
// hide the current clip, if it exists
haste.prototype.removeClip = function() {
if (this.clipper) {
this.clipper.destroy();
}
$('#key .box2 .link').html('');
};
2011-11-23 16:31:50 +00:00
haste.prototype.configureButtons = function() {
var _this = this;
this.buttons = [
{
$where: $('#key .box2 .save'),
label: 'Save',
shortcutDescription: 'control + s',
shortcut: function(evt) {
return evt.ctrlKey && (evt.keyCode === 76 || evt.keyCode === 83);
},
action: function() {
if (_this.$textarea.val().replace(/^\s+|\s+$/g, '') !== '') {
_this.lockDocument();
}
}
},
{
$where: $('#key .box2 .new'),
label: 'New',
shortcut: function(evt) {
return evt.ctrlKey && evt.keyCode === 78
},
shortcutDescription: 'control + n',
action: function() {
_this.newDocument(!_this.doc.key);
}
},
{
$where: $('#key .box2 .duplicate'),
label: 'Duplicate & Edit',
shortcut: function(evt) {
return _this.doc.locked && evt.ctrlKey && evt.keyCode === 68;
},
shortcutDescription: 'control + d',
action: function() {
_this.duplicateDocument();
}
},
{
$where: $('#key .box2 .twitter'),
label: 'Twitter',
shortcut: function(evt) {
return _this.options.twitter && _this.doc.locked && evt.ctrlKey && evt.keyCode == 84;
},
shortcutDescription: 'control + t',
action: function() {
window.open('https://twitter.com/share?url=' + encodeURI(_this.baseUrl + _this.doc.key));
}
},
{
$where: $('#key .box2 .link'),
label: 'Copy URL',
2011-11-27 20:34:09 +00:00
letBubble: true,
action: function() { }
2011-11-23 16:31:50 +00: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();
if ($(this).hasClass('enabled')) {
options.action();
}
});
// Show the label
options.$where.mouseenter(function(evt) {
$('#key .box3 .label').text(options.label);
$('#key .box3 .shortcut').text(options.shortcutDescription || '');
$('#key .box3').show();
});
// Hide the label
options.$where.mouseleave(function(evt) {
$('#key .box3').hide();
});
};
2011-11-18 15:17:41 +00:00
// 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-21 14:56:33 +00:00
$(document.body).keydown(function(evt) {
2011-11-23 16:31:50 +00:00
var button;
for (var i = 0 ; i < _this.buttons.length; i++) {
button = _this.buttons[i];
if (button.shortcut && button.shortcut(evt)) {
2011-11-27 20:34:09 +00:00
if (!button.letBubble) {
evt.preventDefault();
}
2011-11-23 16:31:50 +00:00
button.action();
return;
2011-11-18 22:20:33 +00:00
}
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();
}
}
});
});