summaryrefslogtreecommitdiffstats
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
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>
-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
-rwxr-xr-xweb/labitrackd.lua14
-rw-r--r--web/templates/labitrack.d/objecttable1
-rw-r--r--web/templates/labitrack.d/pagination3
6 files changed, 123 insertions, 42 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');
}());
diff --git a/web/labitrackd.lua b/web/labitrackd.lua
index 1f25371..1d208df 100755
--- a/web/labitrackd.lua
+++ b/web/labitrackd.lua
@@ -166,13 +166,13 @@ hathaway.import()
local htmlpage = sendfile('text/html; charset=UTF-8', 'pub/index.html')
-GET('/', htmlpage)
-GET('/browse', htmlpage)
-GETM('^/browse/(%d+)$', htmlpage)
-GET('/recent', htmlpage)
-GET('/about', htmlpage)
-GETM('^/view/(%d+)$', htmlpage)
-GETM('^/edit/(%d+)$', htmlpage)
+GET('/', htmlpage)
+GET('/browse', htmlpage)
+GETM('^/browse/page/(%d+)$', htmlpage)
+GET('/recent', htmlpage)
+GET('/about', htmlpage)
+GETM('^/view/(%d+)$', htmlpage)
+GETM('^/edit/(%d+)$', htmlpage)
GET('/js/corelibs.min.js', sendfile_js('js/dist/corelibs.min.js'))
GET('/js/corelibs.src.js', sendfile_js('js/dist/corelibs.src.js'))
diff --git a/web/templates/labitrack.d/objecttable b/web/templates/labitrack.d/objecttable
index 5a9f659..089f02b 100644
--- a/web/templates/labitrack.d/objecttable
+++ b/web/templates/labitrack.d/objecttable
@@ -20,3 +20,4 @@
{{/each}}
</tbody>
</table>
+{{pagination}}
diff --git a/web/templates/labitrack.d/pagination b/web/templates/labitrack.d/pagination
index 95df273..ad8eb70 100644
--- a/web/templates/labitrack.d/pagination
+++ b/web/templates/labitrack.d/pagination
@@ -1,7 +1,5 @@
<div class="pagination">
<ul>
- Paging on the way
- <!--
{{#each pages}}
{{#if link}}
<li class="pg_{{id}} {{classes}}">
@@ -13,6 +11,5 @@
</li>
{{/if}}
{{/each}}
- -->
</ul>
</div>