openhab2_tizen/js/app.js
2019-04-07 14:49:51 +02:00

77 lines
No EOL
1.8 KiB
JavaScript

(function() {
window.addEventListener("tizenhwkey", function(ev) {
var activePopup = null, page = null, pageId = "";
if (ev.keyName === "back") {
activePopup = document.querySelector(".ui-popup-active");
page = document.getElementsByClassName("ui-page-active")[0];
pageId = page ? page.id : "";
if (pageId === "main" && !activePopup) {
try {
tizen.application.getCurrentApplication().exit();
} catch (ignore) {
}
} else {
window.history.back();
}
}
});
}());
function parseSitemap(sitemap) {
var output = parseWidgets(sitemap.homepage.widgets);
document.getElementById("main_text").innerHTML = output;
}
function parseWidgets(widgets) {
var s="";
widgets.forEach( function(wdg) {
switch (wdg.type) {
case "Frame":
s+="<p><span>"+wdg.label+"</span><br/>";
if (("widgets" in wdg) && wdg.widgets.length >0){
s+=parseWidgets(wdg.widgets);
}
s+="</p>";
break;
case "Switch":
break;
case "Slider":
var value=0;
if (wdg.item.state !== null){value = wdg.item.state;}
s+=createElement(wdg.label, wdg.item.name,'<input data-freq="200" data-state="'+value.toString()+'" type="range" min="0" max="100" tabindex="0" value="'+value.toString()+'" />');
break;
case "Selection":
s+="Selection<br/>";
break;
case "Text":
s+=createElement(wdg.label,'','');
break;
default:
s+="Other<br/>";
break;
}
});
return s;
}
function createElement(label, id, inner){
return '<div><span>'+label+'</span><span data-item="'+id+'" >'+inner+'</span></div>';
}
function loadgui() {
var client = new XMLHttpRequest();
client.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
var myArr = JSON.parse(this.responseText);
parseSitemap(myArr);
}
};
client.open('GET', 'http://habctrl:8080/rest/sitemaps/default', true);
client.send();
}