openhab2_tizen/js/app.js

202 lines
No EOL
4.9 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');
console.log(parent_name);
var sub_name = ('label' in wdg)?wdg.label:wdg.title;
var newpage = $('<div/>')
.addClass('ui-page')
.prop('id', page_name)
.append($('<header/>')
.append($('<h2/>')
.text(sub_name)
.click(function(){tau.changePage('#'+parent_name);})
)
)
.append(newframe);
parent.append($('<div/>')
.text(sub_name)
.click(function(){tau.changePage('#'+page_name,null,'#'+parent_name,true);}));
$("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 selector=$('<div/>').addClass('ui-selector').hide();
wdg.mappings.forEach(function(entry) {
selector.append($('<div/>')
.addClass('ui-item')
.attr('data-title', entry.label)
.attr('data-value', entry.command))
});
page.append(selector);
page.append($('<div/>').text(wdg.label).click(function(){
tau.widget.Selector(selector);
selector.click(function(){
var activeItem = selector.querySelector(".ui-item-active");
console.log(activeItem.getAttribute("data-title"));
selector.hide();
});
selector.show();
}));
}
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"));
break;
}
});
}
function updateItem(item, value) {
$.post({url:item,
data: value.toString(),
contentType: 'text/plain'});
}
function changePage(to){
console.log('next page:'+to);
tau.changePage(to);
}
function loadgui() {
$.get("http://habctrl:8080/rest/sitemaps/default", parseSitemap);
}
$(document).ready(loadgui);