More backbone

This commit is contained in:
Mark McDowall 2012-11-24 00:03:38 -08:00
parent ccccb9a4cd
commit 0333da1062
9 changed files with 77 additions and 20 deletions

View File

@ -16,14 +16,17 @@ namespace NzbDrone.Api
//Mapper.CreateMap<QualityTypes, Int32>() //Mapper.CreateMap<QualityTypes, Int32>()
// .ForMember(dest => dest, opt => opt.ResolveUsing<QualityTypesToIntResolver>()); // .ForMember(dest => dest, opt => opt.ResolveUsing<QualityTypesToIntResolver>());
Mapper.CreateMap<Int32, QualityTypes>() //Mapper.CreateMap<Int32, QualityTypes>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src)); // .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src));
Mapper.CreateMap<QualityProfile, QualityProfileModel>() //Mapper.CreateMap<QualityProfile, QualityProfileModel>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId)); // .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.QualityProfileId));
Mapper.CreateMap<QualityProfileModel, QualityProfile>() //Mapper.CreateMap<QualityProfileModel, QualityProfile>()
.ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id)); // .ForMember(dest => dest.QualityProfileId, opt => opt.MapFrom(src => src.Id));
Mapper.CreateMap<QualityTypes, QualityProfileType>()
.ForMember(dest => dest.Allowed, opt => opt.Ignore());
} }
} }
} }

View File

@ -103,6 +103,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QualityProfiles\QualityProfileModel.cs" /> <Compile Include="QualityProfiles\QualityProfileModel.cs" />
<Compile Include="QualityProfiles\QualityProfileService.cs" /> <Compile Include="QualityProfiles\QualityProfileService.cs" />
<Compile Include="QualityProfiles\QualityProfileType.cs" />
<Compile Include="Resolvers\QualityTypesToIntResolver.cs" /> <Compile Include="Resolvers\QualityTypesToIntResolver.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -11,6 +11,6 @@ namespace NzbDrone.Api.QualityProfiles
public Int32 Id { get; set; } public Int32 Id { get; set; }
public String Name { get; set; } public String Name { get; set; }
public Int32 Cutoff { get; set; } public Int32 Cutoff { get; set; }
public List<Int32> Allowed { get; set; } public List<QualityProfileType> Qualities { get; set; }
} }
} }

View File

@ -29,11 +29,14 @@ namespace NzbDrone.Api.QualityProfiles
if (request.Id == 0) if (request.Id == 0)
{ {
var profiles = _qualityProvider.All(); var profiles = _qualityProvider.All();
return Mapper.Map<IEnumerable<QualityProfile>, IEnumerable<QualityProfileModel>>(profiles); var models = new List<QualityProfileModel>();
profiles.ForEach(p => models.Add(ToModel(p)));
return models;
} }
var profile = _qualityProvider.Get(request.Id); var profile = _qualityProvider.Get(request.Id);
return Mapper.Map<QualityProfile, QualityProfileModel>(profile); return ToModel(profile);
} }
public override object OnPost(QualityProfileModel data) public override object OnPost(QualityProfileModel data)
@ -58,5 +61,21 @@ namespace NzbDrone.Api.QualityProfiles
return "ok"; return "ok";
} }
public QualityProfileModel ToModel(QualityProfile profile)
{
var model = new QualityProfileModel();
model.Id = profile.QualityProfileId;
model.Name = profile.Name;
model.Cutoff = (int)profile.Cutoff;
model.Qualities = Mapper.Map<List<QualityTypes>, List<QualityProfileType>>(QualityTypes.All());
model.Qualities.ForEach(quality =>
{
quality.Allowed = profile.Allowed.SingleOrDefault(q => q.Id == quality.Id) != null;
});
return model;
}
} }
} }

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Api.QualityProfiles
{
public class QualityProfileType
{
public Int32 Id { get; set; }
public Int32 Weight { get; set; }
public String Name { get; set; }
public Boolean Allowed { get; set; }
}
}

View File

@ -2,7 +2,7 @@
urlRoot: '/api/qualityprofiles', urlRoot: '/api/qualityprofiles',
idAttribute: 'id', idAttribute: 'Id',
initialize: function () { initialize: function () {
this.validators = {}; this.validators = {};
@ -11,9 +11,9 @@
return value.length > 0 ? { isValid: true } : { isValid: false, message: 'You must enter a name' }; return value.length > 0 ? { isValid: true } : { isValid: false, message: 'You must enter a name' };
}; };
this.validators.allowed = function (value) { //this.validators.allowed = function (value) {
return value.length > 0 ? { isValid: true } : { isValid: false, message: 'You must have allowed qualities' }; // return value.length > 0 ? { isValid: true } : { isValid: false, message: 'You must have allowed qualities' };
}; //};
this.validators.cutoff = function (value) { this.validators.cutoff = function (value) {
return value != null ? { isValid: true } : { isValid: false, message: 'You must have a valid cutoff' }; return value != null ? { isValid: true } : { isValid: false, message: 'You must have a valid cutoff' };
@ -42,9 +42,9 @@
}, },
defaults: { defaults: {
id: null, Id: null,
name: '', Name: '',
allowed: {}, //allowed: {},
cutoff: null Cutoff: null
} }
}); });

View File

@ -1,7 +1,17 @@
QualityProfileView = Backbone.Marionette.ItemView.extend({ QualityProfileView = Backbone.Marionette.ItemView.extend({
tagName: "div", tagName: "div",
className: "quality-profile", className: "quality-profile",
template: "#QualityProfileTemplate" template: "#QualityProfileTemplate",
events: {
'click .quality-selectee': 'toggleAllowed'
},
toggleAllowed: function (e) {
//Add to cutoff
//Update model
var checked = $(e.target).attr('checked') != undefined;
this.model.set({ });
}
}); });
QualityProfileCollectionView = Backbone.Marionette.CompositeView.extend({ QualityProfileCollectionView = Backbone.Marionette.CompositeView.extend({

View File

@ -122,7 +122,16 @@
<script id="QualityProfileTemplate" type="text/template"> <script id="QualityProfileTemplate" type="text/template">
<%= Name %> <%= Name %>
<%= Cutoff %> <%= Cutoff %>
<%= Allowed %> <% _.each(Qualities, function(quality) { %>
<input id="<%= Id %>_<%= quality.Id %>"
class="quality-selectee"
type="checkbox"
value="<%= quality.Allowed %>"
data-quality-id="<%= quality.Id %>"
<%= quality.Allowed ? 'checked="checked"' : '' %>
/>
<label for="<%= Id %>_<%= quality.Id %>"><%= quality.Name %></label>
<% }); %>
</script> </script>
<script id="QualityProfileCollectionTemplate" type="text/template"> <script id="QualityProfileCollectionTemplate" type="text/template">