From fad470eeea20dd9609f62764a99241f073648854 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Mon, 24 Apr 2017 14:35:05 -0700 Subject: [PATCH] Checkpoint (this is complicated) --- scripts/system/html/js/SnapshotReview.js | 86 +++++++++++------ scripts/system/snapshot.js | 112 ++++++++++++++++++----- 2 files changed, 148 insertions(+), 50 deletions(-) diff --git a/scripts/system/html/js/SnapshotReview.js b/scripts/system/html/js/SnapshotReview.js index 6e15dad567..cf1f881a46 100644 --- a/scripts/system/html/js/SnapshotReview.js +++ b/scripts/system/html/js/SnapshotReview.js @@ -10,9 +10,20 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -var paths = [], idCounter = 0, imageCount = 1; -function addImage(data, isGifLoading) { - if (!data.localPath) { +var paths = []; +var idCounter = 0; +var imageCount = 0; +function clearImages() { + var snapshotImagesDiv = document.getElementById("snapshot-images"); + while (snapshotImagesDiv.hasChildNodes()) { + snapshotImagesDiv.removeChild(snapshotImagesDiv.lastChild); + } + paths = []; + imageCount = 0; + idCounter = 0; +} +function addImage(image_data, isGifLoading, isShowingPreviousImages) { + if (!image_data.localPath) { return; } var div = document.createElement("DIV"); @@ -29,15 +40,26 @@ function addImage(data, isGifLoading) { if (imageCount > 1) { img.setAttribute("class", "multiple"); } - img.src = data.localPath; + img.src = image_data.localPath; div.appendChild(img); document.getElementById("snapshot-images").appendChild(div); var isGif = img.src.split('.').pop().toLowerCase() === "gif"; - paths.push(data.localPath); - if (!isGifLoading) { + paths.push(image_data.localPath); + if (!isGifLoading && !isShowingPreviousImages) { shareForUrl(id); + } else if (isShowingPreviousImages) { + appendShareBar(id, image_data.story_id, isGif) } } +function appendShareBar(divID, story_id, isGif) { + var story_url = "https://highfidelity.com/user_stories/" + story_id; + var parentDiv = document.getElementById(divID); + parentDiv.setAttribute('data-story-id', story_id); + document.getElementById(divID).appendChild(createShareOverlay(divID, isGif, story_url)); + twttr.events.bind('click', function (event) { + shareButtonClicked(divID); + }); +} function createShareOverlay(parentID, isGif, shareURL) { var shareOverlayContainer = document.createElement("DIV"); shareOverlayContainer.id = parentID + "shareOverlayContainer"; @@ -138,6 +160,13 @@ function shareWithEveryone(selectedID) { story_id: document.getElementById(selectedID).getAttribute("data-story-id") })); } +function shareButtonClicked(selectedID) { + EventBridge.emitWebEvent(JSON.stringify({ + type: "snapshot", + action: "shareButtonClicked", + story_id: document.getElementById(selectedID).getAttribute("data-story-id") + })); +} function cancelSharing(selectedID) { selectedID = selectedID.id; // Why is this necessary? var shareOverlayContainer = document.getElementById(selectedID + "shareOverlayContainer"); @@ -176,9 +205,10 @@ function handleCaptureSetting(setting) { window.onload = function () { // TESTING FUNCTIONS START // Uncomment and modify the lines below to test SnapshotReview in a browser. - //imageCount = 2; + //imageCount = 1; //addImage({ localPath: 'C:/Users/Zach Fox/Desktop/hifi-snap-by-zfox-on-2017-04-20_14-59-12.gif' }); - //addImage({ localPath: 'C:/Users/Zach Fox/Desktop/hifi-snap-by-zfox-on-2017-04-20_14-59-12.jpg' }); + //addImage({ localPath: 'C:/Users/Zach Fox/Desktop/hifi-snap-by-zfox-on-2017-04-24_10-49-20.jpg' }); + //document.getElementById('p0').appendChild(createShareOverlay('p0', false, '')); //addImage({ localPath: 'http://lorempixel.com/553/255' }); //addImage({localPath: 'c:/Users/howar/OneDrive/Pictures/hifi-snap-by--on-2016-07-27_12-58-43.jpg'}); // TESTING FUNCTIONS END @@ -194,6 +224,17 @@ window.onload = function () { } switch (message.action) { + case 'clearPreviousImages': + clearImages(); + break; + case 'showPreviousImages': + clearImages(); + var messageOptions = message.options; + imageCount = message.image_data.length; + message.image_data.forEach(function (element, idx, array) { + addImage(element, true, true); + }); + break; case 'addImages': // The last element of the message contents list contains a bunch of options, // including whether or not we can share stuff @@ -202,13 +243,13 @@ window.onload = function () { if (messageOptions.containsGif) { if (messageOptions.processingGif) { - imageCount = message.data.length + 1; // "+1" for the GIF that'll finish processing soon - message.data.unshift({ localPath: messageOptions.loadingGifPath }); - message.data.forEach(function (element, idx, array) { + imageCount = message.image_data.length + 1; // "+1" for the GIF that'll finish processing soon + message.image_data.unshift({ localPath: messageOptions.loadingGifPath }); + message.image_data.forEach(function (element, idx, array) { addImage(element, idx === 0); }); } else { - var gifPath = message.data[0].localPath; + var gifPath = message.image_data[0].localPath; var p0img = document.getElementById('p0img'); p0img.src = gifPath; @@ -216,8 +257,8 @@ window.onload = function () { shareForUrl("p0"); } } else { - imageCount = message.data.length; - message.data.forEach(function (element, idx, array) { + imageCount = message.image_data.length; + message.image_data.forEach(function (element, idx, array) { addImage(element, false); }); } @@ -227,19 +268,12 @@ window.onload = function () { break; case 'snapshotUploadComplete': var isGif = message.shareable_url.split('.').pop().toLowerCase() === "gif"; - var id = "p0" - if (imageCount > 1 && !isGif) { - id = "p1"; - } - var parentDiv = document.getElementById(id); - parentDiv.setAttribute('data-story-id', message.id); - document.getElementById(id).appendChild(createShareOverlay(id, isGif, message.story_url)); + appendShareBar(isGif || imageCount === 1 ? "p0" : "p1", message.story_id, isGif); break; default: - print("Unknown message action received in SnapshotReview.js."); + console.log("Unknown message action received in SnapshotReview.js."); break; } - }); EventBridge.emitWebEvent(JSON.stringify({ @@ -249,12 +283,6 @@ window.onload = function () { }); }; -function doNotShare() { - EventBridge.emitWebEvent(JSON.stringify({ - type: "snapshot", - action: [] - })); -} function snapshotSettings() { EventBridge.emitWebEvent(JSON.stringify({ type: "snapshot", diff --git a/scripts/system/snapshot.js b/scripts/system/snapshot.js index 6b0b8e1214..1af35f4061 100644 --- a/scripts/system/snapshot.js +++ b/scripts/system/snapshot.js @@ -29,7 +29,8 @@ var button = tablet.addButton({ }); var snapshotOptions; -var imageData; +var imageData = []; +var storyIDsToMaybeDelete = []; var shareAfterLogin = false; var snapshotToShareAfterLogin; var METAVERSE_BASE = location.metaverseServerUrl; @@ -105,9 +106,9 @@ function onMessage(message) { })); tablet.emitScriptEvent(JSON.stringify({ type: "snapshot", - action: "addImages", + action: "showPreviousImages", options: snapshotOptions, - data: imageData + image_data: imageData })); break; case 'openSettings': @@ -127,9 +128,7 @@ function onMessage(message) { Settings.setValue("alsoTakeAnimatedSnapshot", false); break; case 'takeSnapshot': - // In settings, first store the paths to the last snapshot - // - onClicked(); + takeSnapshot(); break; case 'shareSnapshotForUrl': isLoggedIn = Account.isLoggedIn(); @@ -142,6 +141,7 @@ function onMessage(message) { break; case 'shareSnapshotWithEveryone': isLoggedIn = Account.isLoggedIn(); + storyIDsToMaybeDelete.splice(storyIDsToMaybeDelete.indexOf(message.story_id), 1); if (isLoggedIn) { print('Modifying audience of story ID', message.story_id, "to 'for_feed'"); request({ @@ -178,6 +178,10 @@ function onMessage(message) { } }*/ break; + case 'shareButtonClicked': + print('Twitter or FB "Share" button clicked! Removing ID', message.story_id, 'from storyIDsToMaybeDelete[].') + storyIDsToMaybeDelete.splice(storyIDsToMaybeDelete.indexOf(message.story_id), 1); + break; default: print('Unknown message action received by snapshot.js!'); break; @@ -186,7 +190,23 @@ function onMessage(message) { var SNAPSHOT_REVIEW_URL = Script.resolvePath("html/SnapshotReview.html"); var isInSnapshotReview = false; -function reviewSnapshot() { +function openSnapApp() { + var previousStillSnapPath = Settings.getValue("previousStillSnapPath"); + var previousStillSnapStoryID = Settings.getValue("previousStillSnapStoryID"); + var previousAnimatedSnapPath = Settings.getValue("previousAnimatedSnapPath"); + var previousAnimatedSnapStoryID = Settings.getValue("previousAnimatedSnapStoryID"); + snapshotOptions = { + containsGif: previousAnimatedSnapPath !== "", + processingGif: false, + shouldUpload: false + } + imageData = []; + if (previousAnimatedSnapPath !== "") { + imageData.push({ localPath: previousAnimatedSnapPath, story_id: previousAnimatedSnapStoryID }); + } + if (previousStillSnapPath !== "") { + imageData.push({ localPath: previousStillSnapPath, story_id: previousStillSnapStoryID }); + } tablet.gotoWebScreen(SNAPSHOT_REVIEW_URL); tablet.webEventReceived.connect(onMessage); HMD.openTablet(); @@ -195,21 +215,28 @@ function reviewSnapshot() { function snapshotUploaded(isError, reply) { if (!isError) { - print('SUCCESS: Snapshot uploaded! Story with audience:for_url created!'); var replyJson = JSON.parse(reply); + var storyID = replyJson.user_story.id; + var shareableURL = replyJson.user_story.details.shareable_url; + var isGif = shareableURL.split('.').pop().toLowerCase() === "gif"; + print('SUCCESS: Snapshot uploaded! Story with audience:for_url created! ID:', storyID); tablet.emitScriptEvent(JSON.stringify({ type: "snapshot", action: "snapshotUploadComplete", - id: replyJson.user_story.id, - story_url: "https://highfidelity.com/user_stories/" + replyJson.user_story.id, - shareable_url: replyJson.user_story.details.shareable_url, + story_id: storyID, + shareable_url: shareableURL, })); + if (isGif) { + Settings.setValue("previousAnimatedSnapStoryID", storyID); + } else { + Settings.setValue("previousStillSnapStoryID", storyID); + } } else { print(reply); } } var href, domainId; -function onClicked() { +function takeSnapshot() { // Raising the desktop for the share dialog at end will interact badly with clearOverlayWhenMoving. // Turn it off now, before we start futzing with things (and possibly moving). clearOverlayWhenMoving = MyAvatar.getClearOverlayWhenMoving(); // Do not use Settings. MyAvatar keeps a separate copy. @@ -220,8 +247,16 @@ function onClicked() { href = location.href; domainId = location.domainId; + tablet.emitScriptEvent(JSON.stringify({ + type: "snapshot", + action: "clearPreviousImages" + })); + maybeDeleteSnapshotStories(); + Settings.setValue("previousStillSnapPath", ""); + Settings.setValue("previousAnimatedSnapPath", ""); + // update button states - resetOverlays = Menu.isOptionChecked("Overlays"); // For completness. Certainly true if the button is visible to be clicke. + resetOverlays = Menu.isOptionChecked("Overlays"); // For completness. Certainly true if the button is visible to be clicked. reticleVisible = Reticle.visible; Reticle.visible = false; Window.stillSnapshotTaken.connect(stillSnapshotTaken); @@ -281,7 +316,15 @@ function stillSnapshotTaken(pathStillSnapshot, notify) { canShare: !!isDomainOpen(domainId) }; imageData = [{ localPath: pathStillSnapshot, href: href }]; - reviewSnapshot(); + Settings.setValue("previousStillSnapPath", pathStillSnapshot); + + tablet.emitScriptEvent(JSON.stringify({ + type: "snapshot", + action: "addImages", + options: snapshotOptions, + image_data: imageData + })); + if (clearOverlayWhenMoving) { MyAvatar.setClearOverlayWhenMoving(true); // not until after the share dialog } @@ -291,7 +334,7 @@ function stillSnapshotTaken(pathStillSnapshot, notify) { function processingGifStarted(pathStillSnapshot) { Window.processingGifStarted.disconnect(processingGifStarted); if (buttonConnected) { - button.clicked.disconnect(onClicked); + button.clicked.disconnect(openSnapApp); buttonConnected = false; } // show hud @@ -308,7 +351,15 @@ function processingGifStarted(pathStillSnapshot) { canShare: !!isDomainOpen(domainId) }; imageData = [{ localPath: pathStillSnapshot, href: href }]; - reviewSnapshot(); + Settings.setValue("previousStillSnapPath", pathStillSnapshot); + + tablet.emitScriptEvent(JSON.stringify({ + type: "snapshot", + action: "addImages", + options: snapshotOptions, + image_data: imageData + })); + if (clearOverlayWhenMoving) { MyAvatar.setClearOverlayWhenMoving(true); // not until after the share dialog } @@ -318,7 +369,7 @@ function processingGifStarted(pathStillSnapshot) { function processingGifCompleted(pathAnimatedSnapshot) { Window.processingGifCompleted.disconnect(processingGifCompleted); if (!buttonConnected) { - button.clicked.connect(onClicked); + button.clicked.connect(openSnapApp); buttonConnected = true; } @@ -328,15 +379,34 @@ function processingGifCompleted(pathAnimatedSnapshot) { canShare: !!isDomainOpen(domainId) } imageData = [{ localPath: pathAnimatedSnapshot, href: href }]; + Settings.setValue("previousAnimatedSnapPath", pathAnimatedSnapshot); tablet.emitScriptEvent(JSON.stringify({ type: "snapshot", action: "addImages", options: snapshotOptions, - data: imageData + image_data: imageData })); } - +function maybeDeleteSnapshotStories() { + if (storyIDsToMaybeDelete.length > 0) { + print("User took new snapshot & didn't share old one(s); deleting old snapshot stories"); + storyIDsToMaybeDelete.forEach(function (element, idx, array) { + request({ + uri: METAVERSE_BASE + '/api/v1/user_stories/' + element, + method: 'DELETE' + }, function (error, response) { + if (error || (response.status !== 'success')) { + print("ERROR deleting snapshot story: ", error || response.status); + return; + } else { + print("SUCCESS deleting snapshot story with ID", element); + } + }) + }); + storyIDsToMaybeDelete = []; + } +} function onTabletScreenChanged(type, url) { if (isInSnapshotReview) { tablet.webEventReceived.disconnect(onMessage); @@ -351,14 +421,14 @@ function onConnected() { } } -button.clicked.connect(onClicked); +button.clicked.connect(openSnapApp); buttonConnected = true; Window.snapshotShared.connect(snapshotUploaded); tablet.screenChanged.connect(onTabletScreenChanged); Account.usernameChanged.connect(onConnected); Script.scriptEnding.connect(function () { if (buttonConnected) { - button.clicked.disconnect(onClicked); + button.clicked.disconnect(openSnapApp); buttonConnected = false; } if (tablet) {