content/hifi-content/brosche/DomainContent/LoadTest/FeedbackApp/feedbackApp.js
2022-02-13 21:50:01 +01:00

172 lines
5.4 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://s3.amazonaws.com/hifi-content/Experiences/LoadTest/FeedbackApp/feedbackAppWebPage.html?v2";
// Path to the icon art for your app
var APP_ICON_INACTIVE = "https://s3.amazonaws.com/hifi-content/Experiences/LoadTest/FeedbackApp/images/feedback-i.svg";
var APP_ICON_ACTIVE = "https://s3.amazonaws.com/hifi-content/Experiences/LoadTest/FeedbackApp/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 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") {
if (DEBUG) {
print("Rating ", event.data);
}
if(button.isActive === true){
// Define the parameters to output from HiFi to Google Sheets and send them.
var params = {
username: AccountServices.username,
displayName: MyAvatar.displayName,
date: Date.now(),
event: EVENT_NAME,
rating: event.data,
isApp: true,
UUID: MyAvatar.sessionUUID
};
sendInput(params);
Script.setTimeout(function () {
// Unload the app.
cleanup();
}, 2000);
}
}
}
// Check if the Feedback app is open and set the status.
function onScreenChanged(type, url) {
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
print("Finished feedback");
if (request.readyState === 4) {
Settings.setValue(HAS_FEEDBACK_APP_SETTING, VOTED);
cleanup();
}
};
request.send();
}
// "Uninstall" the app after user has made a selection.
function cleanup() {
button.editProperties({
isActive : false
});
tablet.removeButton(button);
tablet.gotoHomeScreen();
ScriptDiscoveryService.stopScript(FEEDBACK_APP_LOCATION);
}
button.clicked.connect(onClicked);
tablet.webEventReceived.connect(onWebEventReceived);
tablet.screenChanged.connect(onScreenChanged);
Script.scriptEnding.connect(cleanup);
}());
/*
function setSettingsName() {
appSettingsName = HAS_FEEDBACK_APP_SETTING;
var listOfEntities = Entities.findEntityByName("feedbackAppLoader", MyAvatar.position, 100);
var properties;
if (listOfEntities.length > 1) {
properties = Entities.getEntityProperties(listOfEntities[0], ["userData"]);
}
try {
var userData = JSON.parse(properties.userData);
if (userData.eventName) {
appSettingsName = HAS_FEEDBACK_APP_SETTING + eventName;
}
} catch (e) {
console.error("Issue parsing userData for feedbackLoader", e);
}
}
setSettingsName();
*/