From ecc8ece1ce8d66164f4315c3342fdc878943a11f Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 13 Dec 2014 00:12:26 -0800 Subject: [PATCH] Secondary sorting on series list New: Sort by title as secondary sort on series list --- src/UI/Mixins/AsSortedCollection.js | 95 +++++++++++++++++++++++++++-- src/UI/Series/SeriesCollection.js | 8 ++- 2 files changed, 96 insertions(+), 7 deletions(-) diff --git a/src/UI/Mixins/AsSortedCollection.js b/src/UI/Mixins/AsSortedCollection.js index d49aa216f..8bf11d313 100644 --- a/src/UI/Mixins/AsSortedCollection.js +++ b/src/UI/Mixins/AsSortedCollection.js @@ -6,6 +6,16 @@ define( return function () { + 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); + }; + this.prototype._getSortMappings = function () { var result = {}; @@ -30,13 +40,90 @@ define( return sortMappings[key] || { name: key, sortKey: key }; }; - var originalSetSorting = this.prototype.setSorting; - this.prototype.setSorting = function (sortKey, order, options) { + this.prototype._getSecondarySorting = function () { + var sortKey = this.state.secondarySortKey; + var sortOrder = this.state.secondarySortOrder || -1; + + if (!sortKey || sortKey === this.state.sortKey) { + return null; + } + var sortMapping = this._getSortMapping(sortKey); - options = _.defaults({ sortValue: sortMapping.sortValue }, options || {}); + if (!sortMapping.sortValue) { + sortMapping.sortValue = function (model, attr) { + return model.get(attr); + }; + } - return originalSetSorting.call(this, sortMapping.sortKey, order, options); + return { + key : sortKey, + order : sortOrder, + sortValue : sortMapping.sortValue + }; + }; + + this.prototype._makeComparator = function (sortKey, order, sortValue) { + var state = this.state; + var secondarySorting = this._getSecondarySorting(); + + sortKey = sortKey || state.sortKey; + order = order || state.order; + + if (!sortKey || !order){ + return; + } + + if (!sortValue) { + sortValue = function (model, attr) { + return model.get(attr); + }; + } + + return function (left, right) { + var l = sortValue(left, sortKey); + var r = sortValue(right, sortKey); + var t; + + if (order === 1) { + t = l; + l = r; + r = t; + } + + if (l === r) { + + if (secondarySorting) { + var ls = secondarySorting.sortValue(left, secondarySorting.key); + var rs = secondarySorting.sortValue(right, secondarySorting.key); + var ts; + + if (secondarySorting.order === 1) { + ts = ls; + ls = rs; + rs = ts; + } + + if (ls === rs) { + return 0; + } + + if (ls < rs) { + return -1; + } + + return 1; + } + + return 0; + } + + else if (l < r) { + return -1; + } + + return 1; + }; }; return this; diff --git a/src/UI/Series/SeriesCollection.js b/src/UI/Series/SeriesCollection.js index 666bc8585..99dd502c8 100644 --- a/src/UI/Series/SeriesCollection.js +++ b/src/UI/Series/SeriesCollection.js @@ -17,9 +17,11 @@ define( tableName: 'series', state: { - sortKey: 'sortTitle', - order : -1, - pageSize: 100000 + sortKey : 'sortTitle', + order : -1, + pageSize : 100000, + secondarySortKey : 'sortTitle', + secondarySortOrder : -1 }, mode: 'client',