haste-server/static/application.min.js

1 line
2.8 KiB
JavaScript
Raw Normal View History

window.Haste={Models:{},Views:{},Routers:{},extensionMap:{clj:"clojure",coffee:"coffeescript",css:"css",diff:"diff",go:"go",hs:"haskell",html:"htmlmixed",js:"javascript",lua:"lua",md:"markdown",markdown:"markdown",sql:"mysql",pl:"perl",php:"php",py:"python",r:"r",rb:"ruby",scm:"scheme",xml:"xml",yml:"yaml"},init:function(){new Haste.Routers.Document,Backbone.history.start({pushState:!0})}},Haste.Models.Document=Backbone.Model.extend({idAttribute:"key",urlRoot:"/documents"}),Haste.Routers.Document=Backbone.Router.extend({routes:{":id.:extension":"show",":id":"show","":"new"},initialize:function(){this.editor=new Haste.Views.EditorView},show:function(a,b){this.editor.load(a,b)},"new":function(){this.editor.new()}}),Haste.Views.ActionsView=Backbone.View.extend({el:"header",events:{"click .new":"new","click .save":"save","click .edit":"edit","click .raw":"raw","click .twitter":"raw"},initialize:function(){this.parent=this.options.parent},toggleActions:function(){var a="disabled";this.parent.model.isNew()?($(".save",this.el).removeClass(a),$(".edit, .raw, .twitter",this.el).addClass(a)):($(".save",this.el).addClass(a),$(".edit, .raw, .twitter",this.el).removeClass(a)),this.setLink(".raw","raw/"+this.parent.model.id),this.setLink(".twitter","https://twitter.com/share?url="+encodeURI(window.location.href))},setLink:function(a,b){this.parent.model.isNew()&&(b="#"),$(a,this.el).attr("href",b)},"new":function(a){a.preventDefault(),this.parent.new(),Backbone.history.navigate("")},save:function(a){a.preventDefault();if(!this.parent.model.isNew())return;this.parent.save()},edit:function(a){a.preventDefault();if(this.parent.model.isNew())return;this.parent.model.set("key",null),Backbone.history.navigate("/")},raw:function(a){this.model.isNew()&&a.preventDefault()}}),Haste.Views.EditorView=Backbone.View.extend({el:"textarea",initialize:function(){this.codeMirror=CodeMirror.fromTextArea(this.el,{mode:"null",lineNumbers:!0,theme:"solarized-dark"}),this.actionsView=new Haste.Views.ActionsView({parent:this})},render:function(){return this.codeMirror.setOption("mode",this.model.get("mode")||"null"),this.codeMirror.setValue(this.model.get("data")||""),this},"new":function(){this.model=new Haste.Models.Document,this.model.on("change",this.render,this),this.model.on("change",this.toggleLock,this),this.model.on("change",this.actionsView.toggleActions,this.actionsView),this.model.trigger("change")},load:function(a,b){this.new();var c=Haste.extensionMap[b];this.model.set({key:a,mode:c},{silent:!0}),this.model.fetch()},save:function(){var a=this.codeMirror.getValue();if(!a)return;this.model.save("data",a,{success:function(a,b){Backbone.history.navigate(a.id)}})},toggleLock:function(){this.codeMirror.setOption("readOnly",!this.model.isNew()),this.actionsView.toggleActions()}}),$(function(){Haste.init()})