Commit 196d576d authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Voting on comments.

Handle the whole voting logic inside comments.List view.
parent 0afee496
Loading
Loading
Loading
Loading
+56 −7
Original line number Diff line number Diff line
@@ -8,7 +8,8 @@ Ext.define('Docs.view.comments.List', {
        'Docs.Auth',
        'Docs.Syntax',
        'Docs.Comments',
        'Docs.view.comments.Template'
        'Docs.view.comments.Template',
        'Docs.Tip'
    ],

    itemSelector: "div.comment",
@@ -19,7 +20,7 @@ Ext.define('Docs.view.comments.List', {
    initComponent: function() {
        this.store = Ext.create('Ext.data.Store', {
            fields: [
                "id",
                "_id",
                "author",
                "emailHash",
                "moderator",
@@ -36,7 +37,59 @@ Ext.define('Docs.view.comments.List', {

        this.callParent(arguments);

        this.on("refresh", this.syntaxHighlight, this);
        this.on("refresh", function() {
            Docs.Syntax.highlight(this.getEl());
        }, this);
        this.on("itemupdate", function(record, index, node) {
            Docs.Syntax.highlight(node);
        }, this);
    },

    afterRender: function() {
        this.callParent(arguments);
        this.delegateClick("a.voteCommentUp", function(el, r) {
            this.vote(el, r, "up");
        }, this);
        this.delegateClick("a.voteCommentDown", function(el, r) {
            this.vote(el, r, "down");
        }, this);
    },

    delegateClick: function(selector, callback, scope) {
        this.getEl().on("click", function(event, el) {
            callback.call(scope, el, this.getRecord(this.findItemByChild(el)));
        }, this, {preventDefault: true, delegate: selector});
    },

    vote: function(el, record, direction) {
        if (!Docs.Auth.isLoggedIn()) {
            Docs.Tip.show('Please login to vote on this comment', el);
            return;
        }
        if (record.get("upVote") && direction === "up" || record.get("downVote") && direction === "down") {
            Docs.Tip.show('You have already voted on this comment', el);
            return;
        }

        Docs.Comments.request("ajax", {
            url: '/comments/' + record.get("_id"),
            method: 'POST',
            params: { vote: direction },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);

                if (data.success) {
                    record.set("score", data.total);
                    record.set("upVote", data.direction === "up");
                    record.set("downVote", data.direction === "down");
                    record.commit();
                }
                else {
                    Docs.Tip.show(data.reason, el);
                }
            },
            scope: this
        });
    },

    /**
@@ -51,10 +104,6 @@ Ext.define('Docs.view.comments.List', {
        }

        this.store.loadData(comments, append);
    },

    syntaxHighlight: function() {
        Docs.Syntax.highlight(this.getEl());
    }

});