content/hifi-content/Experiences/LoadTest/FeedbackApp/AppScripts/feedbackApp.js
2022-02-13 23:16:46 +01:00

145 lines
No EOL
5.1 KiB
JavaScript

"use strict";
//
// feedbackApp.js
// tablet-app
//
// Created by Robin Wilson and Mark Brosche on Aug. 31, 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 AccountServices */
(function () {
var EVENT_NAME = "loadTest_10_6_2018";
var VOTED = "voted";
// Every great app starts with a great name (keep it short so that it can fit in the tablet button)
var APP_NAME = "FEEDBACK";
// Link to your app's HTML file
var APP_URL = "https://hifi-content.s3.amazonaws.com/Experiences/LoadTest/FeedbackApp/AppScripts/feedbackAppWebPage.html?v2";
// Path to the icon art for your app
var APP_ICON_INACTIVE = "https://hifi-content.s3.amazonaws.com/Experiences/LoadTest/FeedbackApp/AppScripts/images/feedback-i.svg";
var APP_ICON_ACTIVE = "https://hifi-content.s3.amazonaws.com/Experiences/LoadTest/FeedbackApp/AppScripts/images/feedback-a.svg";
//Get a reference to the Google Sheets API
var GOOGLE_API_URL = "https://script.google.com/macros/s/AKfycbyWC4btUmE3LD6_dV2Nfkoc6C9FPKtRwtQRr23dNAMcX2ur-f0u/exec";
var HAS_FEEDBACK_APP_SETTING = 'io.highfidelity.feedbackEnabled.appPresent' + EVENT_NAME;
var FEEDBACK_APP_LOCATION = "./feedbackApp.js?v2";
var DEBUG = false;
// Get a reference to the tablet
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system");
// The following lines create a button on the tablet's menu screen
var button = tablet.addButton({
icon: APP_ICON_INACTIVE,
activeIcon: APP_ICON_ACTIVE,
text: APP_NAME
});
// When user clicks the app button, we'll display our app on the tablet screen
function onClicked() {
tablet.gotoWebScreen(APP_URL);
button.editProperties({
isActive: true
});
}
// Handle the events we're recieving from the web UI
function onWebEventReceived(event) {
// Converts the event to a JavasScript Object
if (typeof event === "string") {
try {
event = JSON.parse(event);
} catch (e) {
console.error(e);
}
}
if (event.type === "click" && button.getProperties().isActive) {
if (DEBUG) {
print("Rating ", event.data);
}
// Define the parameters to output from HiFi to Google Sheets and send them.
var params = {
username: AccountServices.username,
displayName: MyAvatar.displayName,
date: Date(Date.now()),
event: EVENT_NAME,
rating: event.data,
isApp: true,
UUID: MyAvatar.sessionUUID
};
sendInput(params);
}
}
// Check if the Feedback app is open and set the status.
function onScreenChanged(type, url) {
if (button) {
var isOpen = (url === APP_URL);
button.editProperties({
isActive: isOpen
});
}
}
// Encode params into a string to send.
function encodeURLParams(params) {
var paramPairs = [];
for (var key in params) {
paramPairs.push(key + "=" + params[key]);
}
return paramPairs.join("&");
}
// Send input
function sendInput(params) {
var paramString = encodeURLParams(params);
var request = new XMLHttpRequest();
request.open('GET', GOOGLE_API_URL + "?" + paramString);
request.timeout = 10000;
request.onreadystatechange = function () { // called after load when readyState = 4
if (request.readyState === 4) {
Settings.setValue(HAS_FEEDBACK_APP_SETTING, VOTED);
Script.setTimeout(function () {
// do the unload
cleanup();
}, 2000);
}
};
request.send();
}
function onDomainChanged(domain) {
cleanup();
}
// "Uninstall" the app after user has made a selection.
function cleanup() {
if (button) {
button.editProperties({
isActive: false
});
button.clicked.disconnect(onClicked);
tablet.removeButton(button);
button = null;
}
tablet.gotoHomeScreen();
tablet.webEventReceived.disconnect(onWebEventReceived);
tablet.screenChanged.disconnect(onScreenChanged);
Script.scriptEnding.disconnect(cleanup);
Script.stop();
ScriptDiscoveryService.stopScript(FEEDBACK_APP_LOCATION);
}
button.clicked.connect(onClicked);
tablet.webEventReceived.connect(onWebEventReceived);
tablet.screenChanged.connect(onScreenChanged);
Script.scriptEnding.connect(cleanup);
Window.domainChanged.connect(onDomainChanged);
}());