Commit 18d7c0a2 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor API calls to Docs.Comments class.

parent bcc2e43a
Loading
Loading
Loading
Loading
+107 −0
Original line number Diff line number Diff line
@@ -78,6 +78,113 @@ Ext.define('Docs.Comments', {
        });
    },

    /**
     * Votes the comment up or down.
     * @param {Object} cfg
     * @param {Docs.model.Comment} cfg.comment
     * @param {Docs.model.Comment} cfg.direction
     * @param {Function} cfg.success Called with resulting direction and total score.
     * @param {Function} cfg.failure
     * @param {Object} cfg.scope
     */
    vote: function(cfg) {
        this.request("ajax", {
            url: '/comments/' + cfg.comment.get("id"),
            method: 'POST',
            params: { vote: cfg.direction },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    cfg.success && cfg.success.call(cfg.scope, data.direction, data.total);
                }
                else {
                    cfg.failure && cfg.failure.call(cfg.scope, data.reason);
                }
            },
            scope: this
        });
    },

    /**
     * Loads the plain Markdown content of comment.
     * @param {Object} cfg
     * @param {Docs.model.Comment} cfg.comment
     * @param {Function} cfg.success Called with the original Markdown content
     * @param {Function} cfg.failure
     * @param {Object} cfg.scope
     */
    loadContent: function(cfg) {
        this.request("ajax", {
            url: '/comments/' + cfg.comment.get("id"),
            method: 'GET',
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    cfg.success && cfg.success.call(cfg.scope, data.content);
                }
                else {
                    cfg.failure && cfg.failure.call(cfg.scope, data.reason);
                }
            },
            scope: this
        });
    },

    /**
     * Saves the plain Markdown content of comment.
     * @param {Object} cfg
     * @param {Docs.model.Comment} cfg.comment
     * @param {String} cfg.newContent The new content.
     * @param {Function} cfg.success Called with the resulting HTML content
     * @param {Function} cfg.failure
     * @param {Object} cfg.scope
     */
    saveContent: function(cfg) {
        this.request("ajax", {
            url: '/comments/' + cfg.comment.get("id"),
            method: 'POST',
            params: {
                content: cfg.newContent
            },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    cfg.success && cfg.success.call(cfg.scope, data.content);
                }
                else {
                    cfg.failure && cfg.failure.call(cfg.scope, data.reason);
                }
            },
            scope: this
        });
    },

    /**
     * Marks the comment as deleted or undoes the delete.
     * @param {Object} cfg
     * @param {Docs.model.Comment} cfg.comment
     * @param {Boolean} cfg.deleted True to delete, false to undo.
     * @param {Function} cfg.success
     * @param {Function} cfg.failure
     * @param {Object} cfg.scope
     */
    setDeleted: function(cfg) {
        Docs.Comments.request("ajax", {
            url: '/comments/' + cfg.comment.get("id") + (cfg.deleted ? '/delete' : '/undo_delete'),
            method: 'POST',
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    cfg.success && cfg.success.call(cfg.scope);
                }
                else {
                    cfg.failure && cfg.failure.call(cfg.scope, data.reason);
                }
            },
            scope: this
        });
    },

    /**
     * Performs request to the comments server.
     *
+52 −78
Original line number Diff line number Diff line
@@ -67,32 +67,27 @@ Ext.define('Docs.view.comments.List', {
        }, this, {preventDefault: true, delegate: selector});
    },

    vote: function(el, record, direction) {
    vote: function(el, comment, 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") {
        if (comment.get("upVote") && direction === "up" || comment.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);
                }
        Docs.Comments.vote({
            comment: comment,
            direction: direction,
            success: function(direction, total) {
                comment.set("upVote", direction === "up");
                comment.set("downVote", direction === "down");
                comment.set("score", total);
                comment.commit();
            },
            failure: function(msg) {
                Docs.Tip.show(msg, el);
            },
            scope: this
        });
@@ -100,7 +95,9 @@ Ext.define('Docs.view.comments.List', {

    // starts an editor on the comment
    edit: function(el, comment) {
        this.loadContent(comment, function(content) {
        Docs.Comments.loadContent({
            comment: comment,
            success: function(content) {
                var contentEl = Ext.get(el).up(".comment").down(".content");
                new Docs.view.comments.Form({
                    renderTo: contentEl,
@@ -108,10 +105,7 @@ Ext.define('Docs.view.comments.List', {
                    content: content,
                    listeners: {
                        submit: function(newContent) {
                        this.saveContent(comment, newContent, function(contentHtml) {
                            comment.set("contentHtml", contentHtml);
                            comment.commit();
                        }, this);
                            this.saveContent(comment, newContent);
                        },
                        cancel: function() {
                            this.refreshComment(comment);
@@ -119,59 +113,39 @@ Ext.define('Docs.view.comments.List', {
                        scope: this
                    }
                });
        }, this);
    },

    // re-renders the comment, discarding the form.
    refreshComment: function(comment) {
        this.refreshNode(this.getStore().findExact("id", comment.get("id")));
    },

    loadContent: function(comment, callback, scope) {
        Docs.Comments.request("ajax", {
            url: '/comments/' + comment.get("id"),
            method: 'GET',
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (data.success) {
                    callback.call(scope, data.content);
                }
            },
            scope: this
        });
    },

    saveContent: function(comment, newContent, callback, scope) {
        Docs.Comments.request("ajax", {
            url: '/comments/' + comment.get("id"),
            method: 'POST',
            params: {
                content: newContent
            },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (data.success) {
                    callback.call(scope, data.content);
                }
    saveContent: function(comment, newContent) {
        Docs.Comments.saveContent({
            comment: comment,
            newContent: newContent,
            success: function(contentHtml) {
                comment.set("contentHtml", contentHtml);
                comment.commit();
            },
            scope: this
        });
    },

    // re-renders the comment, discarding the form.
    refreshComment: function(comment) {
        this.refreshNode(this.getStore().findExact("id", comment.get("id")));
    },

    // marks the comment as deleted or undoes the delete
    setDeleted: function(el, comment, deleted) {
        Docs.Comments.request("ajax", {
            url: '/comments/' + comment.get("id") + (deleted ? '/delete' : '/undo_delete'),
            method: 'POST',
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (data.success) {
        Docs.Comments.setDeleted({
            comment: comment,
            deleted: deleted,
            success: function() {
                comment.set("deleted", deleted);
                comment.commit();
                }
                else {
                    Ext.Msg.alert('Error', data.reason || "There was an error submitting your request");
                }
            },
            failure: function(msg) {
                Ext.Msg.alert('Error', msg || "There was an error submitting your request");
            },
            scope: this
        });