Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple calendars #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bundle/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
BUNDLE_JOBS: 8
BUNDLE_JOBS: "8"
11 changes: 11 additions & 0 deletions _data/icals.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- https://www.meetup.com/chadevs/events/ical/
- https://www.meetup.com/Carbon-Five-Chattanooga-Hack-Nights/events/ical/
- https://www.meetup.com/Programming-Interview-Practice/events/ical/
- https://www.meetup.com/ChattanoogaJS/events/ical/
- https://www.meetup.com/Chattanooga-Python-User-Group/events/ical/
- https://www.meetup.com/Papers-We-Love-Chattanooga/events/ical/
- https://www.meetup.com/CHA-Art-Dev/events/ical/
- https://www.meetup.com/Chattanooga-Elixir/events/ical/
- https://www.meetup.com/Chattanooga-Game-Development-Meetup/events/ical/
- https://www.meetup.com/chattanoogaphp/events/ical/
- https://www.meetup.com/Chattanooga-Drupal-Users-Group/events/ical/
282 changes: 159 additions & 123 deletions assets/js/events.js
Original file line number Diff line number Diff line change
@@ -1,158 +1,194 @@
let days = [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
];
let months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];

var arrangedEvents = {};

class Event {
constructor(title, content, location, start) {
let address = '';
try {
address = location.replace(', United States', '');
} catch (e) {
// Do nothing
}

this.title = title;
this.address = address;
this.content = this.autolink(content);
this.start = this.startDate(start);
this.time = this.prettyTime(this.start);
this.meridian = this.meridian(this.start);
}

prettyTime(start) {
return hour(start) + ":" + minutes(start);

function hour(time) {
var h = time.getHours();
return h > 12 ? h - 12 : h;
}

function minutes(time) {
var m = time.getUTCMinutes();
return m < 10 ? "0" + m : m;
}
}

meridian(time) {
return time.getHours() >= 12 ? 'PM' : 'AM';
}

startDate(start) {
var date = start.dateTime ? start.dateTime : start.date;
date = date ? date : start;
return new Date(date);
}

autolink(text) {
if (typeof (text) == 'undefined') { return text; }

// http://jsfiddle.net/kachibito/hEgvc/1/light/
return text.replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g, "<a href='$1'>$1</a>");
}
}

Event.prettyDate = function (start) {
var day = days[start.getDay()],
month = months[start.getMonth()],
date = start.getDate();
return day + ", " + month + " " + date;
}

function dateKey(date) {
return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
}

function addEvent(obj) {
var key = dateKey(obj.start);
var date = arrangedEvents[key] || { events: [], prettyDate: '' };
date.prettyDate = date.prettyDate || Event.prettyDate(obj.start);
date.events.push(obj);
arrangedEvents[key] = date;
}

const loadRSS = () =>
icalURLs.forEach(fetchRSS);

function fetchRSS(url) {
fetch('https://cors-anywhere.herokuapp.com/' + url)
.then(response => response.text())
.then(cal => ical.parseICS(cal))
.then(data => {
for (let k in data) {
if (data.hasOwnProperty(k)) {
var item = data[k];
if (data[k].type == 'VEVENT') {
addEvent(new Event(
item.summary.replace('/', '/<wbr>'),
item.description,
item.location,
item.start,
));
}
}
}
})
.then(render);
}

function init() {
window.body = document.body;
window.cal = document.getElementById('calendar');
window.days = [
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"
];
window.months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
window.cal = document.getElementById('calendar');
window.tmpl = document.getElementById('template').innerHTML;
window.cal.innerHTML = '';
window.body.classList.add('loading');

loadRSS();
gapi.client.setApiKey('AIzaSyCp_IflIV150pu3Quu-XDIaM7tMYlfO4DQ');
gapi.client.load('calendar', 'v3').then(execute);
}

function execute() {
var start = new Date();
start.setTime(start.getTime() - (60 * 60 * 24 * 5));
displayEventsFor(start);
const end = getEndOfMonth(start);

[
'[email protected]', // Chadev
].forEach(function (calendar) {
displayEventsFor(start, end, calendar);
})
}

function displayEventsFor(start, end) {
end = end || getEndOfMonth(start);
var currentStart = start;
var request = gapi.client.calendar.events.list({
"calendarId": "[email protected]",
function displayEventsFor(start, end, calendar) {
gapi.client.calendar.events.list({
"calendarId": calendar,
"singleEvents": "True",
"orderBy": "startTime",
"timeMin": start.toISOString(),
"timeMax": end.toISOString()

});
request.then(displayEvents);

function getEndOfMonth(start) {
var end = new Date(start.getTime());
end.setMonth(start.getMonth() + 1);
end = new Date(end - (24 * 60 * 60 * 1000));
end.setHours(23);
end.setMinutes(59);
end.setSeconds(59);
end.setMilliseconds(999);
return end;
}
"timeMax": end.toISOString(),
})
.then(addEvents)
.then(render);
}

function displayEvents(data) {
var events = [],
arrangedEvents = {},
today = Date.now();

data.result.items.forEach(function(item) {
var obj = {};
obj.title = item.summary.replace('/', '/<wbr>');
obj.content = autolink(item.description);
obj.start = startDate(item.start);
obj.time = prettyTime(obj.start);
obj.meridian = meridian(obj.start);
console.log(item);

try {
obj.address = item.location.replace(', United States', '');
} catch(e) {
// Do nothing
}
function getEndOfMonth(start) {
var end = new Date(start.getTime());
end.setMonth(start.getMonth() + 1);
end = new Date(end - (24 * 60 * 60 * 1000));
end.setHours(23);
end.setMinutes(59);
end.setSeconds(59);
end.setMilliseconds(999);
return end;
}

var key = dateKey(obj.start);
var date = arrangedEvents[key] || { events: [], prettyDate: '' };
date.prettyDate = date.prettyDate || prettyDate(obj.start);
date.events.push(obj);
arrangedEvents[key] = date;
function addEvents(data) {
data.result.items.forEach(function (item) {
addEvent(new Event(
item.summary.replace('/', '/<wbr>'),
item.description,
item.location,
item.start,
));
});
}

arrangedEvents = convertToArray(arrangedEvents);
function render() {
const dates = convertToArray(arrangedEvents);

var templatedata = {
'dates': arrangedEvents
'dates': dates
};

window.cal.innerHTML = window.Mustache.render(window.tmpl, templatedata);
window.body.classList.remove('loading');

return;

function prettyTime(start) {
return hour(start) + ":" + minutes(start);

function hour(time) {
var h = time.getHours();
return h > 12 ? h - 12 : h;
}

function minutes(time) {
var m = time.getUTCMinutes();
return m < 10 ? "0" + m : m;
}

}

function meridian(time) {
return time.getHours() >= 12 ? 'PM' : 'AM';
}

function prettyDate(start) {
var day = days[start.getDay()],
month = months[start.getMonth()],
date = start.getDate();
return day + ", " + month + " " + date;
}

function startDate(start) {
var date = start.dateTime ? start.dateTime : start.date;
return new Date(date);
}

function dateKey(date) {
return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
}

function autolink(text) {
if(typeof(text) == 'undefined') { return text; }

// http://jsfiddle.net/kachibito/hEgvc/1/light/
return text.replace(/((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g,"<a href='$1'>$1</a>");
}

function formatTime(date) {
var output = [];
output.push((date.getHours() % 12) || 12);
output.push(':');
output.push(('0' + date.getMinutes()).slice(-2));
output.push(date.getHours() > 11 ? ' p.m.' : ' a.m.');
return output.join('');
}

function convertToArray(obj) {
var arr = Object.keys(obj).map(function(key) {
var arr = Object.keys(obj).map(function (key) {
return obj[key];
});

Expand Down
Loading