OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2015 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 <link rel='import' href='../polymer/polymer.html'> |
| 7 <link rel='import' href='../google-apis/google-legacy-loader.html'> |
| 8 |
| 9 <!-- |
| 10 Exposes [Google Feeds API](https://developers.google.com/feed/) |
| 11 |
| 12 <b>Example</b>: |
| 13 |
| 14 <template is='dom-bind'> |
| 15 <google-feeds feed='http://www.engadget.com/rss-full.xml' |
| 16 results='{{result}}'></google-feeds> |
| 17 <p>Feed title: <span>{{result.title}}</span></p> |
| 18 </template> |
| 19 |
| 20 @demo |
| 21 --> |
| 22 <dom-module id='google-feeds'> |
| 23 <template> |
| 24 <div id='content'></div> |
| 25 </template> |
| 26 </dom-module> |
| 27 <script> |
| 28 (function() { |
| 29 Polymer({ |
| 30 |
| 31 is: 'google-feeds', |
| 32 |
| 33 /** |
| 34 * Fired when feed has been loaded |
| 35 * @param {object} feed feed object |
| 36 * @event google-feeds-response |
| 37 */ |
| 38 /** |
| 39 * Fired when feed load fails |
| 40 * @param {string} status load status |
| 41 * @event google-feeds-error |
| 42 */ |
| 43 /** |
| 44 * Fired when multiple feeds have loaded successfully |
| 45 * @param {object} feeds multiple feeds |
| 46 * @event google-multi-feeds-response |
| 47 */ |
| 48 /** |
| 49 * Fired when feed query fails |
| 50 * @param {string} status status |
| 51 * @event google-feeds-queryerror |
| 52 */ |
| 53 /** |
| 54 * Fired when feed query succeeds |
| 55 * @param {object} entries query results |
| 56 * @event google-feeds-queryresponse |
| 57 */ |
| 58 |
| 59 properties: { |
| 60 |
| 61 /** |
| 62 * url of the feed to fetch. |
| 63 */ |
| 64 feed: { |
| 65 type: String, |
| 66 observer: '_feedChanged' |
| 67 }, |
| 68 |
| 69 /** |
| 70 * An array of multiple feeds. Feed will load, and report results in `goog
le-feeds-response` event. |
| 71 */ |
| 72 feeds: { |
| 73 type: Array, |
| 74 value: function() { return []; }, |
| 75 observer: '_feedsChanged' |
| 76 }, |
| 77 |
| 78 /** |
| 79 * Result of loading a single feed url |
| 80 */ |
| 81 results: { |
| 82 type: Object, |
| 83 value: null, |
| 84 notify: true |
| 85 }, |
| 86 |
| 87 /** |
| 88 * Query for google.feeds.findFeeds(). Query result will be reported throu
gh `google-feeds-queryresponse` event |
| 89 */ |
| 90 query: { |
| 91 type: String, |
| 92 observer: '_queryChanged' |
| 93 }, |
| 94 |
| 95 /** |
| 96 * Number of feed items to fetch in fetchFeed |
| 97 */ |
| 98 count: { |
| 99 type: Number, |
| 100 value: 32 |
| 101 }, |
| 102 |
| 103 /** |
| 104 * True if feeds API is loading an item |
| 105 */ |
| 106 loading: { |
| 107 type: Boolean, |
| 108 notify: true |
| 109 } |
| 110 }, |
| 111 |
| 112 attached: function() { |
| 113 this.pendingFeeds = []; |
| 114 }, |
| 115 |
| 116 _feedChanged: function() { |
| 117 if (this.feed) { |
| 118 this.loading = true; |
| 119 // call fetchFeeds async to make sure any binding to count property |
| 120 // is resolved before fetchFeeds is called |
| 121 this._withFeedsApi(this._fetchFeeds.bind(this)); |
| 122 } else { |
| 123 this.results = null; |
| 124 } |
| 125 }, |
| 126 |
| 127 _fetchFeeds: function() { |
| 128 var feed = new google.feeds.Feed(this.feed); |
| 129 feed.includeHistoricalEntries(); // tell the API we want to have old entri
es too |
| 130 feed.setNumEntries(this.count); // we want this maximum number of entries,
if they exist |
| 131 feed.load(this._fetchFeedsDone.bind(this)); |
| 132 }, |
| 133 |
| 134 _fetchFeedsDone: function(results) { |
| 135 this.loading = false; |
| 136 if (results.error) { |
| 137 this.results = {}; |
| 138 this.fire('google-feeds-error', {status: results.status}); |
| 139 } else { |
| 140 this.results = results.feed; |
| 141 this.fire('google-feeds-response', {feed: results.feed}); |
| 142 } |
| 143 }, |
| 144 |
| 145 _feedsChanged: function() { |
| 146 this._withFeedsApi(function(item) { |
| 147 this._fetchMultipleFeeds(this.feeds, function(results) { |
| 148 // TODO: Figure out why the onLoad callback |
| 149 // isn't getting fired. Below isn't fired. |
| 150 this.fire('google-multi-feeds-response', { feeds: results }); |
| 151 }); |
| 152 }.bind(this)); |
| 153 }, |
| 154 |
| 155 _fetchMultipleFeeds: function(feeds, onLoad) { |
| 156 var feedControl = new google.feeds.FeedControl(); |
| 157 if (feeds) { |
| 158 feeds.forEach(function(item) { |
| 159 feedControl.addFeed(item); |
| 160 }); |
| 161 feedControl.setNumEntries(this.count); |
| 162 feedControl.draw(this.$.content); |
| 163 google.setOnLoadCallback(onLoad); |
| 164 } |
| 165 }, |
| 166 |
| 167 _queryChanged: function() { |
| 168 if (this.query) { |
| 169 this.loading = true; |
| 170 this._withFeedsApi(this._findFeeds.bind(this)); |
| 171 } |
| 172 }, |
| 173 |
| 174 _findFeeds: function() { |
| 175 google.feeds.findFeeds(this.query, this._findFeedsDone.bind(this)); |
| 176 }, |
| 177 |
| 178 _findFeedsDone: function(results) { |
| 179 this.loading = false; |
| 180 if (results.error) { |
| 181 this.fire('google-feeds-queryerror', {status: results.status}); |
| 182 } else { |
| 183 this.fire('google-feeds-queryresponse', {entries: results.entries}); |
| 184 } |
| 185 }, |
| 186 |
| 187 // TODO(ffu): integrate the ability to load a specific api like feeds |
| 188 // into google-jsapi. |
| 189 _feedsCallbacks: [], |
| 190 |
| 191 _feedsApiLoaded: function() { |
| 192 while (this._feedsCallbacks.length) { |
| 193 var fn = this._feedsCallbacks.shift(); |
| 194 fn(); |
| 195 } |
| 196 }, |
| 197 |
| 198 _isApiLoaded: function() { |
| 199 return !!(window.google && google.feeds && google.feeds.Feed); |
| 200 }, |
| 201 |
| 202 _withFeedsApi: function(callback) { |
| 203 if (this._isApiLoaded() && callback) { |
| 204 callback(); |
| 205 } else { |
| 206 if (!this.feedsApiLoading) { |
| 207 var loaded = this._feedsApiLoaded.bind(this); |
| 208 var loader = document.createElement('google-legacy-loader'); |
| 209 loader.addEventListener('api-load', function(e) { |
| 210 google.load('feeds', '1', {callback: loaded}); |
| 211 }); |
| 212 this.feedsApiLoading = true; |
| 213 } |
| 214 if (callback) { |
| 215 this._feedsCallbacks.push(callback); |
| 216 } |
| 217 } |
| 218 } |
| 219 |
| 220 }); |
| 221 })(); |
| 222 </script> |
OLD | NEW |