|
| 1 | +<script> |
| 2 | + // Make the calendar globally acecssible so it can be |
| 3 | + // updated with the theme switcher. If this is gross, |
| 4 | + // it's possible to just listen for changes to the root |
| 5 | + // here. |
| 6 | + var calendar; |
| 7 | + |
| 8 | + let category_cnt = 0; |
| 9 | + const CALENDAR_CATEGORIES = {{ site.data.calendar.categories | jsonify }}.map((category) => { |
| 10 | + return { id: String(category_cnt++), ...category }; |
| 11 | + }); |
| 12 | + |
| 13 | + function loadGoogleCalendar(calendar, options, cb) { |
| 14 | + const request = gapi.client.calendar.events.list({ |
| 15 | + calendarId: options.calendarID, |
| 16 | + maxResults: 2500, |
| 17 | + orderBy: 'startTime', |
| 18 | + singleEvents: true, |
| 19 | + }); |
| 20 | + request.execute((resp) => { |
| 21 | + const events = []; |
| 22 | + if (resp.error) { |
| 23 | + console.error(`[calendar] error loading ${options.calendarID}: ${resp.error.message}`); |
| 24 | + } else { |
| 25 | + for (const item of resp.items) { |
| 26 | + if (item.visibility === 'private') continue; |
| 27 | + |
| 28 | + let title = item.summary; |
| 29 | + |
| 30 | + // remove prefix (e.g. class name and sem) from every event |
| 31 | + // added by peyrin Jan 2025 |
| 32 | + title = title.replace('{{ site.data.calendar.remove_prefix }}', ''); |
| 33 | + |
| 34 | + let category = undefined; |
| 35 | + if (!item.summary) { |
| 36 | + console.error('error with item', item); |
| 37 | + } else { |
| 38 | + for (const potentialCategory of CALENDAR_CATEGORIES) { |
| 39 | + if (potentialCategory.match) { |
| 40 | + let expr = new RegExp(potentialCategory.match); |
| 41 | + if (expr.test(title.toLowerCase())) { |
| 42 | + category = potentialCategory.id; |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const event = { |
| 50 | + id: item.id, |
| 51 | + calendarId: options.calendarID, |
| 52 | + title, |
| 53 | + body: item.description, |
| 54 | + location: item.location ?? '', |
| 55 | + start: item.start.dateTime, |
| 56 | + end: item.end.dateTime, |
| 57 | + category, |
| 58 | + links: { |
| 59 | + left: item.htmlLink |
| 60 | + ? { |
| 61 | + text: 'Open in Google Calendar', |
| 62 | + url: item.htmlLink, |
| 63 | + } |
| 64 | + : undefined, |
| 65 | + right: item.htmlLink |
| 66 | + ? { |
| 67 | + text: 'Copy to your calendar', |
| 68 | + url: `https://calendar.google.com/calendar/u/0/r/eventedit/copy/${item.htmlLink.split('eid=')[1]}`, |
| 69 | + } |
| 70 | + : undefined, |
| 71 | + }, |
| 72 | + }; |
| 73 | + |
| 74 | + if (options.addProps) { |
| 75 | + const obj = options.addProps(item, event); |
| 76 | + for (const [key, val] of Object.entries(obj)) { |
| 77 | + if (val !== undefined) { |
| 78 | + event[key] = val; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + events.push(event); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + cb(events); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + /* global initCalendar */ |
| 91 | + window.initCalendar = function initCalendar() { |
| 92 | + if (!document.getElementById('calendarContainer')) return; |
| 93 | + |
| 94 | + const berkeleyTimeZoneName = 'America/Los_Angeles'; |
| 95 | + const hourStart = 8; // in Berkeley timezone |
| 96 | + const hourEnd = 22; |
| 97 | + |
| 98 | + calendar = window.SiteCalendar.Calendar('#calendarContainer', { |
| 99 | + currentMode: window.screen.width >= 760 ? 'week' : 'day', |
| 100 | + showLocal: true, |
| 101 | + defaultTimezone: berkeleyTimeZoneName, |
| 102 | + defaultTimezoneName: 'Berkeley', |
| 103 | + startTime: hourStart, |
| 104 | + endTime: hourEnd, |
| 105 | + weekends: false, |
| 106 | + colorScheme: window.getComputedStyle(document.documentElement).getPropertyValue('color-scheme'), |
| 107 | + categories: CALENDAR_CATEGORIES, |
| 108 | + }); |
| 109 | + |
| 110 | + const allEvents = []; |
| 111 | + |
| 112 | + const googleAPIKey = '{{ site.data.calendar.google_api_key }}'; |
| 113 | + {% if site.data.calendar.calendars %} |
| 114 | + const calendarInfoArr = {{ site.data.calendar.calendars | jsonify }}; |
| 115 | + {% else %} |
| 116 | + const calendarInfoArr = [ |
| 117 | + { |
| 118 | + id: '{{ site.data.calendar.google_calendar_id }}', |
| 119 | + name: 'Events', |
| 120 | + }, |
| 121 | + ]; |
| 122 | + {% endif %} |
| 123 | + |
| 124 | + const buttonGroups = [ |
| 125 | + ...new Set(CALENDAR_CATEGORIES.map((category) => category.name.toLowerCase().replaceAll(' ', '-'))), |
| 126 | + ]; |
| 127 | + const inactiveGroups = []; |
| 128 | + |
| 129 | + let loadCount = calendarInfoArr.length; |
| 130 | + function onCalendarLoad(events) { |
| 131 | + for (const event of events) { |
| 132 | + allEvents.push(event); |
| 133 | + } |
| 134 | + loadCount -= 1; |
| 135 | + if (loadCount == 0) { |
| 136 | + calendar.addEvents(allEvents); |
| 137 | + |
| 138 | + // When we're done loading, activate our filter buttons. |
| 139 | + document.querySelectorAll('#calendarControls button').forEach((elem) => { |
| 140 | + let action = elem.getAttribute('data-action'); |
| 141 | + if (action != undefined) { |
| 142 | + elem.classList.add('active'); |
| 143 | + } |
| 144 | + |
| 145 | + elem.addEventListener('click', (e) => { |
| 146 | + e.preventDefault(); |
| 147 | + |
| 148 | + let group = elem.getAttribute('data-action').replace('toggle-category-', ''); |
| 149 | + if (action != undefined && !inactiveGroups.includes(group)) { |
| 150 | + elem.classList.remove('active'); |
| 151 | + inactiveGroups.push(group); |
| 152 | + } else { |
| 153 | + elem.classList.add('active'); |
| 154 | + const idx = inactiveGroups.indexOf(group); |
| 155 | + inactiveGroups.splice(idx, 1); |
| 156 | + } |
| 157 | + |
| 158 | + // Update the filter, since the only way to rerun it is to |
| 159 | + // update it. |
| 160 | + calendar.updateFilter((event) => { |
| 161 | + return ( |
| 162 | + event.category == undefined || |
| 163 | + inactiveGroups.includes(event.category.name.toLowerCase().replaceAll(' ', '-')) == false |
| 164 | + ); |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + gapi.load('client', () => { |
| 172 | + gapi.client.setApiKey(googleAPIKey); |
| 173 | + gapi.client.load('calendar', 'v3', () => { |
| 174 | + for (const { id: calendarID } of calendarInfoArr) { |
| 175 | + loadGoogleCalendar( |
| 176 | + calendar, |
| 177 | + { |
| 178 | + calendarID, |
| 179 | + }, |
| 180 | + onCalendarLoad |
| 181 | + ); |
| 182 | + } |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + const calendarTable = document.getElementById('calendarTable'); |
| 187 | + if (calendarTable) { |
| 188 | + calendarTable.classList.remove('d-none'); |
| 189 | + |
| 190 | + for (const calendarInfo of calendarInfoArr) { |
| 191 | + const row = calendarTable.insertRow(); |
| 192 | + const nameCell = row.insertCell(); |
| 193 | + nameCell.innerHTML = calendarInfo.name; |
| 194 | + const linkCell = row.insertCell(); |
| 195 | + const linkElem = document.createElement('a'); |
| 196 | + linkElem.href = `https://calendar.google.com/calendar/embed?src=${encodeURIComponent(calendarInfo.id)}`; |
| 197 | + linkElem.innerText = 'Google Calendar'; |
| 198 | + linkCell.appendChild(linkElem); |
| 199 | + } |
| 200 | + } |
| 201 | + }; |
| 202 | +</script> |
| 203 | + |
| 204 | +<link rel="stylesheet" href="{{ '/assets/calendar/calendar.min.css' | relative_url }}"> |
| 205 | +<link rel="stylesheet" href="{{ '/assets/calendar/calendar-overrides.css' | relative_url }}"> |
| 206 | +<script defer src="{{ '/assets/calendar/calendar.min.js' | relative_url }}"></script> |
| 207 | + |
| 208 | +<script |
| 209 | + defer |
| 210 | + src="https://apis.google.com/js/api.js" |
| 211 | + crossorigin="anonymous" |
| 212 | + referrerpolicy="no-referrer" |
| 213 | +></script> |
0 commit comments