From 275a6b6d2d174baf29c314552c1a92dd818f1025 Mon Sep 17 00:00:00 2001 From: sethburkart123 Date: Wed, 19 Jun 2024 17:48:25 +1000 Subject: [PATCH] refactor: Remove unnecessary code and fix issues with notices --- src/SEQTA.ts | 195 ++++++++++++++++----------------------------------- 1 file changed, 60 insertions(+), 135 deletions(-) diff --git a/src/SEQTA.ts b/src/SEQTA.ts index 4920b93f..b9b2bc32 100644 --- a/src/SEQTA.ts +++ b/src/SEQTA.ts @@ -2134,146 +2134,27 @@ export async function loadHomePage() { xhr2.onreadystatechange = function () { if (xhr2.readyState === 4) { - const NoticesPayload = JSON.parse(xhr2.response) - console.log(NoticesPayload) - const NoticeContainer = document.getElementById('notice-container') - if (NoticesPayload.payload.length === 0) { - if (!NoticeContainer!.innerText) { - // If no notices: display no notices - const dummyNotice = document.createElement('div') - dummyNotice.textContent = 'No notices for today.' - dummyNotice.classList.add('dummynotice') - NoticeContainer!.append(dummyNotice) - } - } else { - if (!NoticeContainer!.innerText) { - for (let i = 0; i < NoticesPayload.payload.length; i++) { - console.log(NoticesPayload.payload[i].title, labelArray.includes(JSON.stringify(NoticesPayload.payload[i].label))) - if (labelArray.includes(JSON.stringify(NoticesPayload.payload[i].label))) { - // Create a div, and place information from json response - const NewNotice = document.createElement('div') - NewNotice.classList.add('notice') - const title = stringToHTML( - '

' + NoticesPayload.payload[i].title + '

' - ) - NewNotice.append(title.firstChild!) - - if (NoticesPayload.payload[i].label_title !== undefined) { - const label = stringToHTML( - '
' + NoticesPayload.payload[i].label_title + '
' - ) - NewNotice.append(label.firstChild!) - } - - const staff = stringToHTML( - '
' + NoticesPayload.payload[i].staff + '
' - ) - NewNotice.append(staff.firstChild!) - // Converts the string into HTML - const content = stringToHTML(NoticesPayload.payload[i].contents.replace(/\[\[[\w]+[:][\w]+[\]\]]+/g, '').replace(/ +/, ' '), true) - for (let i = 0; i < content.childNodes.length; i++) { - NewNotice.append(content.childNodes[i]) - } - // Gets the colour for the top section of each notice - - let colour = NoticesPayload.payload[i].colour - if (typeof (colour) === 'string') { - const rgb = GetThresholdOfColor(colour) - const DarkModeResult = settingsState.DarkMode - if (rgb < 100 && DarkModeResult) { - colour = undefined - } - } - - const colourbar = document.createElement('div') - colourbar.classList.add('colourbar') - colourbar.style.background = 'var(--colour)' - NewNotice.style.cssText = `--colour: ${colour}` - // Appends the colour bar to the new notice - NewNotice.append(colourbar) - // Appends the new notice into the notice container - NoticeContainer!.append(NewNotice) - } - } - } - } + processNotices(xhr2.response, labelArray); } - } - // Data sent as the POST request - const dateControl = document.querySelector('input[type="date"]') as HTMLInputElement - xhr2.send(JSON.stringify({ date: dateControl.value })) - function onInputChange (e: any) { - xhr2.open('POST', `${location.origin}/seqta/student/load/notices?`, true) - xhr2.setRequestHeader('Content-Type', 'application/json; charset=utf-8') - xhr2.send(JSON.stringify({ date: e.target.value })) + }; + + const dateControl = document.querySelector('input[type="date"]') as HTMLInputElement; + xhr2.send(JSON.stringify({ date: dateControl.value })); + + function onInputChange(e: any) { + xhr2.open('POST', `${location.origin}/seqta/student/load/notices?`, true); + xhr2.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); + xhr2.send(JSON.stringify({ date: e.target.value })); + xhr2.onreadystatechange = function () { if (xhr2.readyState === 4) { - const NoticesPayload = JSON.parse(xhr2.response) - const NoticeContainer = document.getElementById('notice-container') - if (NoticesPayload.payload.length === 0) { - if (!NoticeContainer!.innerText) { - // If no notices: display no notices - const dummyNotice = document.createElement('div') - dummyNotice.textContent = 'No notices for today.' - dummyNotice.classList.add('dummynotice') - NoticeContainer!.append(dummyNotice) - } - } else { - document.querySelectorAll('.notice').forEach(e => e.remove()) - - for (let i = 0; i < NoticesPayload.payload.length; i++) { - if (labelArray.includes(JSON.stringify(NoticesPayload.payload[i].label))) { - // Create a div, and place information from json response - const NewNotice = document.createElement('div') - NewNotice.classList.add('notice') - const title = stringToHTML( - '

' + NoticesPayload.payload[i].title + '

' - ) - NewNotice.append(title.firstChild!) - - if (NoticesPayload.payload[i].label_title !== undefined) { - const label = stringToHTML( - '
' + NoticesPayload.payload[i].label_title + '
' - ) - NewNotice.append(label.firstChild!) - } - - const staff = stringToHTML( - '
' + NoticesPayload.payload[i].staff + '
' - ) - NewNotice.append(staff.firstChild!) - // Converts the string into HTML - const content = stringToHTML(NoticesPayload.payload[i].contents.replace(/\[\[[\w]+[:][\w]+[\]\]]+/g, '').replace(/ +/, ' '), true) - for (let i = 0; i < content.childNodes.length; i++) { - NewNotice.append(content.childNodes[i]) - } - // Gets the colour for the top section of each notice - - let colour = NoticesPayload.payload[i].colour - if (typeof (colour) === 'string') { - const rgb = GetThresholdOfColor(colour) - const DarkModeResult = settingsState.DarkMode - if (rgb < 100 && DarkModeResult) { - colour = undefined - } - } - - const colourbar = document.createElement('div') - colourbar.classList.add('colourbar') - colourbar.style.background = 'var(--colour)' - NewNotice.style.cssText = `--colour: ${colour}` - // Appends the colour bar to the new notice - NewNotice.append(colourbar) - // Appends the new notice into the notice container - NoticeContainer!.append(NewNotice) - } - } - } + processNotices(xhr2.response, labelArray); } - } + }; } - dateControl.addEventListener('input', onInputChange) - + + dateControl.addEventListener('input', onInputChange); + if (settingsState.notificationcollector) { enableNotificationCollector() } @@ -2317,6 +2198,50 @@ export async function loadHomePage() { await CreateUpcomingSection(CurrentAssessments, activeSubjects) } +function processNotices(responseText: any, labelArray: any) { + const NoticesPayload = JSON.parse(responseText); + const NoticeContainer = document.getElementById('notice-container'); + if (NoticesPayload.payload.length === 0) { + if (!NoticeContainer?.innerText) { + const dummyNotice = document.createElement('div'); + dummyNotice.textContent = 'No notices for today.'; + dummyNotice.classList.add('dummynotice'); + NoticeContainer?.append(dummyNotice); + } + } else { + if (!NoticeContainer?.innerText) { + document.querySelectorAll('.notice').forEach(e => e.remove()); + + NoticesPayload.payload.forEach((notice: any) => { + if (labelArray.includes(JSON.stringify(notice.label))) { + let htmlContent = ` +
+

${notice.title}

+ ${notice.label_title !== undefined ? `
${notice.label_title}
` : ''} +
${notice.staff}
+ ${notice.contents.replace(/\[\[[\w]+[:][\w]+[\]\]]+/g, '').replace(/ +/, ' ')} +
+
+ `; + const NewNotice = stringToHTML(htmlContent).firstChild; + + let colour = notice.colour; + if (typeof colour === 'string') { + const rgb = GetThresholdOfColor(colour); + const DarkModeResult = settingsState.DarkMode; + if (rgb < 100 && DarkModeResult) { + colour = undefined; + } + } + (NewNotice as HTMLElement).style.cssText = `--colour: ${colour}`; + + NoticeContainer!.append(NewNotice!); + } + }); + } + } +} + export function addShortcuts(shortcuts: any) { for (let i = 0; i < shortcuts.length; i++) { const currentShortcut = shortcuts[i]