FeedPanel.js
6.94 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* @class FeedViewer.FeedPanel
* @extends Ext.panel.Panel
*
* Shows a list of available feeds. Also has the ability to add/remove and load feeds.
*
* @constructor
* Create a new Feed Panel
* @param {Object} config The config object
*/
Ext.define('FeedViewer.FeedPanel', {
extend: 'Ext.panel.Panel',
alias: 'widget.feedpanel',
animCollapse: true,
layout: 'fit',
title: 'Feeds',
initComponent: function(){
Ext.apply(this, {
items: this.createView(),
dockedItems: this.createToolbar()
});
this.createMenu();
this.addEvents(
/**
* @event feedremove Fired when a feed is removed
* @param {FeedPanel} this
* @param {String} title The title of the feed
* @param {String} url The url of the feed
*/
'feedremove',
/**
* @event feedselect Fired when a feed is selected
* @param {FeedPanel} this
* @param {String} title The title of the feed
* @param {String} url The url of the feed
*/
'feedselect'
);
this.callParent(arguments);
},
/**
* Create the DataView to be used for the feed list.
* @private
* @return {Ext.view.View}
*/
createView: function(){
this.view = Ext.create('widget.dataview', {
store: Ext.create('Ext.data.Store', {
model: 'Feed',
data: this.feeds
}),
selModel: {
mode: 'SINGLE',
listeners: {
scope: this,
selectionchange: this.onSelectionChange
}
},
listeners: {
scope: this,
contextmenu: this.onContextMenu,
viewready: this.onViewReady
},
trackOver: true,
cls: 'feed-list',
itemSelector: '.feed-list-item',
overItemCls: 'feed-list-item-hover',
tpl: '<tpl for="."><div class="feed-list-item">{title}</div></tpl>'
});
return this.view;
},
onViewReady: function(){
this.view.getSelectionModel().select(this.view.store.first());
},
/**
* Creates the toolbar to be used for controlling feeds.
* @private
* @return {Ext.toolbar.Toolbar}
*/
createToolbar: function(){
this.createActions();
this.toolbar = Ext.create('widget.toolbar', {
items: [this.addAction, this.removeAction]
});
return this.toolbar;
},
/**
* Create actions to share between toolbar and menu
* @private
*/
createActions: function(){
this.addAction = Ext.create('Ext.Action', {
scope: this,
handler: this.onAddFeedClick,
text: 'Add feed',
iconCls: 'feed-add'
});
this.removeAction = Ext.create('Ext.Action', {
itemId: 'remove',
scope: this,
handler: this.onRemoveFeedClick,
text: 'Remove feed',
iconCls: 'feed-remove'
});
},
/**
* Create the context menu
* @private
*/
createMenu: function(){
this.menu = Ext.create('widget.menu', {
items: [{
scope: this,
handler: this.onLoadClick,
text: 'Load feed',
iconCls: 'feed-load'
}, this.removeAction, '-', this.addAction],
listeners: {
hide: function(c){
c.activeFeed = null;
}
}
});
},
/**
* Used when view selection changes so we can disable toolbar buttons.
* @private
*/
onSelectionChange: function(){
var selected = this.getSelectedItem();
this.toolbar.getComponent('remove').setDisabled(!selected);
this.loadFeed(selected);
},
/**
* React to the load feed menu click.
* @private
*/
onLoadClick: function(){
this.loadFeed(this.menu.activeFeed);
},
/**
* Loads a feed.
* @private
* @param {Ext.data.Model} rec The feed
*/
loadFeed: function(rec){
if (rec) {
this.fireEvent('feedselect', this, rec.get('title'), rec.get('url'));
}
},
/**
* Gets the currently selected record in the view.
* @private
* @return {Ext.data.Model} Returns the selected model. false if nothing is selected.
*/
getSelectedItem: function(){
return this.view.getSelectionModel().getSelection()[0] || false;
},
/**
* Listens for the context menu event on the view
* @private
*/
onContextMenu: function(view, index, el, event){
var menu = this.menu;
event.stopEvent();
menu.activeFeed = view.store.getAt(index);
menu.showAt(event.getXY());
},
/**
* React to a feed being removed
* @private
*/
onRemoveFeedClick: function(){
var active = this.menu.activeFeed || this.getSelectedItem();
this.animateNode(this.view.getNode(active), 1, 0, {
scope: this,
afteranimate: function(){
this.view.store.remove(active);
}
});
this.fireEvent('feedremove', this, active.get('title'), active.get('url'));
},
/**
* React to a feed attempting to be added
* @private
*/
onAddFeedClick: function(){
var win = this.addFeedWindow || (this.addFeedWindow = Ext.create('widget.feedwindow', {
listeners: {
scope: this,
feedvalid: this.onFeedValid
}
}));
win.form.getForm().reset();
win.show();
},
/**
* React to a validation on a feed passing
* @private
* @param {FeedViewer.FeedWindow} win
* @param {String} title The title of the feed
* @param {String} url The url of the feed
*/
onFeedValid: function(win, title, url){
var view = this.view,
store = view.store,
rec;
rec = store.add({
url: url,
title: title
})[0];
this.animateNode(view.getNode(rec), 0, 1);
},
/**
* Animate a node in the view when it is added/removed
* @private
* @param {Mixed} el The element to animate
* @param {Number} start The start opacity
* @param {Number} end The end opacity
* @param {Object} listeners (optional) Any listeners
*/
animateNode: function(el, start, end, listeners){
Ext.create('Ext.fx.Anim', {
target: Ext.get(el),
duration: 500,
from: {
opacity: start
},
to: {
opacity: end
},
listeners: listeners
});
},
// Inherit docs
onDestroy: function(){
this.callParent(arguments);
this.menu.destroy();
}
});