Commit 9a351c5b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor how checkboxes are bound to settings.

Encapsulate the checkboxes completely inside commentindex view, which
does the saving of checkbox states completely within itself, only
firering settingChange event for the controller to listen on.
parent 5f353ed2
Loading
Loading
Loading
Loading
+7 −25
Original line number Diff line number Diff line
@@ -121,10 +121,7 @@ Ext.define('Docs.controller.Comments', {
                        [ '.readComment',          'click', this.readComment],
                        [ '.fetchMoreComments',    'click', this.fetchMoreComments],
                        [ '.voteCommentUp',        'click', this.voteUp],
                        [ '.voteCommentDown',      'click', this.voteDown],
                        [ '#hideRead',            'change', this.hideRead],
                        [ '#hideCurrentUser',     'change', this.hideCurrentUser],
                        [ '#sortByScore',         'change', this.sortByScore]
                        [ '.voteCommentDown',      'click', this.voteDown]
                    ], function(delegate) {
                        cmp.el.addListener(delegate[1], delegate[2], this, {
                            preventDefault: true,
@@ -138,6 +135,12 @@ Ext.define('Docs.controller.Comments', {
                }
            },

            'commentindex': {
                settingChange: function() {
                    this.fetchRecentComments();
                }
            },

            'classoverview toolbar': {
                commentcountclick: function(cmp) {
                    var commentsDiv = Ext.get(Ext.query('.comments-section .comments-div')[0]);
@@ -272,27 +275,6 @@ Ext.define('Docs.controller.Comments', {
        });
    },

    hideRead: function() {
        this.updateCommentSetting('hideRead');
        this.fetchRecentComments();
    },

    hideCurrentUser: function() {
        this.updateCommentSetting('hideCurrentUser');
        this.fetchRecentComments();
    },

    sortByScore: function() {
        this.updateCommentSetting('sortByScore');
        this.fetchRecentComments();
    },

    updateCommentSetting: function(name) {
        var settings = Docs.Settings.get('comments');
        settings[name] = this.isChecked(name);
        Docs.Settings.set('comments', settings);
    },

    /**
     * Fetches the most recent comments
     */
+18 −1
Original line number Diff line number Diff line
@@ -66,14 +66,31 @@ Ext.define('Docs.view.comments.Index', {
    },

    // Initializes all checkboxes from settings.
    // Bind event handlers to fire changeSetting event when checked/unchecked.
    initCheckboxes: function() {
        var settings = Docs.Settings.get("comments");
        Ext.Array.forEach(['hideRead', 'hideCurrentUser', 'sortByScore'], function(id) {
            var cb = Ext.get(id);
            if (cb) {
                cb.dom.checked = settings[id];
                cb.on("change", function() {
                    this.saveSetting(id, cb.dom.checked);
                    /**
                     * @event settingChange
                     * Fired when one of the comments settings checkboxes is checked/unchecked.
                     * @param {String} name The name of the setting
                     * @param {Boolean} enabled True if setting is turned on, false when off.
                     */
                    this.fireEvent("settingChange", id, cb.dom.checked);
                }, this);
            }
        });
        }, this);
    },

    saveSetting: function(name, enabled) {
        var settings = Docs.Settings.get('comments');
        settings[name] = enabled;
        Docs.Settings.set('comments', settings);
    },

    /**