openhab2_tizen/js/app.js
2019-04-08 01:36:25 +02:00

190 lines
No EOL
4.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 === "page_0" || pageId === "page_unnamed") && !activePopup) {
try {
tizen.application.getCurrentApplication().exit();
} catch (ignore) {
}
} else {
window.history.back();
}
}
});
}());
__page_count = 0;
__unnamed_page = $('<div/>')
.addClass('ui-page')
.prop('id', 'page_unnamed')
.append($('<div/>').addClass('ui-header').text("Main"))
.append($('<div/>').addClass('ui-content').prop('id', 'frame_unnamed'));
function parseSitemap(sitemap, status) {
parseWidgets(sitemap.homepage.widgets, __unnamed_page);
if (__page_count === 0) {
__unnamed_page.addClass('ui-page-active');
}
$("body").append(__unnamed_page);
var elPageIndicator = document.getElementById("pageIndicator"),
pageIndicator = tau.widget.PageIndicator(elPageIndicator, {numberOfPages: __page_count+1}),
index = 0;
pageIndicator.setActive(0);
}
function createFrame(wdg, sitemap) {
var newframe = $('<div/>')
.addClass('ui-content')
.prop('id', 'frame_' + __page_count.toString());
var newpage = $('<div/>')
.addClass('ui-page')
.prop('id', 'page_' + __page_count.toString())
.append($('<div/>').addClass('ui-header').text(wdg.label))
.append(newframe);
if (__page_count === 0) {
newpage.addClass('ui-page-active');
}
__page_count += 1;
$("body").append(newpage);
return newframe;
}
function createSwitch(wdg, page) {
}
function createSelection(wdg, page) {
}
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) {
page.append($('<div/>').text(wdg.label));
}
function parseWidgets(widgets, parent) {
widgets.forEach(function (wdg) {
switch (wdg.type) {
case "Frame":
var newframe = createFrame(wdg, null);
if (("widgets" in wdg) && wdg.widgets.length > 0) {
parseWidgets(wdg.widgets, newframe);
}
break;
case "Switch":
// if (wdg.mappings.length === 0) {
// s += createElement(
// wdg.label,
// wdg.item.name,
// wdg.item.link,
// '<div class="ui-toggleswitch ui-toggleswitch-large">'
// + '<input type="checkbox" class="ui-switch-input"
// onchange=updateToggle(event) />'
// + '<div class="ui-switch-button"></div>'
// + '</div>');
//
// // <div class="ui-switch">
// // <div class="ui-switch-text">
// // Single Toggle
// // </div>
// // <div class="ui-toggleswitch ui-toggleswitch-large">
// // <input type="checkbox" class="ui-switch-input"
// // id="toggleControl">
// // <div class="ui-switch-button"></div>
// // </div>
// // <div class="ui-switch-sub-text">
// // Toggle state
// // </div>
// // </div>
// } else if (wdg.mappings.length === 1) {
// s += createElement(wdg.label, wdg.item.name,
// wdg.item.link, '<button type="button" value="'
// + wdg.mappings[0].command
// + '" onchange=updateSimple(event)>'
// + wdg.mappings[0].label + '</button>');
// }
break;
case "Slider":
createSlider(wdg, parent);
break;
case "Selection":
// var items = '';
// wdg.mappings.forEach(function(entry) {
// items += '<div class="ui-item"
// data-title="'+entry.label+'"
// data-value="'+entry.command+'" ></div>';
// });
// s+= createElement(wdg.label,
// wdg.item.name,
// wdg.item.link,
// '<div class="ui-selector">'+items+'</div>');
break;
case "Text":
createText(wdg, parent);
break;
default:
parent.append($('<div/>'.text = "other"));
break;
}
});
}
function updateToggle(event) {
var item = event.target.parentElement.parentElement
.getAttribute('data-link');
var value = event.target.checked === true ? "ON" : "OFF";
updateItem(item, value);
}
function updateSelector(event) {
var s = 3;
return true;
}
function updateSimple(event) {
var item = event.target.parentElement.getAttribute('data-link');
var value = event.target.value;
updateItem(item, value);
}
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);