Upgrade to ExtJS 4.0.7 - Released 10/19/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             this.getFeedShow().setTitle(feed.get('name'));
77             grid.enable();
78             store.load({
79                 params: {
80                     feed: feed.get('url')
81                 }
82             });            
83         }
84     },
85     
86     /**
87      * Shows the add feed dialog window
88      */
89     addFeed: function() {
90         this.getFeedWindow().show();
91     },
92     
93     /**
94      * Removes the given feed from the Feeds store
95      * @param {FV.model.Feed} feed The feed to remove
96      */
97     removeFeed: function() {
98         this.getFeedsStore().remove(this.getFeedData().getSelectionModel().getSelection()[0]);
99     },
100     
101     /**
102      * @private
103      * Creates a new feed in the store based on a given url. First validates that the feed is well formed
104      * using FV.lib.FeedValidator.
105      * @param {String} name The name of the Feed to create
106      * @param {String} url The url of the Feed to create
107      */
108     createFeed: function() {
109         var win   = this.getFeedWindow(),
110             form  = this.getFeedForm(),
111             combo = this.getFeedCombo(),
112             store = this.getFeedsStore(),
113             feed  = this.getFeedModel().create({
114                 name: combo.getDisplayValue(),
115                 url: combo.getValue()
116             });
117
118         form.setLoading({
119             msg: 'Validating feed...'
120         });
121         
122         FV.lib.FeedValidator.validate(feed, {
123             success: function() {
124                 store.add(feed);
125                 form.setLoading(false);
126                 win.hide();
127             },
128             failure: function() {
129                 form.setLoading(false);
130                 form.down('[name=feed]').markInvalid('The URL specified is not a valid RSS2 feed.');
131             }
132         });
133     }
134 });