|
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) return; |
|
31 while (this.container_.firstChild) { |
|
32 this.container_.removeChild(this.container_.firstChild); |
|
33 } |
|
34 |
|
35 var blog = this.createDiv_(this.container_, "blog"); |
|
36 var header = this.createElement_("h2", blog, ""); |
|
37 if (!title) { |
|
38 title = result.feed.title; |
|
39 } |
|
40 if (!title_link) { |
|
41 title_link = result.feed.link; |
|
42 } |
|
43 this.createLink_(header, title_link, title); |
|
44 |
|
45 for (var i = 0; i < result.feed.entries.length; i++) { |
|
46 var entry = result.feed.entries[i]; |
|
47 var div = this.createDiv_(blog, "entry"); |
|
48 var linkDiv = this.createDiv_(div, "title"); |
|
49 this.createLink_(linkDiv, entry.link, entry.title); |
|
50 if (entry.author) { |
|
51 this.createDiv_(div, "author", "Posted by " + entry.author); |
|
52 } |
|
53 this.createDiv_(div, "snippet", entry.contentSnippet); |
|
54 } |
|
55 } |
|
56 |
|
57 BlogPreview.prototype.createDiv_ = function(parent, className, opt_text) { |
|
58 return this.createElement_("div", parent, className, opt_text); |
|
59 } |
|
60 |
|
61 BlogPreview.prototype.createLink_ = function(parent, href, text) { |
|
62 var link = this.createElement_("a", parent, "", text); |
|
63 link.href = href; |
|
64 return link; |
|
65 } |
|
66 |
|
67 BlogPreview.prototype.createElement_ = function(tagName, parent, className, |
|
68 opt_text) { |
|
69 var div = document.createElement(tagName); |
|
70 div.className = className; |
|
71 parent.appendChild(div); |
|
72 if (opt_text) { |
|
73 div.appendChild(document.createTextNode(opt_text)); |
|
74 } |
|
75 return div; |
|
76 } |
|
77 |