1 /* Copyright 2008 the Melange authors. |
|
2 * |
|
3 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 * you may not use this file except in compliance with the License. |
|
5 * You may obtain a copy of the License at |
|
6 * |
|
7 * http://www.apache.org/licenses/LICENSE-2.0 |
|
8 * |
|
9 * Unless required by applicable law or agreed to in writing, software |
|
10 * distributed under the License is distributed on an "AS IS" BASIS, |
|
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 * See the License for the specific language governing permissions and |
|
13 * limitations under the License. |
|
14 */ |
|
15 |
|
16 function BlogPreview(container) { |
|
17 this.container_ = container; |
|
18 } |
|
19 |
|
20 BlogPreview.prototype.show = function (url, entries_num, title, title_link) { |
|
21 var feed = new google.feeds.Feed(url); |
|
22 var preview = this; |
|
23 feed.setNumEntries(entries_num); |
|
24 feed.load(function (result) { |
|
25 preview.render_(result, title, title_link); |
|
26 }); |
|
27 }; |
|
28 |
|
29 BlogPreview.prototype.render_ = function (result, title, title_link) { |
|
30 if (!result.feed || !result.feed.entries) { |
|
31 return; |
|
32 } |
|
33 while (this.container_.firstChild) { |
|
34 this.container_.removeChild(this.container_.firstChild); |
|
35 } |
|
36 |
|
37 var blog = this.createDiv_(this.container_, "blog"); |
|
38 var header = this.createElement_("h2", blog, ""); |
|
39 if (!title) { |
|
40 title = result.feed.title; |
|
41 } |
|
42 if (!title_link) { |
|
43 title_link = result.feed.link; |
|
44 } |
|
45 this.createLink_(header, title_link, title); |
|
46 |
|
47 for (var i = 0; i < result.feed.entries.length; i++) { |
|
48 var entry = result.feed.entries[i]; |
|
49 var div = this.createDiv_(blog, "entry"); |
|
50 var linkDiv = this.createDiv_(div, "title"); |
|
51 this.createLink_(linkDiv, entry.link, entry.title); |
|
52 if (entry.author) { |
|
53 this.createDiv_(div, "author", "Posted by " + entry.author); |
|
54 } |
|
55 this.createDiv_(div, "snippet", entry.contentSnippet); |
|
56 } |
|
57 }; |
|
58 |
|
59 BlogPreview.prototype.createDiv_ = function (parent, className, opt_text) { |
|
60 return this.createElement_("div", parent, className, opt_text); |
|
61 }; |
|
62 |
|
63 BlogPreview.prototype.createLink_ = function (parent, href, text) { |
|
64 var link = this.createElement_("a", parent, "", text); |
|
65 link.href = href; |
|
66 return link; |
|
67 }; |
|
68 |
|
69 BlogPreview.prototype.createElement_ = function (tagName, parent, className, |
|
70 opt_text) { |
|
71 var div = document.createElement(tagName); |
|
72 div.className = className; |
|
73 parent.appendChild(div); |
|
74 if (opt_text) { |
|
75 div.appendChild(document.createTextNode(opt_text)); |
|
76 } |
|
77 return div; |
|
78 }; |
|
79 |
|