93 lines
2.7 KiB
JavaScript
93 lines
2.7 KiB
JavaScript
function handleClientLoad() {
|
|
gapi.load('client:auth2', initClient);
|
|
}
|
|
|
|
function findCurrent(events) {
|
|
const now = Date.now();
|
|
for (const event of events) {
|
|
const start = new Date(event.start.dateTime).getTime();
|
|
const end = new Date(event.end.dateTime).getTime();
|
|
if (start <= now && end >= now) {
|
|
return event;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function findNext(events) {
|
|
const current = findCurrent(events);
|
|
const currentEnd = current ? new Date(current.end.dateTime).getTime() : null;
|
|
|
|
const now = Date.now();
|
|
for (const event of events) {
|
|
const start = new Date(event.start.dateTime).getTime();
|
|
const end = new Date(event.end.dateTime).getTime();
|
|
if (start >= now && (!current || currentEnd == start)) {
|
|
return event;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function render(events) {
|
|
const curElem = document.getElementById("current");
|
|
const curName = document.getElementById("currentName");
|
|
const curLeft = document.getElementById("currentLeft");
|
|
curElem.classList.remove("break", "event");
|
|
|
|
const current = findCurrent(events);
|
|
if (current) {
|
|
curElem.classList.add("event");
|
|
curName.innerText = current.summary;
|
|
const elapsed = Date.now() - new Date(current.start.dateTime).getTime();
|
|
const duration = new Date(current.end.dateTime).getTime() - new Date(current.start.dateTime).getTime();
|
|
const perc = Math.round(elapsed / duration * 100000) / 1000;
|
|
curElem.style.background = `-webkit-linear-gradient(left, #e28659 ${perc.toString()}%, transparent ${perc.toString()}%)`;
|
|
} else {
|
|
curElem.classList.add("break");
|
|
curName.innerText = '😎 Break';
|
|
}
|
|
|
|
const nextElem = document.getElementById("next");
|
|
const nextName = document.getElementById("nextName");
|
|
nextElem.classList.remove("break", "event");
|
|
|
|
const next = findNext(events);
|
|
if (next) {
|
|
nextElem.classList.add("event");
|
|
nextName.innerText = next.summary;
|
|
} else {
|
|
nextElem.classList.add("break");
|
|
nextName.innerText = '😎 Break';
|
|
}
|
|
}
|
|
|
|
function initClient() {
|
|
gapi.client.init({
|
|
apiKey: 'AIzaSyDzNMBbLqQoSCMiug_7UbUrgaAvnoyzYYU',
|
|
clientId: '969085949455-0vtpi7g173fi63akr1o2eakp9nm1bp4i.apps.googleusercontent.com',
|
|
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest'],
|
|
scope: 'https://www.googleapis.com/auth/calendar.readonly',
|
|
ux_mode: 'redirect',
|
|
}).then(() => {
|
|
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
|
|
return gapi.client.calendar.events.list({
|
|
'calendarId': 'primary',
|
|
'timeMin': (new Date()).toISOString(),
|
|
'showDeleted': false,
|
|
'singleEvents': true,
|
|
'maxResults': 250,
|
|
'orderBy': 'startTime',
|
|
});
|
|
} else {
|
|
gapi.auth2.getAuthInstance().signIn();
|
|
return;
|
|
}
|
|
}).then((response) => {
|
|
setInterval(() => render(response.result.items), 250);
|
|
});
|
|
}
|
|
|