haste-server/static/application.min.js

1 line
6.9 KiB
JavaScript
Raw Normal View History

var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(e){return e.replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;")},haste_document.prototype.load=function(e,t,n){var r=this;$.ajax("/documents/"+e,{type:"get",dataType:"json",success:function(i){r.locked=!0,r.key=e,r.data=i.data;try{var s;n==="txt"?s={value:r.htmlEscape(i.data)}:n?s=hljs.highlight(n,i.data):s=hljs.highlightAuto(i.data)}catch(o){s=hljs.highlightAuto(i.data)}t({value:s.value,key:e,language:s.language||n,lineCount:i.data.split("\n").length})},error:function(e){t(!1)}})},haste_document.prototype.save=function(e,t){if(this.locked)return!1;this.data=e;var n=this;$.ajax("/documents",{type:"post",data:e,dataType:"json",contentType:"application/json; charset=utf-8",success:function(r){n.locked=!0,n.key=r.key;var i=hljs.highlightAuto(e);t(null,{value:i.value,key:r.key,language:i.language,lineCount:e.split("\n").length})},error:function(e){try{t($.parseJSON(e.responseText))}catch(n){t({message:"Something went wrong!"})}}})};var haste=function(e,t){this.appName=e,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.$linenos=$("#linenos"),this.options=t,this.ignoreLast=!1,this.configureShortcuts(),this.configureButtons(),t.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(e){var t=e?this.appName+" - "+e:this.appName;document.title=t},haste.prototype.showMessage=function(e,t){var n=$('<li class="'+(t||"info")+'">'+e+"</li>");$("#messages").prepend(n),setTimeout(function(){n.slideUp("fast",function(){$(this).remove()})},3e3)},haste.prototype.lightKey=function(){this.configureKey(["new","save"])},haste.prototype.fullKey=function(){this.configureKey(["new","duplicate","twitter","raw"])},haste.prototype.configureKey=function(e){var t,n=0;$("#box2 .function").each(function(){t=$(this);for(n=0;n<e.length;n++)if(t.hasClass(e[n]))return t.addClass("enabled"),!0;t.removeClass("enabled")})},haste.prototype.newDocument=function(e){this.$box.hide(),this.doc=new haste_document,e||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",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",json:"javascript"},haste.prototype.lookupExtensionByType=function(e){for(var t in haste.extensionMap)if(haste.extensionMap[t]===e)return t;return e},haste.prototype.lookupTypeByExtension=function(e){return haste.extensionMap[e]||e},haste.prototype.goToLine=function(e){e=e||parseInt(window.location.hash.substring(2)),typeof this.line!="undefined"&&(this.line.removeClass("highlight"),$(".line[rel='L"+e+"']").removeClass("highlight")),this.line=$("#linenos span[rel='#L"+e+"'], .line[rel='L"+e+"']").addClass("highlight"),window.scrollTo(0,this.line.offset().top)},haste.prototype.addLineNumbers=function(e){var t="";for(var n=0;n<e;n++)num=(n+1).toString(),t+='<span rel="#L'+num+'">'+num+"</span><br/>";var r=this;listener=function(e){e.preventDefault(),r.ignoreLast=!0,window.location.hash=$(this).attr("rel"),r.goToLine($(this).attr("rel").substring(2))},$("#linenos").html(t),$("#linenos span").click(listener)},haste.prototype.removeLineNumbers=function(){$("#linenos").html("&gt;")},haste.prototype.setCode=function(e){var t=new Array;$.each(e.split("\n"),function(e,n){var e=e+1;t[e]="<span class='line' rel='L"+e+"'>"+n+"</span>\n"}),this.$code.html(t.join(""))},haste.prototype.loadDocument=function(e){var t=e.split(".",2),n=this;n.doc=new haste_document,n.doc.load(t[0],function(e){e?(n.setCode(e.value),n.setTitle(e.key),n.fullKey(),n.$textarea.val("").hide(),n.$box.show().focus(),n.addLineNumbers(e.lineCount),typeof window.location.hash!