/** * @author Usman Naeem */ const WS_ROOT = "/ws/"; const HOME = "/"; var page = ""; const login_page = "login_page"; const home_page = "home_page"; const watch_list_page = "watch_list_page"; const preferred_items_page = "preferred_items_page"; const preferred_timeframes_page = "preferred_timeframes_page"; const profile_page = "profile_page"; const delete_profile_page = "delete_profile_page"; const administration_page = "administration_page"; const assign_administration_page = "assign_administration_page"; const change_password_page = "change_password_page"; const disable_account_page = "disable_account_page"; const enable_account_page = "enable_account_page"; const manage_license_page = "manage_license_page"; const revoke_administration_page = "revoke_administration_page"; const send_invitation_page = "send_invitation_page"; var user_json; var is_mobile_device; window.onload = function () { is_mobile_device = isMobileDevice(); load_timeframes().then(() => { set_all_timeframes_elements(); enableHomePage(); }).catch(error => { handleError(error, "home_message_box", true); return; }); initCollapsable(); }; function goHome() { window.location = HOME; } function sleep(time) { if (time > 0) { return new Promise(resolve => { setTimeout(resolve, time); }); } } function xSwipeDetection(e) { var xThreshold = 125; var yThreshold = 10; if (e.deltaY < yThreshold && e.deltaY > (yThreshold * -1)) { if (e.deltaX > xThreshold || e.deltaX < (xThreshold * -1)) { return e.deltaX > 0 ? 1 : -1; } } return 0; } function ySwipeDetection(e) { var xThreshold = 10; var yThreshold = 300; if (e.deltaX < xThreshold && e.deltaX > (xThreshold * -1)) { if (e.deltaY > yThreshold || e.deltaY < (yThreshold * -1)) { return e.deltaY > 0 ? 1 : -1; } } return 0; } function swipePage(nextPage) { if (page === home_page) { if (nextPage) { enableWatchListPage(); } } else if (page === watch_list_page) { if (nextPage) { enablePreferredItemsPage(); } else { enableHomePage(); } } else if (page === preferred_items_page) { if (nextPage) { enablePreferredTimeframesPage(); } else { enableWatchListPage(); } } else if (page === preferred_timeframes_page) { if (nextPage) { enableProfilePage(); } else { enablePreferredItemsPage(); } } else if (page === profile_page) { if (nextPage) { } else { enablePreferredTimeframesPage(); } } } function escapeCharacters(text) { return String(text) .replaceAll("&", "&") .replaceAll("<", "<") .replaceAll(">", ">") .replaceAll('"', """) .replaceAll("'", "'"); } function isCompressionSupported() { return ('DecompressionStream' in window); } async function unzipGzipString(gzippedString, encoded) { if (!encoded) { gzippedString = window.btoa(gzippedString); } const gzippedBuffer = new Uint8Array(window.atob(gzippedString).split("").map(c => c.charCodeAt(0))); const ds = new DecompressionStream("gzip"); const writer = ds.writable.getWriter(); writer.write(gzippedBuffer); writer.close(); const response = new Response(ds.readable); const arrayBuffer = await response.arrayBuffer(); const textDecoder = new TextDecoder(); const unzippedString = textDecoder.decode(arrayBuffer); return unzippedString; } function isMobileDevice() { if (window.matchMedia("(any-hover:none)").matches) { return true; } const userAgent = navigator.userAgent.toLowerCase(); if(/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/.test(userAgent)) { return true; } return false; } function isPagePinched(win) { const innerWidth = win.innerWidth; const visualViewportWidth = win.visualViewport ? win.visualViewport.width : innerWidth; const zoomLevel = innerWidth / visualViewportWidth; return (Math.abs(zoomLevel - 1) > 0.0001); } function getElement(id) { return document.getElementById(id); } function setPage(_page) { hidePage(); page = _page; } function displayPage() { const _page = getElement(page); if (_page) { _page.style.display = "block"; scrollToTop(); } } function hidePage() { const _page = getElement(page); if (_page) { _page.style.display = "none"; } } function scrollToTop() { document.body.scrollTop = 0; document.documentElement.scrollTop = 0; } function removeLoader() { const loaderContainer = getElement('loader_container'); if (loaderContainer) { //loaderContainer.remove(); loaderContainer.style.display = 'none'; } displayPage(); } function addLoader() { const loaderContainer = getElement('loader_container'); if (loaderContainer) { loaderContainer.style.display = 'block'; } displayPage(); } function isObject(obj) { return obj !== null && obj !== undefined && obj.constructor === Object; } function isString(obj) { return obj !== null && obj !== undefined && obj.constructor === String; } function isError(obj) { return obj !== null && obj !== undefined && obj.constructor === Error; } function handleError(error, message_box, remove_loader) { if (remove_loader) { removeLoader(); } var msg = getErrorMessage(error); const messageboxElement = getElement(message_box); if (messageboxElement) { messageboxElement.innerHTML = msg; } } function getErrorMessage(error) { var msg = ""; if (error) { if (isError(error)) { msg = error.message; } else { msg = error; } if (isString(msg)) { var ind = msg.lastIndexOf("Error:"); if (ind !== -1) { msg = msg.substr(ind + 6).trim(); } ind = msg.lastIndexOf("Exception:"); if (ind !== -1) { msg = msg.substr(ind + 10).trim(); } } } return msg; } function addHeader(menue) { const header = getElement("header"); if (header) { header.setAttribute("class", "header"); var content = "AEATAS"; if (menue) { content += "
"; content += "
"; content += "
"; content += "
"; content += "
"; content += "
"; content += "
"; content += "Home"; content += "Watch List"; content += "Preferred Symbols"; content += "Preferred Timeframes"; content += "
"; content += "Administration"; content += "
"; content += "Profile"; content += "Logout"; content += "
"; content += "
"; content += "
"; } header.innerHTML = content; } } function addFooter() { const footer = getElement("footer"); if (footer) { footer.setAttribute("class", "footer"); var content = ""; content += "© Copyright "; content += /\d{4}/.exec(Date())[0]; content += " Powered by MuzTech"; content += ""; content += "
"; footer.innerHTML = content; } } function preventDefaultFormSubmit(formId) { const form = document.getElementById(formId); form.addEventListener('submit', function (e) { e.preventDefault(); }); } function disableOrEnableForm(formId, disable) { const form = document.getElementById(formId); const formChildren = form.elements; for (var i = 0, len = formChildren.length; i < len; ++i) { if (formChildren[i].name) { formChildren[i].readOnly = disable ? true : false; formChildren[i].disabled = disable ? true : false; } } } function getFormData(formId) { const form = document.getElementById(formId); return new URLSearchParams(new FormData(form)); } async function httpPostResponse(url, formData) { const response = await fetch(url, { method: "POST", body: formData }); if (response.status === 404) { throw new Error("Not Found"); } return response; } async function httpGetResponse(url) { const response = await fetch(url, { method: "GET" //}).catch((error) =>{ //console.log("GET FAILED: " + error); }); if (response.status === 404) { throw new Error("Not Found"); } return response; } async function callWebservicePOST(url, formData, message_box, callback_403) { const response = await httpPostResponse(WS_ROOT + url, formData); const json = await response.json(); if (isObject(json)) { if (json.code === 403) { if (callback_403) { try { callback_403(); } catch (er) { console.log(er); } } if (message_box) { message_box.innerHTML = json.message; await sleep(1000); } enableLoginPage(); return; } } return json; } async function callWebserviceGET(url, message_box, callback_403) { const response = await httpGetResponse(WS_ROOT + url); const json = await response.json(); if (isObject(json)) { if (json.code === 403) { if (callback_403) { try { callback_403(); } catch (er) { console.log(er); } enableLoginPage(); } if (message_box) { message_box.innerHTML = json.message; await sleep(1000); } enableLoginPage(); return; } } return json; } async function load_user(messageboxElement) { try { if (user_json) { await set_user(); return user_json; } const url = "GetUser"; const json = await callWebserviceGET(url, messageboxElement, null); if (json.code !== 200) { if (messageboxElement) { messageboxElement.innerHTML = json.message; } throw new Error("Failed to get user"); } else { user_json = json.user; await set_user(); } return json; } catch (error) { throw new Error("Failed to load user"); } } async function set_user() { const userInfoElement = getElement("user_info"); if (userInfoElement) { userInfoElement.innerHTML = user_json.name; } //messageboxElement.innerHTML = JSON.stringify(json); const settingContentAdminElement = getElement('setting-content-admin'); if (settingContentAdminElement) { if (user_json.type === 'Administrator') { settingContentAdminElement.style.display = 'block'; } else { settingContentAdminElement.style.display = 'none'; } } } async function logout() { try { const url = "Logout"; const json = await callWebserviceGET(url, null, null); enableLoginPage(); } catch (error) { console.error(error); } } function closeMenu() { const id = event.target.id; if (!id || !id.startsWith("setting-")) { if (!id || (id !== "setting-menu" && id !== "user_info")) { const settingContent = getElement("setting-content"); if (settingContent) { if (settingContent.style.display !== "none") { settingContent.style.display = "none"; } } } } } function settingMenu() { const settingContent = getElement("setting-content"); if (settingContent.style.display === "block") { settingContent.style.display = "none"; } else { settingContent.style.display = "block"; } }