content/hifi-content/thoys/dev/2017/foodDispenserApp/foodDispenserApp.html
2022-02-14 02:04:11 +01:00

87 lines
3.1 KiB
HTML

<html>
<head>
<title>FoodDispensers</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<h1>FoodDispensers</h1>
<div id="dispensers">
</div>
<div id="buttons">
<button id='launchButton'>Launch</button>
<button id='refreshButton'>Refresh</button>
</div>
</body>
<style>
button {
width: 100%;
height: 42px;
}
#dispensers div {
border: 1px solid black;
}
#dispensers {
margin-bottom: 30px;
}
#buttons button {
margin-right: 20px;
}
</style>
<script>
$(function() {
var $launchButton = $('#launchButton');
function emitJSONEvent(jsonData) {
EventBridge.emitWebEvent(JSON.stringify(jsonData));
}
function refreshDispensers() {
emitJSONEvent({
action: 'refresh'
});
}
EventBridge.scriptEventReceived.connect(function (message) {
var data = JSON.parse(message);
var action = data['action'];
switch (action) {
case "dispensersUpdate":
var $dispensers = $('#dispensers');
$dispensers.empty();
data.dispensers.forEach(function(dispenser) {
$dispensers.append(
$('<div>')
.append(
$('<h2>').text(dispenser.name)
)
.append(
$('<button>')
.text(dispenser.filename !== undefined ? dispenser.filename : 'Browse')
.data('id', dispenser.id)
.on('click', function() {
var id = $(this).data('id');
emitJSONEvent({
action: 'setDispenserJSON',
id: id
});
})
)
);
});
break;
default:
console.log("Unexpected action received: " + action + " with message: " + message);
break;
}
});
$launchButton.on('click', function() {
emitJSONEvent({action: 'launch'});
})
refreshDispensers();
});
</script>
</html>