2011-11-06 08:12:05 -08:00
|
|
|
(function(Snipt) {
|
|
|
|
|
2012-02-24 17:45:00 -08:00
|
|
|
Snipt.SniptModel = Backbone.Model.extend({
|
2012-04-07 20:01:54 -07:00
|
|
|
toSafe: function() {
|
|
|
|
var snipt = this.toJSON();
|
|
|
|
snipt.code = this.escape('code');
|
|
|
|
snipt.title = this.escape('title');
|
|
|
|
snipt.tags_list = this.escape('tags_list');
|
|
|
|
|
|
|
|
if (typeof snipt.tags === 'object') {
|
2012-07-07 09:12:40 -07:00
|
|
|
for (var i; i < snipt.tags.length; i++) {
|
|
|
|
snipt.tags[i].name = _.escape(snipt.tags[i].name);
|
2012-04-07 20:01:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return snipt;
|
|
|
|
}
|
2011-11-06 15:04:03 -08:00
|
|
|
});
|
2012-02-24 17:45:00 -08:00
|
|
|
Snipt.SniptView = Backbone.View.extend({
|
2012-01-15 16:49:53 -08:00
|
|
|
|
2012-02-15 15:25:13 -08:00
|
|
|
tagName: 'article',
|
2012-01-15 16:49:53 -08:00
|
|
|
|
2012-02-15 15:25:13 -08:00
|
|
|
initialize: function() {
|
2011-11-06 15:04:03 -08:00
|
|
|
this.model.view = this;
|
2012-02-15 15:25:13 -08:00
|
|
|
this.model.bind('change', this.render, this);
|
2012-02-15 20:06:11 -08:00
|
|
|
|
|
|
|
this.template = _.template($('#snipt').html());
|
|
|
|
this.editTemplate = _.template($('#edit').html());
|
2011-11-10 13:04:16 -08:00
|
|
|
|
2012-02-18 11:42:30 -08:00
|
|
|
this.initLocalVars();
|
2012-07-30 18:46:52 -07:00
|
|
|
this.initLineNumbers();
|
2011-11-06 15:04:03 -08:00
|
|
|
},
|
|
|
|
events: {
|
2012-01-25 16:39:38 -08:00
|
|
|
'click a.copy': 'copyFromClick',
|
2012-02-15 20:06:11 -08:00
|
|
|
'click a.edit': 'edit',
|
2012-04-12 19:04:37 -07:00
|
|
|
'click a.favorite': 'favoriteToggle',
|
2012-02-20 10:45:04 -08:00
|
|
|
'click a.embed': 'embedFromClick',
|
2012-01-15 16:49:53 -08:00
|
|
|
'click a.expand': 'expand',
|
2012-01-16 18:17:58 -08:00
|
|
|
'click .container': 'selectFromClick',
|
2012-04-07 16:11:05 -07:00
|
|
|
'copyClose': 'copyClose',
|
2012-01-30 08:01:10 -08:00
|
|
|
'copyRaw': 'copy',
|
2012-01-15 16:49:53 -08:00
|
|
|
'detail': 'detail',
|
2012-01-15 17:08:19 -08:00
|
|
|
'deselect': 'deselect',
|
2012-05-11 20:09:59 -07:00
|
|
|
'destroy': 'destroy',
|
2012-02-15 20:06:11 -08:00
|
|
|
'edit': 'edit',
|
2012-01-25 16:39:38 -08:00
|
|
|
'embed': 'embed',
|
2012-04-07 16:11:05 -07:00
|
|
|
'embedClose': 'embedClose',
|
2012-01-15 16:49:53 -08:00
|
|
|
'expand': 'expand',
|
2012-04-05 14:33:47 -07:00
|
|
|
'fadeAndRemove': 'fadeAndRemove',
|
2012-05-11 20:09:59 -07:00
|
|
|
'goToAuthor': 'goToAuthor',
|
2012-01-15 16:49:53 -08:00
|
|
|
'next': 'next',
|
|
|
|
'prev': 'prev',
|
2012-02-18 11:42:30 -08:00
|
|
|
'selectSnipt': 'select'
|
2011-11-06 15:04:03 -08:00
|
|
|
},
|
2012-01-15 16:49:53 -08:00
|
|
|
|
2011-12-04 16:45:31 -08:00
|
|
|
copy: function() {
|
2012-04-05 14:33:47 -07:00
|
|
|
$('textarea', this.$copyModal).remove();
|
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
window.ui_halted = true;
|
2012-02-26 14:57:02 -08:00
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
this.$copyModalBody.append('<textarea class="raw"></textarea>');
|
|
|
|
$textarea = $('textarea.raw', this.$copyModalBody).val(this.model.get('code'));
|
2012-01-25 16:39:38 -08:00
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
this.$copyModal.modal('show');
|
|
|
|
$textarea.select();
|
2012-01-25 16:39:38 -08:00
|
|
|
},
|
2012-04-07 16:11:05 -07:00
|
|
|
copyClose: function() {
|
|
|
|
$('textarea', this.$copyModal).remove();
|
|
|
|
},
|
2012-01-25 16:39:38 -08:00
|
|
|
copyFromClick: function() {
|
|
|
|
this.copy();
|
2011-12-04 16:45:31 -08:00
|
|
|
return false;
|
2012-01-15 11:51:33 -08:00
|
|
|
},
|
2012-01-15 17:08:19 -08:00
|
|
|
deselect: function() {
|
2012-02-20 10:45:04 -08:00
|
|
|
this.$el.removeClass('selected');
|
|
|
|
window.$selected = false;
|
2012-01-15 17:08:19 -08:00
|
|
|
},
|
2012-01-15 11:51:33 -08:00
|
|
|
detail: function() {
|
2012-04-08 17:16:02 -07:00
|
|
|
window.location = this.model.get('absolute_url');
|
2012-01-15 16:49:53 -08:00
|
|
|
},
|
2012-05-11 20:09:59 -07:00
|
|
|
destroy: function() {
|
|
|
|
this.model.destroy();
|
|
|
|
},
|
2012-02-15 20:06:11 -08:00
|
|
|
edit: function() {
|
2012-03-11 20:50:06 -07:00
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
window.editing = true;
|
|
|
|
window.ui_halted = true;
|
|
|
|
|
|
|
|
this.select();
|
|
|
|
|
2012-04-05 14:33:47 -07:00
|
|
|
that = this;
|
2012-04-07 20:01:54 -07:00
|
|
|
var editPane = this.editTemplate({snipt: this.model.toSafe()});
|
2012-03-08 05:59:15 -08:00
|
|
|
|
2012-03-12 17:18:44 -07:00
|
|
|
// Init main view
|
2012-03-08 05:59:15 -08:00
|
|
|
window.site.$main.hide();
|
|
|
|
window.site.$body.addClass('detail editing');
|
2012-03-11 20:50:06 -07:00
|
|
|
window.site.$main_edit.html(editPane);
|
|
|
|
|
2012-03-12 17:18:44 -07:00
|
|
|
// Select lexer
|
|
|
|
$('option[value="' + this.model.get('lexer') + '"]', window.site.$main_edit).attr('selected', 'selected');
|
|
|
|
|
|
|
|
// Init chosen
|
2012-07-20 18:54:54 -07:00
|
|
|
var $selectLexer = $('select#id_lexer', window.site.$main_edit);
|
|
|
|
$selectLexer.chosen();
|
2012-03-11 20:50:06 -07:00
|
|
|
|
2012-05-24 19:20:06 -07:00
|
|
|
// Blog post
|
|
|
|
$('label.blog-post input', window.site.$main_edit).on('change', function() {
|
|
|
|
var $checkbox = $(this);
|
|
|
|
var $label = $checkbox.parent();
|
2012-05-27 11:51:21 -07:00
|
|
|
var $publish_date = $label.siblings('label.publish-date');
|
2012-05-24 19:20:06 -07:00
|
|
|
|
|
|
|
if ($checkbox.attr('checked')) {
|
|
|
|
$label.removeClass('is-not-blog-post').addClass('is-blog-post');
|
2012-05-27 11:51:21 -07:00
|
|
|
$publish_date.show();
|
2012-05-24 19:20:06 -07:00
|
|
|
} else {
|
|
|
|
$label.addClass('is-not-blog-post').removeClass('is-blog-post');
|
2012-05-27 11:51:21 -07:00
|
|
|
$publish_date.hide();
|
2012-05-24 19:20:06 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}).trigger('change');
|
|
|
|
|
2012-03-12 20:48:01 -07:00
|
|
|
// Public / private
|
|
|
|
$('label.public input', window.site.$main_edit).on('change', function() {
|
|
|
|
var $checkbox = $(this);
|
|
|
|
var $label = $checkbox.parent();
|
|
|
|
|
|
|
|
if ($checkbox.attr('checked')) {
|
|
|
|
$label.removeClass('is-private').addClass('is-public');
|
|
|
|
} else {
|
|
|
|
$label.addClass('is-private').removeClass('is-public');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}).trigger('change');
|
|
|
|
|
2012-03-11 20:50:06 -07:00
|
|
|
window.site.$main_edit.show();
|
2012-03-08 05:59:15 -08:00
|
|
|
|
2012-07-20 17:43:16 -07:00
|
|
|
// CodeMirror editor
|
|
|
|
var $editor = $('div#editor', window.site.$main_edit);
|
|
|
|
|
|
|
|
window.editor = CodeMirror($editor.get(0), {
|
2012-07-20 18:54:54 -07:00
|
|
|
autofocus: true,
|
|
|
|
fixedGutter: true,
|
|
|
|
gutter: true,
|
|
|
|
indentUnit: 4,
|
|
|
|
lineNumbers: true,
|
|
|
|
matchBrackets: true,
|
|
|
|
mode: that.guessCodeMirrorLexer($selectLexer.val()),
|
2012-07-20 17:43:16 -07:00
|
|
|
value: that.model.get('code')
|
|
|
|
});
|
2012-07-20 18:54:54 -07:00
|
|
|
$selectLexer.change(function() {
|
|
|
|
var $selectedLexer = $('option:selected', $selectLexer);
|
|
|
|
window.editor.setOption('mode', that.guessCodeMirrorLexer($selectedLexer.val()));
|
|
|
|
});
|
2012-07-20 17:43:16 -07:00
|
|
|
|
2012-07-20 18:54:54 -07:00
|
|
|
window.editor.setSize('100%', $(window).height() - 147);
|
2012-07-20 17:43:16 -07:00
|
|
|
|
2012-03-11 20:50:06 -07:00
|
|
|
$('textarea, input', window.site.$main_edit).bind('keydown', 'esc', function(e) {
|
2012-03-08 05:59:15 -08:00
|
|
|
$(this).blur();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2012-03-12 17:18:44 -07:00
|
|
|
// Edit buttons
|
2012-04-05 14:33:47 -07:00
|
|
|
$('button.delete', window.site.$main_edit).on('click', function() {
|
2012-03-11 20:50:06 -07:00
|
|
|
if (confirm('Are you sure you want to delete this snipt?')) {
|
|
|
|
that.model.destroy();
|
2012-05-03 17:45:21 -07:00
|
|
|
window.site.snipt_list.escapeUI(true);
|
2012-03-11 20:50:06 -07:00
|
|
|
}
|
2012-04-05 14:33:47 -07:00
|
|
|
return false;
|
2012-03-11 20:50:06 -07:00
|
|
|
});
|
2012-04-05 14:33:47 -07:00
|
|
|
$('button.cancel', window.site.$main_edit).on('click', function() {
|
2012-03-11 20:50:06 -07:00
|
|
|
window.site.snipt_list.escapeUI();
|
|
|
|
return false;
|
|
|
|
});
|
2012-04-05 14:33:47 -07:00
|
|
|
$('button.save', window.site.$main_edit).on('click', function() {
|
2012-05-03 19:46:40 -07:00
|
|
|
$('button.cancel').text('Close');
|
|
|
|
that.save();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
$('button.save-and-close', window.site.$main_edit).on('click', function() {
|
2012-04-05 14:33:47 -07:00
|
|
|
that.save();
|
2012-03-20 20:38:47 -07:00
|
|
|
window.site.snipt_list.escapeUI();
|
2012-04-05 14:33:47 -07:00
|
|
|
return false;
|
2012-03-11 20:50:06 -07:00
|
|
|
});
|
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
window.scrollTo(0, 0);
|
|
|
|
|
2012-02-15 20:06:11 -08:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
embed: function() {
|
2012-04-05 14:33:47 -07:00
|
|
|
$('textarea', this.$embedModal).remove();
|
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
window.ui_halted = true;
|
2012-02-26 14:57:02 -08:00
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
this.$embedModalBody.append('<textarea class="raw"></textarea>');
|
|
|
|
$textarea = $('textarea.raw', this.$embedModalBody).val('<script type="text/javascript" src="' + this.model.get('embed_url') + '"></script>');
|
2012-02-20 10:45:04 -08:00
|
|
|
|
2012-03-08 05:59:15 -08:00
|
|
|
this.$embedModal.modal('show');
|
|
|
|
$textarea.select();
|
2012-02-20 10:45:04 -08:00
|
|
|
},
|
|
|
|
embedFromClick: function() {
|
|
|
|
this.embed();
|
2012-02-17 10:14:57 -08:00
|
|
|
return false;
|
2012-02-15 20:06:11 -08:00
|
|
|
},
|
2012-04-07 16:11:05 -07:00
|
|
|
embedClose: function() {
|
|
|
|
$('textarea', this.$embedModal).remove();
|
|
|
|
},
|
2012-01-15 16:49:53 -08:00
|
|
|
expand: function() {
|
|
|
|
this.$container.toggleClass('expanded', 100);
|
|
|
|
this.$tags.toggleClass('expanded');
|
2012-01-16 18:17:58 -08:00
|
|
|
this.select();
|
2012-02-17 10:14:57 -08:00
|
|
|
return false;
|
2012-01-25 16:39:38 -08:00
|
|
|
},
|
2012-04-05 14:33:47 -07:00
|
|
|
fadeAndRemove: function() {
|
2012-05-11 20:18:54 -07:00
|
|
|
|
|
|
|
var $toRemove = $(this.el);
|
|
|
|
var $nextSnipt = $toRemove.next('article.snipt');
|
|
|
|
|
2012-04-05 14:33:47 -07:00
|
|
|
window.$selected = false;
|
2012-05-11 20:18:54 -07:00
|
|
|
$toRemove.fadeOut('fast', function() {
|
2012-04-05 14:33:47 -07:00
|
|
|
$(this).remove();
|
2012-05-11 20:18:54 -07:00
|
|
|
$nextSnipt.trigger('selectSnipt');
|
2012-04-05 14:33:47 -07:00
|
|
|
});
|
2012-05-11 20:18:54 -07:00
|
|
|
|
2012-04-05 14:33:47 -07:00
|
|
|
return false;
|
|
|
|
},
|
2012-05-11 20:09:59 -07:00
|
|
|
goToAuthor: function() {
|
|
|
|
window.location = this.model.get('user').absolute_url;
|
|
|
|
},
|
2012-07-20 18:54:54 -07:00
|
|
|
guessCodeMirrorLexer: function(val) {
|
|
|
|
if (val === 'html') { return 'htmlmixed'; }
|
|
|
|
if (val === 'js') return 'javascript';
|
|
|
|
if (val === 'rb') return 'ruby';
|
|
|
|
return val;
|
|
|
|
},
|
2012-04-12 19:04:37 -07:00
|
|
|
favoriteToggle: function() {
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
if (this.$el.hasClass('favorited')) {
|
2012-04-23 19:09:40 -07:00
|
|
|
$.ajax('/api/private/favorite/' + this.model.get('favorite_id') + '/', {
|
2012-04-12 19:04:37 -07:00
|
|
|
type: 'delete',
|
|
|
|
success: function() {
|
|
|
|
that.$el.removeClass('favorited');
|
2012-04-12 19:14:22 -07:00
|
|
|
that.$favorite.text('Favorite');
|
2012-04-12 19:04:37 -07:00
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'Authorization': 'ApiKey ' + window.user + ':' + window.api_key
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$.ajax('/api/private/favorite/', {
|
|
|
|
data: '{"snipt": ' + this.model.get('id') + '}',
|
|
|
|
contentType: 'application/json',
|
|
|
|
type: 'post',
|
|
|
|
success: function(resp) {
|
|
|
|
that.$el.addClass('favorited');
|
2012-07-07 09:12:40 -07:00
|
|
|
that.model.set({'favorite_id': resp.id}, {'silent': true});
|
2012-04-12 19:14:22 -07:00
|
|
|
that.$favorite.text('Favorited');
|
2012-04-12 19:04:37 -07:00
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'Authorization': 'ApiKey ' + window.user + ':' + window.api_key
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-12 14:28:30 -07:00
|
|
|
},
|
2012-07-30 18:46:52 -07:00
|
|
|
initLineNumbers: function() {
|
|
|
|
var lines = $('span.special', this.$el);
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
$.each(lines, function() {
|
|
|
|
var l = $(this);
|
|
|
|
var num = l.text().trim();
|
|
|
|
var url = that.model.get('absolute_url');
|
|
|
|
l.replaceWith('<a href="' + url + '#line-' + num + '">' + num + '</a>');
|
|
|
|
});
|
|
|
|
},
|
2012-02-18 11:42:30 -08:00
|
|
|
initLocalVars: function() {
|
|
|
|
this.$aside = $('aside', this.$el);
|
|
|
|
this.$container = $('div.container', this.$el);
|
2012-02-20 10:45:04 -08:00
|
|
|
|
2012-02-18 11:42:30 -08:00
|
|
|
this.$copyModal = $('div.copy-modal', this.$el);
|
|
|
|
this.$copyModalBody = $('div.modal-body', this.$copyModal);
|
2012-02-20 10:45:04 -08:00
|
|
|
this.$embedModal = $('div.embed-modal', this.$el);
|
|
|
|
this.$embedModalBody = $('div.modal-body', this.$embedModal);
|
2012-04-12 14:28:30 -07:00
|
|
|
this.$favorite = $('a.favorite', this.$el);
|
2012-02-20 10:45:04 -08:00
|
|
|
|
2012-02-18 11:42:30 -08:00
|
|
|
this.$h1 = $('header h1 a', this.$el);
|
|
|
|
this.$tags = $('section.tags ul', this.$aside);
|
|
|
|
|
|
|
|
this.$copyModal.on('hidden', function(e) {
|
2012-04-07 16:11:05 -07:00
|
|
|
$(this).parent().trigger('copyClose');
|
2012-02-26 14:57:02 -08:00
|
|
|
window.ui_halted = false;
|
|
|
|
window.from_modal = true;
|
2012-02-18 11:42:30 -08:00
|
|
|
});
|
2012-02-20 10:45:04 -08:00
|
|
|
this.$embedModal.on('hidden', function(e) {
|
2012-04-07 16:11:05 -07:00
|
|
|
$(this).parent().trigger('embedClose');
|
2012-02-26 14:57:02 -08:00
|
|
|
window.ui_halted = false;
|
|
|
|
window.from_modal = true;
|
2012-02-20 10:45:04 -08:00
|
|
|
});
|
2012-02-18 11:42:30 -08:00
|
|
|
},
|
2012-01-15 11:51:33 -08:00
|
|
|
next: function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
2012-02-20 10:45:04 -08:00
|
|
|
nextSnipt = this.$el.next('article.snipt');
|
|
|
|
if (nextSnipt.length) {
|
|
|
|
return nextSnipt.trigger('selectSnipt');
|
|
|
|
}
|
2012-01-15 11:51:33 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
prev: function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
2012-02-20 10:45:04 -08:00
|
|
|
prevSnipt = this.$el.prev('article.snipt');
|
|
|
|
if (prevSnipt.length) {
|
|
|
|
return prevSnipt.trigger('selectSnipt');
|
|
|
|
}
|
2012-01-15 11:51:33 -08:00
|
|
|
}
|
2012-01-15 16:49:53 -08:00
|
|
|
},
|
2012-02-15 15:25:13 -08:00
|
|
|
remove: function() {
|
2012-04-05 14:33:47 -07:00
|
|
|
return false;
|
2012-02-15 15:25:13 -08:00
|
|
|
},
|
|
|
|
render: function() {
|
2012-04-07 19:38:26 -07:00
|
|
|
|
2012-04-07 20:01:54 -07:00
|
|
|
this.$el.html(this.template({snipt: this.model.toSafe()}));
|
2012-02-18 11:42:30 -08:00
|
|
|
this.initLocalVars();
|
2012-07-30 18:46:52 -07:00
|
|
|
this.initLineNumbers();
|
2012-02-18 11:42:30 -08:00
|
|
|
|
2012-05-24 19:20:06 -07:00
|
|
|
if (this.model.get('blog_post') === true) {
|
|
|
|
this.$el.addClass('blog-post');
|
|
|
|
} else {
|
|
|
|
this.$el.removeClass('blog-post');
|
|
|
|
}
|
|
|
|
|
2012-04-07 16:11:05 -07:00
|
|
|
if (this.model.get('public') === true) {
|
2012-02-18 11:42:30 -08:00
|
|
|
this.$el.removeClass('private-snipt');
|
|
|
|
} else {
|
|
|
|
this.$el.addClass('private-snipt');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.model.get('user').username === window.user) {
|
|
|
|
this.$el.addClass('editable');
|
|
|
|
} else {
|
|
|
|
this.$el.removeClass('editable');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.model.get('line_count') > 8 && !window.detail) {
|
|
|
|
this.$el.addClass('expandable');
|
|
|
|
} else {
|
|
|
|
this.$el.removeClass('expandable');
|
|
|
|
}
|
|
|
|
|
|
|
|
$('script#disqus').remove();
|
2012-02-26 14:57:02 -08:00
|
|
|
window.site.$body.append('<script id="disqus" type="text/javascript">' + $('script#disqus-template').text() + '</script>');
|
2012-02-18 11:42:30 -08:00
|
|
|
|
2012-04-07 21:52:10 -07:00
|
|
|
// For new snipts.
|
|
|
|
if (this.$el.attr('id') === 'new-snipt') {
|
|
|
|
this.$el.fadeIn('fast');
|
|
|
|
this.$el.attr('id', 'snipt-' + this.model.get('id'));
|
|
|
|
}
|
|
|
|
|
2012-02-15 15:25:13 -08:00
|
|
|
return this;
|
|
|
|
},
|
2012-04-05 14:33:47 -07:00
|
|
|
save: function() {
|
2012-07-18 08:35:52 -07:00
|
|
|
|
|
|
|
$('button.save, button.save-and-close, button.delete, button.cancel',
|
|
|
|
window.site.$main_edit).attr('disabled', 'disabled');
|
|
|
|
|
|
|
|
that.model.save({
|
2012-04-07 21:52:10 -07:00
|
|
|
'title': $('input#snipt_title').val(),
|
|
|
|
'tags': $('label.tags textarea').val(),
|
|
|
|
'tags_list': $('label.tags textarea').val(),
|
|
|
|
'lexer': $('select[name="lexer"]').val(),
|
|
|
|
'lexer_name': $('select[name="lexer"] option:selected').text(),
|
2012-07-20 18:59:34 -07:00
|
|
|
'code': window.editor.getValue(),
|
2012-05-24 19:20:06 -07:00
|
|
|
'blog_post': $('label.blog-post input').is(':checked'),
|
2012-05-27 11:51:21 -07:00
|
|
|
'publish_date': $('label.publish-date input').val(),
|
2012-04-07 21:52:10 -07:00
|
|
|
'public': $('label.public input').is(':checked')
|
2012-07-18 08:35:52 -07:00
|
|
|
}, {
|
|
|
|
success: function(model, response) {
|
|
|
|
$('button.save, button.save-and-close, button.delete, button.cancel',
|
|
|
|
window.site.$main_edit).removeAttr('disabled');
|
|
|
|
},
|
|
|
|
error: function(model, response) {
|
|
|
|
alert('There was a problem saving your snipt. We\'ve been notified. Sorry about that!');
|
|
|
|
}
|
|
|
|
});
|
2012-04-05 14:33:47 -07:00
|
|
|
},
|
2012-01-15 16:49:53 -08:00
|
|
|
select: function(fromClick) {
|
|
|
|
|
2012-02-24 17:45:00 -08:00
|
|
|
$('article.selected', window.site.snipt_list.$el).removeClass('selected');
|
2012-01-15 16:49:53 -08:00
|
|
|
this.$el.addClass('selected');
|
|
|
|
|
|
|
|
if (fromClick !== true) {
|
2012-02-24 17:45:00 -08:00
|
|
|
if (window.site.$snipts.index(this.$el) === 0) {
|
2012-01-15 20:18:38 -08:00
|
|
|
window.scrollTo(0, 0);
|
2012-01-15 16:49:53 -08:00
|
|
|
} else {
|
2012-02-24 17:45:00 -08:00
|
|
|
window.site.$html_body.animate({
|
2012-01-15 16:49:53 -08:00
|
|
|
scrollTop: this.$el.offset().top - 50
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.$selected = this.$el;
|
2012-01-16 18:17:58 -08:00
|
|
|
},
|
2012-01-24 15:48:42 -08:00
|
|
|
selectFromClick: function(e) {
|
2012-01-16 18:17:58 -08:00
|
|
|
this.select(true);
|
2012-01-24 15:48:42 -08:00
|
|
|
e.stopPropagation();
|
2012-06-20 10:31:02 -07:00
|
|
|
window.site.$aside_nav.removeClass('open');
|
2011-11-06 15:04:03 -08:00
|
|
|
}
|
|
|
|
});
|
2012-02-24 17:45:00 -08:00
|
|
|
Snipt.SniptListView = Backbone.View.extend({
|
2011-11-06 08:12:05 -08:00
|
|
|
el: 'section#snipts',
|
|
|
|
|
|
|
|
initialize: function(opts) {
|
2012-01-15 11:51:33 -08:00
|
|
|
|
2012-04-07 20:43:11 -07:00
|
|
|
var that = this;
|
|
|
|
|
2012-02-24 17:45:00 -08:00
|
|
|
opts.snipts.each(this.addExistingSnipt);
|
2012-01-15 11:51:33 -08:00
|
|
|
|
|
|
|
this.keyboardShortcuts();
|
2012-02-20 10:45:04 -08:00
|
|
|
|
|
|
|
var cmd;
|
|
|
|
if (navigator.platform == 'MacPPC' ||
|
|
|
|
navigator.platform == 'MacIntel') {
|
|
|
|
cmd = 'Cmd';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cmd = 'Ctrl';
|
|
|
|
}
|
|
|
|
$('span.cmd-ctrl').text(cmd);
|
2012-04-07 20:43:11 -07:00
|
|
|
|
2012-07-18 08:35:52 -07:00
|
|
|
// This should probably be handled more traditionally
|
|
|
|
// (in a backbone view / events)
|
2012-04-07 20:43:11 -07:00
|
|
|
$('button#add-snipt').click(function() {
|
|
|
|
that.addNewSnipt();
|
|
|
|
});
|
2011-11-06 15:04:03 -08:00
|
|
|
},
|
2012-01-24 15:48:42 -08:00
|
|
|
|
2012-02-15 15:25:13 -08:00
|
|
|
addExistingSnipt: function() {
|
2012-02-17 10:14:57 -08:00
|
|
|
|
|
|
|
var $el = $(this);
|
2012-02-18 11:42:30 -08:00
|
|
|
var $created = $('li.created', $el);
|
2012-02-17 10:14:57 -08:00
|
|
|
var $h1 = $('header h1 a', $el);
|
2012-04-07 16:11:05 -07:00
|
|
|
var $public = $('div.public', $el);
|
2012-05-24 19:20:06 -07:00
|
|
|
var $blog_post = $('div.blog-post', $el);
|
2012-05-27 11:51:21 -07:00
|
|
|
var $publish_date = $('div.publish-date', $el);
|
2012-02-18 11:42:30 -08:00
|
|
|
var $user = $('li.author a', $el);
|
2012-05-24 19:20:06 -07:00
|
|
|
|
2012-04-07 16:11:05 -07:00
|
|
|
var is_public = $public.text() === 'True' ? true : false;
|
2012-05-24 19:20:06 -07:00
|
|
|
var is_blog_post = $blog_post.text() === 'True' ? true : false;
|
|
|
|
|
2012-02-17 16:14:53 -08:00
|
|
|
var tag_lis = $('section.tags li', $el);
|
|
|
|
var tags = [];
|
|
|
|
|
|
|
|
for (var i=0; i < tag_lis.length; i++) {
|
|
|
|
var $tag = $('a', tag_lis.eq(i));
|
|
|
|
tags[i] = {
|
|
|
|
name: $tag.text(),
|
|
|
|
absolute_url: $tag.attr('href')
|
|
|
|
};
|
|
|
|
}
|
2012-02-17 10:14:57 -08:00
|
|
|
|
2012-02-15 15:25:13 -08:00
|
|
|
var data = {
|
2012-04-07 19:38:26 -07:00
|
|
|
code: $('textarea.raw', $el).text(),
|
2012-02-18 11:42:30 -08:00
|
|
|
created: $created.attr('title'),
|
|
|
|
created_formatted: $created.text(),
|
2012-02-17 16:14:53 -08:00
|
|
|
embed_url: $('div.embed-url', $el).text(),
|
2012-04-08 17:16:02 -07:00
|
|
|
absolute_url: $h1.attr('href'),
|
2012-04-12 19:04:37 -07:00
|
|
|
favorite_id: $el.data('favorite-id'),
|
2012-03-20 20:00:49 -07:00
|
|
|
id: parseInt($el.attr('id').replace('snipt-', ''), 0),
|
2012-02-17 16:14:53 -08:00
|
|
|
key: $('div.key', $el).text(),
|
|
|
|
lexer: $('div.lexer', $el).text(),
|
2012-02-18 11:42:30 -08:00
|
|
|
lexer_name: $('div.lexer-name', $el).text(),
|
2012-02-17 16:14:53 -08:00
|
|
|
line_count: parseInt($('div.line-count', $el).text(), 0),
|
|
|
|
modified: $('div.modified', $el).text(),
|
|
|
|
resource_uri: $('div.resource-uri', $el).text(),
|
|
|
|
slug: $('div.slug', $el).text(),
|
|
|
|
stylized: $('div.stylized', $el).text(),
|
|
|
|
tags: tags,
|
2012-05-27 11:51:21 -07:00
|
|
|
publish_date: $publish_date.text(),
|
2012-02-17 16:14:53 -08:00
|
|
|
tags_list: $('div.tags-list', $el).text(),
|
2012-02-15 15:25:13 -08:00
|
|
|
title: $h1.text(),
|
2012-02-18 11:42:30 -08:00
|
|
|
user: {
|
2012-04-08 17:16:02 -07:00
|
|
|
absolute_url: $user.attr('href'),
|
2012-02-18 11:42:30 -08:00
|
|
|
username: $user.text()
|
|
|
|
}
|
2012-02-15 15:25:13 -08:00
|
|
|
};
|
2012-04-07 16:11:05 -07:00
|
|
|
data['public'] = is_public;
|
2012-07-07 09:12:40 -07:00
|
|
|
data.blog_post = is_blog_post;
|
2012-02-17 10:14:57 -08:00
|
|
|
|
2012-02-24 17:45:00 -08:00
|
|
|
var view = new Snipt.SniptView({
|
2012-02-15 15:25:13 -08:00
|
|
|
el: this,
|
2012-02-24 17:45:00 -08:00
|
|
|
model: new Snipt.SniptModel(data)
|
2012-02-15 15:25:13 -08:00
|
|
|
});
|
2012-01-15 11:51:33 -08:00
|
|
|
},
|
2012-04-07 20:43:11 -07:00
|
|
|
addNewSnipt: function() {
|
2012-04-07 21:52:10 -07:00
|
|
|
|
|
|
|
var $articleNewSnipt = $('article#new-snipt');
|
|
|
|
|
|
|
|
if ($articleNewSnipt.length === 0) {
|
|
|
|
window.site.snipt_list.$el.prepend('<article id="new-snipt" class="hidden snipt"></article>');
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
id: '',
|
|
|
|
code: '',
|
|
|
|
tags: [],
|
|
|
|
tags_list: '',
|
|
|
|
title: '',
|
|
|
|
lexer: 'text',
|
|
|
|
lexer_name: 'Text only',
|
2012-04-08 17:16:02 -07:00
|
|
|
new_from_js: true,
|
2012-04-07 21:52:10 -07:00
|
|
|
user: {
|
|
|
|
username: ''
|
|
|
|
}
|
|
|
|
};
|
|
|
|
data['public'] = false;
|
2012-07-07 09:12:40 -07:00
|
|
|
data.blog_post = false;
|
2012-04-07 21:52:10 -07:00
|
|
|
|
|
|
|
var newSniptView = new Snipt.SniptView({
|
|
|
|
el: $('article#new-snipt'),
|
|
|
|
model: new Snipt.SniptModel(data)
|
|
|
|
});
|
|
|
|
|
|
|
|
newSniptView.edit();
|
|
|
|
} else {
|
|
|
|
$articleNewSnipt.trigger('edit');
|
|
|
|
}
|
|
|
|
|
2012-04-07 20:43:11 -07:00
|
|
|
return false;
|
|
|
|
},
|
2012-04-05 14:33:47 -07:00
|
|
|
escapeUI: function(destroyed) {
|
2012-05-11 20:09:59 -07:00
|
|
|
if (window.editing || destroyed) {
|
2012-03-11 20:50:06 -07:00
|
|
|
if (!window.site.$html.hasClass('detail')) {
|
|
|
|
window.site.$body.removeClass('detail');
|
|
|
|
}
|
|
|
|
window.site.$main_edit.hide();
|
|
|
|
window.site.$body.removeClass('editing');
|
|
|
|
window.site.$main.show();
|
|
|
|
|
|
|
|
window.editing = true;
|
|
|
|
window.ui_halted = false;
|
|
|
|
|
|
|
|
if (window.site.$snipts.index(window.$selected) === 0) {
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
} else {
|
|
|
|
window.site.$html_body.animate({
|
|
|
|
scrollTop: window.$selected.offset().top - 50
|
|
|
|
}, 0);
|
|
|
|
}
|
2012-04-05 14:33:47 -07:00
|
|
|
|
|
|
|
if (destroyed) {
|
|
|
|
window.$selected.trigger('fadeAndRemove');
|
|
|
|
}
|
2012-03-11 20:50:06 -07:00
|
|
|
} else {
|
|
|
|
if (!window.ui_halted) {
|
|
|
|
if ($selected) {
|
|
|
|
$selected.trigger('deselect');
|
|
|
|
}
|
2012-06-19 19:28:27 -07:00
|
|
|
window.site.$aside_nav.removeClass('open');
|
2012-03-11 20:50:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-01-15 11:51:33 -08:00
|
|
|
keyboardShortcuts: function() {
|
|
|
|
|
2012-03-11 20:50:06 -07:00
|
|
|
var that = this;
|
|
|
|
|
2012-01-15 11:51:33 -08:00
|
|
|
$selected = window.selected;
|
2012-01-15 20:05:54 -08:00
|
|
|
$document = $(document);
|
2012-01-15 11:51:33 -08:00
|
|
|
|
2012-01-25 16:39:38 -08:00
|
|
|
$document.bind('keydown', 'j', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if (!$selected) {
|
|
|
|
window.site.$snipts.eq(0).trigger('selectSnipt');
|
|
|
|
} else {
|
|
|
|
$selected.trigger('next');
|
|
|
|
}
|
2012-01-25 16:39:38 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$document.bind('keydown', 'k', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if (!$selected) {
|
|
|
|
window.site.$snipts.eq(0).trigger('selectSnipt');
|
|
|
|
} else {
|
|
|
|
$selected.trigger('prev');
|
|
|
|
}
|
2012-01-25 16:39:38 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$document.bind('keydown', 'c', function(e) {
|
2012-06-05 20:10:30 -07:00
|
|
|
if (!window.ui_halted && !window.blog_post) {
|
2012-02-26 14:57:02 -08:00
|
|
|
if ($selected) {
|
|
|
|
e.preventDefault();
|
|
|
|
$selected.trigger('copyRaw');
|
|
|
|
}
|
2012-01-15 16:49:53 -08:00
|
|
|
}
|
|
|
|
});
|
2012-02-17 10:14:57 -08:00
|
|
|
$document.bind('keydown', 'Ctrl+e', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if ($selected) {
|
|
|
|
if ($selected.hasClass('editable')) {
|
|
|
|
$selected.trigger('edit');
|
|
|
|
}
|
2012-01-15 16:49:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-05-11 20:35:21 -07:00
|
|
|
$document.bind('keydown', 'Ctrl+backspace', function() {
|
2012-05-11 20:18:54 -07:00
|
|
|
if (!window.ui_halted || window.editing) {
|
2012-05-11 20:09:59 -07:00
|
|
|
if ($selected) {
|
|
|
|
if ($selected.hasClass('editable')) {
|
|
|
|
if (confirm('Are you sure you want to delete this snipt?')) {
|
|
|
|
$selected.trigger('destroy');
|
|
|
|
window.site.snipt_list.escapeUI(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-05-11 20:35:21 -07:00
|
|
|
$document.bind('keydown', 'Ctrl+del', function() {
|
|
|
|
if (!window.ui_halted || window.editing) {
|
|
|
|
if ($selected) {
|
|
|
|
if ($selected.hasClass('editable')) {
|
|
|
|
if (confirm('Are you sure you want to delete this snipt?')) {
|
|
|
|
$selected.trigger('destroy');
|
|
|
|
window.site.snipt_list.escapeUI(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-06-05 17:55:39 -07:00
|
|
|
$document.bind('keydown', 'Alt+n', function() {
|
2012-06-05 17:54:35 -07:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
that.addNewSnipt();
|
|
|
|
}
|
|
|
|
});
|
2012-05-11 20:35:21 -07:00
|
|
|
$document.bind('keydown', 'Ctrl+n', function() {
|
|
|
|
if (!window.ui_halted) {
|
|
|
|
that.addNewSnipt();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$document.bind('keydown', 'Ctrl+s', function() {
|
|
|
|
if (window.editing) {
|
|
|
|
if ($selected) {
|
|
|
|
if ($selected.hasClass('editable')) {
|
|
|
|
$('button.save').click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$document.bind('keydown', 'Ctrl+c', function() {
|
|
|
|
if (window.editing) {
|
|
|
|
if ($selected) {
|
|
|
|
if ($selected.hasClass('editable')) {
|
|
|
|
$('button.save-and-close').click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-01-15 20:05:54 -08:00
|
|
|
$document.bind('keydown', 'esc', function() {
|
2012-03-11 20:50:06 -07:00
|
|
|
that.escapeUI();
|
2012-01-15 16:49:53 -08:00
|
|
|
});
|
2012-01-15 20:18:38 -08:00
|
|
|
$document.bind('keydown', 'g', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if (window.$selected) {
|
|
|
|
window.$selected.trigger('deselect');
|
|
|
|
}
|
|
|
|
window.scrollTo(0, 0);
|
2012-01-15 20:18:38 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
$document.bind('keydown', 'Shift+g', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if (window.$selected) {
|
|
|
|
window.$selected.trigger('deselect');
|
|
|
|
}
|
|
|
|
window.scrollTo(0, document.body.scrollHeight);
|
2012-01-15 20:18:38 -08:00
|
|
|
}
|
|
|
|
});
|
2012-01-15 20:05:54 -08:00
|
|
|
$document.bind('keydown', 'n', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
var $anc = $('li.next a');
|
|
|
|
if ($anc.length) {
|
|
|
|
if ($anc.attr('href') !== '#') {
|
|
|
|
window.location = $anc.attr('href');
|
|
|
|
}
|
2012-01-16 20:51:46 -08:00
|
|
|
}
|
2012-01-15 20:05:54 -08:00
|
|
|
}
|
|
|
|
});
|
2012-02-17 10:14:57 -08:00
|
|
|
$document.bind('keydown', 'e', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if ($selected) {
|
|
|
|
if ($selected.hasClass('expandable')) {
|
|
|
|
$selected.trigger('expand');
|
|
|
|
}
|
2012-02-15 20:06:11 -08:00
|
|
|
}
|
2012-01-15 11:51:33 -08:00
|
|
|
}
|
|
|
|
});
|
2012-05-11 20:09:59 -07:00
|
|
|
$document.bind('keydown', 'u', function() {
|
|
|
|
if (!window.ui_halted) {
|
|
|
|
if ($selected) {
|
|
|
|
$selected.trigger('goToAuthor');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2012-01-15 20:05:54 -08:00
|
|
|
$document.bind('keydown', 'p', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
var $anc = $('li.prev a');
|
|
|
|
if ($anc.length) {
|
|
|
|
if ($anc.attr('href') !== '#') {
|
|
|
|
window.location = $anc.attr('href');
|
|
|
|
}
|
2012-01-16 20:51:46 -08:00
|
|
|
}
|
2012-01-15 20:05:54 -08:00
|
|
|
}
|
|
|
|
});
|
2012-02-20 10:45:04 -08:00
|
|
|
$document.bind('keydown', 'v', function(e) {
|
2012-06-05 20:10:30 -07:00
|
|
|
if (!window.ui_halted && !window.blog_post) {
|
2012-02-26 14:57:02 -08:00
|
|
|
if ($selected) {
|
|
|
|
e.preventDefault();
|
|
|
|
$selected.trigger('embed');
|
|
|
|
}
|
2012-01-25 16:39:38 -08:00
|
|
|
}
|
|
|
|
});
|
2012-02-17 10:14:57 -08:00
|
|
|
$document.bind('keydown', 'o', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if ($selected) {
|
|
|
|
$selected.trigger('detail');
|
|
|
|
}
|
2012-02-17 10:14:57 -08:00
|
|
|
}
|
|
|
|
});
|
2012-01-15 20:05:54 -08:00
|
|
|
$document.bind('keydown', 'return', function() {
|
2012-02-26 14:57:02 -08:00
|
|
|
if (!window.ui_halted) {
|
|
|
|
if ($selected) {
|
|
|
|
$selected.trigger('detail');
|
|
|
|
}
|
2012-01-15 11:51:33 -08:00
|
|
|
}
|
|
|
|
});
|
2011-11-06 08:12:05 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})(snipt.module('snipt'));
|