Simple RSS Reader inspired by Gxxgle Reader
Revision | 2073d961269a57f17ebc1e250e1e5bd003a118b9 (tree) |
---|---|
Zeit | 2013-07-15 22:58:35 |
Autor | hylom <hylom@hylo...> |
Commiter | hylom |
implement pager
@@ -6,41 +6,61 @@ | ||
6 | 6 | |
7 | 7 | function updatePager(total, skip, count) { |
8 | 8 | var totalPage = Math.floor((total - 1) / count + 1); |
9 | - var currentPage = (skip == 0) ? 0 : (skip - 1) / count + 1; | |
10 | - var elem = ''; | |
9 | + var currentPage = Math.floor((skip - 1) / count + 1); | |
10 | + var pane = $('#contentsPane'); | |
11 | + var feedId = Number(pane.attr('feedId')); | |
12 | + var elem; | |
13 | + var anchor; | |
11 | 14 | |
12 | 15 | console.log(skip + ',' + count + '/' + total); |
13 | 16 | console.log(currentPage + '/' + totalPage); |
14 | 17 | |
15 | - var paging = $('#page-navigation'); | |
16 | - paging.empty(); | |
17 | - | |
18 | - var pagingFirst = '<li id="paging-first"><a href="#">«</a></li>'; | |
19 | - paging.append(pagingFirst); | |
18 | + var pagerRange = 2; | |
19 | + var pagerStart = currentPage - pagerRange; | |
20 | + if (pagerStart < 0) { | |
21 | + pagerStart = 0; | |
22 | + } | |
23 | + var pagerEnd = pagerStart + 2 * pagerRange + 1; | |
24 | + if (pagerEnd > totalPage) { | |
25 | + pagerEnd = totalPage; | |
26 | + pagerStart = pagerEnd - 5; | |
27 | + if (pagerStart < 0) { | |
28 | + pagerStart = 0; | |
29 | + } | |
30 | + } | |
20 | 31 | |
21 | - if (currentPage > 0) { | |
22 | - elem = '<li><a href="#">' | |
23 | - + currentPage + '</a></li>'; | |
24 | - paging.append(elem); | |
32 | + function _genHandler(feedId, skip, count) { | |
33 | + return function(ev) { | |
34 | + showFeed(feedId, skip, count); | |
35 | + } | |
25 | 36 | } |
26 | 37 | |
27 | - elem = '<li class="active"><a href="#">' + (currentPage + 1) | |
28 | - + '</a></li>'; | |
29 | - paging.append(elem); | |
38 | + var paging = $('#page-navigation'); | |
39 | + paging.empty(); | |
30 | 40 | |
31 | - if (currentPage != totalPage) { | |
32 | - elem = '<li><a href="#">' | |
33 | - + (currentPage + 2) + '</a></li>'; | |
41 | + if (pagerStart != 0) { | |
42 | + paging.append($('<li class="disabled"><a href="#">...</a></li>')); | |
43 | + } | |
44 | + for (var i = pagerStart; i < pagerEnd; i++) { | |
45 | + anchor = $('<a href="#">'); | |
46 | + anchor.text(i + 1); | |
47 | + anchor.on('click', _genHandler(feedId, count * i, count)); | |
48 | + elem = $('<li>'); | |
49 | + elem.append(anchor); | |
50 | + if (i == currentPage) { | |
51 | + elem.addClass('disabled'); | |
52 | + } | |
34 | 53 | paging.append(elem); |
35 | 54 | } |
36 | - | |
37 | - var pagingLast = $('<li id="paging-last"><a href="#">»</a></li>'); | |
38 | - paging.append(pagingLast); | |
55 | + if (pagerEnd != totalPage) { | |
56 | + paging.append($('<li class="disabled"><a href="#">...</a></li>')); | |
57 | + } | |
39 | 58 | } |
40 | 59 | |
41 | 60 | function updateContentsPane(data) { |
42 | 61 | var pane = $('#contentsPane'); |
43 | 62 | pane.empty(); |
63 | + | |
44 | 64 | var tmpl = '<tr class="contentHeader" id="chead{{content_id}}">' |
45 | 65 | + '<td class="contentTitle"><h4>' |
46 | 66 | + ' <a href="#" class="contentTitleString" cid="{{content_id}}">{{title}}</a>' |
@@ -64,6 +84,7 @@ | ||
64 | 84 | } |
65 | 85 | pane.attr('skip', data.skip); |
66 | 86 | pane.attr('count', data.count); |
87 | + pane.attr('feedId', data.feedId); | |
67 | 88 | updatePager(data.total, data.skip, data.count); |
68 | 89 | } |
69 | 90 |
@@ -89,6 +110,8 @@ | ||
89 | 110 | $('#feedTitle').text(feedTitle); |
90 | 111 | var pane = $('#contentsPane'); |
91 | 112 | pane.empty(); |
113 | + var paging = $('#page-navigation'); | |
114 | + paging.empty(); | |
92 | 115 | return false; |
93 | 116 | }); |
94 | 117 |
@@ -90,6 +90,7 @@ exports.feedContents = function (req, res) { | ||
90 | 90 | /* |
91 | 91 | returns json: |
92 | 92 | { |
93 | + feedId: Number, | |
93 | 94 | totalCount: Number, |
94 | 95 | skip: Number, |
95 | 96 | itemCount: Number, |
@@ -152,6 +153,7 @@ exports.feedContents = function (req, res) { | ||
152 | 153 | rows[i].formatedTimestamp = formatTimestamp(rows[i].timestamp); |
153 | 154 | } |
154 | 155 | var result = { |
156 | + "feedId": feedId, | |
155 | 157 | "total": totalCount, |
156 | 158 | "skip": skip, |
157 | 159 | "count": count, |
@@ -15,6 +15,3 @@ block content | ||
15 | 15 | table.table#contentsPane |
16 | 16 | .pagination.pagination-centered |
17 | 17 | ul#page-navigation |
18 | - li#paging-first.disabled: a(href='#') « | |
19 | - li.active: a(href='#') 1 | |
20 | - li#paging-last.disabled: a(href='#') » |