Skip to content

Sort by downloads without letter restriction #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/crates.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PaginationMixin from 'cargo/mixins/pagination';
export default Ember.ArrayController.extend(PaginationMixin, {
needs: ['application'],
queryParams: ['letter', 'page', 'per_page', 'sort'],
letter: 'A',
letter: null,
page: '1',
per_page: 10,
sort: 'alpha',
Expand Down
26 changes: 23 additions & 3 deletions app/mixins/pagination.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import Ember from 'ember';

var VIEWABLE_PAGES = 9;

export default Ember.Mixin.create({

// Gives page numbers to the surrounding 9 pages.
pages: function() {
var availablePages = this.get('availablePages');
var pages = [];
for (var i = 0; i < availablePages; i++) {
var currentPage = this.get('currentPage');
var availablePages = this.get('availablePages');
var lowerBound = 0;
var upperBound = 0;

// Always show the same number of pages even if we're
// at the beginning or at the end of the list.
if (availablePages - currentPage < Math.ceil(VIEWABLE_PAGES / 2)) {
lowerBound = Math.max(0, availablePages - VIEWABLE_PAGES);
upperBound = availablePages;
} else if (currentPage <= Math.ceil(VIEWABLE_PAGES / 2)) {
lowerBound = 0;
upperBound = Math.min(availablePages, VIEWABLE_PAGES);
} else {
lowerBound = currentPage - Math.ceil(VIEWABLE_PAGES / 2);
upperBound = currentPage + Math.floor(VIEWABLE_PAGES / 2);
}
for (var i = lowerBound; i < upperBound; i++) {
pages.push(i + 1);
}
return pages;
}.property('availablePages'),
}.property('currentPage', 'availablePages'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to refactor this such that the number of pages is a constant listed in one location? In theory all the other numbers would be based off that one constant.


currentPage: function() {
return parseInt(this.get('selectedPage'), 10) || 1;
Expand Down
6 changes: 6 additions & 0 deletions app/routes/crates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export default Ember.Route.extend({
},

model: function(params) {
// The backend throws an error if the letter param is
// empty or null.
if(!params.letter) {
delete params.letter;
}

return this.store.find('crate', params);
},
});
4 changes: 3 additions & 1 deletion app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
</div>

<div class='nav'>
{{#link-to "crates"}}Browse All Crates{{/link-to}}
{{#link-to "crates" (query-params letter="null" page=1)}}
Browse All Crates
{{/link-to}}
<span class="sep">|</span>
{{#if session.currentUser}}
<div class='dropdown-container'>
Expand Down
10 changes: 6 additions & 4 deletions app/templates/crates.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<img class='logo' src="/assets/crate.png"/>
<h1>All Crates</h1>
</div>
<h2>starting with '{{ letter }}'</h2>
{{#if letter}}
<h2>starting with '{{ letter }}'</h2>
{{/if}}
</div>

<div id='selection'>
Expand All @@ -12,7 +14,7 @@
{{ this }}
{{/link-to}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used by the "mobile site" which you can see if you shrink the page by a good bit, so this needs to stay

{{/each}}
{{view Ember.Select content=alphabet value=letter}}
{{view Ember.Select content=alphabet value=letter prompt="Filter by the letter..."}}
</div>

<div id='results'>
Expand All @@ -35,12 +37,12 @@
</a>
<ul {{bind-attr class='showSortBy:open :dropdown'}}>
<li>
{{#link-to 'crates' (query-params sort="alpha")}}
{{#link-to 'crates' (query-params page=1 sort="alpha")}}
Alphabetical
{{/link-to}}
</li>
<li>
{{#link-to 'crates' (query-params sort="downloads")}}
{{#link-to 'crates' (query-params page=1 sort="downloads")}}
Downloads
{{/link-to}}
</li>
Expand Down