Commit 23fb66e6 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Paging of recent comments.

This stopped working after switching to MongoDB, as the recent_comments
query no more returned the total_rows and other parameters needed for
creating the "Load more..." link.

Also fixed the "Load more..." link to always show the nr of comments
that are to be loaded.
parent 2f1dfb03
Loading
Loading
Loading
Loading
+20 −4
Original line number Diff line number Diff line
@@ -172,15 +172,31 @@ app.namespace('/auth/:sdk/:version', function(){
    });

    /**
     * Returns 100 most recent comments.
     * Returns n most recent comments.
     * Takes two parameters: offset and limit.
     *
     * The last comment object returned will contain `total_rows`,
     * `offset` and `limit` fields. I'd say it's a hack, but at least
     * it works for now.
     */
    app.get('/comments_recent', function(req, res) {
        Comment.find({
        var offset = parseInt(req.query.offset, 10) || 0;
        var limit = parseInt(req.query.limit, 10) || 100;
        var filter = {
            deleted: { '$ne': true },
            sdk: req.params.sdk,
            version: req.params.version
        }).sort('createdAt', -1).limit(100).run(function(err, comments){
            res.json(util.scoreComments(comments, req));
        };
        Comment.find(filter).sort('createdAt', -1).skip(offset).limit(limit).run(function(err, comments) {
            comments = util.scoreComments(comments, req);
            // Count all comments, store count to last comment
            Comment.count(filter).run(function(err, count) {
                var last = comments[comments.length-1];
                last.total_rows = count;
                last.offset = offset;
                last.limit = limit;
                res.json(comments);
            });
        });
    });

+18 −29
Original line number Diff line number Diff line
@@ -274,32 +274,19 @@ Ext.define('Docs.controller.Comments', {
    /**
     * Fetches the most recent comments
     */
    fetchRecentComments: function(id, startkey) {
        var url = this.addSid(Docs.baseUrl + '/' + Docs.commentsDb + '/' + Docs.commentsVersion + '/comments_recent'),
            limit = 100;

        var params = {
            limit: limit
        };

    fetchRecentComments: function(id, offset) {
        Ext.data.JsonP.request({
            url: url,
            url: this.addSid(Docs.baseUrl + '/' + Docs.commentsDb + '/' + Docs.commentsVersion + '/comments_recent'),
            method: 'GET',
            params: params,
            params: {
                offset: offset || 0,
                limit: 100
            },
            success: function(response) {
                var opts = {
                    showCls: true,
                this.renderComments(response, id, {
                    hideCommentForm: true,
                    limit: limit,
                    offset: response.offset,
                    total_rows: response.total_rows
                };

                if (startkey) {
                    opts.append = true;
                }

                this.renderComments(response, id, opts);
                    append: !!offset
                });
            },
            scope: this
        });
@@ -310,7 +297,7 @@ Ext.define('Docs.controller.Comments', {
        var moreLink = Ext.get(el);

        if (moreLink.hasCls('recent')) {
            this.fetchRecentComments('recentcomments', '[' + moreLink.getAttribute('rel') + ']');
            this.fetchRecentComments('recentcomments', moreLink.getAttribute('rel'));
        }
    },

@@ -622,7 +609,6 @@ Ext.define('Docs.controller.Comments', {
        var data = Ext.Array.map(rows, function(r) {
            r.id = r._id;
            r.key = r.target;
            r = Ext.merge(r, opts);
            return r;
        });

@@ -633,18 +619,21 @@ Ext.define('Docs.controller.Comments', {
        if (opts.append) {
            var list = comments.down('.comment-list'),
                more = comments.down('.fetchMoreComments'),
                last = data[data.length - 1];
                last = data[data.length - 1] || {};

            Docs.view.Comments.appendCommentsTpl.append(list, data);

            if ((last.offset + last.limit) > last.total_rows) {
            var total = last.total_rows;
            var loaded = last.offset + last.limit;
            var next_load = Math.min(last.limit, total - loaded);
            if (loaded >= total) {
                more.remove();
            } else {
                more.update(
                    '<span></span>Showing comments 1-' + (last.offset + last.limit) + ' of ' + last.total_rows + '. ',
                    'Click to load ' + last.limit + ' more...'
                    '<span></span>Showing comments 1-' + loaded + ' of ' + total + '. ' +
                    'Click to load ' + next_load + ' more...'
                );
                more.dom.setAttribute('rel', last.key.join(','));
                more.dom.setAttribute('rel', loaded);
            }
        } else {
            Docs.view.Comments.commentsTpl.append(comments, data);
+10 −5
Original line number Diff line number Diff line
@@ -129,12 +129,17 @@ Ext.define('Docs.view.Comments', {
                return '<a href="' + urlPrefix + url + '">' + title + '</a>';
            },
            moreComments: function(values) {
                var values = values[values.length - 1];
                if (values && values.total_rows > (values.offset + values.limit)) {
                var last = values[values.length - 1] || {};

                var total = last.total_rows;
                var loaded = last.offset + last.limit;
                var next_load = Math.min(last.limit, total - loaded);

                if (total > loaded) {
                    return [
                        '<a href="#" class="fetchMoreComments recent" rel="' + values.key.join(',') + '">',
                            '<span></span>Showing comments 1-' + (values.offset + values.limit) + ' of ' + values.total_rows + '. ',
                            'Click to load ' + values.limit + ' more...',
                        '<a href="#" class="fetchMoreComments recent" rel="' + loaded + '">',
                            '<span></span>Showing comments 1-' + loaded + ' of ' + total + '. ',
                            'Click to load ' + next_load + ' more...',
                        '</a>'
                    ].join('');
                } else {