Template:RssFeed

/* * RssFeed is a template for embedding RSS feeds in a page. * * Required Parameters: * * (string) url: the url of the RSS feed * * Optional Parameters: * * (int) timeout: in milliseconds, the time to wait before aborting the feed request. (default: 9000) * (int) numItems: number of rss items to get. (default: 7) /

/ base functions /

window.getWidth = function { if (this.innerWidth) return this.innerWidth; if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth; if (document.body) return document.body.clientWidth; };

/ prototype extensions /

Ajax.Responders.callInProgress = function(xmlhttp) { switch (xmlhttp.readyState) { case 1: case 2: case 3: return true; break; // Case 4 and 0 default: return false; break; } };

// Register global responders that will occur on all AJAX requests Ajax.Responders.register( { onCreate: function(request) { var timeout = (request.options.timeout) ? request.options.timeout : 60000;

request[‘timeoutId’] = window.setTimeout( function { // If we have hit the timeout and the AJAX request is active, abort it if (Ajax.Responders.callInProgress(request.transport)) { request.transport.abort; // Run the onFailure method if we set one up when creating the AJAX object if (request.options[‘onFailure’]) { request.options[‘onFailure’](request.transport, request.json); } } }, timeout ); }, onComplete: function(request) { // Clear the timeout, the request completed ok window.clearTimeout(request[‘timeoutId’]); } }); / * RssFeedFactory: global helper object for dealing with rss feeds * / Page.RssFeedFactory = { feedDescriptions: {}, timer: null, feedRequests: [], activeRequests: 0 };

Page.RssFeedFactory.setTooltips = function(template) {

//make sure there is a tooltip node if (!$(‘RssFeedTooltip’)) { new Insertion.Bottom(‘page’, ‘ ’); } Event.observe(template, ‘mouseout’, function(e) { var source = (e.target) ? e.target : e.srcElement; if (source.tagName.toLowerCase 'li') { $('RssFeedTooltip').innerHTML = ; $('RssFeedTooltip').style.display = 'none'; } if (Page.RssFeedFactory.timer) window.clearTimeout(Page.RssFeedFactory.timer); }); Event.observe($('RssFeedTooltip'), 'mouseout', function (e) { $('RssFeedTooltip').innerHTML = ; $('RssFeedTooltip').style.display = 'none'; if (Page.RssFeedFactory.timer) window.clearTimeout(Page.RssFeedFactory.timer); }); Event.observe(template, 'mouseover', Page.RssFeedFactory.setFeedDescription); }; Page.RssFeedFactory.setFeedDescription = function(e) { var source = (e.target) ? e.target : e.srcElement; var link;

if (source.tagName.toLowerCase ‘li’) link = source.getElementsByTagName(‘a’)[0]; else if (source.parentNode.tagName.toLowerCase 'li') link = source; if (link x%x%x%x% link.tagName.toLowerCase ‘span’) return; //fix for hover bug on x hours label if (source.parentNode.tagName.toLowerCase == ‘li’) { var feedDescription = (Page.RssFeedFactory.feedDescriptions[link.href]) ? Page.RssFeedFactory.feedDescriptions[link.href] : ‘’; $(‘RssFeedTooltip’).innerHTML = ‘ ’ + ‘ ’ + link.innerHTML + ‘ ’ + ‘ ’ + Page.RssFeedFactory.feedDescriptions[link.href] + ‘

’; var positionY = e.clientY + document.body.scrollTop; var positionX = e.clientX + document.body.scrollLeft; if (e.clientY > 100) positionY -= 90; if (e.clientY > document.body.scrollHeight) positionY -= 100; if (e.clientX < (window.getWidth – 550)) positionX += 30; else positionX -= 550; if (positionX < 10) { positionX = 10; positionY -= 100; } if (positionY – document.body.scrollTop < 3) positionY = 3 + document.body.scrollTop; $(‘RssFeedTooltip’).style.left = positionX; $(‘RssFeedTooltip’).style.top = positionY; $(‘RssFeedTooltip’).style.display = ‘none’; if (Page.RssFeedFactory.timer) window.clearTimeout(Page.RssFeedFactory.timer); Page.RssFeedFactory.timer = setTimeout(Page.RssFeedFactory.showFeedDescription, 1000); } }; Page.RssFeedFactory.showFeedDescription = function { var tooltip = $(‘RssFeedTooltip’);

if (tooltip.innerHTML) { tooltip.style.visibility = ‘hidden’; tooltip.style.display = ‘block’; var top = parseInt(tooltip.style.top.slice(0, -2)); var height = tooltip.offsetHeight; var windowHeight = window.innerHeight – 3 + document.body.scrollTop; if ( (top + height) > (windowHeight) ) { var adjustedHeight = top + ( (windowHeight) – (top + height)); if (adjustedHeight < 3 + document.body.scrollTop) adjustedHeight = 3 + document.body.scrollTop; tooltip.style.top = adjustedHeight; } tooltip.style.visibility = ‘visible’; } window.clearTimeout(Page.RssFeedFactory.timer); }; //grab a feed Page.RssFeedFactory.requestFeed = function { var div = document.createElement(‘div’); div.className = ‘RssFeed loading’;

$(this.templateGuid).appendChild(div); Page.RssFeedFactory.feedRequests[Page.RssFeedFactory.feedRequests.length] = { method: ‘get’, parameters: ‘templateId=’ + this.templateGuid + ‘&rss_url=’ + encodeURIComponent(this.url) + ‘&numItems=’ + this.numItems, onComplete: function® { Page.RssFeedFactory.activeRequests—; Page.RssFeedFactory.showFeed®; Page.RssFeedFactory.executeFeedRequest; }, onFailure: eval(“function \ { \ Element.addClassName($(’” + this.templateGuid + ”’).getElementsByTagName(‘div’)[0], ‘error’);\ Page.RssFeedFactory.activeRequests—;\ Page.RssFeedFactory.executeFeedRequest;\ }”), timeout: this.timeout }; Page.RssFeedFactory.executeFeedRequest; }; Page.RssFeedFactory.executeFeedRequest = function { if (Page.RssFeedFactory.activeRequests < 2 && Page.RssFeedFactory.feedRequests.length) { var requestParams = Page.RssFeedFactory.feedRequests.shift;

Page.RssFeedFactory.activeRequests++; new Ajax.Request(location.href.replace(/#/, ‘’).replace(/\?./, ‘’), requestParams); } }; //show a feed on result of an xmlhttprequest Page.RssFeedFactory.showFeed = function® { eval(r.responseText);

var feed = responseArray[‘feed’]; if (feed.items.length < 1) return $(responseArray[‘templateId’]).innerHTML = ‘ ’; var template = $(responseArray[‘templateId’]); var feedHtml = ‘’; feedHtml += ‘ ’ + feed.title + ‘ ’ + ’ XML ’ + ‘ ’; for (var i = 0, item; item = feed.items[i]; i++) { Page.RssFeedFactory.feedDescriptions[item.link] = (item.description) ? item.description : ‘’; var parity = (i % 2) ? ‘odd’ : ‘even’; feedHtml += ‘ ’ + Page.RssFeedFactory.renderItemHtml(item) + ‘’; } feedHtml += ‘ ’; var div = document.createElement(‘div’); div.className = ‘RssFeed’; div.innerHTML = feedHtml; template.innerHTML = ‘’; template.appendChild(div); Page.RssFeedFactory.setTooltips(template); $(‘wikiPage_end’).style.clear = ‘both’; }; //render the item html Page.RssFeedFactory.renderItemHtml = function(item) { var dNow = new Date;

var daysOfWeek = new Array(“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”); var monthsOfYear = new Array(“Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”); var postDate = new Date(item.date * 1000); var dateString = (item.date != -1) ? daysOfWeek[postDate.getDay] : ‘’; if ((dNow.getTime – postDate.getTime) / (1000 * 60 * 60 * 24) > 6 && item.date != -1) dateString = monthsOfYear[postDate.getUTCMonth] + ’ ’ + postDate.getUTCDate.toString; //render a note for new posts if ((dNow.getTime – postDate.getTime) / (1000 * 60 * 60) < 12 && item.date != -1) { var hours = Math.round((dNow.getTime – postDate.getTime) / (1000 * 60 * 60)); hours = (hours > 0) ? hours : 0; dateString = ‘’ + hours.toString + ’ h’; } return dateString + ’ ’ + item.title + ‘’ + ’ ’ + ‘ ’; };

/* rss feed class */

function RssFeed(feedInfo) { this.templateGuid = feedInfo.guid; this.url = feedInfo.url; this.numItems = (feedInfo.numItems) ? feedInfo.numItems : 7; this.timeout = (feedInfo.timeout) ? feedInfo.timeout : 30000; }

RssFeed.prototype.init = Page.RssFeedFactory.requestFeed;