/*
# cpanel - /usr/local/cp-monitoring-whm-plugin/SOURCES/server-monitoring.js
Copyright 2021 cPanel, L.L.C.
# All rights reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
*/
(function() {
"use strict";
/**
* Hide the banner views.
*
* @private
*/
function hideBanner() {
document.getElementById(BANNER.id).style.display = "none";
}
/**
* Show the main banner view.
*
* @private
*/
function showBanner() {
document.getElementById(BANNER.id).style.display = "block";
}
/**
* Generate the key used to store the dismissed flag.
*
* @private
* @param {string} id
* @returns string
*/
function dismissedId ( id ) {
return "dismissed_" + id;
}
/**
* Dismiss the banner ad. This will set the date the banner was dismissed so we can
* calculate the retry period that will allow the banner to be shown again in a few
* days according to the configured showItAgain period in seconds.
*
* @private
* @param {string} bannerId
* @returns
*/
function dismissBanner (bannerId) {
hideBanner();
localStorage.setItem(dismissedId(bannerId), new Date().toISOString());
return;
}
/**
* Retrieve the last time the banner was dismissed.
*
* @param {string} bannerId
* @returns
*/
function lastTimeDismissed(bannerId) {
return localStorage.getItem(dismissedId(bannerId));
}
/**
* Initialized the banner ad.
*/
function init() {
if (BANNER.info.dismissible) {
let dismissed = lastTimeDismissed(BANNER.id);
if (dismissed) {
if (typeof BANNER.info.show_again_in === 'number') {
const nowDate = new Date();
const dismissedDate = new Date(dismissed);
const elapsed = nowDate - dismissedDate;
if (elapsed > BANNER.info.show_again_in) {
showBanner();
} else {
hideBanner();
}
} else {
hideBanner();
}
} else {
showBanner();
}
document.querySelector('#' + BANNER.id + ' .dismiss_banner')
.addEventListener( "click", ev => dismissBanner(BANNER.id) );
document.getElementById("purchaseLink")
.addEventListener( "click", ev => hideBanner(BANNER.id) );
}
}
// Initialize the banner
document.addEventListener( "DOMContentLoaded", init );
})();