/*jslint browser:true */
/*jslint maxlen: 180*/
"use strict";
//
// SnapshotReview.js
// scripts/system/html/js/
//
// Created by Howard Stearns 8/22/2016
// Copyright 2016 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
//
var paths = [];
var idCounter = 0;
var imageCount = 0;
var blastShareText = "Blast to my Connections",
blastAlreadySharedText = "Already Blasted to Connections",
hifiShareText = "Share to Snaps Feed",
hifiAlreadySharedText = "Already Shared to Snaps Feed",
facebookShareText = "Share to Facebook",
twitterShareText = "Share to Twitter",
shareButtonLabelTextActive = "SHARE:",
shareButtonLabelTextInactive = "SHARE";
function fileExtensionMatches(filePath, extension) {
return filePath.split('.').pop().toLowerCase() === extension;
}
function showSetupInstructions() {
var snapshotImagesDiv = document.getElementById("snapshot-images");
snapshotImagesDiv.className = "snapshotInstructions";
snapshotImagesDiv.innerHTML = '
' +
'
' +
'
Take and share snaps and GIFs with people in High Fidelity, Facebook, and Twitter.
' + "Before you can begin taking snaps, please choose where you'd like to save snaps on your computer:
" + 'Snapshot location set.
' + 'Press the big red button to take a snap!
' + 'Take and share snaps and GIFs with people in High Fidelity, Facebook, and Twitter.
' + 'Press the big red button to take a snap!
' + '
Preparing to Share';
shareBarHelp.classList.add("uploading");
shareBarHelp.setAttribute("data-destination", destination);
}
function hideUploadingMessageAndMaybeShare(selectedID, storyID) {
if (selectedID.id) {
selectedID = selectedID.id; // sometimes (?), `containerID` is passed as an HTML object to these functions; we just want the ID
}
var shareBarHelp = document.getElementById(selectedID + "shareBarHelp"),
shareBarHelpDestination = shareBarHelp.getAttribute("data-destination");
shareBarHelp.classList.remove("uploading");
if (shareBarHelpDestination) {
switch (shareBarHelpDestination) {
case 'blast':
blastToConnections(selectedID, selectedID === "p1");
shareBarHelp.innerHTML = blastAlreadySharedText;
break;
case 'hifi':
shareWithEveryone(selectedID, selectedID === "p1");
shareBarHelp.innerHTML = hifiAlreadySharedText;
break;
case 'facebook':
var facebookButton = document.getElementById(selectedID + "facebookButton");
window.open(facebookButton.getAttribute("href"), "_blank");
shareBarHelp.innerHTML = facebookShareText;
// This emitWebEvent() call isn't necessary in the "hifi" and "blast" cases
// because the "removeFromStoryIDsToMaybeDelete()" call happens
// in snapshot.js when sharing with that method.
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "removeFromStoryIDsToMaybeDelete",
story_id: storyID
}));
break;
case 'twitter':
var twitterButton = document.getElementById(selectedID + "twitterButton");
window.open(twitterButton.getAttribute("href"), "_blank");
shareBarHelp.innerHTML = twitterShareText;
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "removeFromStoryIDsToMaybeDelete",
story_id: storyID
}));
break;
}
shareBarHelp.setAttribute("data-destination", "");
}
}
function updateShareInfo(containerID, storyID) {
if (containerID.id) {
containerID = containerID.id; // sometimes (?), `containerID` is passed as an HTML object to these functions; we just want the ID
}
var shareBar = document.getElementById(containerID + "shareBar"),
parentDiv = document.getElementById(containerID),
shareURL = "https://highfidelity.com/user_stories/" + storyID,
facebookButton = document.getElementById(containerID + "facebookButton"),
twitterButton = document.getElementById(containerID + "twitterButton");
parentDiv.setAttribute('data-story-id', storyID);
facebookButton.setAttribute("target", "_blank");
facebookButton.setAttribute("href", 'https://www.facebook.com/dialog/feed?app_id=1585088821786423&link=' + shareURL);
twitterButton.setAttribute("target", "_blank");
twitterButton.setAttribute("href", 'https://twitter.com/intent/tweet?text=I%20just%20took%20a%20snapshot!&url=' + shareURL + '&via=highfidelityVR&hashtags=VR,HiFi');
hideUploadingMessageAndMaybeShare(containerID, storyID);
}
function blastToConnections(selectedID, isGif) {
if (selectedID.id) {
selectedID = selectedID.id; // sometimes (?), `selectedID` is passed as an HTML object to these functions; we just want the ID
}
var blastToConnectionsButton = document.getElementById(selectedID + "blastToConnectionsButton"),
shareBar = document.getElementById(selectedID + "shareBar"),
shareBarHelp = document.getElementById(selectedID + "shareBarHelp");
blastToConnectionsButton.onclick = function () { };
var storyID = document.getElementById(selectedID).getAttribute("data-story-id");
if (storyID) {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "blastToConnections",
story_id: storyID,
isGif: isGif
}));
showConfirmationMessage(selectedID, 'blast');
blastToConnectionsButton.classList.add("disabled");
blastToConnectionsButton.style.backgroundColor = "#000000";
blastToConnectionsButton.style.opacity = "0.5";
shareBarHelp.style.backgroundColor = "#000000";
shareBarHelp.style.opacity = "0.5";
} else {
showUploadingMessage(selectedID, 'blast');
}
}
function shareWithEveryone(selectedID, isGif) {
if (selectedID.id) {
selectedID = selectedID.id; // sometimes (?), `selectedID` is passed as an HTML object to these functions; we just want the ID
}
var shareWithEveryoneButton = document.getElementById(selectedID + "shareWithEveryoneButton"),
shareBar = document.getElementById(selectedID + "shareBar"),
shareBarHelp = document.getElementById(selectedID + "shareBarHelp");
shareWithEveryoneButton.onclick = function () { };
var storyID = document.getElementById(selectedID).getAttribute("data-story-id");
if (storyID) {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "shareSnapshotWithEveryone",
story_id: storyID,
isGif: isGif
}));
showConfirmationMessage(selectedID, 'hifi');
shareWithEveryoneButton.classList.add("disabled");
shareWithEveryoneButton.style.backgroundColor = "#000000";
shareWithEveryoneButton.style.opacity = "0.5";
shareBarHelp.style.backgroundColor = "#000000";
shareBarHelp.style.opacity = "0.5";
} else {
showUploadingMessage(selectedID, 'hifi');
}
}
function shareButtonHovered(destination, selectedID, shouldAlsoModifyOther) {
if (selectedID.id) {
selectedID = selectedID.id; // sometimes (?), `selectedID` is passed as an HTML object to these functions; we just want the ID
}
var shareBarHelp = document.getElementById(selectedID + "shareBarHelp"),
shareButtonsDiv = document.getElementById(selectedID + "shareButtonsDiv").childNodes,
itr;
if (!shareBarHelp.classList.contains("uploading")) {
for (itr = 0; itr < shareButtonsDiv.length; itr += 1) {
shareButtonsDiv[itr].style.backgroundColor = "rgba(0, 0, 0, 0)";
}
shareBarHelp.style.opacity = "1.0";
switch (destination) {
case 'blast':
var blastToConnectionsButton = document.getElementById(selectedID + "blastToConnectionsButton");
if (!blastToConnectionsButton.classList.contains("disabled")) {
shareBarHelp.style.backgroundColor = "#EA4C5F";
shareBarHelp.style.opacity = "1.0";
blastToConnectionsButton.style.backgroundColor = "#EA4C5F";
blastToConnectionsButton.style.opacity = "1.0";
shareBarHelp.innerHTML = blastShareText;
} else {
shareBarHelp.style.backgroundColor = "#000000";
shareBarHelp.style.opacity = "0.5";
blastToConnectionsButton.style.backgroundColor = "#000000";
blastToConnectionsButton.style.opacity = "0.5";
shareBarHelp.innerHTML = blastAlreadySharedText;
}
break;
case 'hifi':
var shareWithEveryoneButton = document.getElementById(selectedID + "shareWithEveryoneButton");
if (!shareWithEveryoneButton.classList.contains("disabled")) {
shareBarHelp.style.backgroundColor = "#1FC6A6";
shareBarHelp.style.opacity = "1.0";
shareWithEveryoneButton.style.backgroundColor = "#1FC6A6";
shareWithEveryoneButton.style.opacity = "1.0";
shareBarHelp.innerHTML = hifiShareText;
} else {
shareBarHelp.style.backgroundColor = "#000000";
shareBarHelp.style.opacity = "0.5";
shareWithEveryoneButton.style.backgroundColor = "#000000";
shareWithEveryoneButton.style.opacity = "0.5";
shareBarHelp.innerHTML = hifiAlreadySharedText;
}
break;
case 'facebook':
shareBarHelp.style.backgroundColor = "#3C58A0";
shareBarHelp.innerHTML = facebookShareText;
document.getElementById(selectedID + "facebookButton").style.backgroundColor = "#3C58A0";
break;
case 'twitter':
shareBarHelp.style.backgroundColor = "#00B4EE";
shareBarHelp.innerHTML = twitterShareText;
document.getElementById(selectedID + "twitterButton").style.backgroundColor = "#00B4EE";
break;
}
}
if (shouldAlsoModifyOther && imageCount > 1) {
if (selectedID === "p0" && !document.getElementById("p1").classList.contains("processingGif")) {
shareButtonHovered(destination, "p1", false);
} else if (selectedID === "p1") {
shareButtonHovered(destination, "p0", false);
}
}
}
function shareButtonClicked(destination, selectedID) {
if (selectedID.id) {
selectedID = selectedID.id; // sometimes (?), `selectedID` is passed as an HTML object to these functions; we just want the ID
}
var storyID = document.getElementById(selectedID).getAttribute("data-story-id");
if (!storyID) {
showUploadingMessage(selectedID, destination);
} else {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "removeFromStoryIDsToMaybeDelete",
story_id: storyID
}));
}
}
function handleCaptureSetting(setting) {
var stillAndGif = document.getElementById('stillAndGif'),
stillOnly = document.getElementById('stillOnly');
stillAndGif.checked = setting;
stillOnly.checked = !setting;
stillAndGif.onclick = function () {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "captureStillAndGif"
}));
};
stillOnly.onclick = function () {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "captureStillOnly"
}));
};
}
window.onload = function () {
// Uncomment the line below to test functionality in a browser.
// See definition of "testInBrowser()" to modify tests.
//testInBrowser(4);
openEventBridge(function () {
// Set up a handler for receiving the data, and tell the .js we are ready to receive it.
EventBridge.scriptEventReceived.connect(function (message) {
message = JSON.parse(message);
if (message.type !== "snapshot") {
return;
}
var messageOptions = message.options;
switch (message.action) {
case 'showSetupInstructions':
showSetupInstructions();
break;
case 'snapshotLocationChosen':
clearImages();
showSetupComplete();
break;
case 'clearPreviousImages':
clearImages();
break;
case 'showPreviousImages':
clearImages();
imageCount = message.image_data.length;
if (imageCount > 0) {
message.image_data.forEach(function (element, idx) {
addImage(element, messageOptions.isLoggedIn, message.canShare, false, true, message.image_data[idx].blastButtonDisabled, message.image_data[idx].hifiButtonDisabled, messageOptions.canBlast);
});
} else {
showSnapshotInstructions();
}
break;
case 'addImages':
// The last element of the message contents list contains a bunch of options,
// including whether or not we can share stuff
// The other elements of the list contain image paths.
if (messageOptions.containsGif === true) {
if (messageOptions.processingGif === true) {
imageCount = message.image_data.length + 1; // "+1" for the GIF that'll finish processing soon
message.image_data.push({ localPath: messageOptions.loadingGifPath });
message.image_data.forEach(function (element, idx) {
addImage(element, messageOptions.isLoggedIn, idx === 0 && messageOptions.canShare, idx === 1, false, false, false, true);
});
document.getElementById("p1").classList.add("processingGif");
document.getElementById("snap-button").disabled = true;
} else {
var gifPath = message.image_data[0].localPath,
p1img = document.getElementById('p1img');
p1img.src = gifPath;
paths[1] = gifPath;
shareForUrl("p1");
appendShareBar("p1", messageOptions.isLoggedIn, messageOptions.canShare, true, false, false, messageOptions.canBlast);
document.getElementById("p1").classList.remove("processingGif");
document.getElementById("snap-button").disabled = false;
}
} else {
imageCount = message.image_data.length;
message.image_data.forEach(function (element) {
addImage(element, messageOptions.isLoggedIn, messageOptions.canShare, false, false, false, false, true);
});
document.getElementById("snap-button").disabled = false;
}
break;
case 'captureSettings':
handleCaptureSetting(message.setting);
break;
case 'setPrintButtonEnabled':
setPrintButtonEnabled();
break;
case 'setPrintButtonLoading':
setPrintButtonLoading();
break;
case 'setPrintButtonDisabled':
setPrintButtonDisabled();
break;
case 'snapshotUploadComplete':
var isGif = fileExtensionMatches(message.image_url, "gif");
updateShareInfo(isGif ? "p1" : "p0", message.story_id);
if (isPrintProcessing()) {
setPrintButtonEnabled();
}
break;
default:
console.log("Unknown message action received in SnapshotReview.js.");
break;
}
});
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "ready"
}));
});;
};
function snapshotSettings() {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "openSettings"
}));
}
function takeSnapshot() {
document.getElementById("snap-button").disabled = true;
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "takeSnapshot"
}));
}
function isPrintDisabled() {
var printElement = document.getElementById('print-icon');
return printElement.classList.contains("print-icon") &&
printElement.classList.contains("print-icon-default") &&
document.getElementById('print-button').disabled;
}
function isPrintProcessing() {
var printElement = document.getElementById('print-icon');
return printElement.classList.contains("print-icon") &&
printElement.classList.contains("print-icon-loading") &&
document.getElementById('print-button').disabled;
}
function isPrintEnabled() {
var printElement = document.getElementById('print-icon');
return printElement.classList.contains("print-icon") &&
printElement.classList.contains("print-icon-default") &&
!document.getElementById('print-button').disabled;
}
function setPrintButtonLoading() {
document.getElementById('print-icon').className = "print-icon print-icon-loading";
document.getElementById('print-button').disabled = true;
}
function setPrintButtonDisabled() {
document.getElementById('print-icon').className = "print-icon print-icon-default";
document.getElementById('print-button').disabled = true;
}
function setPrintButtonEnabled() {
document.getElementById('print-button').disabled = false;
document.getElementById('print-icon').className = "print-icon print-icon-default";
}
function requestPrintButtonUpdate() {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "requestPrintButtonUpdate"
}));
}
function printToPolaroid() {
if (isPrintEnabled()) {
EventBridge.emitWebEvent(JSON.stringify({
type: "snapshot",
action: "printToPolaroid"
}));
} else {
setPrintButtonLoading();
}
}
function testInBrowser(test) {
if (test === 0) {
showSetupInstructions();
} else if (test === 1) {
imageCount = 2;
//addImage({ localPath: 'http://lorempixel.com/553/255' });
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.jpg', story_id: 1338 }, true, true, false, true, false, false, true);
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.gif', story_id: 1337 }, true, true, false, true, false, false, true);
} else if (test === 2) {
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.jpg', story_id: 1338 }, true, true, false, true, false, false, true);
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.gif', story_id: 1337 }, true, true, false, true, false, false, true);
showConfirmationMessage("p0", 'blast');
showConfirmationMessage("p1", 'hifi');
} else if (test === 3) {
imageCount = 2;
//addImage({ localPath: 'http://lorempixel.com/553/255' });
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.jpg', story_id: 1338 }, true, true, false, true, false, false, true);
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.gif', story_id: 1337 }, true, true, false, true, false, false, true);
showUploadingMessage("p0", 'hifi');
} else if (test === 4) {
imageCount = 2;
//addImage({ localPath: 'http://lorempixel.com/553/255' });
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.jpg', story_id: 1338 }, false, true, false, true, false, false, true);
addImage({ localPath: 'D:/Dropbox/Screenshots/High Fidelity Snapshots/hifi-snap-by-zfox-on-2017-05-01_13-28-58.gif', story_id: 1337 }, false, true, false, true, false, false, true);
}
}