]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | Ext.define('FeedViewer.FeedGrid', {\r |
2 | extend: 'Ext.grid.Panel',\r | |
3 | \r | |
4 | alias: 'widget.feedgrid',\r | |
5 | \r | |
6 | /**\r | |
7 | * @event rowdblclick\r | |
8 | * Fires when a row is double clicked\r | |
9 | * @param {FeedViewer.FeedGrid} this\r | |
10 | * @param {Ext.data.Model} model\r | |
11 | */\r | |
12 | \r | |
13 | /**\r | |
14 | * @event select\r | |
15 | * Fires when a grid row is selected\r | |
16 | * @param {FeedViewer.FeedGrid} this\r | |
17 | * @param {Ext.data.Model} model\r | |
18 | */\r | |
19 | \r | |
20 | initComponent: function(){\r | |
21 | Ext.apply(this, {\r | |
22 | cls: 'feed-grid',\r | |
23 | store: Ext.create('Ext.data.Store', {\r | |
24 | model: 'FeedItem',\r | |
25 | sortInfo: {\r | |
26 | property: 'pubDate',\r | |
27 | direction: 'DESC'\r | |
28 | },\r | |
29 | proxy: {\r | |
30 | type: 'ajax',\r | |
31 | url: 'feed-proxy.php',\r | |
32 | reader: {\r | |
33 | type: 'xml',\r | |
34 | record: 'item'\r | |
35 | },\r | |
36 | listeners: {\r | |
37 | exception: this.onProxyException,\r | |
38 | scope: this\r | |
39 | }\r | |
40 | },\r | |
41 | listeners: {\r | |
42 | load: this.onLoad,\r | |
43 | scope: this\r | |
44 | }\r | |
45 | }),\r | |
46 | \r | |
47 | viewConfig: {\r | |
48 | itemId: 'view',\r | |
49 | plugins: [{\r | |
50 | pluginId: 'preview',\r | |
51 | ptype: 'preview',\r | |
52 | bodyField: 'description',\r | |
53 | expanded: true\r | |
54 | }],\r | |
55 | listeners: {\r | |
56 | scope: this,\r | |
57 | itemdblclick: this.onRowDblClick\r | |
58 | }\r | |
59 | },\r | |
60 | columns: [{\r | |
61 | text: 'Title',\r | |
62 | dataIndex: 'title',\r | |
63 | flex: 1,\r | |
64 | renderer: this.formatTitle\r | |
65 | }, {\r | |
66 | text: 'Author',\r | |
67 | dataIndex: 'author',\r | |
68 | hidden: true,\r | |
69 | width: 200\r | |
70 | \r | |
71 | }, {\r | |
72 | text: 'Date',\r | |
73 | dataIndex: 'pubDate',\r | |
74 | renderer: this.formatDate,\r | |
75 | width: 200\r | |
76 | }]\r | |
77 | });\r | |
78 | this.callParent(arguments);\r | |
79 | this.on('selectionchange', this.onSelect, this);\r | |
80 | },\r | |
81 | \r | |
82 | /**\r | |
83 | * Reacts to a double click\r | |
84 | * @private\r | |
85 | * @param {Object} view The view\r | |
86 | * @param {Object} index The row index\r | |
87 | */\r | |
88 | onRowDblClick: function(view, record, item, index, e) {\r | |
89 | this.fireEvent('rowdblclick', this, this.store.getAt(index));\r | |
90 | },\r | |
91 | \r | |
92 | \r | |
93 | /**\r | |
94 | * React to a grid item being selected\r | |
95 | * @private\r | |
96 | * @param {Ext.model.Selection} model The selection model\r | |
97 | * @param {Array} selections An array of selections\r | |
98 | */\r | |
99 | onSelect: function(model, selections){\r | |
100 | var selected = selections[0];\r | |
101 | if (selected) {\r | |
102 | this.fireEvent('select', this, selected);\r | |
103 | }\r | |
104 | },\r | |
105 | \r | |
106 | /**\r | |
107 | * Listens for the store loading\r | |
108 | * @private\r | |
109 | */\r | |
110 | onLoad: function(store, records, success) {\r | |
111 | if (this.getStore().getCount()) {\r | |
112 | this.getSelectionModel().select(0);\r | |
113 | }\r | |
114 | },\r | |
115 | \r | |
116 | /**\r | |
117 | * Listen for proxy eerrors.\r | |
118 | */\r | |
119 | onProxyException: function(proxy, response, operation) {\r | |
120 | Ext.Msg.alert("Error with data from server", operation.error);\r | |
121 | this.view.el.update('');\r | |
122 | \r | |
123 | // Update the detail view with a dummy empty record\r | |
124 | this.fireEvent('select', this, {data:{}});\r | |
125 | },\r | |
126 | \r | |
127 | /**\r | |
128 | * Instructs the grid to load a new feed\r | |
129 | * @param {String} url The url to load\r | |
130 | */\r | |
131 | loadFeed: function(url){\r | |
132 | var store = this.store;\r | |
133 | \r | |
134 | store.getProxy().setExtraParam('feed', url);\r | |
135 | store.load();\r | |
136 | },\r | |
137 | \r | |
138 | /**\r | |
139 | * Title renderer\r | |
140 | * @private\r | |
141 | */\r | |
142 | formatTitle: function(value, p, record){\r | |
143 | return Ext.String.format('<div class="topic"><b>{0}</b><span class="author">{1}</span></div>', value, record.get('author') || "Unknown");\r | |
144 | },\r | |
145 | \r | |
146 | /**\r | |
147 | * Date renderer\r | |
148 | * @private\r | |
149 | */\r | |
150 | formatDate: function(date){\r | |
151 | if (!date) {\r | |
152 | return '';\r | |
153 | }\r | |
154 | \r | |
155 | var now = new Date(), d = Ext.Date.clearTime(now, true), notime = Ext.Date.clearTime(date, true).getTime();\r | |
156 | \r | |
157 | if (notime === d.getTime()) {\r | |
158 | return 'Today ' + Ext.Date.format(date, 'g:i a');\r | |
159 | }\r | |
160 | \r | |
161 | d = Ext.Date.add(d, 'd', -6);\r | |
162 | if (d.getTime() <= notime) {\r | |
163 | return Ext.Date.format(date, 'D g:i a');\r | |
164 | }\r | |
165 | return Ext.Date.format(date, 'Y/m/d g:i a');\r | |
166 | }\r | |
167 | });\r |