snipt/snipt/media/js/src/modules/snipt.js

254 lines
8.3 KiB
JavaScript
Raw Normal View History

2012-01-25 07:13:08 -08:00
2011-11-06 08:12:05 -08:00
(function(Snipt) {
2012-02-15 15:25:13 -08:00
SniptModel = Backbone.Model.extend({
2011-11-06 15:04:03 -08:00
});
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);
this.model.bind('destroy', this.remove, this);
this.template = _.template($('#snipt-template').html());
2011-11-10 13:04:16 -08:00
2012-02-15 15:25:13 -08:00
this.$el = $(this.el);
2011-11-10 11:34:51 -08:00
this.$aside = $('aside', this.$el);
2011-11-10 13:04:16 -08:00
this.$container = $('div.container', this.$el);
2012-01-25 16:39:38 -08:00
this.$copyModal = $('div.copy-modal', this.$el);
this.$copyModalBody = $('div.modal-body', this.$copyModal);
this.$copyModalClose = $('a.close', this.$copyModal);
this.$copyModalType = $('h4 span', this.$copyModal);
2012-02-15 15:25:13 -08:00
this.$h1 = $('header h1 a', this.$el);
2012-01-25 16:39:38 -08:00
this.$raw = $('div.raw', this.$el);
2011-11-10 13:51:19 -08:00
this.$tags = $('section.tags ul', this.$aside);
2012-01-25 16:39:38 -08:00
this.$copyModal.on('hidden', function(e) {
$(this).parent().trigger('copyClose');
});
2011-11-06 15:04:03 -08:00
},
events: {
2012-01-25 16:39:38 -08:00
'click a.copy': 'copyFromClick',
'click a.embed': 'embed',
2012-01-15 16:49:53 -08:00
'click a.expand': 'expand',
2012-01-16 18:17:58 -08:00
'click .container': 'selectFromClick',
2012-01-30 08:01:10 -08:00
'copyRaw': 'copy',
2012-01-25 16:39:38 -08:00
'copyClose': 'copyClose',
2012-01-15 16:49:53 -08:00
'detail': 'detail',
2012-01-15 17:08:19 -08:00
'deselect': 'deselect',
2012-01-25 16:39:38 -08:00
'embed': 'embed',
2012-01-15 16:49:53 -08:00
'expand': 'expand',
'next': 'next',
'prev': 'prev',
2012-02-15 15:25:13 -08:00
'selectSnipt': 'select',
'test': 'test'
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-01-25 16:39:38 -08:00
if (!this.$copyModal.is(':visible')) {
var cmd;
if (navigator.platform == 'MacPPC' ||
navigator.platform == 'MacIntel') {
cmd = 'Cmd';
}
else {
cmd = 'Ctrl';
}
this.$copyModalBody.append('<textarea class="raw"></textarea>');
$textarea = $('textarea.raw', this.$copyModalBody).val(this.$raw.text());
this.$copyModalType.text(cmd);
this.$copyModal.modal('show');
$textarea.select();
2011-12-04 16:57:05 -08:00
}
2012-01-25 16:39:38 -08:00
},
copyClose: function() {
$('textarea', this.$copyModal).remove();
},
copyFromClick: function() {
this.copy();
2011-12-04 16:45:31 -08:00
return false;
},
2012-01-15 17:08:19 -08:00
deselect: function() {
2012-01-25 16:39:38 -08:00
if (!this.$copyModal.is(':visible')) {
this.$el.removeClass('selected');
window.$selected = false;
}
2012-01-15 17:08:19 -08:00
},
detail: function() {
2012-01-15 16:49:53 -08:00
window.location = this.model.get('url');
},
expand: function() {
this.$container.toggleClass('expanded', 100);
this.$tags.toggleClass('expanded');
2012-01-16 18:17:58 -08:00
this.select();
2012-01-25 16:39:38 -08:00
},
embed: function() {
alert('TODO');
},
next: function() {
2012-01-25 16:39:38 -08:00
window.site.$copyModals.modal('hide');
nextSnipt = this.$el.next('article.snipt');
if (nextSnipt.length) {
2012-01-25 16:39:38 -08:00
return nextSnipt.trigger('selectSnipt');
}
},
prev: function() {
2012-01-25 16:39:38 -08:00
window.site.$copyModals.modal('hide');
prevSnipt = this.$el.prev('article.snipt');
if (prevSnipt.length) {
2012-01-25 16:39:38 -08:00
return prevSnipt.trigger('selectSnipt');
}
2012-01-15 16:49:53 -08:00
},
2012-02-15 15:25:13 -08:00
remove: function() {
console.log('SniptView.remove() called');
},
render: function() {
console.log('SniptView.render() called');
this.$el.html(this.template(this.model.toJSON()));
return this;
},
2012-01-15 16:49:53 -08:00
select: function(fromClick) {
$('article.selected', SniptList.$el).removeClass('selected');
this.$el.addClass('selected');
if (fromClick !== true) {
if (SniptList.$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 {
$('html, body').animate({
scrollTop: this.$el.offset().top - 50
}, 0);
}
}
window.$selected = this.$el;
2012-01-16 18:17:58 -08:00
},
selectFromClick: function(e) {
2012-01-16 18:17:58 -08:00
this.select(true);
e.stopPropagation();
2012-02-15 15:25:13 -08:00
},
test: function() {
console.log('test triggered');
this.model.set({'title': 'Changed title!'});
2011-11-06 15:04:03 -08:00
}
});
2011-11-06 08:12:05 -08:00
SniptListView = Backbone.View.extend({
el: 'section#snipts',
initialize: function(opts) {
this.$snipts = opts.snipts;
2012-02-15 15:25:13 -08:00
this.$snipts.each(this.addExistingSnipt);
this.$el = $(this.el);
this.keyboardShortcuts();
2011-11-06 15:04:03 -08:00
},
2012-02-15 15:25:13 -08:00
addExistingSnipt: function() {
var $h1 = $('header h1 a', this);
var data = {
title: $h1.text(),
url: $h1.attr('href')
};
var view = new SniptView({
el: this,
model: new SniptModel(data)
});
},
keyboardShortcuts: function() {
$selected = window.selected;
$document = $(document);
2012-02-15 15:25:13 -08:00
$document.bind('keydown', 'Shift+t', function() {
if ($selected) {
$selected.trigger('test');
}
});
2012-01-25 16:39:38 -08:00
$document.bind('keydown', 'j', function() {
if (!$selected) {
SniptList.$snipts.eq(0).trigger('selectSnipt');
} else {
$selected.trigger('next');
}
});
$document.bind('keydown', 'k', function() {
if (!$selected) {
SniptList.$snipts.eq(0).trigger('selectSnipt');
} else {
$selected.trigger('prev');
}
});
$document.bind('keydown', 'c', function(e) {
2012-01-15 16:49:53 -08:00
if ($selected) {
2012-01-25 16:39:38 -08:00
e.preventDefault();
2012-01-30 08:01:10 -08:00
$selected.trigger('copyRaw');
2012-01-15 16:49:53 -08:00
}
});
$document.bind('keydown', 'e', function() {
2012-01-15 16:49:53 -08:00
if ($selected) {
if ($selected.hasClass('expandable')) {
$selected.trigger('expand');
}
}
});
$document.bind('keydown', 'esc', function() {
2012-01-15 16:49:53 -08:00
if ($selected) {
2012-01-15 17:08:19 -08:00
$selected.trigger('deselect');
2012-01-15 16:49:53 -08:00
}
});
2012-01-15 20:18:38 -08:00
$document.bind('keydown', 'g', function() {
if (window.$selected) {
window.$selected.trigger('deselect');
}
window.scrollTo(0, 0);
});
$document.bind('keydown', 'Shift+g', function() {
if (window.$selected) {
window.$selected.trigger('deselect');
}
window.scrollTo(0, document.body.scrollHeight);
});
$document.bind('keydown', 'n', function() {
var $anc = $('li.next a');
2012-01-16 20:51:46 -08:00
if ($anc.length) {
if ($anc.attr('href') !== '#') {
window.location = $anc.attr('href');
}
}
});
$document.bind('keydown', 'o', function() {
2012-01-15 16:49:53 -08:00
if ($selected) {
$selected.trigger('detail');
}
});
$document.bind('keydown', 'p', function() {
var $anc = $('li.prev a');
2012-01-16 20:51:46 -08:00
if ($anc.length) {
if ($anc.attr('href') !== '#') {
window.location = $anc.attr('href');
}
}
});
2012-01-25 16:39:38 -08:00
$document.bind('keydown', 'v', function() {
if ($selected) {
$selected.trigger('embed');
}
});
$document.bind('keydown', 'return', function() {
if ($selected) {
2012-01-15 16:49:53 -08:00
$selected.trigger('detail');
}
});
2011-11-06 08:12:05 -08:00
}
});
Snipt.Views = {
'SniptListView': SniptListView
};
})(snipt.module('snipt'));