From 4962db3b6a9b7e1cf946135ca76b7cad9776a658 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 16 Jan 2014 09:18:53 -0800 Subject: [PATCH] Fixed: Initial sorting for Next Airing --- src/UI/Mixins/AsPersistedStateCollection.js | 15 +++++++++++ src/UI/Series/Index/SeriesIndexLayout.js | 30 +++------------------ src/UI/Series/SeriesCollection.js | 19 +++++++++++-- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/src/UI/Mixins/AsPersistedStateCollection.js b/src/UI/Mixins/AsPersistedStateCollection.js index 2a957cc84..0a3be931d 100644 --- a/src/UI/Mixins/AsPersistedStateCollection.js +++ b/src/UI/Mixins/AsPersistedStateCollection.js @@ -63,6 +63,21 @@ define( return '1'; }; + _.extend(this.prototype, { + initialSort: function () { + var key = this.state.sortKey; + var order = this.state.order; + + if (this.sorters && this.sorters[key] && this.mode === 'client') { + var sortValue = this[key]; + + var comparator = this._makeComparator(key, order, sortValue); + this.fullCollection.comparator = comparator; + this.fullCollection.sort(); + } + } + }); + return this; }; } diff --git a/src/UI/Series/Index/SeriesIndexLayout.js b/src/UI/Series/Index/SeriesIndexLayout.js index adbd0a84c..52f382e02 100644 --- a/src/UI/Series/Index/SeriesIndexLayout.js +++ b/src/UI/Series/Index/SeriesIndexLayout.js @@ -17,8 +17,7 @@ define( 'Series/Index/FooterView', 'Series/Index/FooterModel', 'Shared/Toolbar/ToolbarLayout', - 'underscore', - 'moment' + 'underscore' ], function (Marionette, Backgrid, PosterCollectionView, @@ -35,8 +34,7 @@ define( FooterView, FooterModel, ToolbarLayout, - _, - Moment) { + _) { return Marionette.Layout.extend({ template: 'Series/Index/SeriesIndexLayoutTemplate', @@ -77,15 +75,7 @@ define( name : 'nextAiring', label : 'Next Airing', cell : RelativeDateCell, - sortValue : function (model) { - var nextAiring = model.get('nextAiring'); - - if (!nextAiring) { - return Number.MAX_VALUE; - } - - return Moment(nextAiring).unix(); - } + sortValue : SeriesCollection.sorters.nextAiring }, { name : 'percentOfEpisodes', @@ -162,15 +152,7 @@ define( { title : 'Next Airing', name : 'nextAiring', - sortValue : function (model) { - var nextAiring = model.get('nextAiring'); - - if (!nextAiring) { - return Number.MAX_VALUE; - } - - return Moment(nextAiring).unix(); - } + sortValue : SeriesCollection.sorters.nextAiring }, { title: 'Episodes', @@ -272,10 +254,6 @@ define( this.viewButtons ]; - if (this.showSortingButton) { - rightButtons.splice(0, 0, this.sortingOptions); - } - rightButtons.splice(0, 0, this.sortingOptions); this.toolbar.show(new ToolbarLayout({ diff --git a/src/UI/Series/SeriesCollection.js b/src/UI/Series/SeriesCollection.js index 2c7b049f8..1b48ba6e5 100644 --- a/src/UI/Series/SeriesCollection.js +++ b/src/UI/Series/SeriesCollection.js @@ -6,8 +6,9 @@ define( 'backbone.pageable', 'Series/SeriesModel', 'api!series', - 'Mixins/AsPersistedStateCollection' - ], function (_, Backbone, PageableCollection, SeriesModel, SeriesData, AsPersistedStateCollection) { + 'Mixins/AsPersistedStateCollection', + 'moment' + ], function (_, Backbone, PageableCollection, SeriesModel, SeriesData, AsPersistedStateCollection, Moment) { var Collection = PageableCollection.extend({ url : window.NzbDrone.ApiRoot + '/series', model: SeriesModel, @@ -19,6 +20,18 @@ define( pageSize: 1000 }, + sorters: { + nextAiring: function (model) { + var nextAiring = model.get('nextAiring'); + + if (!nextAiring) { + return Number.MAX_VALUE; + } + + return Moment(nextAiring).unix(); + } + }, + mode: 'client', save: function () { @@ -49,5 +62,7 @@ define( var MixedIn = AsPersistedStateCollection.call(Collection); var collection = new MixedIn(SeriesData); + collection.initialSort(); + return collection; });