Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Testing

Need to display timestamp on an RSS widget

Author Message
outchy



Joined: 21 Mar 2007
Posts: 1
Location: Onset, MA

Posted: Wed Mar 21, 2007 - 7:04 pm    Post subject: Need to display timestamp on an RSS widget Reply with quote

hey guys. i have an rss widget that displays the 15 most recent postings on my messageboard. it displays the title of the post (as a link) and the date (ex. mar 21). what i want to the widget to show is the link, followed by the date AND the time the post was submitted directly after. for example:

nik on "Here we go!" mar 21 at 11:25 am

can anyone help me? i don't understand that part of the code :/ here is a link to download the widget if that helps and i'll also post the RSS.js file below (sorry if the code is a bit long):

http://www.manonfire.cc/playground/ThePlaygroundWidget2.zip

Code:
/*

File: exampleRSS.js
Author: Fernando Dashiiki http://fernando.dubtribe.com
Abstract: JavaScript logic for example RSS widget
   Displays the RSS Feed of the most recent 15 replies
   from The Playground
Version: 1.1 b rev3
Source: Written from the source code provided as 'RSS Sample'
    from developer.apple.com

*/

var feed = {title:"The Playground", top:"The Playground", bottom:"The Playground", url:"http://www.manonfire.cc/playground/rss.php"};         
      
function load ()
{
   scrollerInit(document.getElementById("myScrollBar"), document.getElementById("myScrollTrack"), document.getElementById("myScrollThumb"));
   calculateAndShowThumb(document.getElementById("contents"));

   if (!window.widget)
   {
      show ();
   }
}


var last_updated = 0;
var xml_request = null;

//------------------------------------------------------------------------------
//  show - post a request to get DUB Messages Overall RSS topics when showing
//    the widget.
//  Space requests by at least 15 minutes to avoid hitting the server too often.
//  Also, note that you should cancel any outstanding request before posting
//  the new one.
//------------------------------------------------------------------------------

function show ()
{
   DEBUG("show");
   var now = (new Date).getTime();
   
   // only check if 1 second has passed
   if ((now - last_updated) > 1)
   {
      if (xml_request != null)
      {
         xml_request.abort();
         xml_request = null;
      }
      xml_request = new XMLHttpRequest();

      xml_request.onload = function(e) {xml_loaded(e, xml_request);}
      xml_request.overrideMimeType("text/xml");
      xml_request.open("GET", feed.url);
      xml_request.setRequestHeader("Cache-Control", "no-cache");
      xml_request.send(null);
    }
    DEBUG("/show");
}

if (window.widget)
{
   widget.onshow = show;
}


//-----------------------------------------------------------------------------
// findChild - scan the children of a given DOM element for a node matching
// nodeName; much more efficient than the standard DOM methods
// (getElementsByTagName, etc) if you know what you're looking for
//-----------------------------------------------------------------------------

function findChild (element, nodeName)
{
   var child;
   
   for (child = element.firstChild; child != null; child = child.nextSibling)
   {
      if (child.nodeName == nodeName)
         return child;
   }
   
   return null;
}

//----------------------------------------------------------------------------
// xml_loaded - extract the content of Apple RSS Hot News feed and place the
// items data into a results array: extract the title, link and publication
// date for each item.
//----------------------------------------------------------------------------

function xml_loaded (e, request)
{
   //alert (request.responseText);
   xml_request = null;
   if (request.responseXML)
   {      
      var contents = document.getElementById('contents');
      while (contents.hasChildNodes())
      {
         contents.removeChild(contents.firstChild);
      }
      
      // Get the top level <rss> element
      var rss = findChild(request.responseXML, 'rss');      
      if (!rss) {alert("no <rss> element"); return;}
      
      // Get single subordinate channel element
      var channel = findChild( rss, 'channel');
      if (!channel) {alert("no <channel> element"); return;}
      
      var results = new Array;
      
      // Get all item elements subordinate to the channel element
      // For each element, get title, link and publication date.
      // Note that all elements of an item are optional.
      for( var item = channel.firstChild; item != null; item = item.nextSibling)
      {
         if( item.nodeName == 'item' )
         {
            var title = findChild (item, 'title');
         
            // we have to have the title to include the item in the list
            if( title != null )
            {
               var link = findChild (item, 'link');
               var pubDate = findChild (item, 'pubDate');
               results[results.length] = {title:title.firstChild.data,
                  link:(link != null ? link.firstChild.data : null),
                  date:new Date(Date.parse(pubDate.firstChild.data))
               };
            }
         }
      }
      
      // sort by date
      results.sort (compFunc);
      
      // copy title and date into rows for display. Store link so it can be used when user
      // clicks on title
      nItems = results.length;
      var even = true;      
            
      for (var i = 0; i < nItems; ++i)
      {
         var item = results[i];
         var row = createRow (item.title, item.link, item.date, even);
         even = !even;
         
         contents.appendChild (row);
      }

      // update the scrollbar so scrollbar matches new data
      calculateAndShowThumb(document.getElementById("contents"));
      
      // set last_updated to the current time to keep track of the last time a request was posted
      last_updated = (new Date).getTime();
   }
}

//-----------------------------------------------------------------------------
// sortFunc - compare function used for sorting dates
//-----------------------------------------------------------------------------

function compFunc (a, b)
{
   if (a.date < b.date)
      return 1;
   else if (a.date > b.date)
      return -1;
   else
      return 0;
}

//----------------------------------------------------------------------------
// createRow - add data to the next row in the widget body. Rows have
// alternating (light and dark backgound). The title and date as displayed
// for each item. The link is used  when the user clicks on a RSS title.
//----------------------------------------------------------------------------


function createRow (title, link, date, even)
{
   var row = document.createElement ('div');
   row.setAttribute ('class', 'row ' + (even ? 'light' : 'dark'));
   
   var title_div = document.createElement ('div');
   title_div.innerText = title;
   title_div.setAttribute ('class', 'title');
   if (link != null)
   {
      title_div.setAttribute ('the_link', link);
      title_div.setAttribute ('onclick', 'clickOnTitle (event, this);');
   }
   row.appendChild (title_div);
   
   if (date != null)
   {
      var date_div = document.createElement ('div');
      date_div.setAttribute ('class', 'date');
      date_div.innerText = createDateStr (date);
      
      row.appendChild (date_div);
   }
   
   return row;   
}

function createDateStr (date)
{
   var month;
   switch (date.getMonth())
   {
      case 0: month = 'Jan'; break;
      case 1: month = 'Feb'; break;
      case 2: month = 'Mar'; break;
      case 3: month = 'Apr'; break;
      case 4: month = 'May'; break;
      case 5: month = 'Jun'; break;
      case 6: month = 'Jul'; break;
      case 7: month = 'Aug'; break;
      case 8: month = 'Sep'; break;
      case 9: month = 'Oct'; break;
      case 10: month = 'Nov'; break;
      case 11: month = 'Dec'; break;
   }   
   return month + ' ' + date.getDate();
}

//----------------------------------------------------------------------------
// clickOnTitle - take user to the RSS link when she clicks on an article's title.
//----------------------------------------------------------------------------
function clickOnTitle (event, div)
{
   if (window.widget)
   {
      widget.openURL (div.the_link);
   } else document.location = div.the_link;
}

//----------------------------------------------------------------------------
// clickOnFeedTitle - take user to the feed main web page when she clicks
// on the widget title.
//----------------------------------------------------------------------------

function clickOnFeedTitle(event)
{
   if (window.widget)
   {
         widget.openURL (feed.url);
   } else document.location = feed.url;
}
[url][/url]
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group