Commit 5f353ed2 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Use localStoreage for saving recent comments settings.

No need to use cookies, we already have Docs.Settings class that uses
localStorage and is meant just for that - storing various settings.
parent 3d63f8e7
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,11 @@ Ext.define("Docs.Settings", {
            "inherited": true,
            "accessor": true
        },
        comments: {
            hideRead: false,
            hideCurrentUser: false,
            sortByScore: false
        },
        showPrivateClasses: false,
        classTreeLogic: "PackageLogic"
    },
+27 −37
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ Ext.define('Docs.controller.Comments', {

    requires: [
        "Docs.view.auth.LoginHelper",
        "Docs.Settings",
        "Docs.Syntax",
        "Docs.Tip"
    ],
@@ -271,27 +272,40 @@ 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
     */
    fetchRecentComments: function(offset) {
        var settings = Docs.Settings.get('comments');
        var params = {
            offset: offset || 0,
            limit: 100
            limit: 100,
            hideRead: settings.hideRead ? 1 : undefined,
            hideCurrentUser: settings.hideCurrentUser ? 1 : undefined,
            sortByScore: settings.sortByScore ? 1 : undefined
        };

        if (Ext.util.Cookies.get('hideRead')) {
            params.hideRead = 1;
        }

        if (Ext.util.Cookies.get('hideCurrentUser')) {
            params.hideCurrentUser = 1;
        }

        if (Ext.util.Cookies.get('sortByScore')) {
            params.sortByScore = 1;
        }

        this.getIndex().setMasked(true);

        this.request("jsonp", {
@@ -316,15 +330,6 @@ Ext.define('Docs.controller.Comments', {
        return cb && cb.dom.checked;
    },

    setCookie: function(name) {
        var checked = this.isChecked(name);
        if (checked) {
            Ext.util.Cookies.set(name, true);
        } else {
            Ext.util.Cookies.clear(name);
        }
    },

    fetchMoreComments: function(cmp, el) {
        this.fetchRecentComments(Ext.get(el).getAttribute('rel'));
    },
@@ -505,21 +510,6 @@ Ext.define('Docs.controller.Comments', {
        this.vote('down', el);
    },

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

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

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

    updateSubscription: function(cmp, el) {
        var commentEl = Ext.get(el).up('.comments-div'),
            labelEl = Ext.get(el).up('label'),
+10 −15
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ Ext.define('Docs.view.comments.Index', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.commentindex',
    mixins: ['Docs.view.Scrolling'],
    requires: ['Docs.Settings'],

    cls: 'comment-index',
    margin: '10 0 0 0',
@@ -48,7 +49,7 @@ Ext.define('Docs.view.comments.Index', {

    afterRender: function() {
        this.callParent(arguments);
        this.initCookies();
        this.initCheckboxes();

        this.setMasked(true);
    },
@@ -64,21 +65,15 @@ Ext.define('Docs.view.comments.Index', {
        }
    },

    /**
     * Loops through each of the filter values (manually set for now) and checked for a previous set cookie value.
     */
    initCookies: function() {
        var checkboxes = ['hideRead', 'hideCurrentUser', 'sortByScore'],
            ln = checkboxes.length,
            i, el;

        for (i = 0; i < ln; i++) {
            name = checkboxes[i];
            el = Ext.get(name);
            if (el) {
                el.dom.checked = Boolean(Ext.util.Cookies.get(name));
            }
    // Initializes all checkboxes from settings.
    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];
            }
        });
    },

    /**