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