From 80a75d9809aad65159c1cd9b36b5a6544db0e1ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Erd=C5=91si?= Date: Fri, 22 Nov 2019 13:37:19 +0100 Subject: Add dashboard widget (#76) Add weather dashboard widget --- js/widget.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 js/widget.js (limited to 'js') diff --git a/js/widget.js b/js/widget.js new file mode 100644 index 0000000..448f545 --- /dev/null +++ b/js/widget.js @@ -0,0 +1,78 @@ +/** + * + * @copyright Copyright (c) 2019, Balint Erdosi (erdosib@gmail.com) + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +/** global: OCA */ +/** global: net */ + +(function () { + + /** + * @constructs Weather + */ + var Weather = function() { + } + + Weather.prototype.divWeather = null; + Weather.prototype.init = function() { + this.getWeather(); + + } + + Weather.prototype.getWeather = function() { + var request = { + widget: "weather", + request: "getWeather" + }; + + net.requestWidget(request, this.updateWeather); + } + + Weather.prototype.updateWeather = function(result) { + var divWeather = document.querySelector("#widget-weather"); + var divInfo = divWeather.querySelector(".info"); + var temperatureRepresentationLookup = { + "kelvin": "°K", + "imperial":"°F", + "metric": "°C" + } + if (result.value.error) { + divInfo.classList.add("error"); + divInfo.innerHTML = "Failed to update: " + result.value.error; + return; + } + try { + divInfo.classList.remove("error"); + divInfo.innerHTML = ""; + divWeather.querySelector(".locationValue").innerHTML = result.value.location; + divWeather.querySelector(".temperatureValue").innerHTML = result.value.temperature; + divWeather.querySelector(".temperatureRepresentation").innerHTML = temperatureRepresentationLookup[result.value.metric]|| "ERROR"; + divWeather.querySelector(".weatherValue").innerHTML = result.value.weather; + divWeather.querySelector(".humidityValue").innerHTML = result.value.humidity; + divWeather.querySelector(".windValue").innerHTML = result.value.wind; + } catch (e) { + divInfo.classList.add("error"); + divInfo.innerHTML = "Failed to update some data."; + } + } + + OCA.DashBoard.Weather = Weather; + OCA.DashBoard.weather = new Weather(); +})() -- cgit v1.2.3