haste-server/static/application.min.js

1 line
6.0 KiB
JavaScript
Raw Normal View History

2012-01-23 17:40:09 +01:00
var haste_document=function(){this.locked=!1};haste_document.prototype.htmlEscape=function(a){return a.replace(/&/g,"&amp;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/"/g,"&quot;")},haste_document.prototype.load=function(a,b,c){var d=this;$.ajax("/documents/"+a,{type:"get",dataType:"json",success:function(e){d.locked=!0,d.key=a,d.data=e.data;try{var f;c==="txt"?f={value:d.htmlEscape(e.data)}:c?f=hljs.highlight(c,e.data):f=hljs.highlightAuto(e.data)}catch(g){f=hljs.highlightAuto(e.data)}b({value:f.value,key:a,language:f.language||c,lineCount:e.data.split("\n").length})},error:function(a){b(!1)}})},haste_document.prototype.save=function(a,b){if(this.locked)return!1;this.data=a;var c=this;$.ajax("/documents",{type:"post",data:a,dataType:"json",success:function(d){c.locked=!0,c.key=d.key;var e=hljs.highlightAuto(a);b(null,{value:e.value,key:d.key,language:e.language,lineCount:a.split("\n").length})},error:function(a){try{b($.parseJSON(a.responseText))}catch(c){b({message:"Something went wrong!"})}}})};var haste=function(a,b){this.appName=a,this.$textarea=$("textarea"),this.$box=$("#box"),this.$code=$("#box code"),this.$linenos=$("#linenos"),this.options=b,this.configureShortcuts(),this.configureButtons(),b.twitter||$("#box2 .twitter").hide()};haste.prototype.setTitle=function(a){var b=a?this.appName+" - "+a:this.appName;document.title=b},haste.prototype.showMessage=function(a,b){var c=$('<li class="'+(b||"info")+'">'+a+"</li>");$("#messages").prepend(c),setTimeout(function(){c.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(a){var b,c=0;$("#box2 .function").each(function(){b=$(this);for(c=0;c<a.length;c++)if(b.hasClass(a[c]))return b.addClass("enabled"),!0;b.removeClass("enabled")})},haste.prototype.newDocument=function(a){this.$box.hide(),this.doc=new haste_document,a||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:""},haste.prototype.lookupExtensionByType=function(a){for(var b in haste.extensionMap)if(haste.extensionMap[b]===a)return b;return a},haste.prototype.lookupTypeByExtension=function(a){return haste.extensionMap[a]||a},haste.prototype.addLineNumbers=function(a){var b="";for(var c=0;c<a;c++)b+=(c+1).toString()+"<br/>";$("#linenos").html(b)},haste.prototype.removeLineNumbers=function(){$("#linenos").html("&gt;")},haste.prototype.loadDocument=function(a){var b=a.split(".",2),c=this;c.doc=new haste_document,c.doc.load(b[0],function(a){a?(c.$code.html(a.value),c.setTitle(a.key),c.fullKey(),c.$textarea.val("").hide(),c.$box.show().focus(),c.addLineNumbers(a.lineCount)):c.newDocument()},this.lookupTypeByExtension(b[1]))},haste.prototype.duplicateDocument=function(){if(this.doc.locked){var a=this.doc.data;this.newDocument(),this.$textarea.val(a)}},haste.prototype.lockDocument=function(){var a=this;this.doc.save(this.$textarea.val(),function(b,c){if(b)a.showMessage(b.message,"error");else if(c){a.$code.html(c.value),a.setTitle(c.key);var d="/"+c.key;c.language&&(d+="."+a.lookupExtensionByType(c.language)),window.history.pushState(null,a.appName+"-"+c.key,d),a.fullKey(),a.$textarea.val("").hide(),a.$box.show().focus(),a.addLineNumbers(c.lineCount)}})},haste.prototype.configureButtons=function(){var a=this;this.buttons=[{$where:$("#box2 .save"),label:"Save",shortcutDescription:"control + s",shortcut:function(a){return a.ctrlKey&&a.keyCode===83},action:function(){a.$textarea.val().replace(/^\s+|\s+$/g,"")!==""&&a.lockDocument(