Commit 34ce5e54 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor comment posting into Docs.Comments class.

parent 5d6153ce
Loading
Loading
Loading
Loading
+47 −2
Original line number Diff line number Diff line
@@ -78,6 +78,51 @@ 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
     * @param {Function} callback Called when finished.
     * @param {Object} callback.comment The newly posted comment.
     * @param {Object} scope
     */
    post: function(target, content, callback, scope) {
        this.request("ajax", {
            url: '/comments',
            method: 'POST',
            params: {
                target: Ext.JSON.encode(target),
                comment: content,
                url: this.buildPostUrl(target)
            },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    callback && callback.call(scope, data.comment);
                }
            },
            scope: this
        });
    },

    buildPostUrl: function(target) {
        var type = target[0];
        var cls = target[1];
        var member = target[2];

        if (type == 'video') {
            var hash = '#!/video/' + cls;
        }
        else if (type == 'guide') {
            var hash = '#!/guide/' + cls;
        }
        else {
            var hash = '#!/api/' + cls + (member ? '-' + member : '');
        }

        return "http://" + window.location.host + window.location.pathname + hash;
    },

    /**
     * Performs request to the comments server.
     *
@@ -89,7 +134,7 @@ Ext.define('Docs.Comments', {
     * @param {Object} config
     */
    request: function(type, config) {
        config.url = this.buildUrl(config.url);
        config.url = this.buildRequestUrl(config.url);
        if (type === "jsonp") {
            Ext.data.JsonP.request(config);
        }
@@ -100,7 +145,7 @@ Ext.define('Docs.Comments', {
        }
    },

    buildUrl: function(url) {
    buildRequestUrl: function(url) {
        url = Docs.baseUrl + '/' + Docs.commentsDb + '/' + Docs.commentsVersion + url;
        return url + (url.match(/\?/) ? '&' : '?') + 'sid=' + Docs.Auth.getSid();
    },
+4 −34
Original line number Diff line number Diff line
@@ -34,40 +34,10 @@ Ext.define('Docs.view.comments.ListWithForm', {
    },

    postComment: function(content) {
        Docs.Comments.request("ajax", {
            url: '/comments',
            method: 'POST',
            params: {
                target: Ext.JSON.encode(this.target),
                comment: content,
                url: this.buildUrl(this.target)
            },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success && data.id) {
        Docs.Comments.post(this.target, content, function(comment) {
            this.form.setValue('');
                    this.list.load([data.comment], true);
                }
                else {
                    Ext.Msg.alert('Error', data.reason || "There was an error submitting your request");
                }
            },
            scope: this
        });
    },

    buildUrl: function(target) {
        if (target[0] == 'video') {
            var hash = '#!/video/' + target[1];
        }
        else if (target[0] == 'guide') {
            var hash = '#!/guide/' + target[1];
        }
        else if (target[2] != '') {
            var hash = '#!/api/' + target[1] + '-' + target[2];
        }

        return "http://" + window.location.host + window.location.pathname + hash;
            this.list.load([comment], true);
        }, this);
    },

    /**