Loading opt/comments-server-side/app.js +20 −11 Original line number Diff line number Diff line Loading @@ -198,20 +198,29 @@ app.get('/auth/:sdk/:version/comments_recent', util.getCommentReads, function(re filter._id = { $nin: req.commentMeta.reads }; } Comment.find(filter).sort('createdAt', -1).skip(offset).limit(limit).run(function(err, comments) { Comment.find(filter).sort("createdAt", -1).run(function(err, comments) { var total_rows = comments.length; comments = util.scoreComments(comments, req); // Count all comments, store count to last comment Comment.count(filter).run(function(err, count) { // For now this is the simplest solution to enable sorting by score. // A bit inefficient to select all records, but it's only for // moderators, so it shouldn't pose much of a performance problem. if (req.query.sortByScore) { util.sortByField(comments, "score", "DESC"); } comments = comments.slice(offset, offset+limit); // store total count to last comment var last = comments[comments.length-1]; if (last) { last.total_rows = count; last.total_rows = total_rows; last.offset = offset; last.limit = limit; } res.json(comments); }); }); }); /** * Returns number of comments for each class/member, Loading opt/comments-server-side/util.js +24 −0 Original line number Diff line number Diff line Loading @@ -58,6 +58,30 @@ exports.scoreComments = function(comments, req) { }); }; /** * Sorts array of objects by the value of given field. * * @param {Array} arr * @param {String} field * @param {String} [direction="ASC"] either "ASC" or "DESC". */ exports.sortByField = function(arr, field, direction) { if (direction === "DESC") { var more = -1; var less = 1; } else { var more = 1; var less = -1; } arr.sort(function(aObj, bObj) { var a = aObj[field]; var b = bObj[field]; return a > b ? more : a < b ? less : 0; }); }; /** * Performs voting on comment. * Loading template/app/controller/Comments.js +8 −4 Original line number Diff line number Diff line Loading @@ -121,7 +121,8 @@ Ext.define('Docs.controller.Comments', { [ '.fetchMoreComments', 'click', this.fetchMoreComments], [ '.voteCommentUp', 'click', this.voteUp], [ '.voteCommentDown', 'click', this.voteDown], [ '#hideRead', 'change', function() { this.fetchRecentComments(); }] [ '#hideRead', 'change', function() { this.fetchRecentComments(); }], [ '#sortByScore', 'change', function() { this.fetchRecentComments(); }] ], function(delegate) { cmp.el.addListener(delegate[1], delegate[2], this, { preventDefault: true, Loading Loading @@ -278,9 +279,12 @@ Ext.define('Docs.controller.Comments', { limit: 100 }; if (this.isHideReadChecked()) { if (this.isChecked('hideRead')) { params.hideRead = 1; } if (this.isChecked('sortByScore')) { params.sortByScore = 1; } this.request("jsonp", { url: '/comments_recent', Loading @@ -297,8 +301,8 @@ Ext.define('Docs.controller.Comments', { }); }, isHideReadChecked: function() { var cb = Ext.get('hideRead'); isChecked: function(id) { var cb = Ext.get(id); return cb && cb.dom.checked; }, Loading template/app/view/comments/Index.js +14 −2 Original line number Diff line number Diff line Loading @@ -11,8 +11,20 @@ Ext.define('Docs.view.comments.Index', { autoScroll: true, items: [ { xtype: 'container', html: '<h1>Recent Comments</h1> Hide read: <input type="checkbox" name="hideRead" id="hideRead" />' }, { xtype: 'container', id: 'recentcomments' } { xtype: 'container', html: [ '<h1>Recent Comments</h1>', '<ul id="comment-index-controls">', '<li><label><input type="checkbox" name="hideRead" id="hideRead" /> Hide read</label></li>', '<li><label><input type="checkbox" name="sortByScore" id="sortByScore" /> Sort by score</label></li>', '</ul>' ].join(" ") }, { xtype: 'container', id: 'recentcomments' } ], /** Loading template/resources/sass/_comments.scss +6 −0 Original line number Diff line number Diff line Loading @@ -298,3 +298,9 @@ border: 1px solid #e1ba53; @include border-radius(4px); font-weight: bold; } } #comment-index-controls { position: absolute; padding-top: 1em; li { list-style-type: none; } } Loading
opt/comments-server-side/app.js +20 −11 Original line number Diff line number Diff line Loading @@ -198,20 +198,29 @@ app.get('/auth/:sdk/:version/comments_recent', util.getCommentReads, function(re filter._id = { $nin: req.commentMeta.reads }; } Comment.find(filter).sort('createdAt', -1).skip(offset).limit(limit).run(function(err, comments) { Comment.find(filter).sort("createdAt", -1).run(function(err, comments) { var total_rows = comments.length; comments = util.scoreComments(comments, req); // Count all comments, store count to last comment Comment.count(filter).run(function(err, count) { // For now this is the simplest solution to enable sorting by score. // A bit inefficient to select all records, but it's only for // moderators, so it shouldn't pose much of a performance problem. if (req.query.sortByScore) { util.sortByField(comments, "score", "DESC"); } comments = comments.slice(offset, offset+limit); // store total count to last comment var last = comments[comments.length-1]; if (last) { last.total_rows = count; last.total_rows = total_rows; last.offset = offset; last.limit = limit; } res.json(comments); }); }); }); /** * Returns number of comments for each class/member, Loading
opt/comments-server-side/util.js +24 −0 Original line number Diff line number Diff line Loading @@ -58,6 +58,30 @@ exports.scoreComments = function(comments, req) { }); }; /** * Sorts array of objects by the value of given field. * * @param {Array} arr * @param {String} field * @param {String} [direction="ASC"] either "ASC" or "DESC". */ exports.sortByField = function(arr, field, direction) { if (direction === "DESC") { var more = -1; var less = 1; } else { var more = 1; var less = -1; } arr.sort(function(aObj, bObj) { var a = aObj[field]; var b = bObj[field]; return a > b ? more : a < b ? less : 0; }); }; /** * Performs voting on comment. * Loading
template/app/controller/Comments.js +8 −4 Original line number Diff line number Diff line Loading @@ -121,7 +121,8 @@ Ext.define('Docs.controller.Comments', { [ '.fetchMoreComments', 'click', this.fetchMoreComments], [ '.voteCommentUp', 'click', this.voteUp], [ '.voteCommentDown', 'click', this.voteDown], [ '#hideRead', 'change', function() { this.fetchRecentComments(); }] [ '#hideRead', 'change', function() { this.fetchRecentComments(); }], [ '#sortByScore', 'change', function() { this.fetchRecentComments(); }] ], function(delegate) { cmp.el.addListener(delegate[1], delegate[2], this, { preventDefault: true, Loading Loading @@ -278,9 +279,12 @@ Ext.define('Docs.controller.Comments', { limit: 100 }; if (this.isHideReadChecked()) { if (this.isChecked('hideRead')) { params.hideRead = 1; } if (this.isChecked('sortByScore')) { params.sortByScore = 1; } this.request("jsonp", { url: '/comments_recent', Loading @@ -297,8 +301,8 @@ Ext.define('Docs.controller.Comments', { }); }, isHideReadChecked: function() { var cb = Ext.get('hideRead'); isChecked: function(id) { var cb = Ext.get(id); return cb && cb.dom.checked; }, Loading
template/app/view/comments/Index.js +14 −2 Original line number Diff line number Diff line Loading @@ -11,8 +11,20 @@ Ext.define('Docs.view.comments.Index', { autoScroll: true, items: [ { xtype: 'container', html: '<h1>Recent Comments</h1> Hide read: <input type="checkbox" name="hideRead" id="hideRead" />' }, { xtype: 'container', id: 'recentcomments' } { xtype: 'container', html: [ '<h1>Recent Comments</h1>', '<ul id="comment-index-controls">', '<li><label><input type="checkbox" name="hideRead" id="hideRead" /> Hide read</label></li>', '<li><label><input type="checkbox" name="sortByScore" id="sortByScore" /> Sort by score</label></li>', '</ul>' ].join(" ") }, { xtype: 'container', id: 'recentcomments' } ], /** Loading
template/resources/sass/_comments.scss +6 −0 Original line number Diff line number Diff line Loading @@ -298,3 +298,9 @@ border: 1px solid #e1ba53; @include border-radius(4px); font-weight: bold; } } #comment-index-controls { position: absolute; padding-top: 1em; li { list-style-type: none; } }