(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) {
var frame_main=$('
')
.prop('id','frame_main')
.addClass('ui-content')
.append($('').addClass('ui-listview'))
parseWidgets(sitemap.homepage.widgets, frame_main);
$('#page_main header').after(frame_main);
}
function createSubFrame(wdg, parent) {
var page_name = 'page_' + __page_count.toString();
var newlist = $('').addClass('ui-listview')
var newframe = $('')
.addClass('ui-content')
.prop('id', 'frame_' + __page_count.toString())
.append(newlist);
if (parent.parent()){
var parent_name = parent.parent().prop('id');
} else {
var parent_name = 'page_main';
}
var sub_name = ('label' in wdg)?wdg.label:wdg.title;
var newpage = $('')
.addClass('ui-page')
.prop('id', page_name)
.append($('')
.append($('')
.append($('')
.text(sub_name)
.attr('href','#'+parent_name)
)
)
)
.append(newframe);
parent.append($('').append($('').append($('')
.text(sub_name)
.attr('href','#'+page_name))));
$("body").append(newpage);
__page_count += 1;
return newlist;
}
function createSwitch(wdg, page) {
if (wdg.mappings.length === 0) {
page.append($('')
.append($('').addClass('ui-switch')
.append($('').addClass('ui-switch-text').text(wdg.label))
.append($('').addClass('ui-toggleswitch')
.append($('')
.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($('').addClass('ui-switch-activation')
.append($('').addClass('ui-switch-inneroffset')
.append($('').addClass('ui-switch-handler'))))
)
));
} else if (wdg.mappings.length === 1) {
page.append($('').append($('')
.append($('').text(wdg.label))
.append($('')
.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)
.click(function(){
updateItem($(this).prop('data-link'),$(this).val());
}))
));
}
}
function createSelection(wdg, page) {
var name=btoa(wdg.label).replace(/[\/+=]/g,'');
var ul=$('')
.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=$('').attr('type','radio').attr('name',name).attr('value',entry.command);
if (wdg.item.state == entry.command){
item.attr('checked','checked');
}
ul.append($('').text(entry.label).append(item)).append($('
'));
});
page.append($('')
.prop('id','pop_'+name)
.addClass('ui-popup')
.append($('').addClass('ui-popup-header').text(wdg.label))
.append($('').addClass('ui-popup-content').append(ul))
)
page.append($('').append($('').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($('').append($('')
.append($('').text(wdg.label))
.append($('
'))
.append($('')
.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($('').append($('').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($('').append($('').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);