Commit 8162326e authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Create Favorites controller.

Moved most of the logic that binds together Docs.Favorites,
FavoritesPanel, and Tree into this controller.

Also moved the drag-drop logic from FavoritesPanel to ClassGrid.
Looks like a better place for it, and I can also fire the needed
events (for controller) more easily directly from ClassGrid.
parent a6032e68
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -22,7 +22,8 @@ Ext.define('Docs.Application', {
    controllers: [
        'Classes',
        'Search',
        'Examples'
        'Examples',
        'Favorites'
    ],

    launch: function() {
+6 −1
Original line number Diff line number Diff line
@@ -9,7 +9,9 @@ Ext.define("Docs.Favorites", {
        observable: 'Ext.util.Observable'
    },

    init: function() {
    constructor: function() {
        this.callParent(arguments);

        this.addEvents(
            /**
             * Fired when favorite added.
@@ -22,6 +24,9 @@ Ext.define("Docs.Favorites", {
             */
            "remove"
        );
    },

    init: function() {
        this.callParent(arguments);

        // For backwards compatibility with old Favorites Model
+74 −0
Original line number Diff line number Diff line
/**
 * Controller for favorites.
 *
 * Syncronizes favorites store, grid and favorites markings in grid.
 */
Ext.define('Docs.controller.Favorites', {
    extend: 'Ext.app.Controller',

    refs: [
        {
            ref: 'panel',
            selector: 'favoritespanel'
        },
        {
            ref: 'tree',
            selector: 'classtree'
        }
    ],

    requires: [
        'Docs.Favorites',
        'Docs.Settings'
    ],

    init: function() {
        this.control({
            'favoritespanel': {
                resize: function(cmp, w, h) {
                    Docs.Settings.set('favorites-height', h);
                }
            },
            'favoritespanel > classgrid': {
                closeclick: function(url) {
                    Docs.Favorites.remove(url);
                },
                reorder: 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; }));
                }
            },
            'classtree': {
                addfavorite: function(url, title) {
                    Docs.Favorites.add(url, title);
                },
                removefavorite: function(url) {
                    Docs.Favorites.remove(url);
                }
            }
        });

        Docs.Favorites.on({
            add: function(url) {
                // Show favorites when first favorite added
                if (Docs.Favorites.getCount() > 0) {
                    this.getPanel().show();
                }
                // Add favorite marking to tree
                this.getTree().setFavorite(url, true);
            },
            remove: function(url) {
                // Hide favorites when favorites list empty
                if (Docs.Favorites.getCount() === 0) {
                    this.getPanel().hide();
                }
                // remove favorite marking from tree
                this.getTree().setFavorite(url, false);
            },
            scope: this
        });
    }

});
 No newline at end of file
+33 −2
Original line number Diff line number Diff line
@@ -34,9 +34,29 @@ Ext.define('Docs.view.ClassGrid', {
             * Fired when close button in grid clicked.
             * @param {String} url  URL of the page that was closed. For example "/api/Ext.Ajax".
             */
            "closeclick"
            "closeclick",
            /**
             * @event
             * Fired when items in grid reordered by drag-drop.
             */
            "reorder"
        );

        this.viewConfig = {
            plugins: [{
                pluginId: 'ddPlugin',
                ptype: 'gridviewdragdrop',
                animate: true,
                dragText: 'Drag and drop to reorganize'
            }],
            listeners: {
                drop: function() {
                    this.fireEvent("reorder");
                },
                scope: this
            }
        };

        this.columns = [
            {
                width: 18,
@@ -81,9 +101,20 @@ Ext.define('Docs.view.ClassGrid', {
            }
        }, this);

        // Initialize selection after rendering
        this.on("afterrender", function() {
            // Initialize selection after rendering
            this.selectUrl(this.selectedUrl);

            // Prevent row highlighting when doing drag-drop
            var ddPlugin = this.getView().getPlugin('ddPlugin');
            var self = this;
            ddPlugin.dragZone.onInitDrag = function() {
                self.addCls('drag');
                Ext.view.DragZone.prototype.onInitDrag.apply(this, arguments);
            };
            ddPlugin.dragZone.afterValidDrop = ddPlugin.dragZone.afterInvalidDrop = function() {
                self.removeCls('drag');
            };
        }, this);
    },

+11 −53
Original line number Diff line number Diff line
@@ -5,8 +5,7 @@ Ext.define('Docs.view.FavoritesPanel', {
    extend: 'Ext.tab.Panel',
    requires: [
        'Docs.view.ClassGrid',
        'Docs.Favorites',
        'Docs.Settings'
        'Docs.Favorites'
    ],
    alias: 'widget.favoritespanel',

@@ -17,47 +16,23 @@ Ext.define('Docs.view.FavoritesPanel', {
    split: true,

    initComponent: function() {
        this.addEvents(
            /**
             * @event
             * Fired when favorites reordered in grid.
             * @param {String} url  The URL of the favorited page.
             */
            "reorder"
        );

        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');
                        };
                    }
                }
                icons: Docs.icons
            }
        ];

@@ -65,24 +40,7 @@ Ext.define('Docs.view.FavoritesPanel', {
            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();
Loading