class BlueWeather { constructor() { // cunstruct xmlhttp this.xhttp = new XMLHttpRequest(); this.host = "https://weather.itsblue.de"//window.location.href.substring(0, window.location.href.lastIndexOf("/")); this.log("init done", 2) this.session = { loggedIn: false, token: "", user: { realName: "Real Name", username: "username" } } this.restoreSession() } // ---------------------- // - session management - // ---------------------- restoreSession() { if(this.getCookie("session") !== "" && this.getCookie("session") !== undefined) { console.log("old session found: " + this.getCookie("session"), 3) // old session found this.session.loggedIn = true this.session.token = this.getCookie("session") } } login(async, username, password, processingFunction) { this.xhttp.abort() var thisObject = this if(async) { this.xhttp.onreadystatechange = function() { if (this.readyState === 4) { // Typical action to be performed when the document is ready: if(this.status === 200) { thisObject.log("login response was: " + this.responseText, 3) var ret = JSON.parse(this.responseText) if(ret && ret['status'] === 200) { //this.session.token = ret["token"] //this.session.loggedIn = true processingFunction(true) return } else { //this.session.token = "" //this.session.loggedIn = false } } processingFunction(false) } } } var url = this.host + "/api/json.php" this.log("starting location request; URL is: " + url, 3) this.xhttp.open("GET", url, async) this.xhttp.send() } endSession() { document.cookie = "session=;" window.location.href = "./index.html" } // -------------------- // - getter functions - // -------------------- getLocations(async, processingFunction, errorFunction) { this.xhttp.abort() var thisObject = this if(async) { this.xhttp.onreadystatechange = function () { if (this.readyState === 4) { // Typical action to be performed when the document is ready: if(this.status === 200) { thisObject.log("getting sensors response was: " + this.responseText, 3) var ret = JSON.parse(this.responseText) if(ret){ processingFunction(ret); return } } if(errorFunction !== null) { errorFunction(this.status, this.text) } } }; } var url = this.host + "/api/json.php" this.log("starting location request; URL is: " + url, 3) this.xhttp.open("GET", url, async) this.xhttp.send() } getLocationData(locId, range = {from:"", to:""}, maxVals = 0, async, processingFunction, errorFunction) { this.xhttp.abort() var thisObject = this if(async) { this.xhttp.onreadystatechange = function () { if (this.readyState === 4) { // Typical action to be performed when the document is ready: if(this.status === 200) { thisObject.log("getting sensors response was: " + this.responseText, 3) var ret = JSON.parse(this.responseText) if(ret){ processingFunction(ret); return } } if(errorFunction !== null) { errorFunction(this.status, this.text) } } }; } var url = this.host + "/api/json.php?locId="+locId+"&range[from]="+range.from + "&range[to]="+range.to + "&maxVals[mode]=" + maxVals.mode + "&maxVals[count]=" + maxVals.count this.log("starting location request; URL is: " + url, 3) this.xhttp.open("GET", url, async) this.xhttp.send() } // ---------------------- // - template functions - // ---------------------- loadProfileObject(parent) { if(!parent) { return false } var thisObject = this var profileObject = document.createElement("li") parent.appendChild(profileObject) profileObject.classList.add("nav-item") if(!this.session.loggedIn) { this.log("not logged in", 3) profileObject.classList.add("text-nowrap") // create login button var loginButton = document.createElement('button') profileObject.appendChild(loginButton) loginButton.type = "button" loginButton.setAttribute("data-toggle", "modal") loginButton.setAttribute("data-target", "#loginModal") loginButton.classList.add("btn", "btn-outline-light") loginButton.innerHTML = "Login" // ------------ // create modal var loginModal = document.createElement('div') loginModal.id = "loginModal" loginModal.setAttribute("tabindex", "-1") loginModal.setAttribute("role", "dialog") loginModal.setAttribute("aria-labelledby", "exampleModalLabel") loginModal.setAttribute("aria-hidden", "true") loginModal.classList.add("modal", "fade") // create login form var loginDiv = document.createElement("div") loginModal.appendChild(loginDiv) loginDiv.classList.add("modal-dialog", "modal-login") loginDiv.setAttribute("role", "document") // - modal content var modalContent = document.createElement("div") loginDiv.appendChild(modalContent) modalContent.classList.add("modal-content") // -- modal header var modalHeader = document.createElement("div") modalContent.appendChild(modalHeader) modalHeader.classList.add("modal-header") // --- close button var modalCloseButton = document.createElement("button") modalHeader.appendChild(modalCloseButton) modalCloseButton.setAttribute("type", "button") modalCloseButton.setAttribute("data-dismiss", "modal") modalCloseButton.setAttribute("aria-label", "Close") modalCloseButton.classList.add("close") modalCloseButton.innerHTML = '
' // -- modal body var modalBody = document.createElement("div") modalContent.appendChild(modalBody) modalBody.classList.add("modal-body", "text-center") // --- login form var loginForm = document.createElement("form") modalBody.appendChild(loginForm) loginForm.classList.add("form-signin") // ---- login form fieldset var fieldset = document.createElement("fieldset") loginForm.appendChild(fieldset) // ----- blueweather icon var loginFormIcon = document.createElement("img") fieldset.appendChild(loginFormIcon) loginFormIcon.width = "72" loginFormIcon.height = "72" loginFormIcon.src = "img/icon.png" loginFormIcon.alt = "" loginFormIcon.classList.add("mb-4") // ----- 'Please sign in' text var loginFormHeadline = document.createElement("h1") fieldset.append(loginFormHeadline) loginFormHeadline.classList.add("h3", "mb-3", "font-weight-normal") loginFormHeadline.innerHTML = "Please sign in" // ----- login failed alert var alert = document.createElement("div") fieldset.append(alert) alert.id = "loginFormLoginFailedAlert" alert.appear = function() { alert.innerHTML = '