From af251c32ce9e44e690443e62d04d598d9dcacbf6 Mon Sep 17 00:00:00 2001 From: davidtrussler Date: Tue, 16 Jan 2024 16:54:49 +0000 Subject: [PATCH] Add logic for component --- ...{layout-footer.js => status-page-alert.js} | 71 +++++++------------ 1 file changed, 27 insertions(+), 44 deletions(-) rename app/assets/javascripts/govuk_publishing_components/components/{layout-footer.js => status-page-alert.js} (69%) diff --git a/app/assets/javascripts/govuk_publishing_components/components/layout-footer.js b/app/assets/javascripts/govuk_publishing_components/components/status-page-alert.js similarity index 69% rename from app/assets/javascripts/govuk_publishing_components/components/layout-footer.js rename to app/assets/javascripts/govuk_publishing_components/components/status-page-alert.js index ccd4434527..87dfb4e7b5 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/layout-footer.js +++ b/app/assets/javascripts/govuk_publishing_components/components/status-page-alert.js @@ -2,7 +2,7 @@ window.GOVUK = window.GOVUK || {} window.GOVUK.Modules = window.GOVUK.Modules || {}; (function (Modules) { - function LayoutFooter ($module) { + function StatusPageAlert ($module) { this.$module = $module this.items = {} this.incidentCurrent = [] @@ -11,9 +11,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; this.maintenanceFuture = [] this.maintenancePast = [] // this.xmlString = fetch("file:///status.xml") - - // console.log(this.xmlString) - this.xmlString = ` @@ -34,16 +31,19 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; `; } - LayoutFooter.prototype.init = function () { - console.log('LayoutFooter init!') + StatusPageAlert.prototype.init = function () { + console.log('StatusPageAlert init!!') var parser = new DOMParser(); var xmlDoc = parser.parseFromString(this.xmlString, "text/xml"); - this.items = xmlDoc.querySelectorAll("item"); + + console.log('xmlDoc: ', xmlDoc) + + this.items = xmlDoc.documentElement.querySelectorAll("item"); this.itemsCategorise() } - LayoutFooter.prototype.itemsCategorise = function() { + StatusPageAlert.prototype.itemsCategorise = function() { var now = new Date(); this.items.forEach((item) => { @@ -73,47 +73,30 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; this.addAlert() } - LayoutFooter.prototype.addAlert = function() { - var status = 0 - var href = document.location.href - var alertLocation = document.querySelector('#status-page-alert') + StatusPageAlert.prototype.addAlert = function() { + var alertLocation = this.$module var alertMessages = alertLocation.querySelectorAll('.alert-message') - // Demo - if (href.includes('?')) { - status = document.location.href.split('?')[1].split('=')[1] - - if (status == '4') { - alertMessages[3].style.display = "block" - } else if (status == '2') { - alertMessages[1].style.display = "block" - } else if (status == '3') { - alertMessages[2].style.display = "block" - } else if (status == '1') { - alertMessages[0].style.display = "block" - } - } else { - if (this.maintenanceFuture.length > 0 && (this.maintenanceCurrent.length > 0 || this.incidentCurrent.length > 0)) { - alertMessages[3].style.display = "block" - } else if (this.maintenanceCurrent.length > 0 || this.incidentCurrent.length > 0) { - alertMessages[1].style.display = "block" - } else if (this.maintenanceFuture.length > 0 && this.maintenanceCurrent.length == 0 && this.incidentCurrent.length == 0) { - var start = new Date(this.maintenanceFuture[0].querySelector('pubDate').textContent) - var end = new Date(this.maintenanceFuture[0].querySelector('maintenanceEndDate').textContent) + if (this.maintenanceFuture.length > 0 && (this.maintenanceCurrent.length > 0 || this.incidentCurrent.length > 0)) { + alertMessages[3].style.display = "block" + } else if (this.maintenanceCurrent.length > 0 || this.incidentCurrent.length > 0) { + alertMessages[1].style.display = "block" + } else if (this.maintenanceFuture.length > 0 && this.maintenanceCurrent.length == 0 && this.incidentCurrent.length == 0) { + var start = new Date(this.maintenanceFuture[0].querySelector('pubDate').textContent) + var end = new Date(this.maintenanceFuture[0].querySelector('maintenanceEndDate').textContent) - var startDate = start.toLocaleDateString('en-GB') - var endDate = end.toLocaleDateString('en-GB') - var startTime = start.toLocaleTimeString('en-GB') - var endTime = end.toLocaleTimeString('en-GB') + var startDate = start.toLocaleDateString('en-GB') + var endDate = end.toLocaleDateString('en-GB') + var startTime = start.toLocaleTimeString('en-GB') + var endTime = end.toLocaleTimeString('en-GB') - alertMessages[2].querySelector('.startDateTime').textContent = `${startDate} at ${startTime}` - alertMessages[2].querySelector('.startEndTime').textContent = `${endDate} at ${endTime}` - alertMessages[2].style.display = "block" - } else { - alertMessages[0].style.display = "block" - } + alertMessages[2].querySelector('.startDateTime').textContent = `${startDate} at ${startTime}` + alertMessages[2].querySelector('.startEndTime').textContent = `${endDate} at ${endTime}` + alertMessages[2].style.display = "block" + } else { + alertMessages[0].style.display = "block" } } - Modules.LayoutFooter = LayoutFooter + Modules.StatusPageAlert = StatusPageAlert })(window.GOVUK.Modules)