diff options
| author | Jules Laplace <jules@okfoc.us> | 2014-09-05 15:54:12 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2014-09-05 15:54:12 -0400 |
| commit | 1082e3be99669671b6c745307131a7cb8d54eabc (patch) | |
| tree | 91897ff420885672a2dc5390ca334b65828737d5 | |
| parent | 1877aa4b508704634774e47385cf5429f67a4565 (diff) | |
pagination on all queries
| -rw-r--r-- | public/assets/stylesheets/staff.css | 6 | ||||
| -rw-r--r-- | server/lib/views/staff.js | 75 | ||||
| -rw-r--r-- | views/staff/_pagination.ejs | 17 | ||||
| -rw-r--r-- | views/staff/index.ejs | 2 | ||||
| -rw-r--r-- | views/staff/media/index.ejs | 2 | ||||
| -rw-r--r-- | views/staff/projects/index.ejs | 2 | ||||
| -rw-r--r-- | views/staff/users/index.ejs | 2 | ||||
| -rw-r--r-- | views/staff/users/media.ejs | 2 | ||||
| -rw-r--r-- | views/staff/users/show.ejs | 17 |
9 files changed, 101 insertions, 24 deletions
diff --git a/public/assets/stylesheets/staff.css b/public/assets/stylesheets/staff.css index 05d3541..aa21f9b 100644 --- a/public/assets/stylesheets/staff.css +++ b/public/assets/stylesheets/staff.css @@ -95,3 +95,9 @@ iframe.embed { .page table.projectList td.border { border: 0; } +#pagination { + margin: 10px 0; +} +#pagination a { + color: #00f; +}
\ No newline at end of file diff --git a/server/lib/views/staff.js b/server/lib/views/staff.js index e776b7f..2ffea0d 100644 --- a/server/lib/views/staff.js +++ b/server/lib/views/staff.js @@ -27,10 +27,14 @@ var staff = module.exports = { middleware: { - ensureUsers: function(req, res, next, criteria){ - var limit = Math.max( Number(req.query.limit) || 50, 200 ) - var offset = Number(req.query.offset) || 0 + ensureUsers: function(req, res, next){ + var paginationInfo = res.locals.pagination = {} + var criteria = req.criteria || {} + var limit = paginationInfo.limit = Math.min( Number(req.query.limit) || 50, 200 ) + var offset = paginationInfo.offset = Number(req.query.offset) || 0 var sort + paginationInfo.sort = req.query.sort + paginationInfo.sortOptions = ["date", "last_seen", "username"] switch (req.query.sort) { case 'date': sort = {'created_at': -1} @@ -41,9 +45,9 @@ var staff = module.exports = { case 'username': default: sort = {'username': 1} + paginationInfo.sort = "username" break } - criteria = criteria || {} User.find(criteria) .select(staff.fields.user) .sort(sort) @@ -60,19 +64,24 @@ var staff = module.exports = { staff.middleware.ensureUsers(dreq, res, next) }, - ensureProjects: function(req, res, next, criteria){ - var limit = Math.max( Number(req.query.limit) || 50, 200 ) - var offset = Number(req.query.offset) || 0 + ensureProjects: function(req, res, next){ + var paginationInfo = res.locals.pagination = {} + var criteria = req.criteria || {} + var limit = paginationInfo.limit = Math.min( Number(req.query.limit) || 50, 200 ) + var offset = paginationInfo.offset = Number(req.query.offset) || 0 var sort + paginationInfo.sort = req.query.sort + paginationInfo.sortOptions = ["date", "name"] switch (req.query.sort) { case 'date': sort = {'created_at': -1} break + case 'name': default: + paginationInfo.sort = "name" sort = {'slug': 1} break } - criteria = criteria || {} Project.find(criteria) .select(staff.fields.project) .sort(sort) @@ -84,17 +93,21 @@ var staff = module.exports = { }) }, - ensureMedia: function(req, res, next, criteria){ - var limit = Math.max( Number(req.query.limit) || 50, 200 ) - var offset = Number(req.query.offset) || 0 + ensureMedia: function(req, res, next){ + var paginationInfo = res.locals.pagination = {} + var criteria = req.criteria || {} + var limit = paginationInfo.limit = Math.min( Number(req.query.limit) || 50, 200 ) + var offset = paginationInfo.offset = Number(req.query.offset) || 0 var sort + paginationInfo.sort = req.query.sort + paginationInfo.sortOptions = ["date"] switch (req.query.sort) { default: case 'date': + paginationInfo.sort = "date" sort = {'created_at': -1} break } - criteria = criteria || {} Media.find(criteria) // .select(staff.fields.media) .sort(sort) @@ -233,7 +246,8 @@ var staff = module.exports = { ensureProfileMedia: function(req, res, next){ if (! res.locals.profile) { return next() } - staff.middleware.ensureMedia(req, res, next, { user_id: res.locals.profile._id }) + req.criteria = { user_id: res.locals.profile._id } + staff.middleware.ensureMedia(req, res, next) }, ensureProject: function(req, res, next){ @@ -328,6 +342,7 @@ var staff = module.exports = { staff.middleware.ensureProfile, staff.middleware.ensureProfileMedia, + staff.middleware.ensureProfileMediaCount, staff.users.media ); @@ -373,6 +388,8 @@ var staff = module.exports = { middleware.ensureAuthenticated, middleware.ensureIsStaff, + staff.middleware.ensureMediaCount, + staff.middleware.ensureMedia, staff.middleware.ensureMediaUsers, @@ -389,6 +406,26 @@ var staff = module.exports = { ); }, + + paginate: function(req, res){ + var info = res.locals.pagination + info.query = "sort=" + info.sort + "&limit=" + info.limit + info.first_page = 0 + info.last_page = Math.max(0, info.max - info.limit) + info.sortOptions = info.sortOptions + if (info.offset > 0) { + info.prev_page = Math.max(0, info.offset - info.limit) + } + else { + info.prev_page = -1 + } + if (info.count == info.limit && info.offset + info.limit < info.max) { + info.next_page = info.offset + info.limit + } + else { + info.next_page = -1 + } + }, index: function(req, res){ res.render('staff/index') @@ -398,6 +435,9 @@ var staff = module.exports = { // /staff/users/:username users: { index: function(req, res){ + res.locals.pagination.count = res.locals.users.length + res.locals.pagination.max = res.locals.userCount + staff.paginate(req, res) res.render('staff/users/index') }, show: function(req, res){ @@ -412,6 +452,9 @@ var staff = module.exports = { }, media: function(req, res){ if (res.locals.profile) { + res.locals.pagination.count = res.locals.media.length + res.locals.pagination.max = res.locals.profile.mediaCount + staff.paginate(req, res) res.render('staff/users/media') } else { @@ -430,6 +473,9 @@ var staff = module.exports = { // /staff/projects/:name projects: { index: function(req, res){ + res.locals.pagination.count = res.locals.projects.length + res.locals.pagination.max = res.locals.projectCount + staff.paginate(req, res) res.render('staff/projects/index') }, show: function(req, res){ @@ -448,6 +494,9 @@ var staff = module.exports = { media: { index: function(req, res){ + res.locals.pagination.count = res.locals.media.length + res.locals.pagination.max = res.locals.mediaCount + staff.paginate(req, res) res.render('staff/media/index') }, show: function(req, res){ diff --git a/views/staff/_pagination.ejs b/views/staff/_pagination.ejs new file mode 100644 index 0000000..6c3bfb1 --- /dev/null +++ b/views/staff/_pagination.ejs @@ -0,0 +1,17 @@ +[[ if (pagination.prev_page !== -1 && pagination.next_page !== -1) { ]] + <div id="pagination"> + + [[ if (pagination.prev_page !== -1) { ]] + <a href="?[[- pagination.query ]]&offset=[[- pagination.prev_page ]]">←</a> + [[ } else { ]] + ← + [[ } ]] + | + [[ if (pagination.next_page !== -1) { ]] + <a href="?[[- pagination.query ]]&offset=[[- pagination.next_page ]]">Next page →</a> + [[ } else { ]] + → + [[ } ]] + + </div> +[[ } ]]
\ No newline at end of file diff --git a/views/staff/index.ejs b/views/staff/index.ejs index f721339..5ca7269 100644 --- a/views/staff/index.ejs +++ b/views/staff/index.ejs @@ -9,7 +9,7 @@ </nav> <hr> - + [[ include _users_recent ]] [[ include _stats ]] diff --git a/views/staff/media/index.ejs b/views/staff/media/index.ejs index 76aaafb..516af2d 100644 --- a/views/staff/media/index.ejs +++ b/views/staff/media/index.ejs @@ -11,6 +11,8 @@ <hr> +[[ include ../_pagination ]] [[ include ../_media ]] +[[ include ../_pagination ]] [[ include ../_footer ]] diff --git a/views/staff/projects/index.ejs b/views/staff/projects/index.ejs index a6fad6b..482ea25 100644 --- a/views/staff/projects/index.ejs +++ b/views/staff/projects/index.ejs @@ -11,6 +11,8 @@ <hr> +[[ include ../_pagination ]] [[ include ../_projects ]] +[[ include ../_pagination ]] [[ include ../_footer ]] diff --git a/views/staff/users/index.ejs b/views/staff/users/index.ejs index 8c56565..f14d666 100644 --- a/views/staff/users/index.ejs +++ b/views/staff/users/index.ejs @@ -11,6 +11,8 @@ <hr> +[[ include ../_pagination ]] [[ include ../_users ]] +[[ include ../_pagination ]] [[ include ../_footer ]] diff --git a/views/staff/users/media.ejs b/views/staff/users/media.ejs index 6ea2668..b13e5fb 100644 --- a/views/staff/users/media.ejs +++ b/views/staff/users/media.ejs @@ -11,6 +11,8 @@ <hr> +[[ include ../_pagination ]] [[ include ../_media ]] +[[ include ../_pagination ]] [[ include ../_footer ]] diff --git a/views/staff/users/show.ejs b/views/staff/users/show.ejs index 127ca8c..8e9b447 100644 --- a/views/staff/users/show.ejs +++ b/views/staff/users/show.ejs @@ -80,17 +80,14 @@ [[- profile.isStaff ? "yes" : "no" ]] </td> </tr> - </tr> - <tr> - <th> - </th> - <td id="actions"> - [[ if (String(user._id) != String(profile._id)) { ]] - <button id="toggle-staff" data-isStaff="[[- !! profile.isStaff ]]">Make Staff</button> - [[ } ]] - </td> - </tr> </table> + + <br><br> + <div id="actions"> + [[ if (String(user._id) != String(profile._id)) { ]] + <button id="toggle-staff" data-isStaff="[[- !! profile.isStaff ]]">Make Staff</button> + [[ } ]] + </div> <h2>Projects</h2> |
