Quality type sizes moved to backbone + SS (API)

This commit is contained in:
Mark McDowall 2013-01-01 18:06:55 -08:00
parent 5ba1c0eceb
commit c8621b8100
24 changed files with 244 additions and 356 deletions

View file

@ -0,0 +1,19 @@
QualityTypeApp = {};
QualityTypeApp.Views = {};
QualityTypeApp.Models = {};
QualityTypeApp.Collections = {};
QualityTypeApp.App = new Backbone.Marionette.Application();
// Setup default application views
QualityTypeApp.App.addInitializer(function () {
QualityTypeApp.App.addRegions({
mainRegion: '#sliders'
});
var qualityTypes = new QualityTypeCollectionView();
QualityTypeApp.App.mainRegion.show(qualityTypes);
});

View file

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

View file

@ -3,4 +3,11 @@
QualityProfileCollection: '#QualityProfileCollectionTemplate',
QualityProfile: '#QualityProfileTemplate'
},
};
QualityTypeApp.Constants = {
Templates: {
QualityTypeCollection: '#QualityTypeCollectionTemplate',
QualityType: '#QualityTypeTemplate'
},
};

View file

@ -1,57 +0,0 @@
(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,38 @@
window.QualityType = Backbone.Model.extend({
urlRoot: '/api/qualitytypes',
idAttribute: 'Id',
initialize: function () {
this.validators = {};
},
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: '',
MaxSize: 100,
MinSize: 0
}
});

View file

@ -0,0 +1,4 @@
window.QualityTypeCollection = Backbone.Collection.extend({
model: QualityType,
url: '/api/qualitytypes'
});

View file

@ -1,53 +0,0 @@
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

@ -107,15 +107,15 @@ QualityProfileCollectionView = Backbone.Marionette.CompositeView.extend({
//Todo: Need to get the default profile from the server, instead of creating it manually...
var newProfile = new QualityProfile({
Name: '', Cutoff: 0, Qualities: [
{ "Id": 0, "Weight": 0, "Name": "Unknown", "Allowed": false },
{ "Id": 1, "Weight": 1, "Name": "SDTV", "Allowed": false },
{ "Id": 8, "Weight": 2, "Name": "WEBDL-480p", "Allowed": false },
{ "Id": 2, "Weight": 3, "Name": "DVD", "Allowed": false },
{ "Id": 4, "Weight": 4, "Name": "HDTV", "Allowed": false },
{ "Id": 5, "Weight": 5, "Name": "WEBDL-720p", "Allowed": false },
{ "Id": 4, "Weight": 4, "Name": "HDTV-720p", "Allowed": false },
{ "Id": 9, "Weight": 5, "Name": "HDTV-1080p", "Allowed": false },
{ "Id": 5, "Weight": 6, "Name": "WEBDL-720p", "Allowed": false },
{ "Id": 3, "Weight": 6, "Name": "WEBDL-1080p", "Allowed": false },
{ "Id": 6, "Weight": 7, "Name": "Bluray720p", "Allowed": false },
{ "Id": 7, "Weight": 8, "Name": "Bluray1080p", "Allowed": false }
{ "Id": 6, "Weight": 8, "Name": "Bluray720p", "Allowed": false },
{ "Id": 7, "Weight": 9, "Name": "Bluray1080p", "Allowed": false }
]
});
//Todo: It would be nice to not have to save this on add (via create)

View file

@ -0,0 +1,29 @@
QualityTypeView = Backbone.Marionette.ItemView.extend({
tagName: "div",
className: "quality-type",
template: QualityTypeApp.Constants.Templates.QualityType,
events: {
'change .slider-value': 'changeSize'
},
changeSize: function (e) {
var target = $(e.target);
var maxSize = parseInt($(target).val());
this.model.set({ "MaxSize": maxSize });
this.model.save();
}
});
QualityTypeCollectionView = Backbone.Marionette.CompositeView.extend({
tagName: "div",
id: "quality-type-collection",
itemView: QualityTypeView,
template: QualityTypeApp.Constants.Templates.QualityTypeCollection,
initialize: function () {
_.bindAll(this, 'render');
this.collection = new QualityTypeCollection();
this.collection.fetch();
this.collection.bind('reset', this.render);
}
});