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',
|
2015-11-04 18:57:04 -08:00
|
|
|
'click a.history': 'toggleHistory',
|
2016-05-12 08:47:07 -07:00
|
|
|
'click a.secure-view-log': 'toggleSecureView',
|
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();
|
|
|
|
},
|
2013-10-21 12:26:11 -07:00
|
|
|
edit: function(adding) {
|
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;
|
2013-02-04 11:40:45 -08: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
|
2015-08-25 09:01:48 -07:00
|
|
|
$('label.blog-post input', window.site.$main_edit).change(function() {
|
2012-05-24 19:20:06 -07:00
|
|
|
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
|
|
|
|
2015-08-25 09:01:48 -07:00
|
|
|
if ($checkbox.is(':checked')) {
|
2012-05-24 19:20:06 -07:00
|
|
|
$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;
|
2015-08-25 09:01:48 -07:00
|
|
|
}).change();
|
2012-05-24 19:20:06 -07:00
|
|
|
|
2012-03-12 20:48:01 -07:00
|
|
|
// Public / private
|
2015-08-25 09:01:48 -07:00
|
|
|
$('label.public input', window.site.$main_edit).change(function() {
|
2012-03-12 20:48:01 -07:00
|
|
|
var $checkbox = $(this);
|
|
|
|
var $label = $checkbox.parent();
|
|
|
|
|
2015-08-25 09:01:48 -07:00
|
|
|
if ($checkbox.is(':checked')) {
|
2012-03-12 20:48:01 -07:00
|
|
|
$label.removeClass('is-private').addClass('is-public');
|
2016-05-12 08:47:07 -07:00
|
|
|
$('label.secure').addClass('disabled');
|
|
|
|
$('label.secure').removeClass('is-secure');
|
|
|
|
$('label.secure input').attr('checked', false);
|
2012-03-12 20:48:01 -07:00
|
|
|
} else {
|
|
|
|
$label.addClass('is-private').removeClass('is-public');
|
2016-05-12 08:47:07 -07:00
|
|
|
$('label.secure').removeClass('disabled');
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}).change();
|
|
|
|
|
|
|
|
// Secure / insecure
|
|
|
|
$('label.secure', window.site.$main_edit).click(function(e) {
|
|
|
|
if ($('label.public input').is(':checked')) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Secure / insecure
|
|
|
|
$('label.secure input', window.site.$main_edit).change(function(e) {
|
|
|
|
var $checkbox = $(this);
|
|
|
|
var $label = $checkbox.parent();
|
|
|
|
|
|
|
|
if ($checkbox.is(':checked')) {
|
|
|
|
$label.addClass('is-secure');
|
|
|
|
} else {
|
|
|
|
$label.removeClass('is-secure');
|
2012-03-12 20:48:01 -07:00
|
|
|
}
|
|
|
|
return false;
|
2015-08-25 09:01:48 -07:00
|
|
|
}).change();
|
2012-03-12 20:48:01 -07:00
|
|
|
|
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
|
2013-02-04 11:40:45 -08:00
|
|
|
this.$editorCodeMirror = $('div.editor', window.site.$main_edit);
|
|
|
|
|
|
|
|
// Textarea editor
|
|
|
|
this.$editorTextarea = $('textarea.editor', window.site.$main_edit);
|
2012-07-20 17:43:16 -07:00
|
|
|
|
2013-02-04 11:40:45 -08:00
|
|
|
window.editor = CodeMirror(this.$editorCodeMirror.get(0), {
|
2012-07-20 18:54:54 -07:00
|
|
|
autofocus: true,
|
|
|
|
fixedGutter: true,
|
|
|
|
gutter: true,
|
|
|
|
indentUnit: 4,
|
|
|
|
lineNumbers: true,
|
2013-01-13 19:30:54 -08:00
|
|
|
javascriptHint: true,
|
2012-07-20 18:54:54 -07:00
|
|
|
matchBrackets: true,
|
|
|
|
mode: that.guessCodeMirrorLexer($selectLexer.val()),
|
2012-07-20 17:43:16 -07:00
|
|
|
value: that.model.get('code')
|
|
|
|
});
|
2013-02-05 13:31:02 -08:00
|
|
|
|
|
|
|
// Lexer selector.
|
2012-07-20 18:54:54 -07:00
|
|
|
$selectLexer.change(function() {
|
2013-02-05 13:31:02 -08:00
|
|
|
|
2012-07-20 18:54:54 -07:00
|
|
|
var $selectedLexer = $('option:selected', $selectLexer);
|
2013-02-05 13:31:02 -08:00
|
|
|
var lexer = $selectLexer.val();
|
|
|
|
|
2012-07-20 18:54:54 -07:00
|
|
|
window.editor.setOption('mode', that.guessCodeMirrorLexer($selectedLexer.val()));
|
2013-02-05 13:31:02 -08:00
|
|
|
|
|
|
|
// Markdown switch.
|
|
|
|
if (lexer === 'markdown' || lexer === 'text') {
|
|
|
|
window.editor.setOption('lineWrapping', true);
|
|
|
|
} else {
|
|
|
|
window.editor.setOption('lineWrapping', false);
|
|
|
|
}
|
|
|
|
|
2012-07-20 18:54:54 -07:00
|
|
|
});
|
2013-02-05 13:31:02 -08:00
|
|
|
$selectLexer.trigger('change');
|
2012-07-20 17:43:16 -07:00
|
|
|
|
2013-02-04 11:40:45 -08:00
|
|
|
// Set the heights.
|
2013-02-05 13:15:09 -08:00
|
|
|
window.editorHeight = $(window).height() - 147;
|
|
|
|
window.editor.setSize('100%', window.editorHeight);
|
|
|
|
this.$editorTextarea.height(window.editorHeight - 8);
|
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;
|
|
|
|
});
|
|
|
|
|
2013-02-04 11:40:45 -08:00
|
|
|
// Editor settings
|
2013-08-13 11:20:46 -07:00
|
|
|
var $selectEditor = $('select#id_editor', window.site.$main_edit);
|
|
|
|
var $selectTheme = $('select#id_theme', window.site.$main_edit);
|
|
|
|
|
|
|
|
$selectEditor.chosen();
|
|
|
|
$selectEditor.change(function() {
|
|
|
|
var newEditor = $selectEditor.val();
|
|
|
|
|
|
|
|
if (newEditor === 'textarea') {
|
|
|
|
that.$editorCodeMirror.hide();
|
|
|
|
that.$editorTextarea.show();
|
|
|
|
|
|
|
|
// TODO: if we introduce other editors, we'll want to make this smarter, obviously.
|
|
|
|
that.$editorTextarea.val(window.editor.getValue());
|
2013-02-05 12:24:51 -08:00
|
|
|
}
|
2013-08-13 11:20:46 -07:00
|
|
|
if (newEditor === 'codemirror') {
|
|
|
|
that.$editorTextarea.hide();
|
|
|
|
that.$editorCodeMirror.show();
|
|
|
|
|
|
|
|
// TODO: Ditto above.
|
|
|
|
window.editor.setValue(that.$editorTextarea.val());
|
2013-02-04 11:40:45 -08:00
|
|
|
}
|
2013-08-13 11:20:46 -07:00
|
|
|
});
|
2013-02-05 13:15:09 -08:00
|
|
|
|
2013-08-13 11:20:46 -07:00
|
|
|
$selectTheme.chosen();
|
|
|
|
$selectTheme.change(function() {
|
|
|
|
window.editor.setOption('theme', $selectTheme.val());
|
|
|
|
});
|
2013-02-05 13:15:09 -08:00
|
|
|
|
2013-08-13 11:20:46 -07:00
|
|
|
if (window.editor_theme != 'default') {
|
|
|
|
$selectTheme.val(window.editor_theme);
|
2015-10-18 21:41:24 -07:00
|
|
|
$selectTheme.trigger('chosen:updated');
|
2013-08-13 11:20:46 -07:00
|
|
|
$selectTheme.trigger('change');
|
2013-02-04 11:40:45 -08:00
|
|
|
}
|
2013-08-13 11:20:46 -07:00
|
|
|
if (window.default_editor != 'codemirror') {
|
|
|
|
$selectEditor.val(window.default_editor);
|
2015-10-18 21:41:24 -07:00
|
|
|
$selectEditor.trigger('chosen:updated');
|
2013-08-13 11:20:46 -07:00
|
|
|
$selectEditor.trigger('change');
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:41:24 -07:00
|
|
|
// Init user
|
2015-10-18 21:53:37 -07:00
|
|
|
if (window.teams.length) {
|
|
|
|
var $selectUser = $('select#id_user', window.site.$main_edit);
|
|
|
|
$selectUser.chosen();
|
|
|
|
$selectUser.val(window.intended_user);
|
|
|
|
$selectUser.trigger('chosen:updated');
|
|
|
|
}
|
2015-10-18 21:41:24 -07:00
|
|
|
|
2013-08-13 11:20:46 -07:00
|
|
|
// Full-screen mode.
|
|
|
|
this.setupCodeMirrorFullScreen();
|
2013-02-04 11:40:45 -08:00
|
|
|
|
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');
|
2013-05-29 09:26:24 -07:00
|
|
|
this.select(true);
|
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';
|
2014-01-06 11:12:04 -08:00
|
|
|
if (val === 'java') return 'clike';
|
|
|
|
if (val === 'c') return 'clike';
|
|
|
|
if (val === 'cpp') return 'clike';
|
|
|
|
if (val === 'csharp') return 'clike';
|
2012-07-20 18:54:54 -07:00
|
|
|
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
|
|
|
},
|
2013-04-02 21:07:54 -07:00
|
|
|
initEmbeddedTweets: function() {
|
|
|
|
var $embeddedTweets = $('div.embedded-tweet');
|
|
|
|
if ($embeddedTweets.length) {
|
|
|
|
$.each($embeddedTweets, function() {
|
|
|
|
var $tweetPlaceholder = $(this);
|
|
|
|
var tweetID = $tweetPlaceholder.attr('data-tweet-id');
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: 'https://api.twitter.com/1/statuses/oembed.json?id=' + tweetID + '&align=center',
|
|
|
|
dataType: 'jsonp',
|
|
|
|
type: 'get',
|
|
|
|
success: function(resp) {
|
|
|
|
$tweetPlaceholder.replaceWith($(resp.html));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
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()}));
|
2013-04-02 21:07:54 -07:00
|
|
|
|
2012-02-18 11:42:30 -08:00
|
|
|
this.initLocalVars();
|
2012-07-30 18:46:52 -07:00
|
|
|
this.initLineNumbers();
|
2013-04-02 21:07:54 -07:00
|
|
|
this.initEmbeddedTweets();
|
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');
|
|
|
|
}
|
|
|
|
|
2016-05-12 08:47:07 -07:00
|
|
|
if (this.model.get('secure') === true) {
|
|
|
|
this.$el.addClass('secure-snipt');
|
|
|
|
} else {
|
|
|
|
this.$el.removeClass('secure-snipt');
|
|
|
|
}
|
|
|
|
|
2012-02-18 11:42:30 -08:00
|
|
|
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');
|
|
|
|
|
2013-02-04 11:40:45 -08:00
|
|
|
var code;
|
|
|
|
if (this.$editorTextarea.is(':visible')) {
|
|
|
|
code = this.$editorTextarea.val();
|
|
|
|
} else {
|
|
|
|
code = window.editor.getValue();
|
|
|
|
}
|
|
|
|
|
2015-10-18 21:53:37 -07:00
|
|
|
var intendedUser;
|
|
|
|
if (window.teams.length) {
|
|
|
|
intendedUser = $('select[name="user"]').val();
|
|
|
|
} else {
|
|
|
|
intendedUser = window.intended_user;
|
|
|
|
}
|
|
|
|
|
2012-07-18 08:35:52 -07:00
|
|
|
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(),
|
2015-10-18 21:53:37 -07:00
|
|
|
'intended_user': intendedUser,
|
2012-04-07 21:52:10 -07:00
|
|
|
'lexer_name': $('select[name="lexer"] option:selected').text(),
|
2013-02-04 11:40:45 -08:00
|
|
|
'code': code,
|
2013-02-08 10:11:14 -08:00
|
|
|
'description': $('textarea[name="description"]').val(),
|
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(),
|
2016-05-12 08:47:07 -07:00
|
|
|
'public': $('label.public input').is(':checked'),
|
|
|
|
'secure': $('label.secure 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');
|
2015-10-18 20:36:45 -07:00
|
|
|
that.model.set('new_from_js', false);
|
2015-10-25 05:42:47 -07:00
|
|
|
|
|
|
|
var $pres = $('td.code pre');
|
|
|
|
$pres.each(function(i) {
|
|
|
|
var pre = $pres.eq(i);
|
2015-10-25 18:07:19 -07:00
|
|
|
pre.width(pre.parents('section.code').width() - 30);
|
2015-10-25 05:42:47 -07:00
|
|
|
});
|
2012-07-18 08:35:52 -07:00
|
|
|
},
|
|
|
|
error: function(model, response) {
|
2015-10-18 08:52:44 -07:00
|
|
|
if (response.responseJSON) {
|
|
|
|
alert(JSON.stringify(response.responseJSON.snipt));
|
|
|
|
} else {
|
|
|
|
alert(JSON.stringify(response.statusText));
|
|
|
|
}
|
2012-07-18 08:35:52 -07:00
|
|
|
}
|
|
|
|
});
|
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');
|
2013-02-05 13:15:09 -08:00
|
|
|
},
|
|
|
|
setupCodeMirrorFullScreen: function() {
|
|
|
|
// This is kind of messy (taken from the CodeMirror docs). Clean it up some day.
|
|
|
|
|
|
|
|
function isFullScreen(cm) {
|
|
|
|
return (/\bCodeMirror-fullscreen\b/).test(cm.getWrapperElement().className);
|
|
|
|
}
|
|
|
|
function winHeight() {
|
|
|
|
return window.innerHeight || (document.documentElement || document.body).clientHeight;
|
|
|
|
}
|
|
|
|
function setFullScreen(cm, full) {
|
|
|
|
var wrap = cm.getWrapperElement();
|
|
|
|
if (full) {
|
|
|
|
wrap.className += ' CodeMirror-fullscreen';
|
|
|
|
wrap.style.height = winHeight() + 'px';
|
|
|
|
document.documentElement.style.overflow = 'hidden';
|
|
|
|
|
|
|
|
// Hax.
|
|
|
|
$('header.fixed-save').hide();
|
|
|
|
$('div.container').addClass('full-screened');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
wrap.className = wrap.className.replace(' CodeMirror-fullscreen', '');
|
|
|
|
window.editor.setSize('100%', window.editorHeight);
|
|
|
|
document.documentElement.style.overflow = '';
|
|
|
|
|
|
|
|
// Hax.
|
|
|
|
$('header.fixed-save').show();
|
|
|
|
$('div.container').removeClass('full-screened');
|
|
|
|
|
|
|
|
}
|
|
|
|
cm.refresh();
|
|
|
|
}
|
|
|
|
window.editor.on(window, 'resize', function() {
|
|
|
|
var showing = document.body.getElementsByClassName('CodeMirror-fullscreen')[0];
|
|
|
|
if (!showing) return;
|
|
|
|
showing.CodeMirror.getWrapperElement().style.height = winHeight() + 'px';
|
|
|
|
});
|
|
|
|
window.editor.setOption('extraKeys', {
|
|
|
|
'Cmd-Enter': function(cm) {
|
2014-02-22 19:54:41 -08:00
|
|
|
that.save();
|
|
|
|
window.site.snipt_list.escapeUI();
|
|
|
|
return false;
|
2013-02-05 13:15:09 -08:00
|
|
|
},
|
|
|
|
'F11': function(cm) {
|
|
|
|
setFullScreen(cm, !isFullScreen(cm));
|
|
|
|
},
|
|
|
|
'Esc': function(cm) {
|
|
|
|
if (isFullScreen(cm)) setFullScreen(cm, false);
|
|
|
|
}
|
|
|
|
});
|
2015-11-04 18:57:04 -08:00
|
|
|
},
|
2016-05-12 08:47:07 -07:00
|
|
|
toggleSecureView: function() {
|
|
|
|
$('div.secure-view-log').toggle();
|
|
|
|
},
|
2015-11-04 18:57:04 -08:00
|
|
|
toggleHistory: function() {
|
|
|
|
$('div.snipt-log').toggle();
|
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
|
|
|
|
2015-10-18 21:41:24 -07:00
|
|
|
var $buttonAddSnipt = $('button#add-snipt');
|
|
|
|
$buttonAddSnipt.click(function(e) {
|
|
|
|
if (window.teams.length) {
|
|
|
|
e.stopPropagation();
|
|
|
|
$buttonAddSnipt.parent().toggleClass('open');
|
|
|
|
} else {
|
|
|
|
that.addNewSnipt();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var $addSniptTeams = $('ul.add-snipt-teams a');
|
|
|
|
$addSniptTeams.click(function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
window.intended_user = $(e.target).attr('data-intended-user') ||
|
|
|
|
$(e.target).parent().attr('data-intended-user');
|
|
|
|
that.addNewSnipt();
|
2012-04-07 20:43:11 -07:00
|
|
|
});
|
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);
|
2016-05-12 08:47:07 -07:00
|
|
|
var $secure = $('div.secure', $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);
|
2015-10-18 19:57:11 -07: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;
|
2016-05-12 08:47:07 -07:00
|
|
|
var is_secure = $secure.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(),
|
2013-02-08 10:11:14 -08:00
|
|
|
description: $('textarea.description', $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(),
|
2013-03-15 11:19:28 -07:00
|
|
|
raw_url: $('div.raw-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-07-31 12:35:12 -07:00
|
|
|
username: $user.text(),
|
2016-03-21 11:48:44 -07:00
|
|
|
profile: {}
|
2012-02-18 11:42:30 -08:00
|
|
|
}
|
2012-02-15 15:25:13 -08:00
|
|
|
};
|
2012-07-31 12:35:12 -07:00
|
|
|
|
2016-05-12 08:47:07 -07:00
|
|
|
data['secure'] = is_secure;
|
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: '',
|
2014-04-30 09:35:20 -07:00
|
|
|
blog_post: false,
|
2012-04-07 21:52:10 -07:00
|
|
|
code: '',
|
2013-02-08 10:11:14 -08:00
|
|
|
description: '',
|
2012-04-07 21:52:10 -07:00
|
|
|
tags: [],
|
|
|
|
tags_list: '',
|
|
|
|
title: '',
|
|
|
|
lexer: 'text',
|
|
|
|
lexer_name: 'Text only',
|
2012-04-08 17:16:02 -07:00
|
|
|
new_from_js: true,
|
2015-08-25 09:01:48 -07:00
|
|
|
public: true,
|
2016-05-12 08:47:07 -07:00
|
|
|
secure: false,
|
2012-04-07 21:52:10 -07:00
|
|
|
user: {
|
2012-07-31 18:39:40 -07:00
|
|
|
username: '',
|
2016-03-21 11:48:44 -07:00
|
|
|
profile: {}
|
2012-04-07 21:52:10 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var newSniptView = new Snipt.SniptView({
|
|
|
|
el: $('article#new-snipt'),
|
|
|
|
model: new Snipt.SniptModel(data)
|
|
|
|
});
|
|
|
|
|
2013-10-21 12:26:11 -07:00
|
|
|
newSniptView.edit(true);
|
2012-04-07 21:52:10 -07:00
|
|
|
} 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: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
|
|
|
}
|
2013-01-14 19:18:44 -08:00
|
|
|
|
2011-11-06 08:12:05 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
})(snipt.module('snipt'));
|