/** * @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; 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; }); await loadEconomicCalendar(); removeLoader(); await jumpToClosestEvent(); } catch (error) { handleError(error, "economic_calendar_message_box", true); removeLoader(); } } async function loadEconomicCalendar() { try { if (loadingEconomicCalendar) { return; } 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; }); await update_economic_event_div_container(); } } 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; } addLoader(); //closeEconomicCalendarOptions(); await loadEconomicCalendar().then(() => { removeLoader(); }); } catch (error) { handleError(error, "economic_calendar_message_box", true); removeLoader(); } } async function update_economic_event_div_container() { 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 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; 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, 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; } } }); closestDisplayedEventNumber = closestEventNumber; 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'}); const calendar_gototop_link = getElement("calendar_gototop_link"); if (eventNumber > 5) { calendar_gototop_link.style.display = "block"; } else { calendar_gototop_link.style.display = "none"; } } async function add_economic_event_div(n, current_time, time, location, currency, title, impact, previous, forecast, actual, dateFormater, timeFormater) { const date = new Date(time); const pre_impact_start_time = time - pre_event_impact_delay; const present_impact_end_time = 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 > time; const impact_recent = impact_over && !impact_occuring && current_time < post_impact_end_time; const impact_approaching = current_time < 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) { const timeDifference = Math.abs(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 += (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(); closestDisplayedEventDiv.scrollIntoView({ behavior: 'auto', // 'auto' (instant) or 'smooth' (animated) block: 'center', // 'start', 'center', 'end', or 'nearest' (vertical alignment) inline: 'end' // 'start', 'center', 'end', or 'nearest' (horizontal alignment) }); //closestDisplayedEventDiv.style.transform.scaleY = 0; //closestDisplayedEventDiv.style.opacity = 0; //const animation0 = closestDisplayedEventDiv.animate([ // {opacity: 0, transform: 'scaleY(0)'}, // {opacity: 1, transform: 'scaleY(1)'} //], {duration: 350, easing: 'ease-in-out'}); //await animation0.finished; await sleep(200); const animation1 = closestDisplayedEventDiv.animate([ {opacity: 1, transform: 'scaleX(1.0)'}, {opacity: 1, transform: 'scaleX(1.2)'} ], {duration: 200, easing: 'ease-in-out'}); await animation1.finished; const animation2 = closestDisplayedEventDiv.animate([ {opacity: 1, transform: 'scaleX(1.2)'}, {opacity: 1, transform: 'scaleX(0.8)'} ], {duration: 200, easing: 'ease-in-out'}); await animation2.finished; 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; 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; 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; 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; 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.scaleY = 1; closestDisplayedEventDiv.style.opacity = 1; } } //function showHideEconomicCalendarOptions() { // const calendar_options_container = getElement('calendar_options_container'); // if (calendar_options_container.style.display === 'block') { // closeEconomicCalendarOptions(); // } else { // openEconomicCalendarOptions(); // } //} function closeEconomicCalendarOptions() { 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"; // calendar_close_btn.innerHTML = "Options"; // calendar_close_btn.style.float = "right"; // calendar_close_btn.style.width = "100%"; // calendar_close_btn.animate([ // {opacity: 0, transform: 'scale(1)'}, // {opacity: 1, transform: 'scale(1)'} // ], {duration: 1000, easing: 'ease-out'}); const calendar_options_container = getElement('calendar_options_container'); calendar_options_container.style.display = "none"; const calendar_settings_link = getElement('calendar_settings_link'); calendar_settings_link.style.display = "block"; } function openEconomicCalendarOptions() { 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"; // calendar_close_btn.innerHTML = "Close"; // calendar_close_btn.style.float = "left"; // calendar_close_btn.style.width = "85px"; const calendar_options_container = getElement('calendar_options_container'); calendar_options_container.style.display = "block"; calendar_options_container.animate([ {opacity: 1, transform: 'scale(0)'}, {opacity: 1, transform: 'scale(1)'} ], {duration: 350, easing: 'ease-in-out'}); }