- SS -

YYY

-
ZZZ
+ +

+
@@ -31,7 +36,19 @@ diff --git a/js/main.js b/js/main.js index 728c071..aad5109 100644 --- a/js/main.js +++ b/js/main.js @@ -6,11 +6,11 @@ function Feeds() { this.add = function(feed) { this.feeds.push(feed); - } + }; this.empty = function() { return this.feeds.length == 0; - } + }; this.entries = function() { var entries = []; @@ -23,12 +23,29 @@ function Feeds() { } } return entries; - } + }; + + this.randomizedEntries = function() { + return this.randomize(this.entries()); + } + + this.randomize = function(myArray) { + var i = myArray.length; + if ( i == 0 ) return false; + while ( --i ) { + var j = Math.floor( Math.random() * ( i + 1 ) ); + var tempi = myArray[i]; + var tempj = myArray[j]; + myArray[i] = tempj; + myArray[j] = tempi; + } + return myArray; + } this.fetch = function(feedUrl, callback) { var _this = this; var feed = new google.feeds.Feed(feedUrl); - feed.setNumEntries(1); + feed.setNumEntries(50); feed.includeHistoricalEntries(); feed.load(function(result) { if (!result.error) { @@ -51,26 +68,39 @@ function Feeds() { } } -function RSSVisualizer() { +function RSSVisualizer(feedUrls) { - this.feedUrls = ["http://cn.engadget.com/rss.xml", "http://news.ycombinator.com/rss"]; + this.feedUrls = feedUrls || ["http://cn.engadget.com/rss.xml", "http://news.ycombinator.com/rss"]; this.F = new Feeds(); this._lastIndex = 0; - this.interval = 3; // every second + this.readingTime = 7; // every second this.entries = []; this.lastShow = "a1"; + this._interval = -1; - this.start = function(feedUrls) { - var urls = feedUrls || this.feedUrls; + this.start = function() { var _this = this; - this.F.fetchAll(urls, function() { - _this.entries = _this.F.entries(); - setInterval(function() { + this.F.fetchAll(this.feedUrls, function() { + _this.entries = _this.F.randomizedEntries(); + this._interval = setInterval(function() { + $("#loading").hide(); _this.playRSS(); - }, _this.interval * 1000); + }, _this.readingTime * 1000); }); }; + this.stop = function() { + clearInterval(this._interval); + }; + + this.restart = function() { + this.stop(); + var _this = this; + setTimeout(function() { + _this.start(); + }, 100); + }; + this.playRSS = function() { if (this.entries.length == 0) return; if (this._lastIndex == this.entries.length) this._lastIndex = 0; @@ -89,16 +119,28 @@ function RSSVisualizer() { }; this.renderContent = function(entry) { + var bgcolor = "#0576dc"; + var content = entry.content; + if (entry.feed_title == "Hacker News") { - return ""; - } - var regexp_img = /]+>/ig; - var matched = entry.content.match(regexp_img); - var img = ""; - if (matched) { - img = matched[0]; + //bgcolor = "#F60"; + content = ""; + } else if (entry.feed_title == "Planet TW") { + bgcolor = "#6C649C"; + content = entry.content.substring(0, 500); + } else { + var regexp_img = /]+>/ig; + var matched = entry.content.match(regexp_img); + var img = ""; + if (matched) { + img = matched[0]; + } + + content = img + entry.content.replace(/<[^>]+>/img, '').substring(0, 100) + "..."; } - return img + entry.content.replace(/<[^>]+>/img, ''); + + $("body").css("background-color", bgcolor); + return content; } diff --git a/test/unittest.html b/test/unittest.html index 8fd037d..b1d7d9a 100644 --- a/test/unittest.html +++ b/test/unittest.html @@ -49,6 +49,10 @@ test("should fetch feeds", function() { }); }); +test("should randomize", function() { + var r = this.F.randomize([1, 2, 3, 4, 5, 6]); + ok( r != [1, 2, 3, 4, 5, 6]); +}) });