start-ffhh/js/main.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-09-10 16:49:19 +02:00
var rss = [
2014-09-12 16:32:49 +02:00
'hamburg.freifunk.net',
2014-09-10 16:49:19 +02:00
];
2015-01-14 20:51:13 +01:00
var MAX_RSS = 3;
function shorten(text, maxLength) {
var ret = text;
if (ret.length > maxLength) {
ret = ret.substr(0,maxLength-3) + "...";
}
return ret;
}
2014-09-10 16:49:19 +02:00
$(document).ready(function() {
2014-09-15 14:53:00 +02:00
var addItem = function(title, link, date, text) {
2014-09-12 16:00:41 +02:00
var item = $('<a>')
.attr('class', 'css-truncate')
.attr('href', link)
.attr('target', '_blank')
2014-09-12 16:32:49 +02:00
.data('date', date)
2014-09-12 16:00:41 +02:00
.text(title);
2014-09-15 14:53:00 +02:00
var prev = $('<p>')
.attr('class', 'css-truncate')
.html(text);
$('#news').append($('<li>').append(item).append(prev));
2014-09-12 16:32:49 +02:00
};
var getAddedItems = function() {
var news = [];
2014-09-15 14:53:00 +02:00
$('#news').find('li').forEach(function(item) {
var a = $(item).children('a');
var p = $(item).children('p');
2014-09-12 16:32:49 +02:00
var title = a.text();
var link = a.attr('href');
var date = a.data('date');
2014-09-15 14:53:00 +02:00
var text = p.html();
2014-09-12 16:32:49 +02:00
2014-09-15 14:53:00 +02:00
news.push({title:title, link:link, date:date, text:text});
2014-09-12 16:32:49 +02:00
});
return news;
};
var clearNewsFeed = function() {
$('#news').children().forEach(function(child) {
child.remove();
});
};
var updateNewsFeed = function(news) {
var items = getAddedItems().concat(news);;
items.sort(function(a, b) {
a = new Date(a.date);
b = new Date(b.date);
return a > b ? -1 : a < b ? 1 : 0;
});
2014-09-12 16:00:41 +02:00
2014-09-12 16:32:49 +02:00
clearNewsFeed();
var i = 0;
items.forEach(function(item) {
if(i++ >= MAX_RSS) return;
2015-01-14 20:51:13 +01:00
addItem(item.title, item.link, item.date, shorten(item.text, 80));
2014-09-12 16:32:49 +02:00
});
2014-09-12 16:00:41 +02:00
};
2014-09-10 16:49:19 +02:00
rss.forEach(function(feed) {
$.get('/feeds/' + feed + '.rss', function(data) {
2014-09-12 16:32:49 +02:00
var news = [];
2014-09-10 17:19:25 +02:00
$(data).find('item').forEach(function(entry) {
var title = $(entry).find('title').text();
var link = $(entry).find('link').text();
2014-09-12 16:32:49 +02:00
var date = $(entry).find('pubDate').text();
2014-09-15 14:53:00 +02:00
var text = $(entry).find('description').text();
2014-09-10 16:49:19 +02:00
2014-09-15 14:53:00 +02:00
news.push({title:title, link:link, date:date, text:text});
2014-09-10 16:49:19 +02:00
});
2014-09-12 15:57:50 +02:00
$(data).find('entry').forEach(function(entry) {
var title = $(entry).find('title').text();
var link = $(entry).find('link').attr('href');
2014-09-12 16:32:49 +02:00
var date = $(entry).find('published').text();
2014-09-15 14:53:00 +02:00
var text = $(entry).find('content').text();
2014-09-12 15:57:50 +02:00
2014-09-15 14:53:00 +02:00
news.push({title:title, link:link, date:date, text:text});
2014-09-12 15:57:50 +02:00
});
2014-09-12 16:32:49 +02:00
updateNewsFeed(news);
2014-09-10 16:49:19 +02:00
});
});
});