diff --git a/config.default.js b/config.default.js index ac403fbd3e..1fcb4a98e2 100644 --- a/config.default.js +++ b/config.default.js @@ -50,6 +50,8 @@ var config = { // 话题列表显示的话题数量 list_topic_count: 20, + // 话题详情里的回复每页显示条数 + list_reply_count: 20, // RSS配置 rss: { diff --git a/controllers/topic.js b/controllers/topic.js index cb28a2d422..1b09e3a029 100644 --- a/controllers/topic.js +++ b/controllers/topic.js @@ -8,16 +8,17 @@ var validator = require('validator'); -var at = require('../common/at'); -var User = require('../proxy').User; -var Topic = require('../proxy').Topic; +var at = require('../common/at'); +var User = require('../proxy').User; +var Topic = require('../proxy').Topic; +var Reply = require('../proxy').Reply; var TopicCollect = require('../proxy').TopicCollect; -var EventProxy = require('eventproxy'); -var tools = require('../common/tools'); -var store = require('../common/store'); -var config = require('../config'); -var _ = require('lodash'); -var cache = require('../common/cache'); +var EventProxy = require('eventproxy'); +var tools = require('../common/tools'); +var store = require('../common/store'); +var config = require('../config'); +var _ = require('lodash'); +var cache = require('../common/cache'); var logger = require('../common/logger') /** @@ -27,7 +28,7 @@ var logger = require('../common/logger') * @param {HttpResponse} res * @param {Function} next */ -exports.index = function (req, res, next) { +exports.index = function(req, res, next) { function isUped(user, reply) { if (!reply.ups) { return false; @@ -37,39 +38,43 @@ exports.index = function (req, res, next) { var topic_id = req.params.tid; var currentUser = req.session.user; + var page = req.query.page; if (topic_id.length !== 24) { return res.render404('此话题不存在或已被删除。'); } - var events = ['topic', 'other_topics', 'no_reply_topics', 'is_collect']; + var events = ['topic', 'other_topics', 'no_reply_topics', 'is_collect', 'pages']; var ep = EventProxy.create(events, - function (topic, other_topics, no_reply_topics, is_collect) { - res.render('topic/index', { - topic: topic, - author_other_topics: other_topics, - no_reply_topics: no_reply_topics, - is_uped: isUped, - is_collect: is_collect, + function(topic, other_topics, no_reply_topics, is_collect, pages) { + console.log(1, pages, page); + res.render('topic/index', { + topic: topic, + author_other_topics: other_topics, + no_reply_topics: no_reply_topics, + is_uped: isUped, + is_collect: is_collect, + pages: pages, + current_page: page ? page : pages + }); }); - }); ep.fail(next); - Topic.getFullTopic(topic_id, ep.done(function (message, topic, author, replies) { + Topic.getFullTopic(topic_id, page, ep.done(function(message, topic, author, replies) { if (message) { - logger.error('getFullTopic error topic_id: ' + topic_id) + logger.error('getFullTopic error topic_id: ' + topic_id); return res.renderError(message); } topic.visit_count += 1; topic.save(); - topic.author = author; + topic.author = author; topic.replies = replies; // 点赞数排名第三的回答,它的点赞数就是阈值 - topic.reply_up_threshold = (function () { - var allUpCount = replies.map(function (reply) { + topic.reply_up_threshold = (function() { + var allUpCount = replies.map(function(reply) { return reply.ups && reply.ups.length || 0; }); allUpCount = _.sortBy(allUpCount, Number).reverse(); @@ -84,24 +89,43 @@ exports.index = function (req, res, next) { ep.emit('topic', topic); // get other_topics - var options = { limit: 5, sort: '-last_reply_at'}; - var query = { author_id: topic.author_id, _id: { '$nin': [ topic._id ] } }; + var options = { + limit: 5, + sort: '-last_reply_at' + }; + var query = { + author_id: topic.author_id, + _id: { + '$nin': [topic._id] + } + }; Topic.getTopicsByQuery(query, options, ep.done('other_topics')); // get no_reply_topics - cache.get('no_reply_topics', ep.done(function (no_reply_topics) { + cache.get('no_reply_topics', ep.done(function(no_reply_topics) { if (no_reply_topics) { ep.emit('no_reply_topics', no_reply_topics); } else { - Topic.getTopicsByQuery( - { reply_count: 0, tab: {$ne: 'job'}}, - { limit: 5, sort: '-create_at'}, - ep.done('no_reply_topics', function (no_reply_topics) { + Topic.getTopicsByQuery({ + reply_count: 0, + tab: { + $ne: 'job' + } + }, { + limit: 5, + sort: '-create_at' + }, + ep.done('no_reply_topics', function(no_reply_topics) { cache.set('no_reply_topics', no_reply_topics, 60 * 1); return no_reply_topics; })); } })); + + Reply.getCountByTopicId(topic_id, ep.done('pages', function (count) { + var pages = Math.ceil(count / config.list_reply_count); + return pages; + })); })); if (!currentUser) { @@ -111,20 +135,20 @@ exports.index = function (req, res, next) { } }; -exports.create = function (req, res, next) { +exports.create = function(req, res, next) { res.render('topic/edit', { tabs: config.tabs }); }; -exports.put = function (req, res, next) { - var title = validator.trim(req.body.title); - var tab = validator.trim(req.body.tab); +exports.put = function(req, res, next) { + var title = validator.trim(req.body.title); + var tab = validator.trim(req.body.tab); var content = validator.trim(req.body.t_content); // 得到所有的 tab, e.g. ['ask', 'share', ..] - var allTabs = config.tabs.map(function (tPair) { + var allTabs = config.tabs.map(function(tPair) { return tPair[0]; }); @@ -151,18 +175,18 @@ exports.put = function (req, res, next) { }); } - Topic.newAndSave(title, content, tab, req.session.user._id, function (err, topic) { + Topic.newAndSave(title, content, tab, req.session.user._id, function(err, topic) { if (err) { return next(err); } var proxy = new EventProxy(); - proxy.all('score_saved', function () { + proxy.all('score_saved', function() { res.redirect('/topic/' + topic._id); }); proxy.fail(next); - User.getUserById(req.session.user._id, proxy.done(function (user) { + User.getUserById(req.session.user._id, proxy.done(function(user) { user.score += 5; user.topic_count += 1; user.save(); @@ -175,10 +199,10 @@ exports.put = function (req, res, next) { }); }; -exports.showEdit = function (req, res, next) { +exports.showEdit = function(req, res, next) { var topic_id = req.params.tid; - Topic.getTopicById(topic_id, function (err, topic, tags) { + Topic.getTopicById(topic_id, function(err, topic, tags) { if (!topic) { res.render404('此话题不存在或已被删除。'); return; @@ -199,21 +223,21 @@ exports.showEdit = function (req, res, next) { }); }; -exports.update = function (req, res, next) { +exports.update = function(req, res, next) { var topic_id = req.params.tid; - var title = req.body.title; - var tab = req.body.tab; - var content = req.body.t_content; + var title = req.body.title; + var tab = req.body.tab; + var content = req.body.t_content; - Topic.getTopicById(topic_id, function (err, topic, tags) { + Topic.getTopicById(topic_id, function(err, topic, tags) { if (!topic) { res.render404('此话题不存在或已被删除。'); return; } if (topic.author_id.equals(req.session.user._id) || req.session.user.is_admin) { - title = validator.trim(title); - tab = validator.trim(tab); + title = validator.trim(title); + tab = validator.trim(tab); content = validator.trim(content); // 验证 @@ -238,12 +262,12 @@ exports.update = function (req, res, next) { } //保存话题 - topic.title = title; - topic.content = content; - topic.tab = tab; + topic.title = title; + topic.content = content; + topic.tab = tab; topic.update_at = new Date(); - topic.save(function (err) { + topic.save(function(err) { if (err) { return next(err); } @@ -259,45 +283,60 @@ exports.update = function (req, res, next) { }); }; -exports.delete = function (req, res, next) { +exports.delete = function(req, res, next) { //删除话题, 话题作者topic_count减1 //删除回复,回复作者reply_count减1 //删除topic_collect,用户collect_topic_count减1 var topic_id = req.params.tid; - Topic.getTopic(topic_id, function (err, topic) { + Topic.getTopic(topic_id, function(err, topic) { if (err) { - return res.send({ success: false, message: err.message }); + return res.send({ + success: false, + message: err.message + }); } if (!req.session.user.is_admin && !(topic.author_id.equals(req.session.user._id))) { res.status(403); - return res.send({success: false, message: '无权限'}); + return res.send({ + success: false, + message: '无权限' + }); } if (!topic) { res.status(422); - return res.send({ success: false, message: '此话题不存在或已被删除。' }); + return res.send({ + success: false, + message: '此话题不存在或已被删除。' + }); } topic.deleted = true; - topic.save(function (err) { + topic.save(function(err) { if (err) { - return res.send({ success: false, message: err.message }); + return res.send({ + success: false, + message: err.message + }); } - res.send({ success: true, message: '话题已被删除。' }); + res.send({ + success: true, + message: '话题已被删除。' + }); }); }); }; // 设为置顶 -exports.top = function (req, res, next) { +exports.top = function(req, res, next) { var topic_id = req.params.tid; - var referer = req.get('referer'); + var referer = req.get('referer'); if (topic_id.length !== 24) { res.render404('此话题不存在或已被删除。'); return; } - Topic.getTopic(topic_id, function (err, topic) { + Topic.getTopic(topic_id, function(err, topic) { if (err) { return next(err); } @@ -306,22 +345,25 @@ exports.top = function (req, res, next) { return; } topic.top = !topic.top; - topic.save(function (err) { + topic.save(function(err) { if (err) { return next(err); } var msg = topic.top ? '此话题已置顶。' : '此话题已取消置顶。'; - res.render('notify/notify', {success: msg, referer: referer}); + res.render('notify/notify', { + success: msg, + referer: referer + }); }); }); }; // 设为精华 -exports.good = function (req, res, next) { +exports.good = function(req, res, next) { var topicId = req.params.tid; var referer = req.get('referer'); - Topic.getTopic(topicId, function (err, topic) { + Topic.getTopic(topicId, function(err, topic) { if (err) { return next(err); } @@ -330,21 +372,24 @@ exports.good = function (req, res, next) { return; } topic.good = !topic.good; - topic.save(function (err) { + topic.save(function(err) { if (err) { return next(err); } var msg = topic.good ? '此话题已加精。' : '此话题已取消加精。'; - res.render('notify/notify', {success: msg, referer: referer}); + res.render('notify/notify', { + success: msg, + referer: referer + }); }); }); }; // 锁定主题,不可再回复 -exports.lock = function (req, res, next) { +exports.lock = function(req, res, next) { var topicId = req.params.tid; var referer = req.get('referer'); - Topic.getTopic(topicId, function (err, topic) { + Topic.getTopic(topicId, function(err, topic) { if (err) { return next(err); } @@ -353,44 +398,53 @@ exports.lock = function (req, res, next) { return; } topic.lock = !topic.lock; - topic.save(function (err) { + topic.save(function(err) { if (err) { return next(err); } var msg = topic.lock ? '此话题已锁定。' : '此话题已取消锁定。'; - res.render('notify/notify', {success: msg, referer: referer}); + res.render('notify/notify', { + success: msg, + referer: referer + }); }); }); }; // 收藏主题 -exports.collect = function (req, res, next) { +exports.collect = function(req, res, next) { var topic_id = req.body.topic_id; - Topic.getTopic(topic_id, function (err, topic) { + Topic.getTopic(topic_id, function(err, topic) { if (err) { return next(err); } if (!topic) { - res.json({status: 'failed'}); + res.json({ + status: 'failed' + }); } - TopicCollect.getTopicCollect(req.session.user._id, topic._id, function (err, doc) { + TopicCollect.getTopicCollect(req.session.user._id, topic._id, function(err, doc) { if (err) { return next(err); } if (doc) { - res.json({status: 'failed'}); + res.json({ + status: 'failed' + }); return; } - TopicCollect.newAndSave(req.session.user._id, topic._id, function (err) { + TopicCollect.newAndSave(req.session.user._id, topic._id, function(err) { if (err) { return next(err); } - res.json({status: 'success'}); + res.json({ + status: 'success' + }); }); - User.getUserById(req.session.user._id, function (err, user) { + User.getUserById(req.session.user._id, function(err, user) { if (err) { return next(err); } @@ -405,24 +459,28 @@ exports.collect = function (req, res, next) { }); }; -exports.de_collect = function (req, res, next) { +exports.de_collect = function(req, res, next) { var topic_id = req.body.topic_id; - Topic.getTopic(topic_id, function (err, topic) { + Topic.getTopic(topic_id, function(err, topic) { if (err) { return next(err); } if (!topic) { - res.json({status: 'failed'}); + res.json({ + status: 'failed' + }); } - TopicCollect.remove(req.session.user._id, topic._id, function (err, removeResult) { + TopicCollect.remove(req.session.user._id, topic._id, function(err, removeResult) { if (err) { return next(err); } if (removeResult.result.n == 0) { - return res.json({status: 'failed'}) + return res.json({ + status: 'failed' + }) } - User.getUserById(req.session.user._id, function (err, user) { + User.getUserById(req.session.user._id, function(err, user) { if (err) { return next(err); } @@ -434,37 +492,41 @@ exports.de_collect = function (req, res, next) { topic.collect_count -= 1; topic.save(); - res.json({status: 'success'}); + res.json({ + status: 'success' + }); }); }); }; -exports.upload = function (req, res, next) { +exports.upload = function(req, res, next) { var isFileLimit = false; - req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { - file.on('limit', function () { - isFileLimit = true; - - res.json({ - success: false, - msg: 'File size too large. Max is ' + config.file_limit - }) - }); + req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { + file.on('limit', function() { + isFileLimit = true; + + res.json({ + success: false, + msg: 'File size too large. Max is ' + config.file_limit + }) + }); - store.upload(file, {filename: filename}, function (err, result) { - if (err) { - return next(err); - } - if (isFileLimit) { - return; - } - res.json({ - success: true, - url: result.url, - }); + store.upload(file, { + filename: filename + }, function(err, result) { + if (err) { + return next(err); + } + if (isFileLimit) { + return; + } + res.json({ + success: true, + url: result.url, }); - }); + }); + req.pipe(req.busboy); -}; +}; \ No newline at end of file diff --git a/proxy/reply.js b/proxy/reply.js index 5e95d45411..aec590d975 100644 --- a/proxy/reply.js +++ b/proxy/reply.js @@ -1,17 +1,20 @@ -var models = require('../models'); -var Reply = models.Reply; +var models = require('../models'); +var Reply = models.Reply; var EventProxy = require('eventproxy'); -var tools = require('../common/tools'); -var User = require('./user'); -var at = require('../common/at'); +var tools = require('../common/tools'); +var User = require('./user'); +var at = require('../common/at'); +var config = require('../config'); /** * 获取一条回复信息 * @param {String} id 回复ID * @param {Function} callback 回调函数 */ -exports.getReply = function (id, callback) { - Reply.findOne({_id: id}, callback); +exports.getReply = function(id, callback) { + Reply.findOne({ + _id: id + }, callback); }; /** @@ -22,11 +25,13 @@ exports.getReply = function (id, callback) { * @param {String} id 回复ID * @param {Function} callback 回调函数 */ -exports.getReplyById = function (id, callback) { +exports.getReplyById = function(id, callback) { if (!id) { return callback(null, null); } - Reply.findOne({_id: id}, function (err, reply) { + Reply.findOne({ + _id: id + }, function(err, reply) { if (err) { return callback(err); } @@ -35,7 +40,7 @@ exports.getReplyById = function (id, callback) { } var author_id = reply.author_id; - User.getUserById(author_id, function (err, author) { + User.getUserById(author_id, function(err, author) { if (err) { return callback(err); } @@ -44,7 +49,7 @@ exports.getReplyById = function (id, callback) { if (reply.content_is_html) { return callback(null, reply); } - at.linkUsers(reply.content, function (err, str) { + at.linkUsers(reply.content, function(err, str) { if (err) { return callback(err); } @@ -63,8 +68,15 @@ exports.getReplyById = function (id, callback) { * @param {String} id 主题ID * @param {Function} callback 回调函数 */ -exports.getRepliesByTopicId = function (id, cb) { - Reply.find({topic_id: id, deleted: false}, '', {sort: 'create_at'}, function (err, replies) { +exports.getRepliesByTopicId = function(id, page, cb) { + Reply.find({ + topic_id: id, + deleted: false + }, '', { + sort: 'create_at', + limit: config.list_reply_count, + skip: (page - 1) * config.list_reply_count + }, function(err, replies) { if (err) { return cb(err); } @@ -73,21 +85,23 @@ exports.getRepliesByTopicId = function (id, cb) { } var proxy = new EventProxy(); - proxy.after('reply_find', replies.length, function () { + proxy.after('reply_find', replies.length, function() { cb(null, replies); }); for (var j = 0; j < replies.length; j++) { - (function (i) { + (function(i) { var author_id = replies[i].author_id; - User.getUserById(author_id, function (err, author) { + User.getUserById(author_id, function(err, author) { if (err) { return cb(err); } - replies[i].author = author || { _id: '' }; + replies[i].author = author || { + _id: '' + }; if (replies[i].content_is_html) { return proxy.emit('reply_find'); } - at.linkUsers(replies[i].content, function (err, str) { + at.linkUsers(replies[i].content, function(err, str) { if (err) { return cb(err); } @@ -108,20 +122,20 @@ exports.getRepliesByTopicId = function (id, cb) { * @param {String} [replyId] 回复ID,当二级回复时设定该值 * @param {Function} callback 回调函数 */ -exports.newAndSave = function (content, topicId, authorId, replyId, callback) { +exports.newAndSave = function(content, topicId, authorId, replyId, callback) { if (typeof replyId === 'function') { callback = replyId; - replyId = null; + replyId = null; } - var reply = new Reply(); - reply.content = content; - reply.topic_id = topicId; + var reply = new Reply(); + reply.content = content; + reply.topic_id = topicId; reply.author_id = authorId; if (replyId) { reply.reply_id = replyId; } - reply.save(function (err) { + reply.save(function(err) { callback(err, reply); }); }; @@ -131,19 +145,38 @@ exports.newAndSave = function (content, topicId, authorId, replyId, callback) { * @param topicId 主题ID * @param callback 回调函数 */ -exports.getLastReplyByTopId = function (topicId, callback) { - Reply.find({topic_id: topicId, deleted: false}, '_id', {sort: {create_at : -1}, limit : 1}, callback); +exports.getLastReplyByTopId = function(topicId, callback) { + Reply.find({ + topic_id: topicId, + deleted: false + }, '_id', { + sort: { + create_at: -1 + }, + limit: 1 + }, callback); }; -exports.getRepliesByAuthorId = function (authorId, opt, callback) { +exports.getRepliesByAuthorId = function(authorId, opt, callback) { if (!callback) { callback = opt; - opt = null; + opt = null; } - Reply.find({author_id: authorId}, {}, opt, callback); + Reply.find({ + author_id: authorId + }, {}, opt, callback); }; // 通过 author_id 获取回复总数 -exports.getCountByAuthorId = function (authorId, callback) { - Reply.count({author_id: authorId}, callback); +exports.getCountByAuthorId = function(authorId, callback) { + Reply.count({ + author_id: authorId + }, callback); }; + +// 通过 author_id 获取回复总数 +exports.getCountByTopicId = function(topicId, callback) { + Reply.count({ + topic_id: topicId + }, callback); +}; \ No newline at end of file diff --git a/proxy/topic.js b/proxy/topic.js index bdcad967d1..b37eb4e553 100644 --- a/proxy/topic.js +++ b/proxy/topic.js @@ -6,6 +6,7 @@ var Reply = require('./reply'); var tools = require('../common/tools'); var at = require('../common/at'); var _ = require('lodash'); +var config = require('../config.js'); /** @@ -124,7 +125,7 @@ exports.getLimit5w = function (callback) { * @param {String} id 主题ID * @param {Function} callback 回调函数 */ -exports.getFullTopic = function (id, callback) { +exports.getFullTopic = function (id, page, callback) { var proxy = new EventProxy(); var events = ['topic', 'author', 'replies']; proxy @@ -150,8 +151,11 @@ exports.getFullTopic = function (id, callback) { } proxy.emit('author', author); })); - - Reply.getRepliesByTopicId(topic._id, proxy.done('replies')); + Reply.getCountByTopicId(id, function (err, count) { + var _page = page ? page : Math.ceil(count / config.list_reply_count); + _page = _page == 0 ? 1 : _page; + Reply.getRepliesByTopicId(id, _page, proxy.done('replies')); + }); })); }; diff --git a/views/index.html b/views/index.html index 9178562501..5a4b6558bf 100644 --- a/views/index.html +++ b/views/index.html @@ -13,10 +13,11 @@ <% if (typeof(topics) !== 'undefined' && topics.length > 0) { %> <div class="inner no-padding"> <%- partial('topic/list', { - topics: topics, - pages: pages, - current_page: current_page, - base: '/' + topics: topics, + pages: pages, + current_page: current_page, + base: '/', + param: 'tab=' + tab !== 'undefined' ? tab : '' }) %> </div> <% } else { %> diff --git a/views/topic/index.html b/views/topic/index.html index 860d2cd562..5ee610550e 100644 --- a/views/topic/index.html +++ b/views/topic/index.html @@ -9,7 +9,7 @@ </div> <% if (!current_user || !current_user.isAdvanced) { %> - <%- partial('../_ads') %> + <%- partial('../_ads') %> <% } %> <div class='panel'> @@ -68,58 +68,59 @@ </span> <% } %> <% if (topic.tab) { %> - <span> 来自 <%= topic.tabName %></span> + <span> 来自 <%= topic.tabName %></span> <%}%> <% if (current_user) { %> - <input class="span-common <%= is_collect ? '' : 'span-success' %> pull-right collect_btn" type="submit" value="<%= is_collect ? '取消收藏' : '收藏' %>" action="<%= is_collect ? 'de_collect' : 'collect' %>"> + <input class="span-common <%= is_collect ? '' : 'span-success' %> pull-right collect_btn" type="submit" + value="<%= is_collect ? '取消收藏' : '收藏' %>" action="<%= is_collect ? 'de_collect' : 'collect' %>"> <%}%> </div> <% if (current_user) { %> <div id="manage_topic"> <% if (current_user.is_admin) { %> - <a href='/topic/<%= topic._id %>/top' data-method="post"> - <% if (topic.top) { %> - <i class="fa fa-lg fa-star-o" title='取消置顶'></i> - <% } else { %> - <i class="fa fa-lg fa-star" title='置顶'/></i> - <% } %> - </a> - - - <a href='/topic/<%= topic._id %>/good' data-method="post"> - <% if (topic.good) { %> - <i class="fa fa-lg fa-heart-o" title="取消精华"></i> - <% } else { %> - <i class="fa fa-lg fa-heart" title="加精华"></i> - <% } %> - </a> - - <a href='/topic/<%= topic._id %>/lock' data-method="post"> - <% if (topic.lock) { %> - <i class="fa fa-lg fa-unlock" title='取消锁定'></i> - <% } else { %> - <i class="fa fa-lg fa-lock" title='锁定(不可再回复)'/></i> - <% } %> - </a> - - - <a href='/topic/<%= topic._id %>/edit'> - <i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a> - <a href='javascript:;' - data-id="<%= topic._id %>" - class='delete_topic_btn'> - <i class="fa fa-lg fa-trash" title='删除'></i></a> + <a href='/topic/<%= topic._id %>/top' data-method="post"> + <% if (topic.top) { %> + <i class="fa fa-lg fa-star-o" title='取消置顶'></i> <% } else { %> - <% if (current_user._id.equals(topic.author_id)) { %> - <a href='/topic/<%= topic._id %>/edit'> - <i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a> - <a href='javascript:;' - data-id="<%= topic._id %>" - class='delete_topic_btn'> - <i class="fa fa-lg fa-trash" title='删除'></i></a> + <i class="fa fa-lg fa-star" title='置顶'/></i> <% } %> + </a> + + + <a href='/topic/<%= topic._id %>/good' data-method="post"> + <% if (topic.good) { %> + <i class="fa fa-lg fa-heart-o" title="取消精华"></i> + <% } else { %> + <i class="fa fa-lg fa-heart" title="加精华"></i> + <% } %> + </a> + + <a href='/topic/<%= topic._id %>/lock' data-method="post"> + <% if (topic.lock) { %> + <i class="fa fa-lg fa-unlock" title='取消锁定'></i> + <% } else { %> + <i class="fa fa-lg fa-lock" title='锁定(不可再回复)'/></i> + <% } %> + </a> + + + <a href='/topic/<%= topic._id %>/edit'> + <i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a> + <a href='javascript:;' + data-id="<%= topic._id %>" + class='delete_topic_btn'> + <i class="fa fa-lg fa-trash" title='删除'></i></a> + <% } else { %> + <% if (current_user._id.equals(topic.author_id)) { %> + <a href='/topic/<%= topic._id %>/edit'> + <i class="fa fa-lg fa-pencil-square-o" title='编辑'></i></a> + <a href='javascript:;' + data-id="<%= topic._id %>" + class='delete_topic_btn'> + <i class="fa fa-lg fa-trash" title='删除'></i></a> + <% } %> <% } %> @@ -138,7 +139,15 @@ <div class='header'> <span class='col_fade'><%= topic.replies.length %> 回复</span> </div> - <%- partial('../reply/reply', topic.replies) %> + <div class="inner no-padding"> + <%- partial('../reply/reply', topic.replies) %> + <%- partial('./paginate', { + pages: pages, + current_page: current_page, + base: '/topic/' + topic._id, + param: '' + }) %> + </div> </div> <% } %> <% if (current_user && typeof(topic) !== 'undefined') { %> @@ -154,7 +163,8 @@ <textarea class='editor' name='r_content' rows='8'></textarea> <div class='editor_buttons'> - <input class='span-primary submit_btn' type="submit" data-loading-text="回复中.." value="回复<%= topic.lock ? '(此主题已锁定)' : ''%>" <%= topic.lock ? 'disabled="disabled"' : ''%>> + <input class='span-primary submit_btn' type="submit" data-loading-text="回复中.." + value="回复<%= topic.lock ? '(此主题已锁定)' : ''%>" <%= topic.lock ? 'disabled="disabled"' : ''%>> </div> </div> @@ -194,7 +204,7 @@ // END 获取所有回复者name // 编辑器相关 - $('textarea.editor').each(function(){ + $('textarea.editor').each(function () { var editor = new Editor({ status: [] }); @@ -205,7 +215,7 @@ $(this).data('editor', editor); var $input = $(editor.codemirror.display.input); - $input.keydown(function(event){ + $input.keydown(function (event) { if (event.keyCode === 13 && (event.ctrlKey || event.metaKey)) { event.preventDefault(); $el.closest('form').submit(); @@ -217,19 +227,19 @@ var codeMirrorGoLineDown = CodeMirror.commands.goLineDown; var codeMirrorNewlineAndIndent = CodeMirror.commands.newlineAndIndent; $input.atwho({ - at: '@', - data: allNames - }) - .on('shown.atwho', function () { - CodeMirror.commands.goLineUp = _.noop; - CodeMirror.commands.goLineDown = _.noop; - CodeMirror.commands.newlineAndIndent = _.noop; - }) - .on('hidden.atwho', function () { - CodeMirror.commands.goLineUp = codeMirrorGoLineUp; - CodeMirror.commands.goLineDown = codeMirrorGoLineDown; - CodeMirror.commands.newlineAndIndent = codeMirrorNewlineAndIndent; - }); + at: '@', + data: allNames + }) + .on('shown.atwho', function () { + CodeMirror.commands.goLineUp = _.noop; + CodeMirror.commands.goLineDown = _.noop; + CodeMirror.commands.newlineAndIndent = _.noop; + }) + .on('hidden.atwho', function () { + CodeMirror.commands.goLineUp = codeMirrorGoLineUp; + CodeMirror.commands.goLineDown = codeMirrorGoLineDown; + CodeMirror.commands.newlineAndIndent = codeMirrorNewlineAndIndent; + }); // END at.js 配置 }); @@ -247,7 +257,7 @@ editorWrap.show('fast', function () { var cm = editor.codemirror; cm.focus(); - if(cm.getValue().indexOf('@' + user) < 0){ + if (cm.getValue().indexOf('@' + user) < 0) { editor.push('@' + user + ' '); } }); @@ -263,7 +273,7 @@ editorWrap.show('fast', function () { var cm = editor.codemirror; cm.focus(); - if(cm.getValue().indexOf('@' + user) < 0){ + if (cm.getValue().indexOf('@' + user) < 0) { editor.push('@' + user + ' '); } }); @@ -328,7 +338,7 @@ $('.delete_topic_btn').click(function () { var topicId = $(this).data('id'); if (topicId && confirm('确定要删除此话题吗?')) { - $.post('/topic/' + topicId + '/delete', { _csrf: $('#_csrf').val() }, function (result) { + $.post('/topic/' + topicId + '/delete', {_csrf: $('#_csrf').val()}, function (result) { if (!result.success) { alert(result.message); } else { @@ -360,7 +370,7 @@ <% } %> <script type="text/javascript"> - (function(){ + (function () { var timer = null; //对话框延时定时器 // 初始化 $('.replies_history') var $repliesHistory = $('.replies_history'); @@ -368,9 +378,9 @@ $repliesHistory.hide(); // END // 鼠标移入对话框清除隐藏定时器;移出时隐藏对话框 - $repliesHistory.on('mouseenter', function(){ + $repliesHistory.on('mouseenter', function () { clearTimeout(timer); - }).on('mouseleave', function(){ + }).on('mouseleave', function () { $repliesHistory.fadeOut('fast'); }); // 显示被 at 用户的本页评论 @@ -386,8 +396,8 @@ var offset = $this.offset(); var width = $this.width(); var mainOffset = $('#main').offset(); - $repliesHistory.css('left', offset.left-mainOffset.left+width+10); // magic number - $repliesHistory.css('top', offset.top-mainOffset.top-10); // magic number + $repliesHistory.css('left', offset.left - mainOffset.left + width + 10); // magic number + $repliesHistory.css('top', offset.top - mainOffset.top - 10); // magic number $repliesHistory.css({ 'z-index': 1, }); @@ -403,13 +413,13 @@ height: '30px', width: '30px', }), // avatar - (replyContent.length>300?replyContent.substr(0,300)+'...':replyContent), // reply content - '<a href="#'+replyToId+'" class="scroll_to_original" title="查看原文">↑</a>' + (replyContent.length > 300 ? replyContent.substr(0, 300) + '...' : replyContent), // reply content + '<a href="#' + replyToId + '" class="scroll_to_original" title="查看原文">↑</a>' ]); } replyToId = $replyItem.attr('reply_to_id'); } - if(chats.length > 0) { + if (chats.length > 0) { chats.reverse(); $repliesHistoryContent.append('<div class="title">查看对话</div>'); @@ -420,12 +430,12 @@ $chat.append(pair[2]); // 查看原文 anchor }); $repliesHistory.fadeIn('fast'); - }else{ + } else { $repliesHistory.hide(); } } }).on('mouseleave', '.reply_content a', function (e) { - timer = setTimeout(function(){ + timer = setTimeout(function () { $repliesHistory.fadeOut('fast'); }, 500); }); @@ -464,12 +474,12 @@ }); // END 点赞 // 图片预览 - (function(){ + (function () { var $previewModal = $('#preview-modal'); var $previewImage = $('#preview-image'); var $body = $('body'); // cache - $(document).on('click', '.markdown-text img', function(e) { + $(document).on('click', '.markdown-text img', function (e) { var $img = $(this); // 图片被a标签包裹时,不显示弹层 if ($img.parent('a').length > 0) { @@ -480,12 +490,12 @@ $previewModal.on('click', hideModal); - $previewModal.on('hidden.bs.modal', function() { + $previewModal.on('hidden.bs.modal', function () { // 在预览框消失之后恢复 body 的滚动能力 $body.css('overflow-y', 'scroll'); }) - $previewModal.on('shown.bs.modal', function() { + $previewModal.on('shown.bs.modal', function () { // 修复上次滚动留下的痕迹,可能会导致短暂的闪烁,不过可以接受 // TODO: to be promote $previewModal.scrollTop(0); diff --git a/views/topic/list.html b/views/topic/list.html index 37444a836e..16e812a000 100644 --- a/views/topic/list.html +++ b/views/topic/list.html @@ -1,57 +1,4 @@ <div id="topic_list"> <%- partial('../topic/abstract', {collection:topics, as:'topic'}) %> </div> -<div class='pagination' current_page='<%= current_page %>'> - <ul> - <% var base_url = base + (base.indexOf('?') < 0 ? '?' : '&') - + 'tab=' + (typeof tab !== 'undefined' ? tab : '') + '&page='; %> - <% if (current_page == 1) { %> - <li class='disabled'><a>«</a></li> - <% } else { %> - <li><a href="<%= base_url %>1">«</a></li> - <% } %> - - <% - var page_start = current_page - 2 > 0 ? current_page - 2 : 1; - var page_end = page_start + 4 >= pages ? pages : page_start + 4; - %> - - <% if (page_start > 1) { %> - <li><a>...</a></li> - <% } %> - - <% for(var i = page_start; i <= page_end; i++) { %> - <% if (i === current_page) { %> - <li class='disabled'><a><%= i %></a></li> - <% } else { %> - <li><a href='<%= base_url + i %>'><%= i %></a></li> - <% } %> - <% } %> - - <% if (page_end < pages ) { %> - <li><a>...</a></li> - <% } %> - - <% if (current_page == pages) { %> - <li class='disabled'><a>»</a></li> - <% } else { %> - <li><a href='<%= base_url + pages %>'>»</a></li> - <% } %> - </ul> -</div> -<script> - $(document).ready(function () { - var $nav = $('.pagination'); - var current_page = $nav.attr('current_page'); - if (current_page) { - $nav.find('li').each(function () { - var $li = $(this); - var $a = $li.find('a'); - if ($a.html() == current_page) { - $li.addClass('active'); - $a.removeAttr('href'); - } - }); - } - }); -</script> +<%- partial('../topic/paginate') %> diff --git a/views/topic/paginate.html b/views/topic/paginate.html new file mode 100644 index 0000000000..3fd67bed36 --- /dev/null +++ b/views/topic/paginate.html @@ -0,0 +1,53 @@ +<div class='pagination' current_page='<%= current_page %>'> + <ul> + <% var base_url = base + '?' + (param ? '' : param + '&') + 'page='; %> + <% if (current_page == 1) { %> + <li class='disabled'><a>«</a></li> + <% } else { %> + <li><a href="<%= base_url %>1">«</a></li> + <% } %> + + <% + var page_start = current_page - 2 > 0 ? current_page - 2 : 1; + var page_end = page_start + 4 >= pages ? pages : page_start + 4; + %> + + <% if (page_start > 1) { %> + <li><a>...</a></li> + <% } %> + + <% for(var i = page_start; i <= page_end; i++) { %> + <% if (i === current_page) { %> + <li class='disabled'><a><%= i %></a></li> + <% } else { %> + <li><a href='<%= base_url + i %>'><%= i %></a></li> + <% } %> + <% } %> + + <% if (page_end < pages ) { %> + <li><a>...</a></li> + <% } %> + + <% if (current_page == pages) { %> + <li class='disabled'><a>»</a></li> + <% } else { %> + <li><a href='<%= base_url + pages %>'>»</a></li> + <% } %> + </ul> +</div> +<script> + $(document).ready(function () { + var $nav = $('.pagination'); + var current_page = $nav.attr('current_page'); + if (current_page) { + $nav.find('li').each(function () { + var $li = $(this); + var $a = $li.find('a'); + if ($a.html() == current_page) { + $li.addClass('active'); + $a.removeAttr('href'); + } + }); + } + }); +</script> \ No newline at end of file diff --git a/views/user/collect_topics.html b/views/user/collect_topics.html index 6496c3f0b5..c6143a612c 100644 --- a/views/user/collect_topics.html +++ b/views/user/collect_topics.html @@ -10,8 +10,13 @@ </div> <div class='inner no-padding'> <% if (topics.length > 0) { %> - <%- partial('../topic/list', { topics: topics, pages: pages, current_pages: current_page, base: '/user/' + - user.loginname + '/collections' }) %> + <%- partial('../topic/list', { + topics: topics, + pages: pages, + current_pages: current_page, + base: '/user/' + user.loginname + '/collections', + param: '' + }) %> <% } else { %> <p>找不到话题 (T_T)</p> <% } %> diff --git a/views/user/replies.html b/views/user/replies.html index 833632016c..04b452ce58 100644 --- a/views/user/replies.html +++ b/views/user/replies.html @@ -13,8 +13,13 @@ <div class="header"><%= user.loginname %> 参与的话题</div> <div class="inner padding"> <% if(typeof(topics) !== 'undefined' && topics.length > 0){ %> - <%- partial('../topic/list', - {topics:topics,pages:pages,current_pages:current_page,base:'/user/'+user.loginname+'/replies'}) %> + <%- partial('../topic/list', { + topics:topics, + pages:pages, + current_pages:current_page, + base:'/user/'+user.loginname+'/replies', + param: '' + }) %> <% }else{ %> <div class='inner'> <p>无话题</p> diff --git a/views/user/topics.html b/views/user/topics.html index 3ac41fba26..1d740220c8 100644 --- a/views/user/topics.html +++ b/views/user/topics.html @@ -13,8 +13,13 @@ <div class="header"><%=user.loginname%> 创建的话题</div> <div class="inner padding"> <% if(typeof(topics) !== 'undefined' && topics.length > 0 ){ %> - <%- partial('../topic/list', - {topics:topics,pages:pages,current_pages:current_page,base:'/user/'+user.loginname+'/topics'}) %> + <%- partial('../topic/list', { + topics:topics, + pages:pages, + current_pages:current_page, + base:'/user/'+user.loginname+'/topics', + param: '' + }) %> <% }else{ %> <div class='inner'> <p>无话题</p>