var ipaa = window.ipaa || {};
ipaa.portal = ipaa.portal || {};
((o) => {
const routeHandlers = {};
o.ignoredUrls = ["/_", "/signup", "/signin", "/account/", "/register"];
o.triggerRouteHandler = (url) => {
const handler = routeHandlers[url];
if (handler) {
handler();
}
};
o.loadRoute = (url) => {
url= url.toLowerCase();
if(o.ignoredUrls.every(x => !url.startsWith(x))){
window.top.location.href=`/#${url}`;
}else{
window.top.location.href=url;
}
};
const linkHandler = (e) => {
const target = $(e.target);
const link = target.closest("a:not([data-link-ignore]");
if (link) {
const url = link.attr("href")?.toLowerCase();
if (url && url !== "#" && o.ignoredUrls.every(x => !url.startsWith(x))) {
e.preventDefault();
o.loadRoute(url);
}
}
}
o.safeAjax= (ajaxOptions)=> {
var deferredAjax = $.Deferred();
ajaxOptions= {...ajaxOptions,contentType: "application/json"};
shell.getTokenDeferred().done(function (token) {
// add headers for AJAX
if (!ajaxOptions.headers) {
$.extend(ajaxOptions, {
headers: {
"__RequestVerificationToken": token
}
});
} else {
ajaxOptions.headers["__RequestVerificationToken"] = token;
}
$.ajax(ajaxOptions)
.done(function(data, textStatus, jqXHR) {
validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
}).fail(deferredAjax.reject); //AJAX
}).fail(function () {
deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args
});
return deferredAjax.promise();
};
o.registerPageHandler = (url, handler) => {
if (url && handler) {
routeHandlers[url.toLowerCase()] = handler;
}
};
o.handleLinkClick = () => {
$("body").on('click', (e) => {
linkHandler(e);
})
}
o.loadInitialRoute=()=>{
if( window.self === window.top){
const currentUrl= new URL(window.location.href);
if(currentUrl.pathname!=="/" && o.ignoredUrls.every(x => !currentUrl.pathname.toLowerCase().startsWith(x))){
currentUrl.hash=currentUrl.pathname;
currentUrl.pathname="/";
window.location.href=currentUrl.toString();
};
}
}
})(ipaa.portal);
$(() => {
ipaa.portal.handleLinkClick();
ipaa.portal.registerPageHandler("/signin", () => {
$("#content").prepend('
')
});
ipaa.portal.registerPageHandler("/signup", () => {
$("#content").prepend('
')
});
ipaa.portal.triggerRouteHandler(window.location.pathname.toLowerCase());
ipaa.portal.loadInitialRoute();
});