overte/scripts/system/html/js/miniTablet.js

172 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2018-08-21 08:36:04 +12:00
//
// miniTablet.js
//
// Created by David Rowe on 20 Aug 2018.
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
/* global EventBridge */
/* eslint-env browser */
(function () {
"use strict";
var // EventBridge
READY_MESSAGE = "ready", // Engine <== Dialog
2018-08-21 10:28:23 +12:00
HOVER_MESSAGE = "hover", // Engine <== Dialog
UNHOVER_MESSAGE = "unhover", // Engine <== Dialog
2018-08-21 08:36:04 +12:00
MUTE_MESSAGE = "mute", // Engine <=> Dialog
2018-09-07 16:43:21 +12:00
GOTO_MESSAGE = "goto", // Engine <=> Dialog
2018-08-21 09:17:53 +12:00
EXPAND_MESSAGE = "expand", // Engine <== Dialog
2018-08-21 08:36:04 +12:00
muteButton,
muteImage,
2018-09-07 16:43:21 +12:00
gotoButton,
gotoImage,
expandButton,
// Work around buttons staying hovered when mini tablet is replaced by tablet proper then subsequently redisplayed.
isUnhover = true;
function setUnhover() {
if (!isUnhover) {
if (gotoButton) {
gotoButton.classList.add("unhover");
}
expandButton.classList.add("unhover");
isUnhover = true;
}
}
function clearUnhover() {
if (isUnhover) {
if (gotoButton) {
gotoButton.classList.remove("unhover");
}
expandButton.classList.remove("unhover");
isUnhover = false;
}
}
2018-08-21 08:36:04 +12:00
function onScriptEventReceived(data) {
var message;
try {
message = JSON.parse(data);
} catch (e) {
console.error("EventBridge message error");
return;
}
switch (message.type) {
case MUTE_MESSAGE:
if (muteImage) {
muteImage.src = message.icon;
}
2018-08-21 08:36:04 +12:00
break;
2018-09-07 16:43:21 +12:00
case GOTO_MESSAGE:
if (gotoImage) {
gotoImage.src = message.icon;
}
2018-08-21 08:36:04 +12:00
break;
}
}
function onBodyHover() {
EventBridge.emitWebEvent(JSON.stringify({
type: HOVER_MESSAGE,
target: "body"
}));
}
function onBodyUnhover() {
EventBridge.emitWebEvent(JSON.stringify({
type: UNHOVER_MESSAGE,
target: "body"
}));
}
function onButtonHover() {
2018-08-21 09:17:53 +12:00
EventBridge.emitWebEvent(JSON.stringify({
type: HOVER_MESSAGE,
target: "button"
2018-08-21 09:17:53 +12:00
}));
clearUnhover();
2018-08-21 09:17:53 +12:00
}
function onMuteButtonClick() {
2018-08-21 10:28:23 +12:00
EventBridge.emitWebEvent(JSON.stringify({
type: MUTE_MESSAGE
2018-08-21 10:28:23 +12:00
}));
}
2018-09-07 16:43:21 +12:00
function onGotoButtonClick() {
setUnhover();
2018-08-21 09:17:53 +12:00
EventBridge.emitWebEvent(JSON.stringify({
2018-09-07 16:43:21 +12:00
type: GOTO_MESSAGE
2018-08-21 09:17:53 +12:00
}));
}
function onExpandButtonClick() {
setUnhover();
2018-08-21 09:17:53 +12:00
EventBridge.emitWebEvent(JSON.stringify({
type: EXPAND_MESSAGE
}));
}
2018-08-21 08:36:04 +12:00
function connectEventBridge() {
EventBridge.scriptEventReceived.connect(onScriptEventReceived);
EventBridge.emitWebEvent(JSON.stringify({
type: READY_MESSAGE
}));
}
function disconnectEventBridge() {
EventBridge.scriptEventReceived.disconnect(onScriptEventReceived);
}
function onUnload() {
disconnectEventBridge();
}
function onLoad() {
muteButton = document.getElementById("mute");
2018-09-07 16:43:21 +12:00
gotoButton = document.getElementById("goto");
2018-08-21 09:17:53 +12:00
expandButton = document.getElementById("expand");
2018-08-21 08:36:04 +12:00
connectEventBridge();
document.body.addEventListener("mouseenter", onBodyHover, false);
document.body.addEventListener("mouseleave", onBodyUnhover, false);
if (muteButton) {
muteImage = document.getElementById("mute-img");
muteButton.addEventListener("mouseenter", onButtonHover, false);
muteButton.addEventListener("click", onMuteButtonClick, true);
}
if (gotoButton) {
gotoImage = document.getElementById("goto-img");
gotoButton.addEventListener("mouseenter", onButtonHover, false);
gotoButton.addEventListener("click", onGotoButtonClick, true);
}
2018-08-21 10:28:23 +12:00
expandButton.addEventListener("mouseenter", onButtonHover, false);
2018-08-21 09:17:53 +12:00
expandButton.addEventListener("click", onExpandButtonClick, true);
2018-08-21 08:36:04 +12:00
document.body.onunload = function () {
onUnload();
};
}
onLoad();
}());