Added backbone

This commit is contained in:
Mark McDowall 2012-11-06 16:41:34 -08:00
commit b101758957
28 changed files with 5507 additions and 3 deletions

View file

@ -0,0 +1,27 @@
NzbDrone = {};
NzbDrone.Views = {};
NzbDrone.Models = {};
NzbDrone.Collections = {};
NzbDrone.App = new Backbone.Marionette.Application();
// Setup default application views
NzbDrone.App.addInitializer(function () {
NzbDrone.App.addRegions({
main: '#main-region'
});
var layout = new NzbDrone.Views.AppLayout();
NzbDrone.App.Layout = layout;
NzbDrone.App.main.show(layout);
layout.header.show(new NzbDrone.Views.HeaderView());
});
NzbDrone.App.addInitializer(function () {
new NzbDrone.AppRouter();
Backbone.history.start();
});

View file

@ -0,0 +1,7 @@
$(function () {
// Legacy support for templating
utils.loadTemplate(['QualityProfilesView', 'QualityProfileView'],
function () {
NzbDrone.App.start();
});
});

View file

@ -0,0 +1,11 @@
NzbDrone.Constants = {
Templates: {
AppLayout: '#AppLayout',
QualityProfilesView: '#QualityProfilesView',
QualityProfileView: '#QualityProfileView'
},
Events: {
MenuItemSelected: 'MenuItemSelected'
}
};

View file

@ -0,0 +1,57 @@
(function (nzbDrone) {
var appController = function () {
return {
home: function (id) {
// if (!this.homeView) {
// this.homeView = new HomeView();
// }
// $('#content').html(this.homeView.el);
nzbDrone.App.Layout.content.show(new nzbDrone.Views.HomeView());
this.menuItemSelected('home-menu');
},
list: function (page) {
var p = page ? parseInt(page, 10) : 1;
var profileList = new ProfileCollection();
profileList.fetch({
success: function () {
$('#content').html(new QualityProfilesView({ model: profileList, page: p }).el);
}
});
this.menuItemSelected('home-menu');
},
wineDetails: function (id) {
var profile = new Profile({ id: id });
profile.fetch({
success: function () {
$('#content').html(new QualityProfileView({ model: profile }).el);
}
});
this.menuItemSelected();
},
addWine: function () {
var wine = new Profile();
$('#content').html(new QualityProfileView({ model: wine }).el);
this.menuItemSelected('add-menu');
},
menuItemSelected: function (item) {
// Using the application vent object as our global event aggregator
nzbDrone.App.vent.trigger(
nzbDrone.Constants.Events.MenuItemSelected, // Event name
item); // Options
}
};
};
nzbDrone.AppController = appController;
})(window.NodeCellar);

View file

@ -0,0 +1,18 @@
window.ProfileCollection = Backbone.Collection.extend({
model: Profile,
url: '/api/qualityprofiles',
search: function (searchTerm, options) {
var self = this;
this.fetch({
success: function () {
if (options.success) {
options.success();
}
}
});
}
});

View file

@ -0,0 +1,50 @@
window.Profile = Backbone.Model.extend({
urlRoot: '/api/qualityprofiles',
idAttribute: 'id',
initialize: function () {
this.validators = {};
this.validators.name = function (value) {
return value.length > 0 ? { isValid: true } : { isValid: false, message: 'You must enter a name' };
};
this.validators.allowed = function (value) {
return value.length > 0 ? { isValid: true } : { isValid: false, message: 'You must have allowed qualities' };
};
this.validators.cutoff = function (value) {
return value != null ? { isValid: true } : { isValid: false, message: 'You must have a valid cutoff' };
};
},
validateItem: function (key) {
return (this.validators[key]) ? this.validators[key](this.get(key)) : { isValid: true };
},
// TODO: Implement Backbone's standard validate() method instead.
validateAll: function () {
var messages = {};
for (var key in this.validators) {
if (this.validators.hasOwnProperty(key)) {
var check = this.validators[key](this.get(key));
if (check.isValid === false) {
messages[key] = check.message;
}
}
}
return _.size(messages) > 0 ? { isValid: false, messages: messages } : { isValid: true };
},
defaults: {
id: null,
name: '',
allowed: {},
cutoff: null
}
});

View file

@ -0,0 +1,16 @@
(function (nzbDrone, backbone) {
nzbDrone.AppRouter = backbone.Marionette.AppRouter.extend({
controller: new nzbDrone.AppController(),
appRoutes: {
'': 'home',
'wines': 'list',
'wines/page/:page': 'list',
'wines/add': 'addWine',
'wines/:id': 'wineDetails',
'about': 'about',
'search/:searchTerm': 'handleSearch'
}
});
})(window.NzbDrone, window.Backbone);s

View file

@ -0,0 +1,53 @@
window.utils = {
// Asynchronously load templates located in separate .html files
loadTemplate: function (views, callback) {
var deferreds = [];
$.each(views, function (index, view) {
if (window[view]) {
deferreds.push($.get('tpl/' + view + '.html', function (data) {
window[view].prototype.template = _.template(data);
}));
} else {
alert(view + ' not found');
}
});
$.when.apply(null, deferreds).done(callback);
},
displayValidationErrors: function (messages) {
for (var key in messages) {
if (messages.hasOwnProperty(key)) {
this.addValidationError(key, messages[key]);
}
}
this.showAlert('Warning!', 'Fix validation errors and try again', 'alert-warning');
},
addValidationError: function (field, message) {
var controlGroup = $('#' + field).parent().parent();
controlGroup.addClass('error');
$('.help-inline', controlGroup).html(message);
},
removeValidationError: function (field) {
var controlGroup = $('#' + field).parent().parent();
controlGroup.removeClass('error');
$('.help-inline', controlGroup).html('');
},
showAlert: function (title, text, klass) {
$('.alert').removeClass('alert-error alert-warning alert-success alert-info');
$('.alert').addClass(klass);
$('.alert').html('<strong>' + title + '</strong> ' + text);
$('.alert').show();
},
hideAlert: function () {
$('.alert').hide();
}
};

View file

@ -0,0 +1,37 @@
window.QualityProfilesView = Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
var profiles = this.model.models;
var len = profiles.length;
var startPos = (this.options.page - 1) * 8;
var endPos = Math.min(startPos + 8, len);
$(this.el).html('<ul class="thumbnails"></ul>');
for (var i = startPos; i < endPos; i++) {
$('.thumbnails', this.el).append(new QualityProfileView({ model: profiles[i] }).render().el);
}
return this;
}
});
window.QualityProfileView = Backbone.View.extend({
tagName: "li",
initialize: function () {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render: function () {
$(this.el).html(this.template(this.model.toJSON()));
return this;
}
});