live updating of calendar

This commit is contained in:
Sriram Hariharan
2018-08-11 19:45:24 -05:00
parent 1beb77187f
commit b92a42ed41
3 changed files with 40 additions and 19 deletions

View File

@@ -9,7 +9,7 @@
</head> </head>
<body> <body>
<div id='calendar' style="height:50%; width: 60%"></div> <div id='calendar' style=" width: 65%; margin-left:auto; margin-right:auto;"></div>
</body> </body>
<script src='js/calendar.js'></script> <script src='js/calendar.js'></script>

View File

@@ -1,6 +1,6 @@
$(function () { $(function () {
const materialColors = ['#4CAF50', '#CDDC39', const materialColors = ['#4CAF50', '#CDDC39',
'#FFC107', '#2196F3', '#F57C00', '#9C27B0', '#FF5722', , '#673AB7', '#FFC107', '#2196F3', '#F57C00', '#9C27B0', '#FF5722', '#673AB7',
'#FF5252', '#E91E63', '#009688', '#00BCD4', '#FF5252', '#E91E63', '#009688', '#00BCD4',
'#4E342E', '#424242', '#9E9E9E' '#4E342E', '#424242', '#9E9E9E'
]; ];
@@ -16,23 +16,12 @@ $(function () {
var colorCounter = 0; var colorCounter = 0;
// Each schedule needs to store 'TITLE - START TIME - END TIME - COLOR' // Each schedule needs to store 'TITLE - START TIME - END TIME - COLOR'
var classSchedules = []; var classSchedules = [];
var savedCourses = [];
chrome.storage.sync.get("savedCourses", function (data) { chrome.storage.sync.get("savedCourses", function (data) {
var savedCourses = data.savedCourses;
// Iterate through each saved course and add to 'event' // Iterate through each saved course and add to 'event'
for (let i = 0; i < savedCourses.length; i++) { savedCourses = data.savedCourses;
var classInfo = savedCourses[i]; setAllEvents(savedCourses);
var department = classInfo.coursename.substring(0, classInfo.coursename.search(/\d/) - 2);
var course_nbr = classInfo.coursename.substring(classInfo.coursename.search(/\d/), classInfo.coursename.indexOf(" ", classInfo.coursename.search(/\d/)));
var uncapProf = classInfo.profname;
uncapProf = uncapProf.charAt(0) + uncapProf.substring(1).toLowerCase();
for (let j = 0; j < classInfo.datetimearr.length; j++) {
let session = classInfo.datetimearr[j]; // One single session for a class
setEventForSection(session, colorCounter, department, course_nbr, uncapProf);
}
colorCounter++;
}
$("#calendar").fullCalendar({ $("#calendar").fullCalendar({
editable: false, // Don't allow editing of events editable: false, // Don't allow editing of events
handleWindowResize: true, handleWindowResize: true,
@@ -69,6 +58,26 @@ $(function () {
}); });
}); });
// Iterate through each saved course and add to 'event'
function setAllEvents(savedCourses) {
colorCounter = 0;
classSchedules = [];
for (let i = 0; i < savedCourses.length; i++) {
var classInfo = savedCourses[i];
var department = classInfo.coursename.substring(0, classInfo.coursename.search(/\d/) - 2);
var course_nbr = classInfo.coursename.substring(classInfo.coursename.search(/\d/), classInfo.coursename.indexOf(" ", classInfo.coursename.search(/\d/)));
var uncapProf = classInfo.profname;
uncapProf = uncapProf.charAt(0) + uncapProf.substring(1).toLowerCase();
for (let j = 0; j < classInfo.datetimearr.length; j++) {
let session = classInfo.datetimearr[j]; // One single session for a class
setEventForSection(session, colorCounter, department, course_nbr, uncapProf);
}
colorCounter++;
}
}
//create the event object for every section
function setEventForSection(session, colorCounter, department, course_nbr, uncapProf) { function setEventForSection(session, colorCounter, department, course_nbr, uncapProf) {
var fullday = days.get(session[0]); var fullday = days.get(session[0]);
classSchedules.push({ classSchedules.push({
@@ -95,8 +104,14 @@ $(function () {
} }
chrome.runtime.onMessage.addListener( chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) { function (request, sender, sendResponse) {
if (request.command == "updateCourseList") { if (request.command == "updateCourseList" || request.command == "courseAdded") {
//update the calendar here chrome.storage.sync.get("savedCourses", function (data) {
savedCourses = data.savedCourses
setAllEvents(data.savedCourses);
console.log(classSchedules);
$('#calendar').fullCalendar('removeEventSources');
$("#calendar").fullCalendar('addEventSource', classSchedules, true);
});
} }
}); });
}); });

View File

@@ -60,8 +60,13 @@ $(function () {
setTimeout(function () { setTimeout(function () {
$("#snackbar").attr("class", ""); $("#snackbar").attr("class", "");
}, 3000); }, 3000);
setTimeout(update(), 1000); chrome.runtime.sendMessage({
command: "updateCourseList"
}, function () {
update();
});
}); });
}); });
$("#Syllabi").click(function () { $("#Syllabi").click(function () {
@@ -90,6 +95,7 @@ $(function () {
chrome.runtime.onMessage.addListener( chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) { function (request, sender, sendResponse) {
if (request.command == "updateCourseList") { if (request.command == "updateCourseList") {
console.log("hello");
update(); update();
} }
}); });