Updated sorting architecture so the collections can specify the sort-oddities instead of in backgrid column definitions.

pull/3113/head
Taloth Saldono 10 years ago
parent 2b6f908038
commit 6941888832

@ -25,12 +25,6 @@ namespace NzbDrone.Api.Blacklist
SortDirection = pagingResource.SortDirection
};
//This is a hack to deal with backgrid setting the sortKey to the column name instead of sortValue
if (pagingSpec.SortKey.Equals("series", StringComparison.InvariantCultureIgnoreCase))
{
pagingSpec.SortKey = "series.sortTitle";
}
return ApplyToPage(_blacklistService.Paged, pagingSpec);
}

@ -33,12 +33,6 @@ namespace NzbDrone.Api.History
SortDirection = pagingResource.SortDirection
};
//This is a hack to deal with backgrid setting the sortKey to the column name instead of sortValue
if (pagingSpec.SortKey.Equals("series", StringComparison.InvariantCultureIgnoreCase))
{
pagingSpec.SortKey = "series.sortTitle";
}
if (pagingResource.FilterKey == "eventType")
{
var filterValue = (HistoryEventType)Convert.ToInt32(pagingResource.FilterValue);

@ -31,12 +31,6 @@ namespace NzbDrone.Api.Wanted
SortDirection = pagingResource.SortDirection
};
//This is a hack to deal with backgrid setting the sortKey to the column name instead of sortValue
if (pagingSpec.SortKey.Equals("series", StringComparison.InvariantCultureIgnoreCase))
{
pagingSpec.SortKey = "series.sortTitle";
}
if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false")
{
pagingSpec.FilterExpression = v => v.Monitored == false || v.Series.Monitored == false;

@ -31,12 +31,6 @@ namespace NzbDrone.Api.Wanted
SortDirection = pagingResource.SortDirection
};
//This is a hack to deal with backgrid setting the sortKey to the column name instead of sortValue
if (pagingSpec.SortKey.Equals("series", StringComparison.InvariantCultureIgnoreCase))
{
pagingSpec.SortKey = "series.sortTitle";
}
if (pagingResource.FilterKey == "monitored" && pagingResource.FilterValue == "false")
{
pagingSpec.FilterExpression = v => v.Monitored == false || v.Series.Monitored == false;

@ -3,9 +3,10 @@ define(
[
'History/Blacklist/BlacklistModel',
'backbone.pageable',
'Mixins/AsSortedCollection',
'Mixins/AsPersistedStateCollection'
], function (BlacklistModel, PageableCollection, AsPersistedStateCollection) {
var collection = PageableCollection.extend({
], function (BlacklistModel, PageableCollection, AsSortedCollection, AsPersistedStateCollection) {
var Collection = PageableCollection.extend({
url : window.NzbDrone.ApiRoot + '/blacklist',
model: BlacklistModel,
@ -27,6 +28,10 @@ define(
}
},
sortMappings: {
'series' : { sortKey: 'series.sortTitle' }
},
parseState: function (resp) {
return { totalRecords: resp.totalRecords };
},
@ -40,5 +45,6 @@ define(
}
});
return AsPersistedStateCollection.apply(collection);
Collection = AsSortedCollection.call(Collection);
return AsPersistedStateCollection.call(Collection);
});

@ -37,14 +37,12 @@ define(
{
name : 'series',
label : 'Series',
cell : SeriesTitleCell,
sortValue : 'series.sortTitle'
cell : SeriesTitleCell
},
{
name : 'sourceTitle',
label : 'Source Title',
cell : 'string',
sortValue : 'sourceTitle'
cell : 'string'
},
{
name : 'quality',

@ -4,9 +4,10 @@ define(
'History/HistoryModel',
'backbone.pageable',
'Mixins/AsFilteredCollection',
'Mixins/AsSortedCollection',
'Mixins/AsPersistedStateCollection'
], function (HistoryModel, PageableCollection, AsFilteredCollection, AsPersistedStateCollection) {
var collection = PageableCollection.extend({
], function (HistoryModel, PageableCollection, AsFilteredCollection, AsSortedCollection, AsPersistedStateCollection) {
var Collection = PageableCollection.extend({
url : window.NzbDrone.ApiRoot + '/history',
model: HistoryModel,
@ -35,6 +36,10 @@ define(
'failed' : ['eventType', '4']
},
sortMappings: {
'series' : { sortKey: 'series.sortTitle' }
},
initialize: function (options) {
delete this.queryParams.episodeId;
@ -58,6 +63,7 @@ define(
}
});
collection = AsFilteredCollection.call(collection);
return AsPersistedStateCollection.call(collection);
Collection = AsFilteredCollection.call(Collection);
Collection = AsSortedCollection.call(Collection);
return AsPersistedStateCollection.call(Collection);
});

@ -47,8 +47,7 @@ define(
{
name : 'series',
label : 'Series',
cell : SeriesTitleCell,
sortValue : 'series.sortTitle'
cell : SeriesTitleCell
},
{
name : 'episode',

@ -7,7 +7,6 @@ define(
return function () {
var originalInit = this.prototype.initialize;
this.prototype.initialize = function (options) {
options = options || {};
@ -30,18 +29,24 @@ define(
}
};
if (!this.prototype._getSortMapping) {
this.prototype._getSortMapping = function(key) {
return { name: key, sortKey: key };
};
}
var _setInitialState = function () {
var key = Config.getValue('{0}.sortKey'.format(this.tableName), this.state.sortKey);
var direction = Config.getValue('{0}.sortDirection'.format(this.tableName), this.state.order);
var order = parseInt(direction, 10);
this.state.sortKey = key;
this.state.sortKey = this._getSortMapping(key).sortKey;
this.state.order = order;
};
var _storeStateFromBackgrid = function (column, sortDirection) {
var order = _convertDirectionToInt(sortDirection);
var sortKey = column.has('sortValue') && _.isString(column.get('sortValue')) ? column.get('sortValue') : column.get('name');
var sortKey = this._getSortMapping(column.get('name')).sortKey;
Config.setValue('{0}.sortKey'.format(this.tableName), sortKey);
Config.setValue('{0}.sortDirection'.format(this.tableName), order);
@ -49,7 +54,7 @@ define(
var _storeState = function (sortModel, sortDirection) {
var order = _convertDirectionToInt(sortDirection);
var sortKey = sortModel.get('name');
var sortKey = this._getSortMapping(sortModel.get('name')).sortKey;
Config.setValue('{0}.sortKey'.format(this.tableName), sortKey);
Config.setValue('{0}.sortDirection'.format(this.tableName), order);
@ -62,20 +67,6 @@ define(
return '1';
};
var originalMakeComparator = this.prototype._makeComparator;
this.prototype._makeComparator = function (sortKey, order, sortValue) {
var state = this.state;
sortKey = sortKey || state.sortKey;
order = order || state.order;
if (!sortKey || !order) return;
if (!sortValue && this[sortKey]) sortValue = this[sortKey];
return originalMakeComparator.call(this, sortKey, order, sortValue);
};
return this;
};

@ -0,0 +1,45 @@
'use strict';
define(
['underscore', 'Config'],
function (_, Config) {
return function () {
this.prototype._getSortMappings = function () {
var result = {};
if (this.sortMappings) {
_.each(this.sortMappings, function (values, key) {
var item = {
name: key,
sortKey: values.sortKey || key,
sortValue: values.sortValue
};
result[key] = item;
result[item.sortKey] = item;
});
}
return result;
};
this.prototype._getSortMapping = function (key) {
var sortMappings = this._getSortMappings();
return sortMappings[key] || { name: key, sortKey: key };
};
var originalSetSorting = this.prototype.setSorting;
this.prototype.setSorting = function (sortKey, order, options) {
var sortMapping = this._getSortMapping(sortKey);
options = _.defaults({ sortValue: sortMapping.sortValue }, options || {});
return originalSetSorting.call(this, sortMapping.sortKey, order, options);
};
return this;
};
}
);

@ -60,8 +60,7 @@ define(
name : 'title',
label : 'Title',
cell : SeriesTitleCell,
cellValue : 'this',
sortValue : 'sortTitle'
cellValue : 'this'
},
{
name : 'qualityProfileId',

@ -77,8 +77,7 @@ define(
{
name : 'nextAiring',
label : 'Next Airing',
cell : RelativeDateCell,
sortValue : SeriesCollection.nextAiring
cell : RelativeDateCell
},
{
name : 'percentOfEpisodes',
@ -173,9 +172,8 @@ define(
name : 'network'
},
{
title : 'Next Airing',
name : 'nextAiring',
sortValue : SeriesCollection.nextAiring
title: 'Next Airing',
name : 'nextAiring'
},
{
title: 'Episodes',

@ -7,16 +7,17 @@ define(
'Series/SeriesModel',
'api!series',
'Mixins/AsFilteredCollection',
'Mixins/AsSortedCollection',
'Mixins/AsPersistedStateCollection',
'moment'
], function (_, Backbone, PageableCollection, SeriesModel, SeriesData, AsFilteredCollection, AsPersistedStateCollection, Moment) {
], function (_, Backbone, PageableCollection, SeriesModel, SeriesData, AsFilteredCollection, AsSortedCollection, AsPersistedStateCollection, Moment) {
var Collection = PageableCollection.extend({
url : window.NzbDrone.ApiRoot + '/series',
model: SeriesModel,
tableName: 'series',
state: {
sortKey: 'title',
sortKey: 'sortTitle',
order : -1,
pageSize: 100000
},
@ -56,27 +57,30 @@ define(
'monitored' : ['monitored', true]
},
//Sorters
nextAiring: function (model, attr) {
var nextAiring = model.get(attr);
if (nextAiring) {
return Moment(nextAiring).unix();
}
var previousAiring = model.get(attr.replace('nextAiring', 'previousAiring'));
if (previousAiring) {
return 10000000000 - Moment(previousAiring).unix();
}
sortMappings: {
'title' : { sortKey: 'sortTitle' },
'nextAiring' : { sortValue: function (model, attr) {
var nextAiring = model.get(attr);
if (nextAiring) {
return Moment(nextAiring).unix();
}
var previousAiring = model.get(attr.replace('nextAiring', 'previousAiring'));
if (previousAiring) {
return 10000000000 - Moment(previousAiring).unix();
}
return Number.MAX_VALUE;
return Number.MAX_VALUE;
}
}
}
});
var FilteredCollection = AsFilteredCollection.call(Collection);
var MixedIn = AsPersistedStateCollection.call(FilteredCollection);
var collection = new MixedIn(SeriesData, { full: true });
Collection = AsFilteredCollection.call(Collection);
Collection = AsSortedCollection.call(Collection);
Collection = AsPersistedStateCollection.call(Collection);
return collection;
return new Collection(SeriesData, { full: true });
});

@ -39,13 +39,12 @@ define(
this.direction(column.get('direction'));
if (this.collection.state) {
var key = this.collection.state.sortKey;
var name = this._getSortMapping().name;
var order = this.collection.state.order;
if (key === this.column.get('name')) {
if (name === column.get('name')) {
this._setSortIcon(order);
}
else {
this._removeSortIcon();
}
@ -69,10 +68,10 @@ define(
var columnDirection = this.column.get('direction');
if (!columnDirection && this.collection.state) {
var key = this.collection.state.sortKey;
var name = this._getSortMapping().name;
var order = this.collection.state.order;
if (key === this.column.get('name')) {
if (name === this.column.get('name')) {
columnDirection = order;
}
}
@ -80,31 +79,41 @@ define(
return columnDirection;
},
_getSortMapping: function() {
var sortKey = this.collection.state.sortKey;
if (this.collection._getSortMapping) {
return this.collection._getSortMapping(sortKey);
}
return { name: sortKey, sortKey: sortKey };
},
onClick: function (e) {
e.preventDefault();
var collection = this.collection;
var event = 'backgrid:sort';
function toggleSort(header, col) {
collection.state.sortKey = col.get('name');
var direction = header.direction();
if (direction === 'ascending' || direction === -1)
{
collection.state.order = 'descending';
collection.trigger(event, col, 'descending');
var column = this.column;
var sortable = Backgrid.callByNeed(column.sortable(), column, collection);
if (sortable) {
var direction = collection.state.order;
if (direction === 'ascending' || direction === -1) {
direction = 'descending';
}
else
{
collection.state.order = 'ascending';
collection.trigger(event, col, 'ascending');
else {
direction = 'ascending';
}
}
var column = this.column;
var sortable = Backgrid.callByNeed(column.sortable(), column, this.collection);
if (sortable) {
toggleSort(this, column);
if (collection.setSorting) {
collection.setSorting(column.get('name'), direction);
}
else {
collection.state.sortKey = column.get('name');
collection.state.order = direction;
}
collection.trigger(event, column, direction);
}
},

@ -10,20 +10,6 @@ define(
'title' : '',
'active' : false,
'tooltip': undefined
},
sortValue: function () {
var sortValue = this.get('sortValue');
if (_.isString(sortValue)) {
return this[sortValue];
}
else if (_.isFunction(sortValue)) {
return sortValue;
}
return function (model, colName) {
return model.get(colName);
};
}
});
});

@ -34,52 +34,11 @@ define(
else {
order = null;
}
var comparator = this.makeComparator(sortModel.get('name'), order,
order ?
sortModel.sortValue() :
function (model) {
return model.cid;
});
if (PageableCollection &&
collection instanceof PageableCollection) {
collection.setSorting(order && sortModel.get('name'), order,
{sortValue: sortModel.sortValue()});
if (collection.mode === 'client') {
if (collection.fullCollection.comparator === null) {
collection.fullCollection.comparator = comparator;
}
collection.fullCollection.sort();
}
else {
collection.fetch({reset: true});
}
}
else {
collection.comparator = comparator;
collection.sort();
}
collection.setSorting(sortModel.get('name'), order);
collection.fullCollection.sort();
return this;
},
makeComparator: function (attr, order, func) {
return function (left, right) {
// extract the values from the models
var l = func(left, attr), r = func(right, attr), t;
// if descending order, swap left and right
if (order === 1) t = l, l = r, r = t;
// compare as usual
if (l === r) return 0;
else if (l < r) return -1;
return 1;
};
}
});
});

@ -26,13 +26,13 @@ define(
onRender: function () {
if (this.viewCollection.state) {
var key = this.viewCollection.state.sortKey;
var sortKey = this.viewCollection.state.sortKey;
var name = this.viewCollection._getSortMapping(sortKey).name;
var order = this.viewCollection.state.order;
if (key === this.model.get('name')) {
if (name === this.model.get('name')) {
this._setSortIcon(order);
}
else {
this._removeSortIcon();
}
@ -45,19 +45,16 @@ define(
var collection = this.viewCollection;
var event = 'drone:sort';
collection.state.sortKey = this.model.get('name');
var direction = collection.state.order;
if (direction === 'ascending' || direction === -1)
{
collection.state.order = 'descending';
collection.trigger(event, this.model, 'descending');
if (direction === 'ascending' || direction === -1) {
direction = 'descending';
}
else
{
collection.state.order = 'ascending';
collection.trigger(event, this.model, 'ascending');
else {
direction = 'ascending';
}
collection.setSorting(this.model.get('name'), direction);
collection.trigger(event, this.model, direction);
},
_convertDirectionToIcon: function (dir) {

@ -38,12 +38,14 @@ define(
{
name : 'filename',
label: 'Filename',
cell : FilenameCell
cell : FilenameCell,
sortable: false
},
{
name : 'lastWriteTime',
label: 'Last Write Time',
cell : RelativeDateCell
cell : RelativeDateCell,
sortable: false
},
{
name : 'downloadUrl',

@ -5,9 +5,10 @@ define(
'Series/EpisodeModel',
'backbone.pageable',
'Mixins/AsFilteredCollection',
'Mixins/AsSortedCollection',
'Mixins/AsPersistedStateCollection'
], function (_, EpisodeModel, PagableCollection, AsFilteredCollection, AsPersistedStateCollection) {
var collection = PagableCollection.extend({
], function (_, EpisodeModel, PagableCollection, AsFilteredCollection, AsSortedCollection, AsPersistedStateCollection) {
var Collection = PagableCollection.extend({
url : window.NzbDrone.ApiRoot + '/wanted/cutoff',
model: EpisodeModel,
tableName: 'wanted.cutoff',
@ -36,6 +37,10 @@ define(
'unmonitored' : ['monitored', 'false'],
},
sortMappings: {
'series' : { sortKey: 'series.sortTitle' }
},
parseState: function (resp) {
return {totalRecords: resp.totalRecords};
},
@ -49,6 +54,7 @@ define(
}
});
collection = AsFilteredCollection.call(collection);
return AsPersistedStateCollection.call(collection);
Collection = AsFilteredCollection.call(Collection);
Collection = AsSortedCollection.call(Collection);
return AsPersistedStateCollection.call(Collection);
});

@ -5,9 +5,10 @@ define(
'Series/EpisodeModel',
'backbone.pageable',
'Mixins/AsFilteredCollection',
'Mixins/AsSortedCollection',
'Mixins/AsPersistedStateCollection'
], function (_, EpisodeModel, PagableCollection, AsFilteredCollection, AsPersistedStateCollection) {
var collection = PagableCollection.extend({
], function (_, EpisodeModel, PagableCollection, AsFilteredCollection, AsSortedCollection, AsPersistedStateCollection) {
var Collection = PagableCollection.extend({
url : window.NzbDrone.ApiRoot + '/wanted/missing',
model: EpisodeModel,
tableName: 'wanted.missing',
@ -35,6 +36,10 @@ define(
'unmonitored' : ['monitored', 'false']
},
sortMappings: {
'series' : { sortKey: 'series.sortTitle' }
},
parseState: function (resp) {
return {totalRecords: resp.totalRecords};
},
@ -48,6 +53,7 @@ define(
}
});
collection = AsFilteredCollection.call(collection);
return AsPersistedStateCollection.call(collection);
Collection = AsFilteredCollection.call(Collection);
Collection = AsSortedCollection.call(Collection);
return AsPersistedStateCollection.call(Collection);
});

Loading…
Cancel
Save