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