$(document).ready(() => { window.addEventListener('message', function (event) { let data = event.data; if (data.update == 'newCall') { addNewCall(data.callID, data.timer, data.data, data.isPolice); } }); }); const MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ]; function getFormattedDate(date, prefomattedDate = false, hideYear = false) { const day = date.getDate(); const month = MONTH_NAMES[date.getMonth()]; const year = date.getFullYear(); const hours = date.getHours(); let minutes = date.getMinutes(); if (minutes < 10) { minutes = `0${minutes}`; } if (prefomattedDate) { return `${prefomattedDate} at ${hours}:${minutes}`; } if (hideYear) { return `${day}. ${month} at ${hours}:${minutes}`; } return `${day}. ${month} ${year}. at ${hours}:${minutes}`; } function timeAgo(dateParam) { if (!dateParam) { return null; } const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam); const DAY_IN_MS = 86400000; const today = new Date(); const yesterday = new Date(today - DAY_IN_MS); const seconds = Math.round((today - date) / 1000); const minutes = Math.round(seconds / 60); const isToday = today.toDateString() === date.toDateString(); const isYesterday = yesterday.toDateString() === date.toDateString(); const isThisYear = today.getFullYear() === date.getFullYear(); if (seconds < 5) { return 'Just Now'; } else if (seconds < 60) { return `${seconds} Seconds ago`; } else if (seconds < 90) { return 'About a minute ago'; } else if (minutes < 60) { return `${minutes} Minutes ago`; } else if (isToday) { return getFormattedDate(date, 'Today'); } else if (isYesterday) { return getFormattedDate(date, 'Yesterday'); } else if (isThisYear) { return getFormattedDate(date, false, true); } return getFormattedDate(date); } function addNewCall(callID, timer, info, isPolice) { const prio = info['priority']; let DispatchItem; if (info['isDead']) { DispatchItem = `
#${callID}
${info.dispatchCode}
${info.dispatchMessage}
`; } else { DispatchItem = `
#${callID}
${info.dispatchCode}
${info.dispatchMessage}
`; } // Above we are defining a default dispatch item and then we will append the data we have been sent. if (info['time']) { DispatchItem += `
${timeAgo( info['time'] )}
`; } if (info['firstStreet']) { DispatchItem += `
${info['firstStreet']}
`; } if (info['heading']) { DispatchItem += `
${info['heading']}
`; } if (info['callsign']) { DispatchItem += `
${info['callsign']}
`; } if (info['doorCount']) { DispatchItem += `
${info['doorCount']}
`; } if (info['weapon']) { DispatchItem += `
${info['weapon']}
`; } if (info['camId']) { DispatchItem += `
${info['camId']}
`; } if (info['gender']) { DispatchItem += `
${info['gender']}
`; } if (info['model'] && info['plate']) { DispatchItem += `
${info['model']}${info['plate']}
`; } else if (info['plate']) { DispatchItem += `
${info['plate']}
`; } else if (info['model']) { DispatchItem += `
${info['model']}
`; } if (info['firstColor']) { DispatchItem += `
${info['firstColor']}
`; } if (info['automaticGunfire'] == true) { DispatchItem += `
Automatic Gunfire
`; } if (info['name'] && info['number']) { DispatchItem += `
${info['name']}${info['number']}
`; } else if (info['number']) { DispatchItem += `
${info['number']}
`; } else if (info['name']) { DispatchItem += `
${info['name']}
`; } if (info['information']) { DispatchItem += `
${info['information']}
`; } DispatchItem += `
`; $('.dispatch-holder').prepend(DispatchItem); var timer = 4000; if (prio == 1) { timer = 12000; } else if (prio == 2) { timer = 9000; } $(`.${callID}`).addClass('animate__backInRight'); setTimeout(() => { $(`.${callID}`).addClass('animate__backOutRight'); setTimeout(() => { $(`.${callID}`).remove(); }, 1000); }, timer || 4500); }