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

Implement subscribing/unsubscribing.

parent 06227043
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -28,6 +28,15 @@ Ext.define('Docs.CommentSubscriptions', {
     */
    has: function(target) {
        return this.subscriptions[target.join("__")];
    },

    /**
     * Sets the subscribed/unsubscribed status of a target.
     * @param {String[]} target
     * @param {Boolean} subscribed
     */
    set: function(target, subscribed) {
        this.subscriptions[target.join("__")] = subscribed;
    }

});
+26 −0
Original line number Diff line number Diff line
@@ -146,6 +146,32 @@ Ext.define('Docs.Comments', {
        return "http://" + window.location.host + window.location.pathname + hash;
    },

    /**
     * Subscribes (or unsubscribes) the current user to particular target.
     * @param {String[]} target An array of `[type, cls, member]`
     * @param {Boolean} subscribed True to subscribe, false to unsubscribe.
     * @param {Function} callback Called when finished.
     * @param {Object} scope
     */
    subscribe: function(target, subscribed, callback, scope) {
        this.request("ajax", {
            url: '/subscribe',
            method: 'POST',
            params: {
                target: Ext.JSON.encode(target),
                subscribed: subscribed
            },
            callback: function(options, success, response) {
                var data = Ext.JSON.decode(response.responseText);
                if (success && data.success) {
                    this.subscriptions.set(target, subscribed);
                    callback && callback.call(scope);
                }
            },
            scope: this
        });
    },

    /**
     * Performs request to the comments server.
     *
+2 −2
Original line number Diff line number Diff line
@@ -187,12 +187,12 @@ Ext.define('Docs.view.comments.Form', {

        this.getEl().on("click", function(event, el) {
            /**
             * @event changeSubscription
             * @event subscriptionChange
             * Fired when the subscription checkbox ticked.
             * @param {Boolean} subscribe True to subscribe.
             * False to unsubscribe.
             */
            this.fireEvent("changeSubscription", Ext.get(el).getAttribute("checked"));
            this.fireEvent("subscriptionChange", Ext.get(el).dom.checked);
        }, this, {delegate: "input.subscriptionCheckbox"});
    },

+14 −7
Original line number Diff line number Diff line
@@ -31,13 +31,6 @@ Ext.define('Docs.view.comments.ListWithForm', {
        this.callParent(arguments);
    },

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

    /**
     * Loads array of comments into the view.
     * @param {Object[]} comments
@@ -81,10 +74,24 @@ Ext.define('Docs.view.comments.ListWithForm', {
            userSubscribed: Docs.Comments.hasSubscription(this.target),
            listeners: {
                submit: this.postComment,
                subscriptionChange: this.subscribe,
                scope: this
            }
        });
        this.add(this.commentingForm);
    },

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

    subscribe: function(subscribed) {
        Docs.Comments.subscribe(this.target, subscribed, function() {
            alert("Subscription changed!");
        }, this);
    }

});