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

144 lines
4.8 KiB
JavaScript
Raw Normal View History

2011-11-06 08:12:05 -08:00
(function(Site) {
var Snipt = snipt.module('snipt');
2012-03-20 20:38:47 -07:00
Backbone.oldSync = Backbone.sync;
Backbone.Model.prototype.idAttribute = 'resource_uri';
var addSlash = function(str) {
return str + ((str.length > 0 && str.charAt(str.length - 1) === '/') ? '' : '/');
};
Backbone.sync = function(method, model, options) {
options.headers = _.extend({
'Authorization': 'ApiKey ' + window.user + ':' + window.api_key
}, options.headers);
return Backbone.oldSync(method, model, options);
};
Backbone.Model.prototype.url = function() {
var url = this.id;
if (!url) {
url = this.urlRoot;
url = url || this.collection && (_.isFunction(this.collection.url) ? this.collection.url() : this.collection.url);
if (url && this.has('id')) {
url = addSlash(url) + this.get('id');
}
}
url = url && addSlash(url);
2012-04-07 21:52:10 -07:00
if (typeof url === 'undefined') {
url = '/api/private/snipt/';
this.unset('id', {'silent': true});
this.unset('user', {'silent': true});
}
2012-03-20 20:38:47 -07:00
return url || null;
};
2012-02-24 17:45:00 -08:00
Site.SiteView = Backbone.View.extend({
2011-11-06 08:12:05 -08:00
el: 'body',
initialize: function(opts) {
2012-02-26 14:57:02 -08:00
this.$body = $(this.el);
2012-02-24 17:45:00 -08:00
this.$html = $('html');
2012-02-26 14:57:02 -08:00
this.$html_body = this.$body.add(this.$html);
this.$search_form = $('form.search', this.$body);
this.$search_query = $('input#search-query', this.$body);
this.$snipts = $('section#snipts article.snipt', this.$body);
2012-02-20 10:45:04 -08:00
this.$modals = $('div.modal', this.$snipts);
2012-02-24 17:49:58 -08:00
this.$main_edit = $('section#main-edit');
this.$main = $('section#main');
2011-11-06 08:12:05 -08:00
this.keyboardShortcuts();
this.inFieldLabels();
2012-04-09 10:57:27 -07:00
var SniptListView = Snipt.SniptListView;
this.snipt_list = new SniptListView({ 'snipts': this.$snipts });
2012-04-09 10:57:27 -07:00
this.$body.click(function() {
if (!window.ui_halted && !window.from_modal && window.$selected) {
window.$selected.trigger('deselect');
}
if (window.from_modal) {
window.from_modal = false;
}
});
2011-11-06 08:12:05 -08:00
2012-02-14 08:39:45 -08:00
$search_query = this.$search_query;
$search_query.focus(function() {
2012-01-15 17:08:19 -08:00
if (window.$selected) {
$selected.trigger('deselect');
}
});
2012-02-14 08:39:45 -08:00
this.$search_form.submit(function() {
window.location = 'https://www.google.com/search?q=' + $search_query.val() + ' site:snipt.net%20';
return false;
});
2012-01-15 17:08:19 -08:00
this.$body.on('click', 'a.close', function() {
2012-01-25 17:26:53 -08:00
$(this).parent().parent().modal('hide');
2012-02-26 14:57:02 -08:00
window.ui_halted = false;
2012-01-25 17:26:53 -08:00
return false;
});
2012-02-20 10:45:04 -08:00
2012-02-26 14:57:02 -08:00
window.ui_halted = false;
2011-11-06 08:12:05 -08:00
},
2012-01-24 16:08:24 -08:00
events: {
'showKeyboardShortcuts': 'showKeyboardShortcuts'
},
2011-11-06 08:12:05 -08:00
keyboardShortcuts: function() {
2012-02-26 14:57:02 -08:00
var $body = this.$body;
2011-11-06 08:12:05 -08:00
$search_query = this.$search_query;
$document = $(document);
$document.bind('keydown', '/', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
e.preventDefault();
$search_query.focus();
}
2011-11-06 08:12:05 -08:00
});
2012-02-12 21:42:25 -08:00
$document.bind('keydown', 'h', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
$body.trigger('showKeyboardShortcuts');
}
2012-01-24 16:08:24 -08:00
});
2012-02-14 21:49:55 -08:00
$document.bind('keydown', 't', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
window.open('', '_blank');
}
2012-02-14 21:49:55 -08:00
});
$document.bind('keydown', 'r', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
location.reload(true);
}
2012-02-14 21:49:55 -08:00
});
$document.bind('keydown', 'Ctrl+h', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
history.go(-1);
}
2012-02-14 21:49:55 -08:00
});
$document.bind('keydown', 'Ctrl+l', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
history.go(1);
}
2012-02-14 21:49:55 -08:00
});
2012-02-26 15:34:20 -08:00
this.$search_query.bind('keydown', 'esc', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
e.preventDefault();
this.blur();
}
2011-11-06 08:12:05 -08:00
});
},
2012-01-24 16:08:24 -08:00
showKeyboardShortcuts: function() {
2012-01-25 06:49:36 -08:00
$('#keyboard-shortcuts').modal('toggle');
2012-01-24 16:08:24 -08:00
},
2011-11-06 08:12:05 -08:00
inFieldLabels: function () {
2012-02-26 14:57:02 -08:00
$('div.infield label', this.$body).inFieldLabels({
2012-01-15 17:08:19 -08:00
fadeDuration: 200
});
2011-11-06 08:12:05 -08:00
}
});
})(snipt.module('site'));