From b1a5a0dd5dc0a7910c305b90c7084766f5a0d729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Thu, 9 Aug 2012 17:30:58 +0000 Subject: Pagination MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Asbjørn Sloth Tønnesen --- web/js/labitrack.d/.gitignore | 1 + web/js/labitrack.d/39-pagination.coffee | 103 ++++++++++++++++++++++++++++++++ web/js/labitrack.d/43-browse.js | 43 ++++--------- 3 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 web/js/labitrack.d/.gitignore create mode 100644 web/js/labitrack.d/39-pagination.coffee (limited to 'web/js') diff --git a/web/js/labitrack.d/.gitignore b/web/js/labitrack.d/.gitignore new file mode 100644 index 0000000..cf1c2ca --- /dev/null +++ b/web/js/labitrack.d/.gitignore @@ -0,0 +1 @@ +39-pagination.js diff --git a/web/js/labitrack.d/39-pagination.coffee b/web/js/labitrack.d/39-pagination.coffee new file mode 100644 index 0000000..ca50b9b --- /dev/null +++ b/web/js/labitrack.d/39-pagination.coffee @@ -0,0 +1,103 @@ +λ.pagination = (-> + link = (pg) -> '/browse' + (if pg > 1 then '/page/' + pg else '') + + class pgs + constructor: -> @pages = [] + dots: -> @pages.push { id: 'dots', label: '…', classes: 'disabled' } + page: (pgno) -> @pages.push { + id: pgno + link: link pgno + label: pgno + } + first: -> @pages.push { + id: 'first' + link: link 1 + label: '|←' + classes: 'prev' + } + prev: (pg) -> @pages.push { + id: 'prev' + link: link pg + label: '←' + } + next: (pg) -> @pages.push { + id: 'next' + link: link pg + label: '→' + } + last: (pg) -> @pages.push { + id: 'last' + link: link pg + label: '→|' + classes: 'next' + } + + return (page, cnt) -> + p = new pgs + first = page != 1 + prev = page > 2 + next = page + 1 < cnt + last = page < cnt + slots = 11 + + left = right = Math.floor(slots / 2) + + left -= first + prev + right -= next + last + + if page - left < 1 + right += left - page + 1 + left = page - 1 + + if page + right > cnt + oldright = right + right = cnt - page + left += oldright - right + + if page - left < 1 + left = page - 1 + + if page - left isnt 1 + ldots = true + left-- + + if page + right isnt cnt + rdots = true + right-- + + p.first() if first + p.prev page-1 if prev + p.dots() if ldots + while left > 0 + p.page page - left-- + p.pages[(p.page page)-1].classes += ' active' + while right-- > 0 + p.page page + ++left + p.dots() if rdots + p.next page+1 if next + p.last cnt if last + return p.pages +)() + +###cnt = parseInt process.argv[2], 10 + +str_repeat = (str, fac) -> new Array(fac + 1).join(str) + +for i in [1..cnt] + paging = pagination i, cnt + pgs = [] + k = 0 + for page, j in paging + if page.id is 'dots' + pgs.push '…' + else if typeof page.id is 'string' + pgs.push page.id[0] + else + str = page.id+'' + pgs.push str[str.length-1] + if page.id is i + k = j + console.log pgs.join ' ' + console.log str_repeat(' ', k)+'^' + ### + diff --git a/web/js/labitrack.d/43-browse.js b/web/js/labitrack.d/43-browse.js index 7605e78..14399ac 100644 --- a/web/js/labitrack.d/43-browse.js +++ b/web/js/labitrack.d/43-browse.js @@ -1,4 +1,5 @@ (function(){ + function hdl_add(){ console.log('add'); } @@ -41,39 +42,14 @@ messages.bind("remove", hdl_remove, this); }, render: function(page){ + if (page) this.page = page; + page = this.page; var stats = this.collection.stats; var pages = []; - var dots = { id: 'dots', label: '…' }; - pages.push({ - id: 'first', - link: '/browse', - label: '|←', - classes: 'prev' - }); - pages.push({ - id: 'prev', - link: '/browse', - label: '←' - }); - pages.push(dots); - pgno = 1; - pages.push({ - id: pgno, - link: '/browse/'+pgno, - label: pgno - }); - pages.push(dots); - pages.push({ - id: 'next', - link: '/browse', - label: '→' - }); - pages.push({ - id: 'last', - link: '/browse', - label: '→|', - classes: 'next' - }); + if (stats !== undefined) { + var pgcnt = Math.ceil(stats.count / 10); + pages = λ.pagination(page, pgcnt); + } var data = { rows: this.collection.toJSON(), pages: pages @@ -92,13 +68,16 @@ }, render: function (page) { page || (page = 1); + page = parseInt(page, 10); + console.log('page', page); λ.setcontent('browse', {page: page}); this.browse.el = $(this.el).find('#objecttable_ph')[0]; + console.log(this.browse.render); this.browse.render(page); this.browse.collection.fetchpage(page); } }); view.route('browse'); - view.route('browse/:page'); + view.route('browse/page/:page'); }()); -- cgit v1.2.1