kiwi-pagination
Version:
Kiwi's plugin for pagination
41 lines (34 loc) • 1.24 kB
text/coffeescript
Chaplin = require 'chaplin'
template = require './templates/pagination'
module.exports = (options) ->
class PaginationView extends Chaplin.View
autoRender: no
tagName: 'ul'
containerMethod: 'html'
template: options.template or template
events:
'click li>a': 'changePage'
initialize: ->
super
opt = .getAttributes()
last = Math.ceil opt.total / opt.size
start = Math.max 1, opt.page - opt.left
end = Math.min last, start + opt.left + opt.right
start = Math.max 1, end - opt.left - opt.right
pages = [start..end]
.set 'prev', opt.page - 1
.set 'next', if opt.page is last then 0 else opt.page + 1
.set 'first', if 1 in pages then 0 else 1
.set 'last', if last in pages then 0 else last
pages.shift() if 1 not in pages
pages.pop() if last not in pages
.set 'pages', pages
changePage: (e) ->
cur = $ e.currentTarget || e
curPage = .get "page"
page = cur.data "page"
return if page is undefined or page.length is 0
page = utils.intval page
return if curPage is page
'ChangePage', page: page