Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / examples / key-feed-viewer / FeedWindow.js
1 /*!
2  * Ext JS Library 3.0.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 FeedWindow = function() {\r
8     this.feedUrl = new Ext.form.ComboBox({\r
9         id: 'feed-url',\r
10         fieldLabel: 'Enter the URL of the feed to add',\r
11         emptyText: 'http://example.com/blog/feed',\r
12         width: 450,\r
13         validationEvent: false,\r
14         validateOnBlur: false,\r
15         msgTarget: 'under',\r
16         triggerAction: 'all',\r
17         displayField: 'url',\r
18         mode: 'local',\r
19 \r
20         listeners:{\r
21             valid: this.syncShadow,\r
22             invalid: this.syncShadow,\r
23             scope: this\r
24         },\r
25         tpl: new Ext.XTemplate(\r
26                 '<tpl for="."><div class="x-combo-list-item">',\r
27                 '<em>{url}</em><strong>{text}</strong>',\r
28                 '<div class="x-clear"></div>',\r
29                 '</div></tpl>'),\r
30         store: new Ext.data.ArrayStore({\r
31             fields: ['url', 'text'],\r
32             data : this.defaultFeeds\r
33         })\r
34     });\r
35 \r
36     this.form = new Ext.FormPanel({\r
37         labelAlign:'top',\r
38         items:this.feedUrl,\r
39         border: false,\r
40         bodyStyle:'background:transparent;padding:10px;'\r
41     });\r
42 \r
43     FeedWindow.superclass.constructor.call(this, {\r
44         title: 'Add Feed',\r
45         iconCls: 'feed-icon',\r
46         id: 'add-feed-win',\r
47         autoHeight: true,\r
48         width: 500,\r
49         resizable: false,\r
50         plain:true,\r
51         modal: true,\r
52         y: 100,\r
53         autoScroll: true,\r
54         closeAction: 'hide',\r
55 \r
56         buttons:[{\r
57             text: 'Add Feed!',\r
58             handler: this.onAdd,\r
59             scope: this\r
60         },{\r
61             text: 'Cancel',\r
62             handler: this.hide.createDelegate(this, [])\r
63         }],\r
64 \r
65         items: this.form\r
66     });\r
67 \r
68     this.addEvents({add:true});\r
69 }\r
70 \r
71 Ext.extend(FeedWindow, Ext.Window, {\r
72     defaultFeeds : [\r
73         ['http://www.divergingpath.com/rss.cfm?mode=full', 'Aaron Conran\'s Blog'],\r
74         ['http://feeds.yuiblog.com/YahooUserInterfaceBlog',  'Yahoo! UI Blog'],\r
75         ['http://feeds.feedburner.com/jquery/', 'jQuery Blog'],\r
76         ['http://sports.yahoo.com/nba/rss.xml', 'NBA News'],\r
77         ['http://feeds.dzone.com/dzone/frontpage', 'DZone.com']\r
78     ],\r
79 \r
80     show : function(){\r
81         if(this.rendered){\r
82             this.feedUrl.setValue('');\r
83         }\r
84         FeedWindow.superclass.show.apply(this, arguments);\r
85     },\r
86 \r
87     onAdd: function() {\r
88         this.el.mask('Validating Feed...', 'x-mask-loading');\r
89         var url = this.feedUrl.getValue();\r
90         Ext.Ajax.request({\r
91             url: 'feed-proxy.php',\r
92             params: {feed: url},\r
93             success: this.validateFeed,\r
94             failure: this.markInvalid,\r
95             scope: this,\r
96             feedUrl: url\r
97         });\r
98     },\r
99 \r
100     markInvalid : function(){\r
101         this.feedUrl.markInvalid('The URL specified is not a valid RSS2 feed.');\r
102         this.el.unmask();\r
103     },\r
104 \r
105     validateFeed : function(response, options){\r
106         var dq = Ext.DomQuery;\r
107         var url = options.feedUrl;\r
108 \r
109         try{\r
110             var xml = response.responseXML;\r
111             var channel = xml.getElementsByTagName('channel')[0];\r
112             if(channel){\r
113                 var text = dq.selectValue('title', channel, url);\r
114                 var description = dq.selectValue('description', channel, 'No description available.');\r
115                 this.el.unmask();\r
116                 this.hide();\r
117 \r
118                 return this.fireEvent('validfeed', {\r
119                     url: url,\r
120                     text: text,\r
121                     description: description\r
122                 });\r
123             }\r
124         }catch(e){\r
125         }\r
126         this.markInvalid();\r
127     }\r
128 });