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