Last active
August 29, 2015 14:04
-
-
Save tylerjohnst/df0de577fc7c5ed8aac7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.PaginationMixin = Ember.Mixin.create({ | |
// Define a property called "paginate" that returns the collection to paginate. | |
currentPage: 0, | |
perPage: 15, | |
collectionSize: Ember.computed.alias('paginate.length'), | |
actions: { | |
setPage: function(pageNumber) { | |
this.set('currentPage', pageNumber); | |
Ember.$(document).scrollTop(0); | |
} | |
}, | |
paginatedCollection: function() { | |
var props = this.getProperties('paginate', 'paginationStart', 'paginationStop'); | |
return props.paginate.slice(props.paginationStart, props.paginationStop); | |
}.property('paginate', 'paginationStart', 'paginationStop'), | |
needsPagiationLinks: Ember.computed.gt('numberOfPages', 1), | |
paginationStart: function() { | |
var props = this.getProperties('perPage', 'currentPage'); | |
return props.perPage * props.currentPage; | |
}.property('perPage', 'currentPage'), | |
paginationStop: function() { | |
var props = this.getProperties('paginationStart', 'perPage'); | |
return props.paginationStart + props.perPage; | |
}.property('paginationStart', 'perPage'), | |
numberOfPages: function() { | |
var props = this.getProperties('collectionSize', 'perPage'); | |
return Math.ceil(props.collectionSize / props.perPage); | |
}.property('collectionSize', 'perPage'), | |
paginationLinks: function() { | |
var props = this.getProperties('numberOfPages', 'currentPage'); | |
var links = []; | |
if ( props.currentPage > 0 ) { | |
links = links.concat([ | |
this.buildPaginationLink(0, 'pagination.first', false), | |
this.buildPaginationLink(props.currentPage - 1, 'pagination.previous', false) | |
]); | |
} | |
for (var i = 0; i < props.numberOfPages; i++) { | |
links.push( | |
this.buildPaginationLink(i, i + 1, props.currentPage === i) | |
); | |
} | |
if ( props.currentPage < props.numberOfPages - 1) { | |
links = links.concat([ | |
this.buildPaginationLink(props.currentPage + 1, 'pagination.next', false), | |
this.buildPaginationLink(props.numberOfPages - 1, 'pagination.last', false) | |
]); | |
} | |
return links; | |
}.property('numberOfPages', 'currentPage'), | |
buildPaginationLink: function(page, label, active) { | |
var translation = I18n.t("" + label); | |
if ( /missing/.test(translation) ) translation = label; | |
return { page: page, label: translation, active: active }; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment