Commit 6f13fcb6 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move all comment updating methods into Comments model.

parent df8c87ab
Loading
Loading
Loading
Loading
+0 −133
Original line number Diff line number Diff line
@@ -78,139 +78,6 @@ 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
        });
    },

    /**
     * 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
     */
    markRead: function(cfg) {
        Docs.Comments.request("ajax", {
            url: '/comments/' + cfg.comment.get("id") + '/read',
            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.
     *
+110 −0
Original line number Diff line number Diff line
@@ -3,6 +3,9 @@
 */
Ext.define('Docs.model.Comment', {
    extend: 'Ext.data.Model',
    requires: [
        "Docs.Comments"
    ],
    fields: [
        {name: "id", mapping: "_id"},
        "author",
@@ -20,5 +23,112 @@ Ext.define('Docs.model.Comment', {
    proxy: {
        type: "ajax",
        reader: "json"
    },

    /**
     * Votes the comment up or down.
     * @param {"up"/"down"} direction
     * @param {Object} cfg Additional configuration
     * @param {Function} cfg.failure
     * @param {Object} cfg.scope
     */
    vote: function(direction, cfg) {
        this.request({
            method: 'POST',
            url: '/comments/' + this.get("id"),
            params: { vote: direction },
            success: function(data) {
                this.set("upVote", data.direction === "up");
                this.set("downVote", data.direction === "down");
                this.set("score", data.total);
                this.commit();
            },
            failure: Ext.Function.bind(cfg.failure, cfg.scope),
            scope: this
        });
    },

    /**
     * Loads the plain Markdown content of comment.
     * @param {Function} callback Called with the plain Markdown content.
     * @param {Object} scope
     */
    loadContent: function(callback, scope) {
        this.request({
            url: '/comments/' + this.get("id"),
            method: 'GET',
            success: function(data) {
                callback.call(scope, data.content);
            },
            scope: this
        });
    },

    /**
     * Saves the plain Markdown content of comment.
     * @param {String} newContent The new content.
     */
    saveContent: function(newContent) {
        this.request({
            url: '/comments/' + this.get("id"),
            method: 'POST',
            params: {
                content: newContent
            },
            success: function(data) {
                this.set("contentHtml", data.content);
                this.commit();
            },
            scope: this
        });
    },

    /**
     * Marks the comment as deleted or undoes the delete.
     * @param {Boolean} deleted True to delete, false to undo.
     */
    setDeleted: function(deleted) {
        this.request({
            url: '/comments/' + this.get("id") + (deleted ? '/delete' : '/undo_delete'),
            method: 'POST',
            success: function() {
                this.set("deleted", deleted);
                this.commit();
            },
            scope: this
        });
    },

    /**
     * Marks the comment as read.
     */
    markRead: function(cfg) {
        this.request({
            url: '/comments/' + this.get("id") + '/read',
            method: 'POST',
            success: function() {
                this.set("read", true);
                this.commit();
            },
            scope: this
        });
    },

    request: function(cfg) {
        Docs.Comments.request("ajax", {
            url: cfg.url,
            method: cfg.method,
            params: cfg.params,
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    cfg.success && cfg.success.call(cfg.scope, data);
                }
                else {
                    cfg.failure && cfg.failure.call(cfg.scope, data.reason);
                }
            },
            scope: this
        });
    }
});
+21 −66
Original line number Diff line number Diff line
@@ -84,35 +84,21 @@ Ext.define('Docs.view.comments.List', {
            return;
        }

        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) {
        comment.vote(direction, {failure: function(msg) {
            Docs.Tip.show(msg, el);
            },
            scope: this
        });
        }});
    },

    // starts an editor on the comment
    edit: function(el, comment) {
        Docs.Comments.loadContent({
            comment: comment,
            success: function(content) {
                var contentEl = Ext.get(el).up(".comment").down(".content");
        comment.loadContent(function(content) {
            new Docs.view.comments.Form({
                    renderTo: contentEl,
                renderTo: Ext.get(el).up(".comment").down(".content"),
                user: Docs.Auth.getUser(),
                content: content,
                listeners: {
                    submit: function(newContent) {
                            this.saveContent(comment, newContent);
                        comment.saveContent(newContent);
                    },
                    cancel: function() {
                        this.refreshComment(comment);
@@ -120,21 +106,7 @@ Ext.define('Docs.view.comments.List', {
                    scope: this
                }
            });
            },
            scope: this
        });
    },

    saveContent: function(comment, newContent) {
        Docs.Comments.saveContent({
            comment: comment,
            newContent: newContent,
            success: function(contentHtml) {
                comment.set("contentHtml", contentHtml);
                comment.commit();
            },
            scope: this
        });
        }, this);
    },

    // re-renders the comment, discarding the form.
@@ -144,28 +116,11 @@ Ext.define('Docs.view.comments.List', {

    // marks the comment as deleted or undoes the delete
    setDeleted: function(el, comment, deleted) {
        Docs.Comments.setDeleted({
            comment: comment,
            deleted: deleted,
            success: function() {
                comment.set("deleted", deleted);
                comment.commit();
            },
            failure: function(msg) {
                Ext.Msg.alert('Error', msg || "There was an error submitting your request");
            },
            scope: this
        });
        comment.setDeleted(deleted);
    },

    markRead: function(el, comment) {
        Docs.Comments.markRead({
            comment: comment,
            success: function() {
                comment.set("read", true);
                comment.commit();
            }
        });
        comment.markRead();
    },

    /**