A three-panel application layout with a single line of code
This recipe explains how you can build a reusable Ext JS component that encapsulates one of the most popular GUI styles—a three-panel layout. Using the component will produce a layout like the one shown in the following screenshot:
This layout consists of a Navigation panel, a Master panel, and a Details panel as shown in the following screenshot:
The Master panel has a toolbar with a button that switches the Details panel's location from bottom to right, or hides it altogether:
Besides being able to add components to any of the layout's panels via configuration objects, you can also reconfigure any of the panels by the same means.
Check if a configuration object for the Master/Details container panel was specified, and create the object's instance:
Ext.extend(MasterDetailsCnt, Ext.Panel);
var masterDetailsCntConfig={};
if (conf && conf.masterDetailsCntConfig) {
masterDetailsCntConfig=conf.masterDetailsCntConfig;
}
var masterDetailsCnt=new MasterDetailsCnt(masterDetailsCntConfig);
Now that the panels have been created, define the layout:
In the following screenshot, you will see what the finished layout looks like:
How it works...
The ThreePanelApp layout is essentially a custom Ext JS component based on the Ext.Viewport class. Creating the layout starts with a basic code template. This template has placeholders for each of the panels in the layout.
Notice that in addition to the placeholders for the Navigation, Master, and Details panels, there is a place for a fourth panel—MasterDetailsCnt. This panel will be a container for the Master panel and two additional panels that will host the Details panel when it is positioned below or to the right of the Master panel respectively. ThreePanelApp will use a border layout that will contain the Navigation panel (which will take the west region) and the MasterDetailsCnt panel (which will take the center region). MasterDetailsCnt will use a BorderLayout to position the Master and Details panels' hosts.
Moving on, the Master panel is an Ext.Panel extension that contains the cycle button for repositioning the Details panel:
The Master Panel uses a default config object that gets overridden by the masterPanelConf object passed during the construction of a ThreePanelApp instance:
This pattern of overriding the default configuration (also used with the rest of the panels), allows you to add your application's components to ThreePanelApp.
Another interesting area in the Master panel is the toggleDetails() function. This function achieves the repositioning of the Details panel by switching the panel's container or hiding it altogether as shown here:
toggleDetails: function(m, pressed) {
var details=Ext.getCmp('details-panel');
var right=Ext.getCmp('right-details');
var bot=Ext.getCmp('bottom-details');
switch (m.text) {
case 'Bottom':
right.hide();
right.remove(details, false);
bot.add(details);
bot.show();
bot.ownerCt.doLayout();
break;
case 'Right':
bot.hide();
bot.remove(details, false);
right.add(details);
right.show();
right.ownerCt.doLayout();
break;
case 'Hide':
bot.hide();
right.hide();
right.remove(details, false);
bot.remove(details, false);
right.ownerCt.doLayout();
break;
}
}
After defining the Master panel, the layout creates a working instance of this panel and applies any supplied configuration for it.
var masterPanelConf={};
if (conf && conf.masterPanelConf) {
masterPanelConf=conf.masterPanelConf;
}
var masterPanel=new MasterPanel
The Details and Navigation panels do not have any out-of-the-box extra functionality and hence their creation is less involved. As with the Master panel, you can add components to these panels when building the layout by overriding their default configurations.
MasterDetailsCnt is a container panel for the Master and Details panels hosts. Notice how one host uses the south region of the MasterDetailsCnt layout, whereas the other uses the east region:
When all of the panels needed by the ThreePanelApp component are created, what's left is defining the custom layout for the component. Notice the inclusion of a north region where you can put a logo and other components:
var navPanel={ ... }
var detailsPanel={ ... }
var masterPanel={ ... }
var container=new Ext.ux.ThreePanelApp({
detailsPanelConf: detailsPanel,
navPanelConf: navPanel,
masterPanelConf: masterPanel
});
There's more...
Use this layout when you need a simple three-panel GUI without having to worry about all its plumbing. More importantly, the extension model used in ThreePanelApp can be used as a reference for creating even more complex reusable layouts and custom components.
See also...
The recipe, Creating a modern application layout with collapsible region (seen earlier in this chapter), explains how to create one of the most popular UI layouts