Factory paginator scope postRequest throttle scope spinner true http p

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
}