Advertisement
Guest User

Embedding Announcements in a Canvas Home Page

a guest
Apr 18th, 2014
1,692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Original code from Matt Huwiler modified by Josh Blumberg for Champlain College
  2. // Includes Truncate code from Pathable https://github.com/pathable/truncate
  3. // Edited 3/20/2014
  4.  
  5. $(function() {
  6.  
  7.     // Config
  8.     // At most display this many announcements
  9.     var maxDisplay = 20;
  10.  
  11.     // Don't display announcements older than n days.  CURRENTLY NOT USED
  12.     var noOlderThan = 0;
  13.  
  14.     // Abbreviate announcements to a set length
  15.     var truncateLength = 150;
  16.  
  17.     // Matches trailing non-space characters.
  18.     var chop = /(\s*\S+|\s)$/;
  19.  
  20.     // Return a truncated html string.  Delegates to $.fn.truncate.
  21.     $.truncate = function(html, options) {
  22.         return $('<div></div>').append(html).truncate(options).html();
  23.     };
  24.  
  25.     // Truncate the contents of an element in place.
  26.     $.fn.truncate = function(options) {
  27.         if ($.isNumeric(options)) options = {length: options};
  28.         var o = $.extend({}, $.truncate.defaults, options);
  29.  
  30.         return this.each(function() {
  31.             var self = $(this);
  32.  
  33.             if (o.noBreaks) self.find('br').replaceWith(' ');
  34.  
  35.             var text = self.text();
  36.             var excess = text.length - o.length;
  37.  
  38.             if (o.stripTags) self.text(text);
  39.  
  40.             // Chop off any partial words if appropriate.
  41.             if (o.words && excess > 0) {
  42.                 excess = text.length - text.slice(0, o.length).replace(chop, '').length - 1;
  43.             }
  44.  
  45.             if (excess < 0 || !excess && !o.truncated) return;
  46.  
  47.             // Iterate over each child node in reverse, removing excess text.
  48.             $.each(self.contents().get().reverse(), function(i, el) {
  49.                 var $el = $(el);
  50.                 var text = $el.text();
  51.                 var length = text.length;
  52.  
  53.                 // If the text is longer than the excess, remove the node and continue.
  54.                 if (length <= excess) {
  55.                     o.truncated = true;
  56.                     excess -= length;
  57.                     $el.remove();
  58.                     return;
  59.                 }
  60.  
  61.                 // Remove the excess text and append the ellipsis.
  62.                 if (el.nodeType === 3) {
  63.                     $(el.splitText(length - excess - 1)).replaceWith(o.ellipsis);
  64.                     return false;
  65.                 }
  66.  
  67.                 // Recursively truncate child nodes.
  68.                 $el.truncate($.extend(o, {length: length - excess}));
  69.                 return false;
  70.             });
  71.         });
  72.     };
  73.  
  74.     $.truncate.defaults = {
  75.  
  76.         // Strip all html elements, leaving only plain text.
  77.         stripTags: false,
  78.  
  79.         // Only truncate at word boundaries.
  80.         words: false,
  81.  
  82.         // Replace instances of <br> with a single space.
  83.         noBreaks: false,
  84.  
  85.         // The maximum length of the truncated html.
  86.         length: Infinity,
  87.  
  88.         // The character to use as the ellipsis.  The word joiner (U+2060) can be
  89.         // used to prevent a hanging ellipsis, but displays incorrectly in Chrome
  90.         // on Windows 7.
  91.         // http://code.google.com/p/chromium/issues/detail?id=68323
  92.         ellipsis: '\u2026' // '\u2060\u2026'
  93.  
  94.     };
  95.  
  96.     // Are we on the homepage?  If so, can we extract the atom feed URL?  And is it a valid feed URL?
  97.     if ( ! /courses\/\d+(?:\/(?:pages|wiki)\/front\-page)?\/?$/.test(document.location.href) ) return;
  98.  
  99.     // If accessed from /pages/front-page or /wiki/front-page, atom feed doesn't exist.  Display a note explaining this.
  100.     if ( /courses\/\d+(?:\/(?:pages|wiki)\/front\-page)\/?$/.test(document.location.href) ) {
  101.         //window.location.replace(document.location.href.replace(/(?:pages|wiki)\/front\-page\/?/, ''));
  102.         $("#cc-homepage-announcements").append('<p>To view the latest announcements please click on "Announcements" or "Home"</p>');
  103.         return;
  104.     }
  105.  
  106.     var $atomLink = $('link[rel=alternate][title="Course Atom Feed"]');
  107.     if ( ! $atomLink ) return;
  108.     var atomCourseUrl = $.trim($atomLink.attr('href'));
  109.     if (!atomCourseUrl || !~atomCourseUrl.indexOf("_") ||!~atomCourseUrl.indexOf(".atom")) return;
  110.  
  111.     // Sweet.  Now let's extract the atom feed ID from the course feed url so we can construct an announcements feed URL
  112.     var feedId = atomCourseUrl.substring(atomCourseUrl.lastIndexOf("/") + 1, atomCourseUrl.lastIndexOf(".atom"));
  113.     if (!feedId) return;
  114.  
  115.     // Now that we have the feed ID, we can construct the announcements feed URL from it
  116.     var announcementsFeedUrl = 'https://' + location.hostname + '/feeds/announcements/' + feedId + '.atom';
  117.  
  118.     console.log("Found feed URL: " + announcementsFeedUrl.toString());
  119.  
  120.     var $xhr = $.ajax({
  121.             url: announcementsFeedUrl,
  122.             dataType: "xml"
  123.         }),
  124.         announcements = [];
  125.  
  126.     $xhr.done(function( data ) {
  127.         var $xml = $(data);
  128.         $xml.find("entry").each(function(i, entry) {
  129.             var $this = $(entry);
  130.             announcements.push({
  131.                 link: $.trim($this.find('link').attr('href')),
  132.                 title: $.trim($this.find('title').text()),
  133.                 published: new Date($.trim($this.find('published').text())),
  134.                 author: $.trim($this.find('author').text()),
  135.                 content: $.trim($this.find('content').text())
  136.             });
  137.         });
  138.  
  139.         //This should work but it appears to be backwards for Safari (works in Firefox and Chrome)
  140.         announcements.sort(function( a, b ) {
  141.  
  142.             var aDate = a.published.getTime(),
  143.                 bDate = b.published.getTime();
  144.  
  145.             return aDate < bDate ? 1 : (aDate > bDate ? -1 : 0);
  146.  
  147.         });
  148.  
  149.         $("#cc-homepage-announcements").append((function() {
  150.             var announcementsHtml = [];
  151.             var displayedCount = 0;
  152.             var shortenedTitle;
  153.             for (var i = 0; i < announcements.length; i++) {
  154.                 // Was announcement published longer than than 1 week ago?
  155.                 //var oneWeekAgo = new Date().getTime() - noOlderThan * 24 * 60 * 60 * 1000;
  156.                 //if (announcements[i].published.getTime() > oneWeekAgo) {
  157.                 //}
  158.  
  159.                 //Strip the word "Announcement: " that is at the start of every title
  160.                 if(announcements[i].title.search("Announcement:") >= 0) {
  161.                     shortenedTitle = announcements[i].title.substring(14);
  162.                 }else{
  163.                     shortenedTitle = announcements[i].title;
  164.                 }
  165.  
  166.                 announcementsHtml.push('<h6 style="text-align:left;color:#C47C12">' + (announcements[i].published.getMonth() + 1) + '/'+ announcements[i].published.getDate() + '/' + announcements[i].published.getFullYear() + '</h6>');
  167.                 announcementsHtml.push('<h5 style="margin:0px;padding:0px;text-align:left;"><a href="' + announcements[i].link + '">' + shortenedTitle + '</a></h5>');
  168.                 announcementsHtml.push('<p>' + $.truncate(announcements[i].content,truncateLength) + '</p>');
  169.                 displayedCount++;
  170.  
  171.  
  172.                 if (displayedCount >= maxDisplay) break;
  173.                 announcementsHtml.push('<hr style="color:#1C1C1C; background-color:#1C1C1C; height:1px;">');  //Add a <hr> if there are more announcements
  174.             }
  175.  
  176.             return $( announcementsHtml.join(""));
  177.  
  178.         }()));
  179.  
  180.     });
  181.  
  182.  
  183.     $xhr.fail(function( xhr, status, err ) {
  184.         // Show an error if desired if feed failed to load
  185.     });
  186.  
  187. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement