Skip to content

Commit b505685

Browse files
committed
Merge pull request #102 from markberger/sort-by-downloads
Sort by downloads without letter restriction
2 parents 4dbc385 + c78f4a7 commit b505685

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

app/controllers/crates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import PaginationMixin from 'cargo/mixins/pagination';
44
export default Ember.ArrayController.extend(PaginationMixin, {
55
needs: ['application'],
66
queryParams: ['letter', 'page', 'per_page', 'sort'],
7-
letter: 'A',
7+
letter: null,
88
page: '1',
99
per_page: 10,
1010
sort: 'alpha',

app/mixins/pagination.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
import Ember from 'ember';
22

3+
var VIEWABLE_PAGES = 9;
4+
35
export default Ember.Mixin.create({
6+
7+
// Gives page numbers to the surrounding 9 pages.
48
pages: function() {
5-
var availablePages = this.get('availablePages');
69
var pages = [];
7-
for (var i = 0; i < availablePages; i++) {
10+
var currentPage = this.get('currentPage');
11+
var availablePages = this.get('availablePages');
12+
var lowerBound = 0;
13+
var upperBound = 0;
14+
15+
// Always show the same number of pages even if we're
16+
// at the beginning or at the end of the list.
17+
if (availablePages - currentPage < Math.ceil(VIEWABLE_PAGES / 2)) {
18+
lowerBound = Math.max(0, availablePages - VIEWABLE_PAGES);
19+
upperBound = availablePages;
20+
} else if (currentPage <= Math.ceil(VIEWABLE_PAGES / 2)) {
21+
lowerBound = 0;
22+
upperBound = Math.min(availablePages, VIEWABLE_PAGES);
23+
} else {
24+
lowerBound = currentPage - Math.ceil(VIEWABLE_PAGES / 2);
25+
upperBound = currentPage + Math.floor(VIEWABLE_PAGES / 2);
26+
}
27+
for (var i = lowerBound; i < upperBound; i++) {
828
pages.push(i + 1);
929
}
1030
return pages;
11-
}.property('availablePages'),
31+
}.property('currentPage', 'availablePages'),
1232

1333
currentPage: function() {
1434
return parseInt(this.get('selectedPage'), 10) || 1;

app/routes/crates.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export default Ember.Route.extend({
88
},
99

1010
model: function(params) {
11+
// The backend throws an error if the letter param is
12+
// empty or null.
13+
if(!params.letter) {
14+
delete params.letter;
15+
}
16+
1117
return this.store.find('crate', params);
1218
},
1319
});

app/templates/application.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
</div>
2020

2121
<div class='nav'>
22-
{{#link-to "crates"}}Browse All Crates{{/link-to}}
22+
{{#link-to "crates" (query-params letter="null" page=1)}}
23+
Browse All Crates
24+
{{/link-to}}
2325
<span class="sep">|</span>
2426
{{#if session.currentUser}}
2527
<div class='dropdown-container'>

app/templates/crates.hbs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
<img class='logo' src="/assets/crate.png"/>
44
<h1>All Crates</h1>
55
</div>
6-
<h2>starting with '{{ letter }}'</h2>
6+
{{#if letter}}
7+
<h2>starting with '{{ letter }}'</h2>
8+
{{/if}}
79
</div>
810

911
<div id='selection'>
@@ -12,7 +14,7 @@
1214
{{ this }}
1315
{{/link-to}}
1416
{{/each}}
15-
{{view Ember.Select content=alphabet value=letter}}
17+
{{view Ember.Select content=alphabet value=letter prompt="Filter by the letter..."}}
1618
</div>
1719

1820
<div id='results'>
@@ -35,12 +37,12 @@
3537
</a>
3638
<ul {{bind-attr class='showSortBy:open :dropdown'}}>
3739
<li>
38-
{{#link-to 'crates' (query-params sort="alpha")}}
40+
{{#link-to 'crates' (query-params page=1 sort="alpha")}}
3941
Alphabetical
4042
{{/link-to}}
4143
</li>
4244
<li>
43-
{{#link-to 'crates' (query-params sort="downloads")}}
45+
{{#link-to 'crates' (query-params page=1 sort="downloads")}}
4446
Downloads
4547
{{/link-to}}
4648
</li>

0 commit comments

Comments
 (0)