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