Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / key-feed-viewer / viewer / FeedPost.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class FeedViewer.FeedPost
17  * @extends Ext.panel.Panel
18  *
19  * Shows the detail of a feed post
20  *
21  * @constructor
22  * Create a new Feed Post
23  * @param {Object} config The config object
24  */
25 Ext.define('FeedViewer.FeedPost', {
26
27     extend: 'Ext.panel.Panel',
28     alias: 'widget.feedpost',
29     cls: 'preview',
30     autoScroll: true,
31     border: true,
32     
33     initComponent: function(){
34         Ext.apply(this, {
35             dockedItems: [this.createToolbar()],
36             tpl: Ext.create('Ext.XTemplate',
37                 '<div class="post-data">',
38                     '<span class="post-date">{pubDate:this.formatDate}</span>',
39                     '<h3 class="post-title">{title}</h3>',
40                     '<h4 class="post-author">by {author:this.defaultValue}</h4>',
41                 '</div>',
42                 '<div class="post-body">{content:this.getBody}</div>',
43                 {
44                     getBody: function(value, all){
45                         return Ext.util.Format.stripScripts(value);
46                     },
47
48                     defaultValue: function(v){
49                         return v ? v : 'Unknown';
50                     },
51
52                     formatDate: function(value){
53                         if (!value) {
54                             return '';
55                         }
56                         return Ext.Date.format(value, 'M j, Y, g:i a');
57                     }
58                 }
59              )
60         });
61         this.callParent(arguments);
62     },
63
64     /**
65      * Set the active post
66      * @param {Ext.data.Model} rec The record
67      */
68     setActive: function(rec) {
69         this.active = rec;
70         this.update(rec.data);
71     },
72
73     /**
74      * Create the top toolbar
75      * @private
76      * @return {Ext.toolbar.Toolbar} toolbar
77      */
78     createToolbar: function(){
79         var items = [],
80             config = {};
81         if (!this.inTab) {
82             items.push({
83                 scope: this,
84                 handler: this.openTab,
85                 text: 'View in new tab',
86                 iconCls: 'tab-new'
87             }, '-');
88         }
89         else {
90             config.cls = 'x-docked-noborder-top';
91         }
92         items.push({
93             scope: this,
94             handler: this.goToPost,
95             text: 'Go to post',
96             iconCls: 'post-go'
97         });
98         config.items = items;
99         return Ext.create('widget.toolbar', config);
100     },
101
102     /**
103      * Navigate to the active post in a new window
104      * @private
105      */
106     goToPost: function(){
107         window.open(this.active.get('link'));
108     },
109
110     /**
111      * Open the post in a new tab
112      * @private
113      */
114     openTab: function(){
115         this.fireEvent('opentab', this, this.active);
116     }
117
118 });
119