From bdab0d585f746bf4a89a07195ca131994bf4f761 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 31 Jul 2013 19:12:39 -0700 Subject: [PATCH] Added DateHeaderCell to prevent drunk sorting on null dates --- UI/Series/Index/SeriesIndexLayout.js | 9 ++-- UI/Shared/Grid/DateHeaderCell.js | 66 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 UI/Shared/Grid/DateHeaderCell.js diff --git a/UI/Series/Index/SeriesIndexLayout.js b/UI/Series/Index/SeriesIndexLayout.js index 7d4738f60..564fe4942 100644 --- a/UI/Series/Index/SeriesIndexLayout.js +++ b/UI/Series/Index/SeriesIndexLayout.js @@ -10,6 +10,7 @@ define( 'Cells/SeriesTitleCell', 'Cells/TemplatedCell', 'Cells/QualityProfileCell', + 'Shared/Grid/DateHeaderCell', 'Series/Index/Table/SeriesStatusCell', 'Series/Index/Table/Row', 'Series/Index/FooterView', @@ -25,6 +26,7 @@ define( SeriesTitleCell, TemplatedCell, QualityProfileCell, + DateHeaderCell, SeriesStatusCell, SeriesIndexRow, FooterView, @@ -69,9 +71,10 @@ define( cell : 'string' }, { - name : 'nextAiring', - label: 'Next Airing', - cell : RelativeDateCell + name : 'nextAiring', + label : 'Next Airing', + cell : RelativeDateCell, + headerCell: DateHeaderCell }, { name : 'this', diff --git a/UI/Shared/Grid/DateHeaderCell.js b/UI/Shared/Grid/DateHeaderCell.js new file mode 100644 index 000000000..5473f1093 --- /dev/null +++ b/UI/Shared/Grid/DateHeaderCell.js @@ -0,0 +1,66 @@ +'use strict'; + +define( + [ + 'backgrid', + 'Shared/Grid/HeaderCell' + ], function (Backgrid, NzbDroneHeaderCell) { + + Backgrid.DateHeaderCell = NzbDroneHeaderCell.extend({ + events: { + 'click': 'onClick' + }, + + onClick: function (e) { + e.preventDefault(); + + var self = this; + var columnName = this.column.get('name'); + + if (this.column.get('sortable')) { + if (this.direction() === 'ascending') { + this.sort(columnName, 'descending', function (left, right) { + var leftVal = left.get(columnName); + var rightVal = right.get(columnName); + + return self._comparator(leftVal, rightVal) + }); + } + else { + this.sort(columnName, 'ascending', function (left, right) { + var leftVal = left.get(columnName); + var rightVal = right.get(columnName); + + return self._comparator(rightVal, leftVal) + }); + } + } + }, + + _comparator: function (leftVal, rightVal) { + if (!leftVal && !rightVal) { + return 0 + } + + if (!leftVal) { + return -1; + } + + if (!rightVal) { + return 1 + } + + if (leftVal === rightVal) { + return 0; + } + + if (leftVal > rightVal) { + return -1; + } + + return 1; + } + }); + + return Backgrid.DateHeaderCell; + });