/**
* @author Usman Naeem
*/
var economic_event_list = null;
//var pre_event_impact_delay = 1000 * 60 * 60 * 6;
var present_event_impact_delay = 1000 * 60 * 60 * 1;
//var post_event_impact_delay = 1000 * 60 * 60 * 5;
var event_date_format_options = {
timeZone: 'America/New_York', /*timeZoneName: 'short',*/
weekday: 'short', /*year: 'numeric',*/ month: 'short', day: 'numeric'
};
var event_time_format_options = {
timeZone: 'America/New_York', /*timeZoneName: 'short',*/
hour: '2-digit',
minute: '2-digit',
hour12: true
};
//
var loadingEconomicCalendar = false;
var closestDisplayedEventNumber = -1;
var economic_calendar_cashe_expiry_time = 0;
//
const economic_event_div_container_auto_update_delay = 1000 * 5;
var economic_event_div_container_auto_update_time = 0;
//
const highest_economic_event_check_delay = 1000 * 10;
var highest_economic_event_check_time = 0;
var highest_economic_event_impact = "no";
async function enableEconomicCalendarPage() {
try {
addLoader();
setPage(economic_calendar_page);
addHeader(true);
addFooter();
const messageboxElement = getElement("economic_calendar_message_box");
messageboxElement.innerHTML = "";
await load_user(messageboxElement).then(() => {
}).catch(error => {
handleError(error, "economic_calendar_message_box", true);
enableLoginPage();
return;
});
// const pre_event_impact_delay_hours = getElement("pre_event_impact_delay_hours").value;
// pre_event_impact_delay = 1000 * 60 * 60 * pre_event_impact_delay_hours;
//
// const post_event_impact_delay_hours = getElement("post_event_impact_delay_hours").value;
// post_event_impact_delay = 1000 * 60 * 60 * post_event_impact_delay_hours;
const load_economic_calendar = loadEconomicCalendar();
await load_economic_calendar.finished;
await update_economic_event_div_container(true, true);
removeLoader();
await jumpToClosestEvent();
} catch (error) {
handleError(error, "economic_calendar_message_box", true);
removeLoader();
}
}
async function loadEconomicCalendar() {
try {
if (loadingEconomicCalendar) {
return;
}
const current_time = Date.now();
if (economic_calendar_cashe_expiry_time > current_time) {
return;
}
economic_calendar_cashe_expiry_time = current_time + cache_expiry_delay;
loadingEconomicCalendar = true;
const economicCalendarMessageboxElement = getElement("economic_calendar_message_box");
economicCalendarMessageboxElement.innerHTML = "";
var url = "GetEconomicCalendarOfThisWeek";
if (isCompressionSupported()) {
url += "?compress=true&encode=true";
}
const json = await callWebserviceGET(url, economicCalendarMessageboxElement, null);
if (json.code !== 200) {
if (economicCalendarMessageboxElement) {
economicCalendarMessageboxElement.innerHTML = json.message;
}
} else {
var compressed = json.compressed;
var encoded = json.encoded;
var economicCalendar = json.economic_calendar;
//
if (compressed || encoded) {
if (compressed) {
await unzipGzipString(economicCalendar, encoded).then((unzipped) => {
economicCalendar = unzipped;
encoded = false;
}).catch((error) => {
console.error("Error unzipping:", error);
throw error;
});
} else if (encoded) {
if (encoded) {
economicCalendar = window.atob(economicCalendar);
}
}
economicCalendar = JSON.parse(economicCalendar);
}
economic_event_list = new Array();
economicCalendar.economic_events.forEach(economicEvent => {
economic_event_list.push(economicEvent);
});
economic_event_list = economic_event_list.sort(function (a, b) {
return a.time - b.time;
});
_updateHighestEconomicEventImpactIndicator(false);
}
} catch (error) {
console.log(error);
handleError("Failed to get load economic events " + error, "economic_calendar_message_box", false);
}
loadingEconomicCalendar = false;
}
async function refreshEconomicCalendar() {
try {
if (loadingEconomicCalendar) {
return;
}
economic_calendar_cashe_expiry_time = 0;
addLoader();
//closeEconomicCalendarSettings();
await loadEconomicCalendar().then(() => {
update_economic_event_div_container(true, true);
removeLoader();
});
} catch (error) {
handleError(error, "economic_calendar_message_box", true);
removeLoader();
}
}
async function update_economic_event_div_container(animate, checkHighestEconomicEventImpactIndicator) {
closestDisplayedEventNumber = -1;
const economic_event_div_container = getElement("economic_event_div_container");
economic_event_div_container.innerHTML = "";
const timezoneElement = getElement("timezone");
var selected_timezone = timezoneElement.value ? timezoneElement.value : user_json.timezone;
event_date_format_options.timeZone = selected_timezone;
const eventDateFormater = new Intl.DateTimeFormat('en-US', event_date_format_options);
event_time_format_options.timeZone = selected_timezone;
const eventTimeFormater = new Intl.DateTimeFormat('en-US', event_time_format_options);
const current_time = Date.now();
const economic_calendar_filter_past_hours = getElement("economic_calendar_filter_past_hours");
const economic_calendar_filter_future_hours = getElement("economic_calendar_filter_future_hours");
if (!isNumeric(economic_calendar_filter_past_hours.value)) {
economic_calendar_filter_past_hours.value = 1;
}
if (!isNumeric(economic_calendar_filter_future_hours.value)) {
economic_calendar_filter_future_hours.value = 1;
}
const startTimeLimit = current_time - (1000 * 60 * 60 * economic_calendar_filter_past_hours.value);
const endTimeLimit = current_time + (1000 * 60 * 60 * economic_calendar_filter_future_hours.value);
const pre_event_impact_delay_hours = getElement("pre_event_impact_delay_hours");
if (!isNumeric(pre_event_impact_delay_hours.value)) {
pre_event_impact_delay_hours.value = 1;
}
const pre_event_impact_delay = 1000 * 60 * 60 * pre_event_impact_delay_hours.value;
const post_event_impact_delay_hours = getElement("post_event_impact_delay_hours");
if (!isNumeric(post_event_impact_delay_hours.value)) {
post_event_impact_delay_hours.value = 1;
}
const post_event_impact_delay = 1000 * 60 * 60 * post_event_impact_delay_hours.value;
const economic_calendar_filter_high_impact = getElement("economic_calendar_filter_high_impact").checked;
const economic_calendar_filter_medium_impact = getElement("economic_calendar_filter_medium_impact").checked;
const economic_calendar_filter_low_impact = getElement("economic_calendar_filter_low_impact").checked;
var all_impact = false;
if (!economic_calendar_filter_high_impact && !economic_calendar_filter_medium_impact && !economic_calendar_filter_low_impact) {
all_impact = true;
}
const economic_calendar_filter_US = getElement("economic_calendar_filter_US").checked;
const economic_calendar_filter_JP = getElement("economic_calendar_filter_JP").checked;
const economic_calendar_filter_EU = getElement("economic_calendar_filter_EU").checked;
const economic_calendar_filter_GB = getElement("economic_calendar_filter_GB").checked;
const economic_calendar_filter_CH = getElement("economic_calendar_filter_CH").checked;
const economic_calendar_filter_CA = getElement("economic_calendar_filter_CA").checked;
const economic_calendar_filter_AU = getElement("economic_calendar_filter_AU").checked;
const economic_calendar_filter_NZ = getElement("economic_calendar_filter_NZ").checked;
var all_location = false;
if (!economic_calendar_filter_US && !economic_calendar_filter_JP && !economic_calendar_filter_EU && !economic_calendar_filter_GB && !economic_calendar_filter_CH && !economic_calendar_filter_CA && !economic_calendar_filter_AU && !economic_calendar_filter_NZ) {
all_location = true;
}
//
var closestEventTimeGap = current_time;
var closestEventNumber = 0;
var eventNumber = 0;
if (economic_event_list) {
await economic_event_list.forEach(economicEvent => {
var ok = true;
if (economicEvent.time < startTimeLimit || economicEvent.time > endTimeLimit) {
ok = false;
}
if (ok) {
if (!all_impact) {
if (economicEvent.impact === "unknown") {
ok = false;
} else if (economicEvent.impact === "high") {
ok = economic_calendar_filter_high_impact;
} else if (economicEvent.impact === "medium") {
ok = economic_calendar_filter_medium_impact;
} else if (economicEvent.impact === "low") {
ok = economic_calendar_filter_low_impact;
} else {
ok = false;
}
}
if (ok) {
if (!all_location) {
if (economicEvent.location === "US") {
ok = economic_calendar_filter_US;
} else if (economicEvent.location === "JP") {
ok = economic_calendar_filter_JP;
} else if (economicEvent.location === "EU") {
ok = economic_calendar_filter_EU;
} else if (economicEvent.location === "GB") {
ok = economic_calendar_filter_GB;
} else if (economicEvent.location === "CH") {
ok = economic_calendar_filter_CH;
} else if (economicEvent.location === "CA") {
ok = economic_calendar_filter_CA;
} else if (economicEvent.location === "AU") {
ok = economic_calendar_filter_AU;
} else if (economicEvent.location === "NZ") {
ok = economic_calendar_filter_NZ;
} else {
ok = false;
}
}
}
}
if (ok) {
eventNumber += 1;
add_economic_event_div(eventNumber, current_time, pre_event_impact_delay, post_event_impact_delay, economicEvent.time, economicEvent.location, economicEvent.currency, economicEvent.title, economicEvent.impact, economicEvent.previous, economicEvent.forecast, economicEvent.actual, eventDateFormater, eventTimeFormater);
//
if (current_time > economicEvent.time || Math.abs(current_time - economicEvent.time) < closestEventTimeGap) {
closestEventTimeGap = Math.abs(current_time - economicEvent.time);
closestEventNumber = eventNumber;
}
}
});
}
if (eventNumber === 0) {
var noEventDiv = "
" + "No economic event to display." + "
";
const economic_event_div_container = getElement("economic_event_div_container");
economic_event_div_container.innerHTML += noEventDiv;
}
closestDisplayedEventNumber = closestEventNumber;
if (animate) {
economic_event_div_container.style.transformOrigin = "right";
economic_event_div_container.animate([
{opacity: 1, transform: 'scaleX(0)'},
{opacity: 1, transform: 'scaleX(1)'}
], {duration: 350, easing: 'ease-in-out'});
}
economic_event_div_container_auto_update_time = current_time + economic_event_div_container_auto_update_delay;
//
if (checkHighestEconomicEventImpactIndicator) {
_updateHighestEconomicEventImpactIndicator(false);
}
}
async function add_economic_event_div(n, current_time, pre_event_impact_delay, post_event_impact_delay, event_time, location, currency, title, impact, previous, forecast, actual, dateFormater, timeFormater) {
const date = new Date(event_time);
const pre_impact_start_time = event_time - pre_event_impact_delay;
const present_impact_end_time = event_time + present_event_impact_delay;
const post_impact_end_time = present_impact_end_time + post_event_impact_delay;
const impact_over = current_time > present_impact_end_time;
const impact_occuring = !impact_over && current_time > event_time;
const impact_recent = impact_over && !impact_occuring && current_time < post_impact_end_time;
const impact_approaching = current_time < event_time && current_time > pre_impact_start_time;
var eventDiv = "";
eventDiv += "
";
eventDiv += "
";
eventDiv += "
" + location + "
";
eventDiv += "
";
eventDiv += "
";
eventDiv += "
";
eventDiv += "
";
eventDiv += (impact_recent ? "*" : impact_occuring ? "*" : (impact_approaching ? "*" : ""));
eventDiv += timeFormater.format(date);
if (impact_approaching || impact_recent) {
const timeDifference = Math.abs(event_time - current_time);
const totalMinutes = Math.floor(timeDifference / (1000 * 60));
const totalHours = Math.floor(timeDifference / (1000 * 60 * 60));
const hours = Math.floor(timeDifference / 3600000);
const minutes = Math.floor((timeDifference % 3600000) / 60000);
eventDiv += "";
eventDiv += (impact_recent ? "-" : "");
eventDiv += (hours > 0 ? hours + "h " : "");
eventDiv += (minutes > 0 ? minutes + "m " : "");
eventDiv += "";
}
eventDiv += "
";
eventDiv += "
" + dateFormater.format(date) + "
";
eventDiv += "
";
eventDiv += "
";
eventDiv += "
" + title + "
[ " + impact + " impact event ]" + "
";
eventDiv += "
" + (forecast ? forecast : "na") + "
" + (previous ? previous : "na") + "
" + (actual ? actual : "") + "
";
eventDiv += "
";
eventDiv += "
";
eventDiv += "
";
const economic_event_div_container = getElement("economic_event_div_container");
economic_event_div_container.innerHTML += eventDiv;
}
async function jumpToClosestEvent() {
if (closestDisplayedEventNumber > 0) {
const closestDisplayedEventDiv = getElement("economic_event_div_" + closestDisplayedEventNumber);
closestDisplayedEventDiv.scrollIntoView({
behavior: 'auto',
block: 'center',
inline: 'end'
});
await sleep(200);
const animation1 = closestDisplayedEventDiv.animate([
{opacity: 1, transform: 'scaleX(1.0)'},
{opacity: 1, transform: 'scaleX(1.1)'}
], {duration: 200, easing: 'ease-in-out'});
await animation1.finished;
closestDisplayedEventDiv.style.transform = "scaleX(1.2)";
const animation2 = closestDisplayedEventDiv.animate([
{opacity: 1, transform: 'scaleX(1.1)'},
{opacity: 1, transform: 'scaleX(0.8)'}
], {duration: 200, easing: 'ease-in-out'});
await animation2.finished;
closestDisplayedEventDiv.style.transform = "scaleX(0.8)";
const animation3 = closestDisplayedEventDiv.animate([
{opacity: 1, transform: 'scaleX(0.8)'},
{opacity: 1, transform: 'scaleX(1.07)'}
], {duration: 200, easing: 'ease-in-out'});
await animation3.finished;
closestDisplayedEventDiv.style.transform = "scaleX(1.07)";
const animation4 = closestDisplayedEventDiv.animate([
{opacity: 1, transform: 'scaleX(1.07)'},
{opacity: 1, transform: 'scaleX(0.9)'}
], {duration: 200, easing: 'ease-in-out'});
await animation4.finished;
closestDisplayedEventDiv.style.transform = "scaleX(0.9)";
const animation5 = closestDisplayedEventDiv.animate([
{opacity: 1, transform: 'scaleX(0.9)'},
{opacity: 1, transform: 'scaleX(1.03)'}
], {duration: 200, easing: 'ease-in-out'});
await animation5.finished;
closestDisplayedEventDiv.style.transform = "scaleX(1.03)";
const animation6 = closestDisplayedEventDiv.animate([
{opacity: 1, transform: 'scaleX(1.03)'},
{opacity: 1, transform: 'scaleX(0.97)'}
], {duration: 200, easing: 'ease-in-out'});
await animation6.finished;
closestDisplayedEventDiv.style.transform = "scaleX(0.97)";
const animation7 = closestDisplayedEventDiv.animate([
{opacity: 1, transform: 'scaleX(0.97)'},
{opacity: 1, transform: 'scaleX(1.0)'}
], {duration: 200, easing: 'ease-in-out'});
await animation7.finished;
closestDisplayedEventDiv.style.transform = "scaleX(1.0)";
closestDisplayedEventDiv.style.transform = "scaleY(1.0)";
closestDisplayedEventDiv.style.opacity = 1;
}
}
function closeEconomicCalendarSettings() {
const calendar_refresh_btn = getElement('calendar_refresh_btn');
calendar_refresh_btn.style.display = "none";
const calendar_jump_btn = getElement('calendar_jump_btn');
calendar_jump_btn.style.display = "none";
const calendar_close_btn = getElement('calendar_close_btn');
calendar_close_btn.style.display = "none";
const calendar_settings_container = getElement('calendar_settings_container');
calendar_settings_container.style.display = "none";
const calendar_settings_link = getElement('calendar_settings_link');
calendar_settings_link.style.display = "block";
}
function openEconomicCalendarSettings() {
const calendar_settings_link = getElement('calendar_settings_link');
calendar_settings_link.style.display = "none";
const calendar_refresh_btn = getElement('calendar_refresh_btn');
calendar_refresh_btn.style.display = "block";
const calendar_jump_btn = getElement('calendar_jump_btn');
calendar_jump_btn.style.display = "block";
const calendar_close_btn = getElement('calendar_close_btn');
calendar_close_btn.style.display = "block";
const calendar_settings_container = getElement('calendar_settings_container');
calendar_settings_container.style.display = "block";
calendar_settings_container.animate([
{opacity: 1, transform: 'scale(0)'},
{opacity: 1, transform: 'scale(1)'}
], {duration: 350, easing: 'ease-in-out'});
}
//
function updateHighestEconomicEventImpactIndicator(currentTime) {
try {
if (currentTime < highest_economic_event_check_time) {
return;
}
_updateHighestEconomicEventImpactIndicator(true);
} catch (err) {
console.log(err);
}
}
function _updateHighestEconomicEventImpactIndicator(reload_economic_calendar) {
const currentTime = Date.now();
highest_economic_event_check_time = currentTime + highest_economic_event_check_delay;
//
var impact;
const economic_calendar_filter_event_indicator = getElement("economic_calendar_filter_event_indicator");
if (!economic_calendar_filter_event_indicator.checked) {
highest_economic_event_impact = "no";
} else {
const post_event_indicator_minutes = getElement("post_event_indicator_minutes");
const pre_event_indicator_minutes = getElement("pre_event_indicator_minutes");
if (!isNumeric(post_event_indicator_minutes.value)) {
post_event_indicator_minutes.value = 1;
}
if (!isNumeric(pre_event_indicator_minutes.value)) {
pre_event_indicator_minutes.value = 1;
}
const startTime = currentTime - (1000 * 60 * post_event_indicator_minutes.value);
const endTime = currentTime + (1000 * 60 * pre_event_indicator_minutes.value);
impact = getCurrentlyHighestImpactingEconomicEventImpact(reload_economic_calendar, startTime, endTime);
highest_economic_event_impact = impact === null ? "no" : impact;
}
const header = getElement("header");
header.setAttribute("class", "header " + highest_economic_event_impact + "-economic-event-impact-header");
}
function getCurrentlyHighestImpactingEconomicEventImpact(reload_economic_calendar, startTimeLimit, endTimeLimit) {
if (reload_economic_calendar) {
loadEconomicCalendar();
}
if (!economic_event_list) {
return null;
}
//
const economic_calendar_filter_US = getElement("economic_calendar_filter_US").checked;
const economic_calendar_filter_JP = getElement("economic_calendar_filter_JP").checked;
const economic_calendar_filter_EU = getElement("economic_calendar_filter_EU").checked;
const economic_calendar_filter_GB = getElement("economic_calendar_filter_GB").checked;
const economic_calendar_filter_CH = getElement("economic_calendar_filter_CH").checked;
const economic_calendar_filter_CA = getElement("economic_calendar_filter_CA").checked;
const economic_calendar_filter_AU = getElement("economic_calendar_filter_AU").checked;
const economic_calendar_filter_NZ = getElement("economic_calendar_filter_NZ").checked;
var all_location = false;
if (!economic_calendar_filter_US && !economic_calendar_filter_JP && !economic_calendar_filter_EU && !economic_calendar_filter_GB && !economic_calendar_filter_CH && !economic_calendar_filter_CA && !economic_calendar_filter_AU && !economic_calendar_filter_NZ) {
all_location = true;
}
//
const economic_calendar_filter_high_impact = getElement("economic_calendar_filter_high_impact").checked;
const economic_calendar_filter_medium_impact = getElement("economic_calendar_filter_medium_impact").checked;
const economic_calendar_filter_low_impact = getElement("economic_calendar_filter_low_impact").checked;
var all_impact = false;
if (!economic_calendar_filter_high_impact && !economic_calendar_filter_medium_impact && !economic_calendar_filter_low_impact) {
all_impact = true;
}
//
var highestImpact = null;
economic_event_list.forEach(economicEvent => {
var ok = true;
if (economicEvent.time < startTimeLimit || economicEvent.time > endTimeLimit) {
ok = false;
} else if (highestImpact === "high") {
ok = false;
} else if (highestImpact === "medium" && economicEvent.impact !== "high") {
ok = false;
} else if ((highestImpact === "low" || highestImpact === "unknown") && (economicEvent.impact !== "high" && economicEvent.impact !== "medium")) {
ok = false;
}
if (ok) {
if (!all_impact) {
if (economicEvent.impact === "unknown") {
ok = false;
} else if (economicEvent.impact === "high") {
ok = economic_calendar_filter_high_impact;
} else if (economicEvent.impact === "medium") {
ok = economic_calendar_filter_medium_impact;
} else if (economicEvent.impact === "low") {
ok = economic_calendar_filter_low_impact;
} else {
ok = false;
}
}
if (ok) {
if (!all_location) {
if (economicEvent.location === "US") {
ok = economic_calendar_filter_US;
} else if (economicEvent.location === "JP") {
ok = economic_calendar_filter_JP;
} else if (economicEvent.location === "EU") {
ok = economic_calendar_filter_EU;
} else if (economicEvent.location === "GB") {
ok = economic_calendar_filter_GB;
} else if (economicEvent.location === "CH") {
ok = economic_calendar_filter_CH;
} else if (economicEvent.location === "CA") {
ok = economic_calendar_filter_CA;
} else if (economicEvent.location === "AU") {
ok = economic_calendar_filter_AU;
} else if (economicEvent.location === "NZ") {
ok = economic_calendar_filter_NZ;
} else {
ok = false;
}
}
}
if (ok) {
highestImpact = economicEvent.impact;
if (highestImpact === "high") {
return "high";
}
}
}
});
return highestImpact;
}