-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.js
436 lines (411 loc) · 11.1 KB
/
application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* global $, hljs, window, document */
///// represents a single document
var haste_document = function () {
this.locked = false;
};
// Escapes HTML tag characters
haste_document.prototype.htmlEscape = function (s) {
return s
.replace(/&/g, "&")
.replace(/>/g, ">")
.replace(/</g, "<")
.replace(/"/g, """);
};
// Get this document from the server and lock it here
haste_document.prototype.load = function (key, callback, lang) {
var _this = this;
$.ajax("/documents/" + key, {
type: "get",
dataType: "json",
success: function (res) {
_this.locked = true;
_this.key = key;
_this.data = res.data;
try {
var high;
if (lang === "txt") {
high = { value: _this.htmlEscape(res.data) };
} else if (lang) {
high = hljs.highlight(lang, res.data);
} else {
high = hljs.highlightAuto(res.data);
}
} catch (err) {
// failed highlight, fall back on auto
high = hljs.highlightAuto(res.data);
}
callback({
value: high.value,
key: key,
language: high.language || lang,
lineCount: res.data.split("\n").length,
});
},
error: function () {
callback(false);
},
});
};
// Save this document to the server and lock it here
haste_document.prototype.save = function (data, callback) {
if (this.locked) {
return false;
}
this.data = data;
var _this = this;
$.ajax("/documents", {
type: "post",
data: data,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (res) {
_this.locked = true;
_this.key = res.key;
var high = hljs.highlightAuto(data);
callback(null, {
value: high.value,
key: res.key,
language: high.language,
lineCount: data.split("\n").length,
});
},
error: function (res) {
try {
callback($.parseJSON(res.responseText));
} catch (e) {
callback({ message: "Something went wrong!" });
}
},
});
};
///// represents the paste application
var haste = function (appName, options) {
this.appName = appName;
this.$textarea = $("textarea");
this.$box = $("#box");
this.$code = $("#box code");
this.$linenos = $("#linenos");
this.options = options;
this.configureShortcuts();
this.configureButtons();
// If twitter is disabled, hide the button
if (!options.twitter) {
$("#box2 .twitter").hide();
}
};
// Set the page title - include the appName
haste.prototype.setTitle = function (ext) {
var title = ext ? this.appName + " - " + ext : this.appName;
document.title = title;
};
// 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);
};
// Show the light key
haste.prototype.lightKey = function () {
this.configureKey(["new", "save"]);
};
// Show the full key
haste.prototype.fullKey = function () {
this.configureKey(["new", "duplicate", "twitter", "raw"]);
};
// Set the key up for certain things to be enabled
haste.prototype.configureKey = function (enable) {
var $this,
i = 0;
$("#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");
});
};
// Remove the current document (if there is one)
// and set up for a new one
haste.prototype.newDocument = function (hideHistory) {
this.$box.hide();
this.doc = new haste_document();
if (!hideHistory) {
window.history.pushState(null, this.appName, "/");
}
this.setTitle();
this.lightKey();
this.$textarea.val("").show("fast", function () {
this.focus();
});
this.removeLineNumbers();
};
// Map of common extensions
// 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
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",
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",
json: "javascript",
swift: "swift",
};
// 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;
};
// 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(">");
};
// Load a document and show it
haste.prototype.loadDocument = function (key) {
// Split the key up
var parts = key.split(".", 2);
// Ask for what we want
var _this = this;
_this.doc = new haste_document();
_this.doc.load(
parts[0],
function (ret) {
if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
_this.fullKey();
_this.$textarea.val("").hide();
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
} else {
_this.newDocument();
}
},
this.lookupTypeByExtension(parts[1])
);
};
// Duplicate the current document - only if locked
haste.prototype.duplicateDocument = function () {
if (this.doc.locked) {
var currentData = this.doc.data;
this.newDocument();
this.$textarea.val(currentData);
}
};
// Lock the current document
haste.prototype.lockDocument = function () {
var _this = this;
this.doc.save(this.$textarea.val(), function (err, ret) {
if (err) {
_this.showMessage(err.message, "error");
} else if (ret) {
console.log("RET: " + ret.success);
_this.$code.html(ret.value);
_this.setTitle(ret.key);
var file = "/" + ret.key;
if (ret.language) {
file += "." + _this.lookupExtensionByType(ret.language);
}
window.history.pushState(null, _this.appName + "-" + ret.key, file);
_this.fullKey();
_this.$textarea.val("").hide();
_this.$box.show().focus();
_this.addLineNumbers(ret.lineCount);
}
});
};
haste.prototype.configureButtons = function () {
var _this = this;
this.buttons = [
{
$where: $("#box2 .save"),
label: "Save",
shortcutDescription: "control + s",
shortcut: function (evt) {
return evt.ctrlKey && evt.keyCode === 83;
},
action: function () {
if (_this.$textarea.val().replace(/^\s+|\s+$/g, "") !== "") {
_this.lockDocument();
}
},
},
{
$where: $("#box2 .new"),
label: "New",
shortcut: function (evt) {
return evt.ctrlKey && evt.keyCode === 78;
},
shortcutDescription: "control + n",
action: function () {
_this.newDocument(!_this.doc.key);
},
},
{
$where: $("#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: $("#box2 .raw"),
label: "Just Text",
shortcut: function (evt) {
return evt.ctrlKey && evt.shiftKey && evt.keyCode === 82;
},
shortcutDescription: "control + shift + r",
action: function () {
window.location.href = "/raw/" + _this.doc.key;
},
},
{
$where: $("#box2 .twitter"),
label: "Twitter",
shortcut: function (evt) {
return (
_this.options.twitter &&
_this.doc.locked &&
evt.shiftKey &&
evt.ctrlKey &&
evt.keyCode == 84
);
},
shortcutDescription: "control + shift + t",
action: function () {
window.open(
"https://twitter.com/share?url=" + encodeURI(window.location.href)
);
},
},
];
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 (!options.clickDisabled && $(this).hasClass("enabled")) {
options.action();
}
});
// Show the label
options.$where.mouseenter(function () {
$("#box3 .label").text(options.label);
$("#box3 .shortcut").text(options.shortcutDescription || "");
$("#box3").show();
$(this).append($("#pointer").remove().show());
});
// Hide the label
options.$where.mouseleave(function () {
$("#box3").hide();
$("#pointer").hide();
});
};
// Configure keyboard shortcuts for the textarea
haste.prototype.configureShortcuts = function () {
var _this = this;
$(document.body).keydown(function (evt) {
var button;
for (var i = 0; i < _this.buttons.length; i++) {
button = _this.buttons[i];
if (button.shortcut && button.shortcut(evt)) {
evt.preventDefault();
button.action();
return;
}
}
});
};
///// Tab behavior in the textarea - 2 spaces per tab
$(function () {
$("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();
var 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();
}
}
});
});