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

172 lines
6.0 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.$aside_main = $('aside.main', this.$body);
2012-06-19 19:11:53 -07:00
this.$aside_nav = $('aside.nav', this.$body);
2015-09-30 13:33:05 -07:00
this.$teams_nav = $('li.teams-nav', this.$body);
this.$add_snipt = $('li.add-snipt', this.$body);
2012-06-19 19:11:53 -07:00
this.$aside_nav_ul = $('ul', this.$aside_nav);
2012-02-26 14:57:02 -08:00
this.$search_form = $('form.search', this.$body);
this.$search_query = $('input#search-query', this.$body);
2012-04-22 18:47:49 -07:00
this.$search_page_query = $('input.search-query', this.$body);
this.$search_queries = this.$search_query.add(this.$search_page_query);
2012-02-26 14:57:02 -08:00
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');
this.$keyboard_shortcuts = $('#keyboard-shortcuts', this.$body);
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-06-19 19:11:53 -07:00
var that = this;
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;
}
2012-06-19 19:11:53 -07:00
that.$aside_nav.removeClass('open');
2015-09-30 13:33:05 -07:00
that.$teams_nav.removeClass('open');
that.$add_snipt.removeClass('open');
2012-06-19 19:11:53 -07:00
});
this.$aside_nav_ul.click(function(e) {
e.stopPropagation();
2012-04-09 10:57:27 -07:00
});
2011-11-06 08:12:05 -08:00
2012-04-22 18:47:49 -07:00
$search_queries = this.$search_queries;
$search_queries.focus(function() {
2012-01-15 17:08:19 -08:00
if (window.$selected) {
$selected.trigger('deselect');
}
});
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
this.$keyboard_shortcuts.on('hidden', function() {
window.ui_halted = false;
});
if (this.$body.hasClass('login')) {
$('input#id_username').focus();
}
// Highlight any Markdown code.
$('div.markdown pre code').each(function(i, e) {
hljs.highlightBlock(e);
});
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: {
2012-06-19 19:11:53 -07:00
'showKeyboardShortcuts': 'showKeyboardShortcuts',
2015-09-30 13:33:05 -07:00
'click a.mini-profile': 'toggleMiniProfile',
'click a.teams-nav': 'toggleTeamsNav'
2012-01-24 16:08:24 -08:00
},
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
var that = this;
2012-04-22 18:47:49 -07:00
$search_queries = this.$search_queries;
$search_page_query = this.$search_page_query;
$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();
2012-04-22 18:47:49 -07:00
if ($body.hasClass('search')) {
$search_page_query.focus();
} else {
$search_query.focus();
}
2012-02-26 14:57:02 -08:00
}
2011-11-06 08:12:05 -08:00
});
2013-12-18 13:36:17 -08:00
$document.bind('keydown', 'Ctrl+h', function(e) {
2012-02-26 14:57:02 -08:00
if (!window.ui_halted) {
window.ui_halted = true;
2012-02-26 14:57:02 -08:00
$body.trigger('showKeyboardShortcuts');
} else {
if (that.$keyboard_shortcuts.is(':visible')) {
that.$keyboard_shortcuts.modal('hide');
}
2012-02-26 14:57:02 -08:00
}
2012-01-24 16:08:24 -08:00
});
2012-04-22 18:47:49 -07:00
this.$search_queries.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() {
this.$keyboard_shortcuts.modal('toggle');
2012-01-24 16:08:24 -08:00
},
2012-06-19 19:11:53 -07:00
toggleMiniProfile: function(e) {
this.$aside_nav.toggleClass('open');
return false;
},
2015-09-30 13:33:05 -07:00
toggleTeamsNav: function(e) {
this.$teams_nav.toggleClass('open');
return false;
},
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'));