diff --git a/common/static/common/js/components/collections/paging_collection.js b/common/static/common/js/components/collections/paging_collection.js index ae93d25029..4ac4bf93b3 100644 --- a/common/static/common/js/components/collections/paging_collection.js +++ b/common/static/common/js/components/collections/paging_collection.js @@ -82,7 +82,7 @@ setPage: function (page) { var oldPage = this.currentPage, self = this; - this.goTo(page - (this.isZeroIndexed ? 1 : 0), {reset: true}).then( + return this.goTo(page - (this.isZeroIndexed ? 1 : 0), {reset: true}).then( function () { self.trigger('page_changed'); }, diff --git a/common/static/common/js/components/views/paging_header.js b/common/static/common/js/components/views/paging_header.js index bd5e741778..8cd01bb9a1 100644 --- a/common/static/common/js/components/views/paging_header.js +++ b/common/static/common/js/components/views/paging_header.js @@ -9,12 +9,16 @@ var PagingHeader = Backbone.View.extend({ initialize: function (options) { this.srInfo = options.srInfo; - this.collections = options.collection; + this.showSortControls = options.showSortControls; this.collection.bind('add', _.bind(this.render, this)); this.collection.bind('remove', _.bind(this.render, this)); this.collection.bind('reset', _.bind(this.render, this)); }, + events: { + 'change #paging-header-select': 'sortCollection' + }, + render: function () { var message, start = _.isUndefined(this.collection.start) ? 0 : this.collection.start, @@ -31,9 +35,18 @@ } this.$el.html(_.template(headerTemplate, { message: message, - srInfo: this.srInfo + srInfo: this.srInfo, + sortableFields: this.collection.sortableFields, + sortOrder: this.sortOrder, + showSortControls: this.showSortControls })); return this; + }, + + sortCollection: function () { + var selected = this.$('#paging-header-select option:selected'); + this.sortOrder = selected.attr('value'); + this.collection.setSortField(this.sortOrder); } }); return PagingHeader; diff --git a/common/static/common/js/spec/components/paging_header_spec.js b/common/static/common/js/spec/components/paging_header_spec.js index 2e664d449e..dee7efcb6e 100644 --- a/common/static/common/js/spec/components/paging_header_spec.js +++ b/common/static/common/js/spec/components/paging_header_spec.js @@ -8,7 +8,7 @@ define([ var pagingHeader, newCollection = function (size, perPage) { var pageSize = 5, - results = _.map(_.range(size), function () { return {}; }); + results = _.map(_.range(size), function (i) { return {foo: i}; }); var collection = new PagingCollection( { count: results.length, @@ -22,6 +22,14 @@ define([ collection.start = 0; collection.totalCount = results.length; return collection; + }, + sortableHeader = function (sortable) { + var collection = newCollection(5, 4); + collection.registerSortableField('foo', 'Display Name'); + return new PagingHeader({ + collection: collection, + showSortControls: _.isUndefined(sortable) ? true : sortable + }); }; it('correctly displays which items are being viewed', function () { @@ -47,5 +55,16 @@ define([ expect(pagingHeader.$el.find('.search-count').text()) .toContain('Showing 1 out of 1 total'); }); + + it('optionally shows sorting controls', function () { + pagingHeader = sortableHeader().render(); + expect(pagingHeader.$el.find('.listing-sort').text()) + .toMatch(/Sorted by\s+Display Name/); + }); + + it('does not show sorting controls if the `showSortControls` option is not passed', function () { + pagingHeader = sortableHeader(false).render(); + expect(pagingHeader.$el.text()).not.toContain('Sorted by'); + }); }); }); diff --git a/common/static/common/templates/components/paging-header.underscore b/common/static/common/templates/components/paging-header.underscore index 04a5d60e44..7484299b72 100644 --- a/common/static/common/templates/components/paging-header.underscore +++ b/common/static/common/templates/components/paging-header.underscore @@ -1,8 +1,21 @@ <% if (!_.isUndefined(srInfo)) { %>