Commit d1af389f authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Initial loading and posting of replies.

For now it's somewhat buggy, throwing errors here and there, but
the essential functionality works.
parent d2538b41
Loading
Loading
Loading
Loading
+32 −10
Original line number Diff line number Diff line
@@ -157,27 +157,49 @@ Ext.define('Docs.Comments', {
    },

    /**
     * Posts a new comments to a particular target.
     * @param {String[]} target An array of `[type, cls, member]`
     * @param {String} content The new content
     * Loads the replies of a particular comment.
     * @param {String[]} parentId ID of parent comment.
     * @param {Function} callback Called when finished.
     * @param {Object} callback.comment The newly posted comment.
     * @param {Object[]} callback.comments An array of comments.
     * @param {Object} scope
     */
    post: function(target, content, callback, scope) {
    loadReplies: function(parentId, callback, scope) {
        this.request("jsonp", {
            url: '/replies',
            method: 'GET',
            params: {
                parentId: parentId
            },
            success: callback,
            scope: scope
        });
    },

    /**
     * Posts a new comments to a particular target.
     * @param {Object} cfg A config for posting new comment:
     * @param {String[]} cfg.target An array of `[type, cls, member]`
     * @param {Number}   cfg.parentId ID of parent comment.
     * @param {String}   cfg.content The new content
     * @param {Function} cfg.callback Called when finished.
     * @param {Object}   cfg.callback.comment The newly posted comment.
     * @param {Object}   cfg.scope
     */
    post: function(cfg) {
        this.request("ajax", {
            url: '/comments',
            method: 'POST',
            params: {
                target: Ext.JSON.encode(target),
                comment: content,
                url: this.buildPostUrl(target)
                target: Ext.JSON.encode(cfg.target),
                parentId: cfg.parentId,
                comment: cfg.content,
                url: this.buildPostUrl(cfg.target)
            },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    this.changeCount(target, +1);
                    callback && callback.call(scope, data.comment);
                    this.changeCount(cfg.target, +1);
                    cfg.callback && cfg.callback.call(cfg.scope, data.comment);
                }
            },
            scope: this
+3 −1
Original line number Diff line number Diff line
@@ -19,7 +19,9 @@ Ext.define('Docs.model.Comment', {
        "contentHtml",
        "read",
        "tags",
        "deleted"
        "deleted",
        "parentId",
        "replyCount"
    ],
    proxy: {
        type: "ajax",
+23 −8
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@ Ext.define('Docs.view.comments.Expander', {
    alias: "widget.commentsExpander",
    extend: 'Ext.Component',
    requires: [
        'Docs.Comments',
        'Docs.Comments'
    ],
    uses: [
        'Docs.view.comments.ListWithForm'
    ],
    componentCls: "comments-expander",
@@ -14,6 +16,10 @@ Ext.define('Docs.view.comments.Expander', {
     * @cfg {String[]} target
     * The target specification array `[type, cls, member]`.
     */
    /**
     * @cfg {String[]} parentId
     * ID of the parent comment, if any.
     */
    /**
     * @cfg {Number} count
     */
@@ -54,10 +60,11 @@ Ext.define('Docs.view.comments.Expander', {

    afterRender: function() {
        this.callParent(arguments);
        this.getEl().on("click", this.toggle, this, {
            preventDefault: true,
            delegate: ".toggleComments"
        this.getEl().select(".toggleComments").each(function(el) {
            el.on("click", this.toggle, this, {
                preventDefault: true
            });
        }, this);
    },

    toggle: function() {
@@ -96,13 +103,21 @@ Ext.define('Docs.view.comments.Expander', {
    loadComments: function() {
        this.list = new Docs.view.comments.ListWithForm({
            target: this.target,
            parentId: this.parentId,
            newCommentTitle: this.newCommentTitle,
            renderTo: this.getEl()
        });

        if (this.parentId) {
            Docs.Comments.loadReplies(this.parentId, function(comments) {
                this.list.load(comments);
            }, this);
        }
        else {
            Docs.Comments.load(this.target, function(comments) {
                this.list.load(comments);
            }, this);
        }
    },

    /**
+20 −1
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ Ext.define('Docs.view.comments.List', {
        'Docs.view.comments.Template',
        'Docs.view.comments.Form',
        'Docs.view.comments.TagEditor',
        'Docs.view.comments.Expander',
        'Docs.model.Comment',
        'Docs.Tip'
    ],
@@ -38,9 +39,27 @@ Ext.define('Docs.view.comments.List', {

        this.on("refresh", function() {
            Docs.Syntax.highlight(this.getEl());
            this.renderExpanders(this.store.getRange());
        }, this);
        this.on("itemupdate", function(record, index, node) {
        this.on("itemupdate", function(comment, index, node) {
            Docs.Syntax.highlight(node);
            this.renderExpanders([comment]);
        }, this);
    },

    renderExpanders: function(comments) {
        if (comments[0] && comments[0].get("parentId")) {
            return;
        }

        Ext.Array.forEach(comments, function(comment) {
            new Docs.view.comments.Expander({
                count: comment.get("replyCount"),
                target: comment.get("target"),
                parentId: comment.get("id"),
                newCommentTitle: "<b>Reply to comment</b>",
                renderTo: this.getNode(comment)
            });
        }, this);
    },

+14 −4
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@ Ext.define('Docs.view.comments.ListWithForm', {
     * @cfg {String[]} target
     * The target of the comments (used for posting new comment).
     */
    /**
     * @cfg {Number} parentId
     * ID of parent comment.
     */
    /**
     * @cfg {String} newCommentTitle
     * A custom title for the new comment form.
@@ -82,10 +86,16 @@ Ext.define('Docs.view.comments.ListWithForm', {
    },

    postComment: function(content) {
        Docs.Comments.post(this.target, content, function(comment) {
        Docs.Comments.post({
            target: this.target,
            parentId: this.parentId,
            content: content,
            callback: function(comment) {
                this.commentingForm.setValue('');
                this.list.load([comment], true);
        }, this);
            },
            scope: this
        });
    },

    subscribe: function(subscribed) {