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

Hide favorites panel when no favorites.

No more add the default top10 favorites, leave the favorites empty if
it's empty.  Automatically show the favorites panel when first item
marked as favorite.

Moved the tabpanel with favorites to separate class.

The Docs.Favorites is now observable, firering "add" and "remove" events
which are listened both by the tree and the grid.  The Docs.Favorites no
more knows anything about the tree.
parent aba9988d
Loading
Loading
Loading
Loading
+26 −31
Original line number Diff line number Diff line
@@ -5,33 +5,28 @@ Ext.define("Docs.Favorites", {
    extend: 'Docs.LocalStore',
    storeName: 'Favorites',
    singleton: true,
    mixins: {
        observable: 'Ext.util.Observable'
    },

    init: function() {
        this.addEvents(
            /**
             * Fired when favorite added.
             * @param {String} url The URL of the favorited page
             */
            "add",
            /**
             * Fired when favorite removed.
             * @param {String} url The URL of the favorited page
             */
            "remove"
        );
        this.callParent(arguments);

        // Populate favorites with Top 10 classes
        if (this.store.data.items.length == 0) {
            var top10 = [
                'Ext.data.Store',
                'Ext',
                'Ext.grid.Panel',
                'Ext.panel.Panel',
                'Ext.form.field.ComboBox',
                'Ext.data.Model',
                'Ext.form.Panel',
                'Ext.button.Button',
                'Ext.tree.Panel',
                'Ext.Component'
            ];
            this.store.add(Ext.Array.map(top10, function(clsName) {
                return {url: "/api/"+clsName, title: clsName};
            }));
            this.syncStore();
        }
        
        // For backwards compatibility with old Favorites Model
        // convert the old-style records to new schema.
        else if (!this.store.first().get("url")) {
        if (this.store.first() && !this.store.first().get("url")) {
            this.store.each(function(r) {
                r.set("title", r.data.cls);
                r.set("url", "/api/"+r.get("cls"));
@@ -41,14 +36,6 @@ Ext.define("Docs.Favorites", {
        }
    },

    /**
     * Associates Favorites with Docs TreePanel component.
     * @param {Docs.view.tree.Tree} tree
     */
    setTree: function(tree) {
        this.tree = tree;
    },

    /**
     * Adds page to favorites
     *
@@ -59,7 +46,7 @@ Ext.define("Docs.Favorites", {
        if (!this.has(url)) {
            this.store.add({url: url, title: title});
            this.syncStore();
            this.tree.setFavorite(url, true);
            this.fireEvent("add", url);
        }
    },

@@ -72,7 +59,7 @@ Ext.define("Docs.Favorites", {
        if (this.has(url)) {
            this.store.removeAt(this.store.findExact('url', url));
            this.syncStore();
            this.tree.setFavorite(url, false);
            this.fireEvent("remove", url);
        }
    },

@@ -84,5 +71,13 @@ Ext.define("Docs.Favorites", {
     */
    has: function(url) {
        return this.store.findExact('url', url) > -1;
    },

    /**
     * Returns the number of favorites.
     * @return {Number}
     */
    getCount: function() {
        return this.store.getCount();
    }
});
+91 −0
Original line number Diff line number Diff line
/**
 * TabPanel with favorites grid
 */
Ext.define('Docs.view.FavoritesPanel', {
    extend: 'Ext.tab.Panel',
    requires: [
        'Docs.view.ClassGrid',
        'Docs.Favorites',
        'Docs.Settings'
    ],
    alias: 'widget.favoritespanel',

    padding: '2 4 0 0',
    bodyPadding: '3 15 0 12',
    border: false,
    plain: true,
    split: true,

    initComponent: function() {
        this.items = [
            {
                xtype: 'classgrid',
                id: 'favorites-grid',
                title: 'Favorites',
                iconCls: 'icon-fav',
                viewConfig: {
                    plugins: [{
                        pluginId: 'favGridDD',
                        ptype: 'gridviewdragdrop',
                        animate: true,
                        dragText: 'Drag and drop to reorganize'
                    }],
                    listeners: {
                        drop: function() {
                            // Hack to fix a bug in localStorage which prevents the order of
                            // items being saved when they're changed
                            var store = Ext.getStore('Favorites');
                            store.getProxy().setIds(Ext.Array.map(store.data.items, function(i) { return i.data.id; }));
                        }
                    }
                },
                store: Ext.getStore('Favorites'),
                icons: Docs.icons,
                listeners: {
                    closeclick: function(cls) {
                        Docs.Favorites.remove(cls);
                    },
                    // Prevent row highlighting when doing drag-drop
                    afterrender: function() {
                        var ddPlugin = this.getView().getPlugin('favGridDD');

                        ddPlugin.dragZone.onInitDrag = function() {
                            Ext.getCmp('favorites-grid').addCls('drag');
                            Ext.view.DragZone.prototype.onInitDrag.apply(this, arguments);
                        };
                        ddPlugin.dragZone.afterValidDrop = ddPlugin.dragZone.afterInvalidDrop = function() {
                            Ext.getCmp('favorites-grid').removeCls('drag');
                        };
                    }
                }
            }
        ];

        this.on({
            afterRender: function() {
                // Add 7px padding at left side of tab-bar
                this.tabBar.insert(0, {width: 7, xtype: 'container'});
            },
            resize: function(cmp, w, h) {
                Docs.Settings.set('favorites-height', h);
            }
        });

        Docs.Favorites.on({
            add: function() {
                if (Docs.Favorites.getCount() > 0) {
                    this.show();
                }
            },
            remove: function() {
                if (Docs.Favorites.getCount() === 0) {
                    this.hide();
                }
            },
            scope: this
        });

        this.callParent();
    }

});
+3 −60
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ Ext.define('Docs.view.Viewport', {
        'Docs.view.cls.Container',
        'Docs.view.index.Container',
        'Docs.view.tree.Tree',
        'Docs.view.ClassGrid',
        'Docs.view.FavoritesPanel',
        'Docs.Favorites',
        'Docs.Settings',
        'Docs.History'
@@ -21,7 +21,6 @@ Ext.define('Docs.view.Viewport', {

    initComponent: function() {
        this.items = [

            {
                region:'west',
                width: 240,
@@ -80,67 +79,11 @@ Ext.define('Docs.view.Viewport', {
                        border: false,
                        items: [
                            {
                                xtype: 'favoritespanel',
                                id: 'classes-tab-panel',
                                xtype: 'tabpanel',
                                region: 'north',
                                height: Docs.Settings.get('favorites-height') || 150,
                                padding: '2 4 0 0',
                                bodyPadding: '3 15 0 12',
                                border: false,
                                plain: true,
                                split: true,
                                listeners: {
                                    afterRender: function() {
                                        // Add 7px padding at left side of tab-bar
                                        this.tabBar.insert(0, {width: 7, xtype: 'container'});
                                    },
                                    resize: function(cmp, w, h) {
                                        Docs.Settings.set('favorites-height', h);
                                    }
                                },
                                items: [
                                    {
                                        xtype: 'classgrid',
                                        id: 'favorites-grid',
                                        title: 'Favorites',
                                        iconCls: 'icon-fav',
                                        viewConfig: {
                                            plugins: [{
                                                pluginId: 'favGridDD',
                                                ptype: 'gridviewdragdrop',
                                                animate: true,
                                                dragText: 'Drag and drop to reorganize'
                                            }],
                                            listeners: {
                                                drop: function() {
                                                    // Hack to fix a bug in localStorage which prevents the order of
                                                    // items being saved when they're changed
                                                    var store = Ext.getStore('Favorites');
                                                    store.getProxy().setIds(Ext.Array.map(store.data.items, function(i) { return i.data.id; }));
                                                }
                                            }
                                        },
                                        store: Ext.getStore('Favorites'),
                                        icons: Docs.icons,
                                        listeners: {
                                            closeclick: function(cls) {
                                                Docs.Favorites.remove(cls);
                                            },
                                            // Prevent row highlighting when doing drag-drop
                                            afterrender: function() {
                                                var ddPlugin = this.getView().getPlugin('favGridDD');

                                                ddPlugin.dragZone.onInitDrag = function() {
                                                    Ext.getCmp('favorites-grid').addCls('drag');
                                                    Ext.view.DragZone.prototype.onInitDrag.apply(this, arguments);
                                                };
                                                ddPlugin.dragZone.afterValidDrop = ddPlugin.dragZone.afterInvalidDrop = function() {
                                                    Ext.getCmp('favorites-grid').removeCls('drag');
                                                };
                                            }
                                        }
                                    }
                                ]
                                hidden: Docs.Favorites.getCount() === 0
                            },
                            {
                                region: 'center',
+7 −1
Original line number Diff line number Diff line
@@ -39,10 +39,16 @@ Ext.define('Docs.view.tree.Tree', {

        this.callParent();

        Docs.Favorites.on("add", function(url) {
            this.setFavorite(url, true);
        }, this);
        Docs.Favorites.on("remove", function(url) {
            this.setFavorite(url, false);
        }, this);

        // Add links for favoriting classes.
        // Do this after callParent, because the getRootNode() will
        // work after initComponent has run.
        Docs.Favorites.setTree(this);
        this.initFavIcons();
    },