Background event refresh
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
|
let events = [];
|
||||||
|
|
||||||
function handleClientLoad() {
|
function handleClientLoad() {
|
||||||
gapi.load('client:auth2', initClient);
|
gapi.load('client:auth2', initClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
function findPrevious(events) {
|
function findPrevious() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
let max = null;
|
let max = null;
|
||||||
let maxTime = 0;
|
let maxTime = 0;
|
||||||
@@ -18,7 +20,7 @@ function findPrevious(events) {
|
|||||||
return max;
|
return max;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findCurrent(events) {
|
function findCurrent() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
for (const event of events) {
|
for (const event of events) {
|
||||||
const start = new Date(event.start.dateTime).getTime();
|
const start = new Date(event.start.dateTime).getTime();
|
||||||
@@ -31,8 +33,8 @@ function findCurrent(events) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function anyRemaining(events) {
|
function anyRemaining() {
|
||||||
const current = findCurrent(events);
|
const current = findCurrent();
|
||||||
const currentEnd = current ? new Date(current.end.dateTime).getTime() : null;
|
const currentEnd = current ? new Date(current.end.dateTime).getTime() : null;
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
@@ -47,8 +49,8 @@ function anyRemaining(events) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findNext(events) {
|
function findNext() {
|
||||||
const current = findCurrent(events);
|
const current = findCurrent();
|
||||||
const currentEnd = current ? new Date(current.end.dateTime).getTime() : null;
|
const currentEnd = current ? new Date(current.end.dateTime).getTime() : null;
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
@@ -107,16 +109,16 @@ function timezoneOffset() {
|
|||||||
return `${offset > 0 ? '-' : '+'}${Math.floor(offset / 60).toString().padStart(2, '0')}:${(offset % 60).toString().padStart(2, '0')}`;
|
return `${offset > 0 ? '-' : '+'}${Math.floor(offset / 60).toString().padStart(2, '0')}:${(offset % 60).toString().padStart(2, '0')}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function render(events) {
|
function render() {
|
||||||
const curElem = document.getElementById("current");
|
const curElem = document.getElementById("current");
|
||||||
const curName = document.getElementById("currentName");
|
const curName = document.getElementById("currentName");
|
||||||
const curLeft = document.getElementById("currentLeft");
|
const curLeft = document.getElementById("currentLeft");
|
||||||
const nextElem = document.getElementById("next");
|
const nextElem = document.getElementById("next");
|
||||||
const nextName = document.getElementById("nextName");
|
const nextName = document.getElementById("nextName");
|
||||||
|
|
||||||
const current = findCurrent(events);
|
const current = findCurrent();
|
||||||
const prev = findPrevious(events);
|
const prev = findPrevious();
|
||||||
const next = findNext(events);
|
const next = findNext();
|
||||||
|
|
||||||
if (current) {
|
if (current) {
|
||||||
// Currently in an event
|
// Currently in an event
|
||||||
@@ -154,7 +156,7 @@ function render(events) {
|
|||||||
} else if (current) {
|
} else if (current) {
|
||||||
nextElem.classList.remove("event");
|
nextElem.classList.remove("event");
|
||||||
nextElem.classList.add("break");
|
nextElem.classList.add("break");
|
||||||
if (anyRemaining(events)) {
|
if (anyRemaining()) {
|
||||||
setText(nextName, "😎 Break");
|
setText(nextName, "😎 Break");
|
||||||
} else {
|
} else {
|
||||||
setText(nextName, "✅ Done!");
|
setText(nextName, "✅ Done!");
|
||||||
@@ -166,6 +168,20 @@ function render(events) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadEvents() {
|
||||||
|
const tz = timezoneOffset();
|
||||||
|
const date = new Date().toISOString().substring(0,10);
|
||||||
|
return gapi.client.calendar.events.list({
|
||||||
|
'calendarId': 'primary',
|
||||||
|
'timeMin': `${date}T00:00:00${tz}`,
|
||||||
|
'timeMax': `${date}T23:59:59${tz}`,
|
||||||
|
'showDeleted': false,
|
||||||
|
'singleEvents': true,
|
||||||
|
'maxResults': 250,
|
||||||
|
'orderBy': 'startTime',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function initClient() {
|
function initClient() {
|
||||||
gapi.client.init({
|
gapi.client.init({
|
||||||
apiKey: 'AIzaSyDzNMBbLqQoSCMiug_7UbUrgaAvnoyzYYU',
|
apiKey: 'AIzaSyDzNMBbLqQoSCMiug_7UbUrgaAvnoyzYYU',
|
||||||
@@ -175,23 +191,19 @@ function initClient() {
|
|||||||
ux_mode: 'redirect',
|
ux_mode: 'redirect',
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
|
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
|
||||||
const tz = timezoneOffset();
|
return loadEvents();
|
||||||
const date = new Date().toISOString().substring(0,10);
|
|
||||||
return gapi.client.calendar.events.list({
|
|
||||||
'calendarId': 'primary',
|
|
||||||
'timeMin': `${date}T00:00:00${tz}`,
|
|
||||||
'timeMax': `${date}T23:59:59${tz}`,
|
|
||||||
'showDeleted': false,
|
|
||||||
'singleEvents': true,
|
|
||||||
'maxResults': 250,
|
|
||||||
'orderBy': 'startTime',
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
gapi.auth2.getAuthInstance().signIn();
|
gapi.auth2.getAuthInstance().signIn();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}).then((response) => {
|
}).then((response) => {
|
||||||
setInterval(() => render(response.result.items), 250);
|
events = response.result.items;
|
||||||
|
setInterval(() => render(), 250);
|
||||||
|
setInterval(() => {
|
||||||
|
loadEvents().then((response) => {
|
||||||
|
events = response.result.items;
|
||||||
|
});
|
||||||
|
}, 30 * 60 * 1000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>EasyCal</title>
|
<title>EasyCal</title>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta http-equiv="refresh" content="3603">
|
<meta http-equiv="refresh" content="50000">
|
||||||
<link rel="stylesheet" href="easycal.css">
|
<link rel="stylesheet" href="easycal.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user