Factory paginator
$scope.postRequest = _.throttle () ->
$scope.spinner = true
$http.post '/finder',
type : $scope.type
sport : $scope.sport
city : $scope.city
bounds : myMap.getBounds()
boundsCheck : if $rootScope.btnBindBounds then true else null
requestParam : 'first'
part:
skip : 0
limit: itemsOnPage
.error () ->
console.log 'Error post request /finderRequest', arguments
.success (data) ->
$scope.spinner = false
$scope.totalCount = data.totalCount
$scope.pageCount = Number data.pageCount
$scope.selectedPage = data.selectedPage
$scope.user = data.user
pagination.setStuffElements(data.stuffElements, data.totalCount)
$scope.allStuffElements = data.stuffElements
$scope.stuffElements = pagination.getPageElements($scope.selectedPage)
$scope.paginationList = pagination.getPaginationList()
$scope.initMapObjects(true, $scope.stuffElements)
if $scope.totalCount > itemsOnPage
PART_SIZE = 1000
fullCountParts = Math.floor($scope.totalCount/PART_SIZE)
arrayOfParts = _.range(fullCountParts+1)
asyncEachSeries arrayOfParts, (i, cb) ->
skip = 0
limit = 0
if i is 0
skip = itemsOnPage
limit = (PART_SIZE - itemsOnPage)
else if i < fullCountParts
skip = i*PART_SIZE
limit = PART_SIZE
else
skip = fullCountParts*PART_SIZE
limit = (data.totalCount - fullCountParts*PART_SIZE)
$http.post '/finder',
type : $scope.type
sport : $scope.sport
city : $scope.city
bounds : myMap.getBounds()
boundsCheck : if $rootScope.btnBindBounds then true else null
requestParam : 'part'
part:
skip : skip
limit: limit
detailsCount: data.detailsCount
.error () ->
console.log 'Error second post request /finderRequest', arguments
.success (dataReminder) ->
$scope.allStuffElements = $scope.allStuffElements.concat dataReminder.stuffElements
pagination.setStuffElements($scope.allStuffElements, data.totalCount)
$scope.paginationList = pagination.getPaginationList()
cb()
, (err) ->
if !!err
console.log 'Request upload reminder error, ', err
, postRequestTimeout
#
# paginator controllers
# ---------------------------------
$scope.getSelectedPageNum = () -> return pagination.getSelectedPageNum()
$scope.showPage = (page) ->
if page isnt $scope.selectedPage
if page is 'prev'
$scope.stuffElements = pagination.getPrevPageStuff()
else if page is 'next'
$scope.stuffElements = pagination.getNextPageStuff()
else
$scope.stuffElements = pagination.getPageElements(page)
$scope.initMapObjects(true, $scope.stuffElements)
$scope.selectedPage = pagination.getSelectedPageNum()
# ----------------------------------
# -- end of paginator controllers --
#
# factory Paginator with self methods
#
.factory 'pagination', () ->
selectedPage = 0
totalCount = 0
stuffElements = []
return {
setStuffElements: (newStuffElements, newTotalCount) ->
stuffElements = newStuffElements
totalCount = newTotalCount
getPageElements: (num) ->
num = if !num or Number(num) is 0 then 0 else (Number(num) - 1)
firstEl = itemsOnPage * num
lastEl = itemsOnPage + firstEl
selectedPage = num + 1
lastEl = if lastEl > stuffElements?.length then stuffElements.length else lastEl
return stuffElements.slice(firstEl, lastEl)
getTotalPagesNum: () ->
return Math.ceil(totalCount / itemsOnPage)
getPaginationList: () ->
pagesNum = this.getTotalPagesNum()
paginationList = []
i = 1
while i <= pagesNum
paginationList.push i
i++
if pagesNum > 1
return paginationList
else
return null
getPrevPageStuff: () ->
prevPageNum = selectedPage - 1
if prevPageNum < 0
prevPageNum = 0
return this.getPageElements(prevPageNum)
getNextPageStuff: () ->
nextPageNum = selectedPage + 1
pagesNum = this.getTotalPagesNum()
if nextPageNum > pagesNum
nextPageNum = pagesNum - 1
return this.getPageElements(nextPageNum)
getSelectedPageNum: () ->
return selectedPage
}