FeedWindow.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @class FeedViewer.FeedWindow
  3. * @extends Ext.window.Window
  4. *
  5. * Shows a dialog for creating and validating a new feed.
  6. *
  7. * @constructor
  8. * Create a new Feed Window
  9. * @param {Object} config The config object
  10. */
  11. Ext.define('FeedViewer.FeedWindow', {
  12. extend: 'Ext.window.Window',
  13. alias: 'widget.feedwindow',
  14. plain: true,
  15. resizable: false,
  16. modal: true,
  17. closeAction: 'hide',
  18. defaultFocus: '#feed',
  19. defaultFeeds: [
  20. ['http://rss.cnn.com/rss/edition.rss', 'CNN Top Stories'],
  21. ['http://sports.espn.go.com/espn/rss/news', 'ESPN Top News'],
  22. ['http://news.google.com/news?ned=us&topic=t&output=rss', 'Sci/Tech - Google News'],
  23. ['http://rss.news.yahoo.com/rss/software', 'Yahoo Software News']
  24. ],
  25. initComponent: function(){
  26. var me = this;
  27. me.addEvents(
  28. /**
  29. * @event feedvalid
  30. * @param {FeedViewer.FeedWindow} this
  31. * @param {String} title
  32. * @param {String} url
  33. * @param {String} description
  34. */
  35. 'feedvalid'
  36. );
  37. me.form = Ext.create('widget.form', {
  38. bodyPadding: '12 10 10',
  39. border: false,
  40. unstyled: true,
  41. items: [{
  42. anchor: '100%',
  43. itemId: 'feed',
  44. fieldLabel: 'Enter the URL of the feed to add',
  45. labelAlign: 'top',
  46. msgTarget: 'under',
  47. xtype: 'combo',
  48. store: this.defaultFeeds,
  49. getInnerTpl: function(){
  50. return '<div class="feed-picker-url">{field1}</div><div class="feed-picker-title">{field2}</div>';
  51. }
  52. }]
  53. });
  54. Ext.apply(me, {
  55. width: 500,
  56. title: 'Add Feed',
  57. iconCls: 'feed',
  58. layout: 'fit',
  59. items: me.form,
  60. buttons: [{
  61. xtype: 'button',
  62. text: 'Add Feed',
  63. scope: me,
  64. handler: me.onAddClick
  65. }, {
  66. xtype: 'button',
  67. text: 'Cancel',
  68. scope: me,
  69. handler: me.hide
  70. }]
  71. });
  72. me.callParent(arguments);
  73. },
  74. /**
  75. * React to the add button being clicked.
  76. * @private
  77. */
  78. onAddClick: function(addBtn) {
  79. addBtn.disable();
  80. var url = this.form.getComponent('feed').getValue();
  81. this.form.setLoading({
  82. msg: 'Validating feed...'
  83. });
  84. Ext.Ajax.request({
  85. url: 'feed-proxy.php',
  86. params: {
  87. feed: url
  88. },
  89. success: this.validateFeed,
  90. failure: this.markInvalid,
  91. scope: this
  92. });
  93. },
  94. /**
  95. * React to the feed validation passing
  96. * @private
  97. * @param {Object} response The response object
  98. */
  99. validateFeed: function(response) {
  100. this.form.setLoading(false);
  101. this.down('button[text=Add Feed]').enable();
  102. var dq = Ext.DomQuery,
  103. url = this.form.getComponent('feed').getValue(),
  104. xml,
  105. channel,
  106. title;
  107. try {
  108. xml = response.responseXML;
  109. channel = xml.getElementsByTagName('channel')[0];
  110. if (channel) {
  111. title = dq.selectValue('title', channel, url);
  112. this.fireEvent('feedvalid', this, title, url);
  113. this.hide();
  114. return;
  115. }
  116. } catch(e) {
  117. }
  118. this.markInvalid();
  119. },
  120. /**
  121. * React to the feed validation failing
  122. * @private
  123. */
  124. markInvalid: function(){
  125. this.down('button[text=Add Feed]').enable();
  126. this.form.setLoading(false);
  127. this.form.getComponent('feed').markInvalid('The URL specified is not a valid RSS2 feed.');
  128. }
  129. });