From 09d3dc3a32a5a23d58036c658d406788f6b40272 Mon Sep 17 00:00:00 2001 From: Josef Citrine Date: Mon, 16 May 2016 01:19:34 +0100 Subject: [PATCH] Pagination for admin track viewer --- public/templates/admin/tracks.html | 17 ++++++++ .../app/controllers/admin-tracks.coffee | 42 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/public/templates/admin/tracks.html b/public/templates/admin/tracks.html index 6f12f17a..72a5f353 100644 --- a/public/templates/admin/tracks.html +++ b/public/templates/admin/tracks.html @@ -1,3 +1,20 @@ + +
diff --git a/resources/assets/scripts/app/controllers/admin-tracks.coffee b/resources/assets/scripts/app/controllers/admin-tracks.coffee index 64262ba0..140ec3cb 100644 --- a/resources/assets/scripts/app/controllers/admin-tracks.coffee +++ b/resources/assets/scripts/app/controllers/admin-tracks.coffee @@ -31,4 +31,46 @@ module.exports = angular.module('ponyfm').controller "admin-tracks", [ ($scope, tracks, $state) -> tracks.mainQuery.fetch().done (searchResults) -> $scope.tracks = searchResults.tracks + + $scope.currentPage = parseInt(searchResults.current_page) + $scope.totalPages = parseInt(searchResults.total_pages) + delete $scope.nextPage + delete $scope.prevPage + + $scope.nextPage = $scope.currentPage + 1 if $scope.currentPage < $scope.totalPages + $scope.prevPage = $scope.currentPage - 1 if $scope.currentPage > 1 + $scope.allPages = [1..$scope.totalPages] + + # TODO: turn this into a directive + # The actual first page will always be in the paginator. + $scope.pages = [1] + + # This logic determines how many pages to add prior to the current page, if any. + firstPage = Math.max(2, $scope.currentPage-3) + $scope.pages = $scope.pages.concat [firstPage..$scope.currentPage] unless $scope.currentPage == 1 + + pagesLeftToAdd = 8-$scope.pages.length + + lastPage = Math.min($scope.totalPages - 1, $scope.currentPage+1+pagesLeftToAdd) + $scope.pages = $scope.pages.concat([$scope.currentPage+1..lastPage]) unless $scope.currentPage >= lastPage + + # The actual last page will always be in the paginator. + $scope.pages.push($scope.totalPages) unless $scope.totalPages in $scope.pages + + $scope.pageSelectorShown = false + + $scope.gotoPage = (page) -> + $state.transitionTo 'content.tracks.list', {filter: $state.params.filter, page: page} + + $scope.showPageSelector = () -> + $scope.pageSelectorShown = true + focus('#pagination-jump-destination') + + $scope.hidePageSelector = () -> + $scope.pageSelectorShown = false + + + $scope.jumpToPage = (inputPageNumber) -> + $scope.gotoPage(inputPageNumber) + ]