jam-cloud/web/app/assets/javascripts/customUrlHandler.js

140 lines
4.3 KiB
JavaScript

(function (context, $) {
"use strict";
context.JK = context.JK || {};
context.JK.CustomUrlHandler = function () {
var hashchangeEvent;
var loadEvent;
function handleCustomUrlScheme(customUrl) {
var maxAttempts = 10;
//TODO: show a notification while waiting for the screen to load
//extract the hash from the custom URL passed in
var url = new URL(customUrl);
var hash = url.hash;
if (hash) { // if the hash contains the custom yes flag
var screenLoadInterval = setInterval(() => {
//handle create session. #/createSession is a valid hash. switch to the createSession screen and handle creating a session from the custom URL
if (hash.includes("#/createSession/")) { // if the hash contains the createSession flag
if(context.SessionStore && context.SessionStore.inSession()){
// clearInterval(screenLoadInterval);
// return;
}else{
console.log("attempting to create session", maxAttempts);
if (JK.createScheduledSessionScreen && JK.CreateScheduledSessionDataIsLoaded) { // if the createScheduledSessionScreen is loaded
JK.createScheduledSessionScreen.createSessionByCustomUrlScheme(hash); // this will create a session
clearInterval(screenLoadInterval);
} else {
console.log("attempting to create session. ", maxAttempts);
maxAttempts--;
if (maxAttempts <= 0) {
clearInterval(screenLoadInterval);
}
}
}
}
//handle join session. there is no joinSession screen. JK.SessionUtils is a helper class that handles joining a session
if (hash.includes("#/joinSession/")) { // if the hash contains the joinSession flag
if (JK.SessionUtils) { // if the SessionUtils is loaded
JK.SessionUtils.joinSessionFromCustomUrlScheme(hash);
clearInterval(screenLoadInterval);
} else {
console.log("attempting to join session", maxAttempts);
maxAttempts--;
if (maxAttempts <= 0) {
clearInterval(screenLoadInterval);
}
}
}
}, 1000); // check every second
}
}
function isValidUrl(customUrl) {
if (!customUrl) {
return false;
}
try {
var url = new URL(customUrl);
var hash = url.hash;
//if the customUrl is valid, return true
if (hash.includes("#/createSession/") || hash.includes("#/joinSession/")) {
return true;
}
}catch (e) {
return false;
}
return false
}
function getCustomUrl() {
//get customUrl from local storage
if (localStorage && localStorage.getItem("customUrl")) {
try{
return decodeURIComponent(localStorage.getItem("customUrl"));
}
catch(e){
console.error("error decoding customUrl", e);
return null;
}
}
return null;
}
function handle() {
//clear the event listeners
if (hashchangeEvent) {
window.removeEventListener('hashchange', hashchangeEvent);
}
if (loadEvent) {
window.removeEventListener('load', loadEvent);
}
// check periodically if localStorage has customUrl available for 10 seconds
//if available call handleCustomUrlScheme and clear the interval and localStorage customUrl
var attempts = 5;
var customUrlInterval = setInterval(() => {
const customUrl = getCustomUrl();
if(customUrl && isValidUrl(customUrl)){
handleCustomUrlScheme(customUrl);
localStorage.removeItem("customUrl");
clearInterval(customUrlInterval);
}
if (attempts < 0) {
localStorage.removeItem("customUrl");
clearInterval(customUrlInterval);
}
attempts--;
}, 1000);
}
function initialize() {
hashchangeEvent = window.addEventListener('hashchange', function () {
handle();
});
loadEvent = window.addEventListener('load', (event) => {
handle();
});
}
return {
initialize: initialize,
isValidUrl: isValidUrl,
};
};
})(window, jQuery);