content/hifi-content/Experiences/Releases/events/loveseat/scripts/html/setPiecesDirector.js
2022-02-13 23:16:46 +01:00

120 lines
3 KiB
JavaScript

/*!
setPiecesDirector.js
Created by David Rowe on 8 Aug 2019.
Copyright 2019 David Rowe.
Distributed under the Apache License, Version 2.0.
See: http://www.apache.org/licenses/LICENSE-2.0.html
*/
/* eslint-env browser */
/* global EventBridge */
(function () {
"use strict";
var
// Event bridge commands.
ADD_GROUP = "add-group", // Main script ==> Dialog
SET_BUTTON = "set-button", // Main script ==> Dialog
TOGGLE_BUTTON = "toggle-button", // Main script <== Dialog
SET_COUNTDOWN = "set-countdowm", // Main script ==> Dialog
groupTable;
function onButtonClick(event) {
var id;
id= event.target.parentNode.parentNode.id;
EventBridge.emitWebEvent(JSON.stringify({
command: TOGGLE_BUTTON,
id: id
}));
}
function addGroup(id, group) {
var tr,
td,
input;
tr = document.createElement("tr");
tr.id = id;
td = document.createElement("td");
td.innerHTML = group.name;
tr.appendChild(td);
td = document.createElement("td");
input = document.createElement("input");
input.type = "button";
input.value = group.isOut ? "OUT" : "IN";
input.className = group.isOut ? "out" : "in";
input.addEventListener("click", onButtonClick);
td.appendChild(input);
tr.appendChild(td);
td = document.createElement("td");
tr.appendChild(td);
groupTable.appendChild(tr);
}
function setButton(id, isOut) {
var tr,
input;
tr = document.getElementById(id);
input = tr.getElementsByTagName("input")[0];
input.value = isOut ? "OUT" : "IN";
input.className = isOut ? "out" : "in";
}
function setCountDown(id, value) {
var tr,
td;
tr = document.getElementById(id);
td = tr.lastChild;
td.innerHTML = value > 0 ? value.toFixed(1) + " s" : "";
}
function onScriptEvent(message) {
var data;
data = JSON.parse(message);
switch (data.command) {
case ADD_GROUP:
addGroup(data.id, data.group);
break;
case SET_BUTTON:
setButton(data.id, data.isOut);
break;
case SET_COUNTDOWN:
setCountDown(data.id, data.value);
break;
default:
print("ERROR: Unexpected message! - " + message);
}
}
function onLoad() {
groupTable = document.getElementById("groups");
EventBridge.scriptEventReceived.connect(onScriptEvent);
}
function onUnload() {
EventBridge.scriptEventReceived.disconnect(onScriptEvent);
}
onLoad();
document.body.onunload = function () {
onUnload();
};
}());