"use strict"; console.log("init client 1.2.5 start"); //dbg if (!navigator.cookieEnabled) { alert("Please allow to store cookies!"); } let url = window.location.href; let h, l; if (url.indexOf("?lang=") > 0) { h = url.substring(url.indexOf("h=") + 2, url.indexOf("?lang=")); if (url.indexOf("?opt=") > 0) { l = url.substring(url.indexOf("lang=") + 5, url.indexOf("?opt=")); } else { l = url.substring(url.indexOf("lang=") + 5); } document.cookie = "languageID=" + l; console.log("init url Lang=[" + l + "]"); } else { h = url.substring(url.indexOf("h=") + 2); console.log("init url no Lang"); } let main, header = {}; let buttons = []; let langID; let lang; let langCookie = getCookie("languageID"); let langUser = navigator.language.substr(0, 2); var isPrv = sessionStorage.getItem("isprv") === "true"; console.log("init langCookie=[" + langCookie + "] isPrv=[" + isPrv + "]"); if (langCookie == "" && langUser != "en") { if (isPrv) { // preview mode so no cookies are sent console.log("init lang PREVIEW 1"); langID = l; } else { console.log("init lang 1"); langID = "en"; } setTimeout(() => { if (!isPrv) { userStats(langID, h); } document.cookie = "languageID=en"; langCookie = "en"; }, 5000); } else if (langCookie === "" && langUser === "en") { if (isPrv) { // preview mode so no cookies are sent console.log("init lang PREVIEW 2"); langID = l; langCookie = l; } else { console.log("init lang 2"); langID = "en"; document.cookie = "languageID=en"; langCookie = "en"; } if (!isPrv) { userStats(langID, h); } } else { langID = langCookie; console.log("init lang 3 langId=[" + langID + "]"); userStats(langID, h); } const checkLan = (lang) => { //lang.forEach((lan) => { lang.every((lan) => { // console.log( // "checkLan lan.code=[" + // lan.code + // "] langUser[" + // langUser + // "] lan.counter=[" + // lan.counter + // "] langCookie=[" + // langCookie + // "]" // ); //dbg if (lan.code == langUser && lan.counter > 0 && langCookie === "") { document.cookie = "languageID=" + lan.code; //console.log("checkLan reload"); //dbg location.reload(); return false; } else { //console.log("checkLan NO reload"); //dbg } return true; }); }; lang = langID; //HTTPS request const sendHttpRequest = (method, url, data) => { const promise = new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open(method, url); xhr.responseType = "json"; if (data) { xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } xhr.onload = () => { resolve(xhr.response); }; xhr.send(data); }); return promise; }; //Fonts load //new fonts function loadFontsDataFromJson() { let promise = new Promise((resolve, reject) => { var buttonContentPath = "https://storage.googleapis.com/hotensifycloud.appspot.com/images/" + h + "/conf/fonts.json?nocache=" + new Date().getTime(); fetch(buttonContentPath, { mode: "cors", headers: { "Content-Type": "application/json", }, }) .then((response) => { return response.json(); }) .then((data) => { // Work with JSON data here resolve(data); }) .catch((e) => { // console.error(e); reject(e); }); }); return promise; } //new fonts function loadFontsDataFromBackEnd() { // console.log("loadFontsFromBackEnd"); sendHttpRequest( "POST", "https://us-central1-hotensifycloud.cloudfunctions.net/getFonts", "customer=" + h ).then((resp) => { if (resp) { let fontNames = Object.keys(resp); fontNames.forEach((fontName) => { gArrayFonts.push(fontName); }); let fonts = Object.values(resp); fonts.forEach((font) => { document.getElementById("importedFonts").innerHTML += font; }); } }); } //new fonts loadFontsDataFromJson() .then((resp) => { if (resp) { let fontNames = Object.keys(resp); fontNames.forEach((fontName) => { gArrayFonts.push(fontName); }); let fonts = Object.values(resp); fonts.forEach((font) => { document.getElementById("importedFonts").innerHTML += font; }); } }) .catch((err) => { loadFontsDataFromBackEnd(); }); //Button load function loadButtonDataFromJson() { let promise = new Promise((resolve, reject) => { var buttonContentPath = "https://storage.googleapis.com/hotensifycloud.appspot.com/images/" + h + "/conf/button" + langID + ".json?nocache=" + new Date().getTime(); fetch(buttonContentPath, { mode: "cors", headers: { "Content-Type": "application/json", }, }) .then((response) => { return response.json(); }) .then((data) => { // Work with JSON data here resolve(data); }) .catch((e) => { // console.error(e); reject(e); }); }); return promise; } function loadButtonDataFromBackEnd() { // console.log("loadDataFromBackEnd"); sendHttpRequest( "POST", "https://us-central1-hotensifycloud.cloudfunctions.net/newGetButtons", "customer=" + h + "&buttonid=" + langID ).then((resp) => { if ( l === "en" || l === "de" || l === "fr" || l === "it" || l === "es" || l === "ru" || l === "ar" || l === "cn" || l === "tr" || l === "jp" || l === "fi" || l === "se" ) { null; } else if (resp.length === 0 && getCookie("languageID") !== "en") { document.cookie = "languageID=en"; location.reload(); } for (let x = 0; x <= resp.length; x++) { resp.forEach((res) => { if (x === parseInt(res.id)) { generateButton(res); } }); } buttons = resp; //load main loadMainDataFromJson() .then((resp) => { generateMain(resp); }) .catch((err) => { // console.log(err); // console.log("Failed to load from Json, trying backend"); loadMainDataFromBackEnd(); }); }); } //Main load function loadMainDataFromJson() { let promise = new Promise((resolve, reject) => { var buttonContentPath = "https://storage.googleapis.com/hotensifycloud.appspot.com/images/" + h + "/conf/main.json?nocache=" + new Date().getTime(); fetch(buttonContentPath, { mode: "cors", headers: { "Content-Type": "application/json", }, }) .then((response) => { return response.json(); }) .then((data) => { // Work with JSON data here resolve(data); }) .catch((e) => { // console.error(e); reject(e); }); }); return promise; } function loadMainDataFromBackEnd() { // console.log("loadMainFromBackEnd"); sendHttpRequest( "POST", "https://us-central1-hotensifycloud.cloudfunctions.net/newGetMain", "customer=" + h + "&buttonid=" + langID ).then((resp) => { generateMain(resp); }); } //Header load function loadHeaderDataFromJson() { let promise = new Promise((resolve, reject) => { var buttonContentPath = "https://storage.googleapis.com/hotensifycloud.appspot.com/images/" + h + "/conf/header.json?nocache=" + new Date().getTime(); fetch(buttonContentPath, { mode: "cors", headers: { "Content-Type": "application/json", }, }) .then((response) => { return response.json(); }) .then((data) => { // Work with JSON data here resolve(data); }) .catch((e) => { // console.error(e); reject(e); }); }); return promise; } function loadHeaderDataFromBackEnd() { // console.log("loadHeaderFromBackEnd"); sendHttpRequest( "POST", "https://us-central1-hotensifycloud.cloudfunctions.net/newGetHeader", "customer=" + h + "&buttonid=" + langID ).then((resp) => { generateHeader(resp); }); } //Start loading... loadButtonDataFromJson() .then((resp) => { for (let x = 0; x <= resp.length; x++) { resp.forEach((res) => { buttons = resp; if (x === parseInt(res.id)) { generateButton(res); } }); } loadMainDataFromJson() .then((resp) => { generateMain(resp); }) .catch((err) => { // console.log(err); loadMainDataFromBackEnd(); }); }) .catch((err) => { // console.log(err); loadButtonDataFromBackEnd(); }); loadHeaderDataFromJson() .then((resp) => { generateHeader(resp); }) .catch((err) => { // console.error(err); loadHeaderDataFromBackEnd(); }); var buttonContentLoaded = false; //WebRTC load function loadWebRTCDataFromJson() { let promise = new Promise((resolve, reject) => { var buttonContentPath = "https://storage.googleapis.com/hotensifycloud.appspot.com/images/" + h + "/conf/webrtc.json?nocache=" + new Date().getTime(); fetch(buttonContentPath, { mode: "cors", headers: { "Content-Type": "application/json", }, }) .then((response) => { return response.json(); }) .then((data) => { // Work with JSON data here resolve(data); }) .catch((e) => { // console.error(e); reject(e); }); }); return promise; } function loadWebRTCDataFromBackEnd() { sendHttpRequest( "POST", "https://us-central1-hotensifycloud.cloudfunctions.net/getWebRTC", "customer=" + h ).then((rtcSet) => { initPBXset(rtcSet); }); } loadWebRTCDataFromJson() .then((resp) => { initPBXset(resp); }) .catch((err) => { // console.error(err); loadWebRTCDataFromBackEnd(); });