openhab2_tizen/js/app.js
2019-04-10 21:00:06 +02:00

215 lines
No EOL
5.4 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 === "page_main") && !activePopup) {
try {
tizen.application.getCurrentApplication().exit();
} catch (ignore) {
}
} else {
window.history.back();
}
}
});
}());
__page_count = 0;
function parseSitemap(sitemap, status) {
parseWidgets(sitemap.homepage.widgets, $('#frame_main'));
$('#page_main').addClass('ui-page-active');
$("body").append($('#page_main'));
}
function createSubFrame(wdg, parent) {
var page_name = 'page_' + __page_count.toString();
var newframe = $('<div/>')
.addClass('ui-content')
.prop('id', 'frame_' + __page_count.toString());
var parent_name = parent.parent().prop('id');
var sub_name = ('label' in wdg)?wdg.label:wdg.title;
var newpage = $('<div/>')
.addClass('ui-page')
.prop('id', page_name)
.append($('<header/>')
.append($('<h2/>')
.append($('<a/>')
.text(sub_name)
.attr('href','#'+parent_name)
)
)
)
.append(newframe);
parent.append($('<div/>').append($('<a/>')
.text(sub_name)
.attr('href','#'+page_name)));
$("body").append(newpage);
__page_count += 1;
return newframe;
}
function createSwitch(wdg, page) {
if (wdg.mappings.length === 0) {
page.append($('<div/>').addClass('ui-switch')
.append($('<div/>').addClass('ui-switch-text').text(wdg.label))
.append($('<label/>').addClass('ui-toggleswitch')
.append($('<input/>')
.prop('type','checkbox')
.prop('data-link', wdg.item.link)
.prop('data-item', wdg.item.name)
.addClass('ui-switch-input')
.change(function(){
updateItem($(this).prop('data-link'),$(this).is(':checked')?"ON":"OFF");
}))
.append($('<div/>').addClass('ui-switch-activation')
.append($('<div/>').addClass('ui-switch-inneroffset')
.append($('<div/>').addClass('ui-switch-handler'))))
)
);
} else if (wdg.mappings.length === 1) {
page.append($('<div/>')
.append($('<span/>').text(wdg.label))
.append($('<button/>')
.prop('data-link', wdg.item.link)
.prop('data-item', wdg.item.name)
.prop('type','button')
.prop('value',wdg.mappings[0].command)
.text(wdg.mappings[0].label)
.change(function(){
updateItem($(this).prop('data-link'),$(this).val());
}))
);
}
}
function createSelection(wdg, page) {
var name=btoa(wdg.label).replace(/[\/+=]/g,'');
var ul=$('<div/>')
.prop('data-link',wdg.item.link)
.change(function(){
var checked = $("input[name='"+name+"']:checked");
if (checked !== undefined){
updateItem($(this).prop('data-link'),checked.val());
}
});
wdg.mappings.forEach(function(entry) {
var item=$('<input/>').attr('type','radio').attr('name',name).attr('value',entry.command);
if (wdg.item.state == entry.command){
item.attr('checked','checked');
}
ul.append($('<label/>').text(entry.label).append(item)).append($('<br/>'));
});
page.append($('<div/>')
.prop('id','pop_'+name)
.addClass('ui-popup')
.append($('<div/>').addClass('ui-popup-header').text(wdg.label))
.append($('<div/>').addClass('ui-popup-content').append(ul))
)
page.append($('<a/>').attr('href','#pop_'+name).addClass('ui-btn').attr('data-rel','popup').text(wdg.label))
}
function createSlider(wdg, page) {
var value = 0;
if (wdg.item.state !== null) {
value = wdg.item.state;
}
page.append($('<div/>')
.append($('<span/>').text(wdg.label))
.append($('<input/>')
.prop('data-link', wdg.item.link)
.prop('data-item', wdg.item.name)
.prop('type','range')
.prop('data-freq','200')
.prop('data-state',value)
.prop('min','0')
.prop('max','100')
.prop('value',value)
.change(function(){
updateItem($(this).prop('data-link'),$(this).val());
}))
);
}
function createText(wdg, page) {
if ('linkedPage' in wdg) {
var newframe = createSubFrame(wdg.linkedPage, page);
if (("widgets" in wdg.linkedPage) && wdg.linkedPage.widgets.length > 0) {
parseWidgets(wdg.linkedPage.widgets, newframe);
}
} else {
page.append($('<div/>').text(wdg.label));
}
}
function parseWidgets(widgets, parent) {
widgets.forEach(function (wdg) {
switch (wdg.type) {
case "Frame":
var newframe = createSubFrame(wdg, parent);
if (("widgets" in wdg) && wdg.widgets.length > 0) {
parseWidgets(wdg.widgets, newframe);
}
break;
case "Switch":
createSwitch(wdg, parent);
break;
case "Slider":
createSlider(wdg, parent);
break;
case "Selection":
createSelection(wdg, parent);
break;
case "Text":
createText(wdg, parent);
break;
default:
parent.append($('<div/>').text("other["+wdg.type+"]"));
break;
}
});
}
function changePage(to,transition,back,change){
console.log(document.location.href);
console.log(to);
console.log(transition);
console.log(back);
console.log(change);
tau.changePage(to,transition,back,change);
console.log(document.location.href);
}
function updateItem(item, value) {
$.post({url:item,
data: value.toString(),
contentType: 'text/plain'});
}
function loadgui() {
$.get("http://habctrl:8080/rest/sitemaps/default", parseSitemap);
}
$(document).ready(loadgui);