1 line
5.8 KiB
JavaScript
1 line
5.8 KiB
JavaScript
var haste_document=function(){this.locked=!1},haste=(haste_document.prototype.htmlEscape=function(t){return t.replace(/&/g,"&").replace(/>/g,">").replace(/</g,"<").replace(/"/g,""")},haste_document.prototype.load=function(t,n,s){var i=this;$.ajax("/documents/"+t,{type:"get",dataType:"json",success:function(e){i.locked=!0,i.key=t,i.data=e.data;try{var o="txt"===s?{value:i.htmlEscape(e.data)}:s?hljs.highlight(s,e.data):hljs.highlightAuto(e.data)}catch(t){o=hljs.highlightAuto(e.data)}n({value:o.value,key:t,language:o.language||s,lineCount:e.data.split("\n").length})},error:function(){n(!1)}})},haste_document.prototype.save=function(o,n){if(this.locked)return!1;this.data=o;var s=this;$.ajax("/documents",{type:"post",data:o,dataType:"json",contentType:"application/json; charset=utf-8",success:function(t){s.locked=!0,s.key=t.key;var e=hljs.highlightAuto(o);n(null,{value:e.value,key:t.key,language:e.language,lineCount:o.split("\n").length})},error:function(t){try{n($.parseJSON(t.responseText))}catch(t){n({message:"Something went wrong!"})}}})},function(t,e){this.appName=t,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.$linenos=$("#linenos"),this.options=e,this.configureShortcuts(),this.configureButtons(),e.twitter||$("#box2 .twitter").hide()});haste.prototype.setTitle=function(t){t=t?this.appName+" - "+t:this.appName;document.title=t},haste.prototype.showMessage=function(t,e){var o=$('<li class="'+(e||"info")+'">'+t+"</li>");$("#messages").prepend(o),setTimeout(function(){o.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","save","duplicate","raw"])},haste.prototype.configureKey=function(t){var e,o=0;$("#box2 .function").each(function(){for(e=$(this),o=0;o<t.length;o++)if(e.hasClass(t[o]))return e.addClass("enabled"),!0;e.removeClass("enabled")})},haste.prototype.newDocument=function(t){this.$box.hide(),this.doc=new haste_document,t||window.history.pushState(null,this.appName,"/"),this.setTitle(),this.lightKey(),this.$textarea.val("").show("fast",function(){this.focus()}),this.removeLineNumbers()},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"},haste.prototype.lookupExtensionByType=function(t){for(var e in haste.extensionMap)if(haste.extensionMap[e]===t)return e;return t},haste.prototype.lookupTypeByExtension=function(t){return haste.extensionMap[t]||t},haste.prototype.addLineNumbers=function(t){for(var e="",o=0;o<t;o++)e+=(o+1).toString()+"<br/>";$("#linenos").html(e)},haste.prototype.removeLineNumbers=function(){$("#linenos").html(">")},haste.prototype.loadDocument=function(t){var t=t.split(".",2),e=this;e.doc=new haste_document,e.doc.load(t[0],function(t){t?(e.$code.html(t.value),e.setTitle(t.key),e.fullKey(),e.$textarea.val("").hide(),e.$box.show().focus(),e.addLineNumbers(t.lineCount)):e.newDocument()},this.lookupTypeByExtension(t[1]))},haste.prototype.duplicateDocument=function(){var t;this.doc.locked&&(t=this.doc.data,this.newDocument(),this.$textarea.val(t))},haste.prototype.lockDocument=function(){var o=this;this.doc.save(this.$textarea.val(),function(t,e){t?o.showMessage(t.message,"error"):e&&(o.$code.html(e.value),o.setTitle(e.key),t="/"+e.key,e.language&&(t+="."+o.lookupExtensionByType(e.language)),window.history.pushState(null,o.appName+"-"+e.key,t),o.fullKey(),o.$textarea.val("").hide(),o.$box.show().focus(),o.addLineNumbers(e.lineCount))})},haste.prototype.configureButtons=function(){var e=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcut:function(t){return t.ctrlKey&&83===t.keyCode},shortcutDescription:"control + s",action:function(){e.doc.locked?e.duplicate():e.lockDocument()}},{$where:$("#box2 .new"),label:"New",shortcut:function(t){return t.ctrlKey&&78===t.keyCode},shortcutDescription:"control + n",action:function(){e.newDocument(!e.doc.key)}},{$where:$("#box2 .duplicate"),label:"Duplicate & Edit",shortcut:function(t){return e.doc.locked&&t.ctrlKey&&68===t.keyCode},shortcutDescription:"control + d",action:function(){e.duplicate()}},{$where:$("#box2 .raw"),label:"Just Text",shortcut:function(t){return t.ctrlKey&&t.shiftKey&&82===t.keyCode},shortcutDescription:"control + shift + r",action:function(){window.location.href="/raw/"+e.doc.key}}];for(var t=0;t<this.buttons.length;t++)this.configureButton(this.buttons[t])},haste.prototype.configureButton=function(e){e.$where.click(function(t){t.preventDefault(),!e.clickDisabled&&$(this).hasClass("enabled")&&e.action()}),e.$where.mouseenter(function(){$("#box3 .label").text(e.label),$("#box3 .shortcut").text(e.shortcutDescription||""),$("#box3").show(),$(this).append($("#pointer").remove().show())}),e.$where.mouseleave(function(){$("#box3").hide(),$("#pointer").hide()})},haste.prototype.configureShortcuts=function(){var n=this;$(document.body).keydown(function(t){for(var e,o=0;o<n.buttons.length;o++)if((e=n.buttons[o]).shortcut&&e.shortcut(t))return t.preventDefault(),void e.action()})},$(function(){$("textarea").keydown(function(t){var e,o,n;9===t.keyCode&&(t.preventDefault(),t=" ",document.selection?(this.focus(),document.selection.createRange().text=t,this.focus()):this.selectionStart||"0"==this.selectionStart?(e=this.selectionStart,o=this.selectionEnd,n=this.scrollTop,this.value=this.value.substring(0,e)+t+this.value.substring(o,this.value.length),this.focus(),this.selectionStart=e+t.length,this.selectionEnd=e+t.length,this.scrollTop=n):(this.value+=t,this.focus()))})}); |