haste-server/static/application.js

172 lines
3.7 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: true });
}
2011-11-18 16:17:41 +01:00
};
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(id, extension) {
this.editor.load(id, extension);
},
2011-11-18 22:22:00 +01:00
new: function() {
this.editor.new();
2011-11-18 16:17:41 +01:00
}
});
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 klass = 'disabled';
if (this.parent.model.isNew()) {
$('.save', this.el).removeClass(klass);
$('.edit, .raw, .twitter', this.el).addClass(klass);
} else {
$('.save', this.el).addClass(klass);
$('.edit, .raw, .twitter', this.el).removeClass(klass);
2011-11-18 16:49:00 +01:00
}
2011-11-18 16:17:41 +01:00
this.setLink('.raw', 'raw/' + this.parent.model.id);
this.setLink('.twitter', 'https://twitter.com/share?url=' + encodeURI(window.location.href));
},
2011-11-18 16:17:41 +01:00
setLink: function(el, href) {
if (this.parent.model.isNew()) {
href = '#';
}
2011-11-18 16:17:41 +01:00
$(el, this.el).attr('href', href);
},
2011-12-19 18:44:12 +01:00
new: function(event) {
event.preventDefault();
this.parent.new();
Backbone.history.navigate('');
},
2011-11-19 06:30:14 +01:00
save: function(event) {
event.preventDefault();
2011-11-23 17:31:50 +01:00
if (!this.parent.model.isNew()) { return; }
this.parent.save();
},
edit: function(event) {
event.preventDefault();
if (this.parent.model.isNew()) { return; }
this.parent.model.set('key', null);
Backbone.history.navigate('/');
},
raw: function(event) {
if (this.model.isNew()) {
event.preventDefault();
2011-11-23 17:31:50 +01:00
}
},
});
2011-11-19 06:30:14 +01:00
Haste.Views.EditorView = Backbone.View.extend({
el: 'textarea',
2011-11-18 16:17:41 +01:00
initialize: function() {
this.codeMirror = CodeMirror.fromTextArea(this.el, {
mode: 'null',
lineNumbers: true,
theme: 'solarized-dark'
});
2011-11-23 02:29:46 +01:00
this.actionsView = new Haste.Views.ActionsView({ parent: this });
},
2011-11-29 16:39:12 +01:00
render: function() {
this.codeMirror.setOption('mode', this.model.get('mode') || 'null');
this.codeMirror.setValue(this.model.get('data') || '');
2011-11-23 02:29:46 +01:00
return 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);
2011-11-18 22:22:00 +01:00
this.model.trigger('change');
},
2011-11-18 21:18:38 +01:00
load: function(key, extension) {
this.new();
2011-11-18 16:17:41 +01:00
var mode = Haste.extensionMap[extension];
this.model.set({ key: key, mode: mode }, { silent: true });
2011-11-23 17:31:50 +01:00
this.model.fetch();
},
2011-11-23 17:31:50 +01:00
save: function() {
var data = this.codeMirror.getValue();
2011-11-18 16:17:41 +01:00
if (!data) { return; }
2011-11-18 16:17:41 +01:00
this.model.save('data', data, {
success: function(model, response) {
Backbone.history.navigate(model.id);
2011-11-18 16:17:41 +01:00
}
});
},
2011-11-18 16:17:41 +01:00
toggleLock: function() {
this.codeMirror.setOption('readOnly', !this.model.isNew());
this.actionsView.toggleActions();
}
});
$(function() {
Haste.init();
2011-11-18 16:17:41 +01:00
});