Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / app / feed-viewer / app / controller / Feeds.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 Ext.define('FV.controller.Feeds', {
16     extend: 'Ext.app.Controller',
17
18     stores: ['Feeds', 'Articles'],
19     models: ['Feed'],
20     views: ['feed.Add'],
21     
22     refs: [
23         {ref: 'feedList', selector: 'feedlist'},
24         {ref: 'feedData', selector: 'feedlist dataview'},
25         {ref: 'feedShow', selector: 'feedshow'},
26         {ref: 'feedForm', selector: 'feedwindow form'},
27         {ref: 'feedCombo', selector: 'feedwindow combobox'},
28         {ref: 'articleGrid', selector: 'articlegrid'},
29         {
30             ref: 'feedWindow', 
31             selector: 'feedwindow', 
32             autoCreate: true,
33             xtype: 'feedwindow'
34         }
35     ],
36     
37     requires: ['FV.lib.FeedValidator'],
38
39     // At this point things haven't rendered yet since init gets called on controllers before the launch function
40     // is executed on the Application
41     init: function() {
42         this.control({
43             'feedlist dataview': {
44                 selectionchange: this.loadFeed
45             },
46             'feedlist button[action=add]': {
47                 click: this.addFeed
48             },
49             'feedlist button[action=remove]': {
50                 click: this.removeFeed
51             },
52             'feedwindow button[action=create]': {
53                 click: this.createFeed
54             }
55         });
56     },
57     
58     onLaunch: function() {
59         var dataview = this.getFeedData(),
60             store = this.getFeedsStore();
61             
62         dataview.bindStore(store);
63         dataview.getSelectionModel().select(store.getAt(0));
64     },
65     
66     /**
67      * Loads the given feed into the viewer
68      * @param {FV.model.feed} feed The feed to load
69      */
70     loadFeed: function(selModel, selected) {
71         var grid = this.getArticleGrid(),
72             store = this.getArticlesStore(),
73             feed = selected[0];
74
75         if (feed) {
76             grid.enable();
77             store.load({
78                 params: {
79                     feed: feed.get('url')
80                 }
81             });            
82         }
83     },
84     
85     /**
86      * Shows the add feed dialog window
87      */
88     addFeed: function() {
89         this.getFeedWindow().show();
90     },
91     
92     /**
93      * Removes the given feed from the Feeds store
94      * @param {FV.model.Feed} feed The feed to remove
95      */
96     removeFeed: function() {
97         this.getFeedsStore().remove(this.getFeedData().getSelectionModel().getSelection()[0]);
98     },
99     
100     /**
101      * @private
102      * Creates a new feed in the store based on a given url. First validates that the feed is well formed
103      * using FV.lib.FeedValidator.
104      * @param {String} name The name of the Feed to create
105      * @param {String} url The url of the Feed to create
106      */
107     createFeed: function() {
108         var win   = this.getFeedWindow(),
109             form  = this.getFeedForm(),
110             combo = this.getFeedCombo(),
111             store = this.getFeedsStore(),
112             feed  = this.getFeedModel().create({
113                 name: combo.getDisplayValue(),
114                 url: combo.getValue()
115             });
116
117         form.setLoading({
118             msg: 'Validating feed...'
119         });
120         
121         FV.lib.FeedValidator.validate(feed, {
122             success: function() {
123                 store.add(feed);
124                 form.setLoading(false);
125                 win.hide();
126             },
127             failure: function() {
128                 form.setLoading(false);
129                 form.down('[name=feed]').markInvalid('The URL specified is not a valid RSS2 feed.');
130             }
131         });
132     }
133 });