summaryrefslogtreecommitdiffstats
path: root/web/js
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2012-08-09 17:30:58 +0000
committerAsbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>2012-08-09 17:30:58 +0000
commitb1a5a0dd5dc0a7910c305b90c7084766f5a0d729 (patch)
tree3f066e198c382df75dbfe355e642dea67572cf6c /web/js
parent7c3af4ea9b75361e9d726d72ba587318e4e92c0f (diff)
downloadlabitrack-b1a5a0dd5dc0a7910c305b90c7084766f5a0d729.tar.gz
labitrack-b1a5a0dd5dc0a7910c305b90c7084766f5a0d729.tar.xz
labitrack-b1a5a0dd5dc0a7910c305b90c7084766f5a0d729.zip
Pagination
Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
Diffstat (limited to 'web/js')
-rw-r--r--web/js/labitrack.d/.gitignore1
-rw-r--r--web/js/labitrack.d/39-pagination.coffee103
-rw-r--r--web/js/labitrack.d/43-browse.js43
3 files changed, 115 insertions, 32 deletions
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: '|&larr;'
+ classes: 'prev'
+ }
+ prev: (pg) -> @pages.push {
+ id: 'prev'
+ link: link pg
+ label: '&larr;'
+ }
+ next: (pg) -> @pages.push {
+ id: 'next'
+ link: link pg
+ label: '&rarr;'
+ }
+ last: (pg) -> @pages.push {
+ id: 'last'
+ link: link pg
+ label: '&rarr;|'
+ 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: '|&larr;',
- classes: 'prev'
- });
- pages.push({
- id: 'prev',
- link: '/browse',
- label: '&larr;'
- });
- pages.push(dots);
- pgno = 1;
- pages.push({
- id: pgno,
- link: '/browse/'+pgno,
- label: pgno
- });
- pages.push(dots);
- pages.push({
- id: 'next',
- link: '/browse',
- label: '&rarr;'
- });
- pages.push({
- id: 'last',
- link: '/browse',
- label: '&rarr;|',
- 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');
}());