/* ==PAYMENT MAINTENANCE WINDOW LIBRARY JAVASCRIPT== Enables target portal to utilize payment-related maintenance specified on cdph-maintenance environment. Ver 1.0.0 - Base version Changelog: >>2024/08/09 - Base version 1.0.0 established. */ //----------Reserved global variables-------------- var PAYMW_maintenanceURL = "https://cdph-maintenance.cdph.ca.gov"; var PAYMW_currentUser; //Set outside after including the script but before calling any function. Need liquid to set email. var PAYMW_MessageObject; /*PAYMW_MessageObject current structure: data: { type: "PaymentMaintenance" status: either "DisplayPaymentMaintenance" or "StartPaymentMaintenance". States the maintenance evaluation. content: returns the HTML/message content stated in the maintenance manager window exemptionArray: array of emails of users that should be exempt from the payment maintenance behavior startDate: Start Date and Time of when the maintenance begins. endDate: End Date and Time of when the maintenance ends. } */ //------------------FUNCTIONS---------------------- /* ============================ Function PAYMW_InitializePaymentMaintenance -Prerequisite: None -Use: Should be called first, ideally in the header. Initializes the global object "PAYMW_MessageObject" which returns the response from the maintenance site on whether maintenance is active or not. However, it's possible to only run this function, and the developer is able to manipulate the object such as customizing the message/content. This is asynchronous so to use it, should use then() and wrap following synchronous actions inside. -Technical details: Appends an iframe to the body to load up the maintenance site and receives a message. The message is then stored in the global variable "PAYMW_MessageObject" for further use. -Usage: No parameters. Use as a promise and use then() to call the other functions. ============================ */ async function PAYMW_InitializePaymentMaintenance(){ //Prepping event listener to listen for iframe loading the maintenance site. //If successful, PAYMW_MessageObject will contain the maintenance info for this site. window.addEventListener("message", function(message) { if(message.origin == PAYMW_maintenanceURL && message.data.type == "PaymentMaintenance") { PAYMW_MessageObject = message; //Data contains status and content. } }); var targetIframeID = "PAYMW_iframe"; if(!document.getElementById(targetIframeID)){ var cdphmwNode = document.createElement("span"); cdphmwNode.innerHTML=` `; document.body.appendChild(cdphmwNode); } var targetIframe = document.getElementById(targetIframeID); //Changing the iframe source to query the maintenance site for maintenance info regarding this site. const wait_iframe_Reload = () => ( new Promise(resolve => targetIframe.addEventListener('load', () => resolve()) )); targetIframe.src = ""+PAYMW_maintenanceURL+"/paymw/?System=" + document.location.hostname.split(".")[0]; await wait_iframe_Reload(); return wait_iframe_Reload; }; /* ============================ Function PAYMW_DefaultBehavior -Prerequisite: --PAYMW_InitializePaymentMaintenance must have been executed before this. --PAYMW_currentUser should be set in the header or before calling this function. --Element with ID of "PAYMW_banner_ongoing" must exist. --Element with ID of "PAYMW_section_ongoing" must exist. -Use: Will run the default set behavior for payment maintenance specified. It will look for the reserved named IDs of "PAYMW_banner_ongoing", and "PAYMW_section_ongoing". Default behavior results in a banner in the header stating the duration, and the specified section will be completely replaced. -Technical details: Calls for the default behavior functions but does not allow the IDs of the target elements to be specified. It is assumed the target elements have already been set-up. -Usage: No parameters. ============================ */ function PAYMW_DefaultBehavior(){ //Commented out. Warning behavior not in use. /* var defaultBannerWarningID = "PAYMW_banner_warning"; PAYMW_DefaultBehavior_Warning(defaultBannerWarningID); */ var defaultBannerOngoingID = "PAYMW_banner_ongoing"; var defaultSectionOngoingID = "PAYMW_section_ongoing"; PAYMW_DefaultBehavior_Ongoing(defaultBannerOngoingID,defaultSectionOngoingID); }; /* ============================ Function PAYMW_DefaultBehavior_Ongoing -Prerequisite: --PAYMW_InitializePaymentMaintenance must have been executed before this. --PAYMW_currentUser should be set in the header or before calling this function. -Use: Describes the behavior that should occur when maintenance is evaluated to be active/ongoing. And the following: --For a given banner element, it will replace its contents with the maintenance message for ongoing maintenance. --For a given payment section, it will replace its contents with the maintenance message for ongoing maintenance. --For a given CSV list of IDs to disable, it will disable each one. --For a given CSV list of IDs to hide, it will hide each one. --For a given CSV list of IDs to show, it will show each one. -Technical details: Appends an iframe to the body to load up the maintenance site and receives a message. The message is then stored in the global variable "PAYMW_MessageObject" for further use. -Usage: All IDs are case-sensitive --bannerElementID (Optional) = Single ID, put NULL if unused. Replaces element with the content in the maintenance response object. --paymentSectionID (Optional) = Single ID, put NULL if unused. Replaces element with the content in the maintenance response object. --csvIDsToDisable (Optional) = CSV of IDs, put NULL if unused. Gets all elements with the listed IDs and disables them. --csvIDsToHide (Optional) = CSV of IDs, put NULL if unused. Gets all elements with the listed IDs and hides them. --csvIDsToShow (Optional) = CSV of IDs, put NULL if unused. Gets all elements with the listed IDs and shows them. ============================ */ function PAYMW_DefaultBehavior_Ongoing(bannerElementID,paymentSectionID,csvIDsToDisable,csvIDsToHide,csvIDsToShow){ var bannerToModify = document.getElementById(bannerElementID); //Default is "PAYMW_banner_ongoing" var sectionToModify = document.getElementById(paymentSectionID); //Default is "PAYMW_section_ongoing" var currentUserEmail = (PAYMW_currentUser ? PAYMW_currentUser.toLowerCase() : PAYMW_currentUser); var message = PAYMW_MessageObject; if (message){ if(message.data.status == "StartPaymentMaintenance") { if (currentUserEmail !=""&& message.data.exemptionArray.findIndex(function(i){return i==currentUserEmail;})!=-1){ console.log(currentUserEmail+" is exempt from payment maintenance."); } else{ //Banner Behavior //Replacing target banner with content. if(bannerToModify){ bannerToModify.innerHTML = message.data.content; bannerToModify.style.display = "block"; } else {console.log("PAYMW. Element: ["+bannerElementID+"] was not found.");} //Section Behavior //Replacing target section with content. if(sectionToModify){ sectionToModify.innerHTML = message.data.content; } else {console.log("PAYMW. Element: ["+paymentSectionID+"] was not found.");} //Disable Behavior //For a list of IDs, disable each one. if(csvIDsToDisable){ let csvListPrep = csvIDsToDisable || ""; if (csvListPrep !=""){ let csvArrayPrep = csvListPrep.split(","); var csvArray = csvArrayPrep.map(function(i){return i.trim();}); for (i in csvArray){ var findElement = document.getElementById(csvArray[i]); if (findElement) {findElement.disabled = true;}; }; } } //Hide Behavior //For a list of IDs, hide each one. if(csvIDsToHide){ let csvListPrep = csvIDsToHide || ""; if (csvListPrep !=""){ let csvArrayPrep = csvListPrep.split(","); var csvArray = csvArrayPrep.map(function(i){return i.trim();}); for (i in csvArray){ var findElement = document.getElementById(csvArray[i]); if (findElement) {findElement.style.display = "none";}; }; } } //Show Behavior //For a list of IDs, show each one. if(csvIDsToShow){ let csvListPrep = csvIDsToShow || ""; if (csvListPrep !=""){ let csvArrayPrep = csvListPrep.split(","); var csvArray = csvArrayPrep.map(function(i){return i.trim();}); for (i in csvArray){ var findElement = document.getElementById(csvArray[i]); if (findElement) {findElement.style.display = "block";}; }; } } } } } }; /* ============================ Function PAYMW_DefaultBehavior_Warning -Use: Should be called first, ideally in the header. Initializes the global object "PAYMW_MessageObject" which returns the response from the maintenance site on whether maintenance is active or not. -Technical details: Appends an iframe to the body to load up the maintenance site and receives a message. The message is then stored in the global variable "PAYMW_MessageObject" for further use. -Usage: No parameters. Run before running any other function in this library. -Prerequisite: None ============================ */ function PAYMW_DefaultBehavior_Warning(bannerElementID){ var bannerToModify = document.getElementById(bannerElementID); //Default is "PAYMW_banner_warning" var message = PAYMW_MessageObject; if (message){ if(message.data.status == "DisplayPaymentMaintenance") { //Banner Behavior if(bannerToModify){ bannerToModify.innerHTML = message.data.content; bannerToModify.style.display = "block"; } else {console.log("PAYMW. Element: ["+bannerElementID+"] was not found.");} } } };