var latestDocumentIndex = 0; var latestDocumentMobileIndex = 0; var latestDocuments = []; var lastPageIndex = 0; var currentPage = 0; var totalDocuments = 0 var lastPage = 0; // makes a POST request to a specified endpoint for filters and then parse the JSON response. Uses these documents to build the latestDocumentContainer and latestDocumentMobileContainer. async function getLatestDocuments() { var request = await SendRequest('POST', baseUrl + 'savedFilters', { GetDocs: true }); var json = JSON.parse(request.response); var docs = await json2array(json); latestDocuments = docs; lastPageIndex = Math.ceil(docs.length / 7); totalDocuments = docs.length; if (totalDocuments !== 0) { await buildLatestDocuments(0); await buildLatestDocumentsMobile(); $("#latestDocumentWidget-mobile").remove(); } else { $("#latestDocumentLoadingText").text("No documents published"); $("#latestDocumentMobileLoadingText").text("No documents published"); } } // updates the current page number and sets the page numbers in the navigation bar for latest documents widget async function calculateCurrentPage(pageNumber) { var amountOfPages = lastPageIndex; if (pageNumber === 0) { pageNumber++; } $('#pageCount').text('Page ' + pageNumber + ' of ' + lastPageIndex); currentPage = pageNumber; } // moves the user to the next page and rebuilds the latestDocumentContainer async function PageNext(addition) { if (currentPage < (lastPageIndex)) { addSkeletonLoader(); latestDocumentIndex = latestDocumentIndex + addition; await rebuildLatestDocuments(++currentPage); } } // moves the user to the previous page and rebuilds the latestDocumentContainer async function PagePrevious(subtraction) { if (currentPage != 1) { addSkeletonLoader(); latestDocumentIndex = latestDocumentIndex - subtraction; await rebuildLatestDocuments(--currentPage); } } // takes the user back to the first page and rebuilds the latestDocumentContainer async function PageFirst() { if (currentPage != 1) { addSkeletonLoader(); latestDocumentIndex = 0; await rebuildLatestDocuments(0); } } // takes the user to the last page and rebuilds the latestDocumentContainer async function PageLast() { if (currentPage < lastPageIndex) { addSkeletonLoader(); latestDocumentIndex = totalDocuments - 2; lastPage = (lastPageIndex); await rebuildLatestDocuments(lastPage); } } /// Desktop View // takes a pageNumber as a parameter and builds a set of HTML elements for the latestDocumentPane. It loops through 7 documents starting from the most recent and formats their data. async function buildLatestDocuments(pageNumber) { setTimeout(async function () { var startArray = latestDocumentIndex; var docs = ""; var TodayAdded = false; var YesterdayAdded = false; var EarlyAdded = false; docs += `
${latestDocumentMenu}
`; for (let index = 0; index < 7; index++) { if (startArray >= latestDocuments.length) { break; } const document = latestDocuments[startArray]; var publisheDate = new Date(document["publishedDate"]); var documentedDate = new Date(document["documentedDate"]); if (!TodayAdded && 1 === document["dateType"]) { TodayAdded = true; docs += '
Today
' } if (!YesterdayAdded && 2 === document["dateType"]) { YesterdayAdded = true; docs += '
Yesterday
' } if (!EarlyAdded && 3 === document["dateType"]) { EarlyAdded = true; docs += '
Earlier
' } docs += '
' + '
' + '
' + '' + '
' + '
' + '
' + '
' + '

' + document["title"] + '

' + '
' + '
' + '
' + 'Client: ' + document["clientName"] + '' + ' Category: ' + document["category"] + '' + '
' + '
' + '
' + 'Documented: ' + documentedDate.toLocaleDateString("en-AU", { day: "numeric", year: "numeric", month: "long" }) + '' + ' Published: ' + publisheDate.toLocaleDateString("en-AU", { day: "numeric", year: "numeric", month: "long", hour: "2-digit", minute: "2-digit" }).replace(' at', '') + '' + '
'; startArray++; } docs += `
`; $('#latestDocumentContainer').empty(); $("#latestDocumentContainer").append(docs); addSkeletonLoader(); await calculateCurrentPage(pageNumber); }, 2000); } // takes a pageNumber as a parameter and rebuilds the latestDocumentData element based on the most recent documents. async function rebuildLatestDocuments(pageNumber) { setTimeout(async function () { var startArray = latestDocumentIndex; var docs = ""; var count = 1 var TodayAdded = false; var YesterdayAdded = false; var EarlyAdded = false; for (let index = 0; index < 7; index++) { if (startArray >= latestDocuments.length) { break; } const document = latestDocuments[startArray]; if (!TodayAdded && 1 === document["dateType"]) { TodayAdded = true; docs += '
Today
' } if (!YesterdayAdded && 2 === document["dateType"]) { YesterdayAdded = true; docs += '
Yesterday
' } if (!EarlyAdded && 3 === document["dateType"]) { EarlyAdded = true; docs += '
Earlier
' } docs += '
' + '
' + '
' + '' + '
' + '
' + '
' + '
' + '

' + document["title"] + '

' + '
' + '
' + '
' + 'Client: ' + document["clientName"] + '' + ' Category: ' + document["category"] + '' + '
' + '
' + '
' + 'Documented: ' + document["documentedDateLabel"] + ''+ ' Published: ' + document["publishedDateLabel"] + '' docs += '
' startArray++; } $('#latestDocumentData').empty(); $("#latestDocumentData").append(docs); await calculateCurrentPage(pageNumber); addSkeletonLoader(); }, 2000); } // takes a JSON object as an argument, and it converts its values into an array. async function json2array(json) { var result = []; var keys = Object.keys(json); keys.forEach(function (key) { result.push(json[key]); }); return result; } // selects all the checkboxes on the page. async function SelectAll() { $('input:checkbox').prop('checked', true); } // deselects all checkboxes on the page. async function ClearAll() { $('input:checkbox').prop('checked', false); } /// Desktop View // takes a pageNumber as a parameter and builds a set of HTML elements for the latestDocumentPane. It loops through 7 documents starting from the most recent and formats their data. async function buildLatestDocuments(pageNumber) { setTimeout(async function () { var startArray = latestDocumentIndex; var docs = ""; var TodayAdded = false; var YesterdayAdded = false; var EarlyAdded = false; var todayType = 1; var yesterType = 2; var earlierType = 3; docs += `
${latestDocumentMenu}
`; for (let index = 0; index < 7; index++) { if (startArray >= latestDocuments.length) { break; } const document = latestDocuments[startArray]; if (!TodayAdded && todayType === document["dateType"]) { TodayAdded = true; docs += '
Today
' } if (!YesterdayAdded && yesterType === document["dateType"]) { YesterdayAdded = true; docs += '
Yesterday
' } if (!EarlyAdded && earlierType === document["dateType"]) { EarlyAdded = true; docs += '
Earlier
' } docs += '
' + '
' + '
' + '' + '
' + '
' + '
' + '
' + '

' + document["title"] + '

' + '
' + '
' + '
' + 'Client: ' + document["clientName"] + '' + ' Category: ' + document["category"] + '' + '
' + '
' + '
' + 'Documented: ' + document["documentedDateLabel"] + ''+ ' Published: ' + document["publishedDateLabel"] + '' + '
'; startArray++; } docs += `
`; $('#latestDocumentContainer').empty(); $("#latestDocumentContainer").append(docs); addSkeletonLoader(); await calculateCurrentPage(pageNumber); }, 2000); } function formatDocumentDate(date) { //date = convertLatestDocumentsDateToUCT(date); var year = date.getFullYear(); var month = date.toLocaleString('default', { month: 'long' }); var day = date.getDate(); return `${day} ${month} ${year}` } function formatPublishDate(oldDate) { var date = new Date(oldDate.getTime() - (oldDate.getTimezoneOffset() * 60000) - (10 * 60 * 60000)); var year = date.getFullYear(); var month = date.toLocaleString('default', { month: 'long' }); var day = date.getDate(); var hours = date.getHours(); var minutes = date.getMinutes(); var ampm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12; hours = hours ? hours : 12; // the hour '0' should be '12' minutes = minutes < 10 ? '0'+minutes : minutes; var time = hours + ':' + minutes + ' ' + ampm; return `${day} ${month} ${year} ${time}` } function convertLatestDocumentsDateToUCT(date) { if (date != null && date != "") { const offsetInMinutes = new Date().getTimezoneOffset(); const offsetInMilliseconds = offsetInMinutes * 60 * 1000; return new Date((offsetInMilliseconds * -1) + (date.getTime())); } return date; } /// Mobile View // creates elements for the mobile view of the latestDocumentPane. async function buildLatestDocumentsMobile() { setTimeout(async function () { var startArray = latestDocumentMobileIndex; var docs = ""; var TodayAdded = false; var YesterdayAdded = false; var EarlyAdded = false; for (let index = 0; index < 6; index++) { if (startArray >= latestDocuments.length) { $("#viewMoreLatestDocumentsButton").empty(); break; } const document = latestDocuments[startArray]; var publisheDate = new Date(document["publishedDate"]); var documentedDate = new Date(document["documentedDate"]); if (!TodayAdded && 1 === document["dateType"]) { TodayAdded = true; docs += '
Today
' } if (!YesterdayAdded && 2 === document["dateType"]) { YesterdayAdded = true; docs += '
Yesterday
' } if (!EarlyAdded && 3 === document["dateType"]) { EarlyAdded = true; docs += '
Earlier
' } docs += '
' + '
' + '
' + document["title"] + '
' + '
' + '
' + '
Client: ' + document["clientName"] + '' + '
' + '
' + '
' + '
Category: ' + document["category"] + '
' + '
' + '
' + '
Documented: ' + document["documentedDateLabel"] + '
' + '
' + '
' + '
Published: ' + document["publishedDateLabel"] + '' + '
' + '
' + `
`+ '
' startArray++; } $("#latestDocumentMobileContainer").append(docs); latestDocumentMobileIndex = startArray; }, 2000); } async function viewMoreLatestDocumentsMobile() { var startArray = latestDocumentMobileIndex; var docs = ""; var TodayAdded = false; var YesterdayAdded = false; var EarlyAdded = false; for (let index = 0; index < 6; index++) { if (startArray >= latestDocuments.length) { $("#viewMoreLatestDocumentsButton").empty(); break; } const document = latestDocuments[startArray]; var publisheDate = new Date(document["publishedDate"]); var documentedDate = new Date(document["documentedDate"]); if (!TodayAdded && 1 === document["dateType"]) { TodayAdded = true; docs += '
Today
' } if (!YesterdayAdded && 2 === document["dateType"]) { YesterdayAdded = true; docs += '
Yesterday
' } if (!EarlyAdded && 3 === document["dateType"]) { EarlyAdded = true; docs += '
Earlier
' } docs += '
' + '
' + '
' + document["title"] + '
' + '
' + '
' + '
Client: ' + document["clientName"] + '' + '
' + '
' + '
' + '
Category: ' + document["category"] + '
' + '
' + '
' + '
Documented: ' + document["documentedDateLabel"] + '
' + '
' + '
' + '
Published: ' + document["publishedDateLabel"] + '' + '
' + '
' + `
`+ '
' startArray++; } if (startArray >= latestDocuments.length) { $("#viewMoreLatestDocumentsButton").empty(); } $("#latestDocumentMobileContainer").append(docs); latestDocumentMobileIndex = startArray; } // adds a loading effect to the page while the latestDocumentData loads. function addSkeletonLoader() { if ($("#latestDocumentData").hasClass('loading')) { $("#latestDocumentData").removeClass('loading'); } else { $("#latestDocumentData").addClass('loading'); $(function () { var itemsToHide = document.getElementsByClassName('latest-document-data-checkbox cursor-pointer'); for (let index = 0; index < itemsToHide.length; index++) { itemsToHide[index].style = "display: none"; } $("#latestDocumentCheckBox").addClass("hide-check-box"); $("#latestDocumentData, #latestDocumentData *") // selects the element and all element nodes inside it .contents() // selects all child nodes including tags, comments and text .filter(function () { return this.nodeType === Node.TEXT_NODE; // filter text nodes }).remove(); // remove text }); } }