Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / examples / feed-viewer / MainPanel.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 MainPanel = function(){
8     this.preview = new Ext.Panel({
9         id: 'preview',
10         region: 'south',
11         cls:'preview',
12         autoScroll: true,
13         listeners: FeedViewer.LinkInterceptor,
14
15         tbar: [{
16             id:'tab',
17             text: 'View in New Tab',
18             iconCls: 'new-tab',
19             disabled:true,
20             handler : this.openTab,
21             scope: this
22         },
23         '-',
24         {
25             id:'win',
26             text: 'Go to Post',
27             iconCls: 'new-win',
28             disabled:true,
29             scope: this,
30             handler : function(){
31                 window.open(this.gsm.getSelected().data.link);
32             }
33         }],
34
35         clear: function(){
36             this.body.update('');
37             var items = this.topToolbar.items;
38             items.get('tab').disable();
39             items.get('win').disable();
40         }
41     });
42
43     this.grid = new FeedGrid(this, {
44         tbar:[{
45             text:'Open All',
46             tooltip: {title:'Open All',text:'Opens all item in tabs'},
47             iconCls: 'tabs',
48             handler: this.openAll,
49             scope:this
50         },
51         '-',
52         {
53             split:true,
54             text:'Reading Pane',
55             tooltip: {title:'Reading Pane',text:'Show, move or hide the Reading Pane'},
56             iconCls: 'preview-bottom',
57             handler: this.movePreview.createDelegate(this, []),
58             menu:{
59                 id:'reading-menu',
60                 cls:'reading-menu',
61                 width:100,
62                 items: [{
63                     text:'Bottom',
64                     checked:true,
65                     group:'rp-group',
66                     checkHandler:this.movePreview,
67                     scope:this,
68                     iconCls:'preview-bottom'
69                 },{
70                     text:'Right',
71                     checked:false,
72                     group:'rp-group',
73                     checkHandler:this.movePreview,
74                     scope:this,
75                     iconCls:'preview-right'
76                 },{
77                     text:'Hide',
78                     checked:false,
79                     group:'rp-group',
80                     checkHandler:this.movePreview,
81                     scope:this,
82                     iconCls:'preview-hide'
83                 }]
84             }
85         },
86         '-',
87         {
88             pressed: true,
89             enableToggle:true,
90             text:'Summary',
91             tooltip: {title:'Post Summary',text:'View a short summary of each item in the list'},
92             iconCls: 'summary',
93             scope:this,
94             toggleHandler: function(btn, pressed){
95                 this.grid.togglePreview(pressed);
96             }
97         }]
98     });
99
100     MainPanel.superclass.constructor.call(this, {
101         id:'main-tabs',
102         activeTab:0,
103         region:'center',
104         margins:'0 5 5 0',
105         resizeTabs:true,
106         tabWidth:150,
107         minTabWidth: 120,
108         enableTabScroll: true,
109         plugins: new Ext.ux.TabCloseMenu(),
110         items: {
111             id:'main-view',
112             layout:'border',
113             title:'Loading...',
114             hideMode:'offsets',
115             items:[
116                 this.grid, {
117                 id:'bottom-preview',
118                 layout:'fit',
119                 items:this.preview,
120                 height: 250,
121                 split: true,
122                 border:false,
123                 region:'south'
124             }, {
125                 id:'right-preview',
126                 layout:'fit',
127                 border:false,
128                 region:'east',
129                 width:350,
130                 split: true,
131                 hidden:true
132             }]
133         }
134     });
135
136     this.gsm = this.grid.getSelectionModel();
137
138     this.gsm.on('rowselect', function(sm, index, record){
139         FeedViewer.getTemplate().overwrite(this.preview.body, record.data);
140         var items = this.preview.topToolbar.items;
141         items.get('tab').enable();
142         items.get('win').enable();
143     }, this, {buffer:250});
144
145     this.grid.store.on('beforeload', this.preview.clear, this.preview);
146     this.grid.store.on('load', this.gsm.selectFirstRow, this.gsm);
147
148     this.grid.on('rowdblclick', this.openTab, this);
149 };
150
151 Ext.extend(MainPanel, Ext.TabPanel, {
152
153     loadFeed : function(feed){
154         this.grid.loadFeed(feed.url);
155         Ext.getCmp('main-view').setTitle(feed.text);
156     },
157
158     movePreview : function(m, pressed){
159         if(!m){ // cycle if not a menu item click
160             var items = Ext.menu.MenuMgr.get('reading-menu').items.items;
161             var b = items[0], r = items[1], h = items[2];
162             if(b.checked){
163                 r.setChecked(true);
164             }else if(r.checked){
165                 h.setChecked(true);
166             }else if(h.checked){
167                 b.setChecked(true);
168             }
169             return;
170         }
171         if(pressed){
172             var preview = this.preview;
173             var right = Ext.getCmp('right-preview');
174             var bot = Ext.getCmp('bottom-preview');
175             var btn = this.grid.getTopToolbar().items.get(2);
176             switch(m.text){
177                 case 'Bottom':
178                     right.hide();
179                     bot.add(preview);
180                     bot.show();
181                     bot.ownerCt.doLayout();
182                     btn.setIconClass('preview-bottom');
183                     break;
184                 case 'Right':
185                     bot.hide();
186                     right.add(preview);
187                     right.show();
188                     right.ownerCt.doLayout();
189                     btn.setIconClass('preview-right');
190                     break;
191                 case 'Hide':
192                     preview.ownerCt.hide();
193                     preview.ownerCt.ownerCt.doLayout();
194                     btn.setIconClass('preview-hide');
195                     break;
196             }
197         }
198     },
199
200     openTab : function(record){
201         record = (record && record.data) ? record : this.gsm.getSelected();
202         var d = record.data;
203         var id = !d.link ? Ext.id() : d.link.replace(/[^A-Z0-9-_]/gi, '');
204         var tab;
205         if(!(tab = this.getItem(id))){
206             tab = new Ext.Panel({
207                 id: id,
208                 cls:'preview single-preview',
209                 title: d.title,
210                 tabTip: d.title,
211                 html: FeedViewer.getTemplate().apply(d),
212                 closable:true,
213                 listeners: FeedViewer.LinkInterceptor,
214                 autoScroll:true,
215                 border:true,
216
217                 tbar: [{
218                     text: 'Go to Post',
219                     iconCls: 'new-win',
220                     handler : function(){
221                         window.open(d.link);
222                     }
223                 }]
224             });
225             this.add(tab);
226         }
227         this.setActiveTab(tab);
228     },
229
230     openAll : function(){
231         this.beginUpdate();
232         this.grid.store.data.each(this.openTab, this);
233         this.endUpdate();
234     }
235 });
236
237 Ext.reg('appmainpanel', MainPanel);