From 126c430fe6a3dec1deab709d3f6cc08b891f8df6 Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio.bekerle@tugraz.at> Date: Tue, 6 Aug 2019 11:01:13 +0200 Subject: [PATCH] Implement support for reloading JSONLD after vpu-auth-init event --- packages/common/jsonld.js | 141 ++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 68 deletions(-) diff --git a/packages/common/jsonld.js b/packages/common/jsonld.js index a259b570..ced7d320 100644 --- a/packages/common/jsonld.js +++ b/packages/common/jsonld.js @@ -45,79 +45,84 @@ export default class JSONLD { initStarted[apiUrl] = true; - // window.VPUAuthToken will be set by on vpu-auth-init - window.addEventListener("vpu-auth-init", function(e) - { - const xhr = new XMLHttpRequest(); - xhr.open("GET", apiUrl, true); - xhr.setRequestHeader('Authorization', 'Bearer ' + window.VPUAuthToken); - - xhr.onreadystatechange = function () { - if (xhr.readyState === 4 && xhr.status === 200) { - const json = JSON.parse(xhr.responseText); - - let entryPoints = {}; - for (let property in json) { - // for some reason the properties start with a lower case character - if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property]; - } - - // read the link header of the api response -// const utils = require("./utils"); - const links = utils.parseLinkHeader(this.getResponseHeader("link")); - - // get the hydra apiDocumentation url - const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"]; - - if (apiDocUrl !== undefined) { - // load the hydra apiDocumentation - const docXhr = new XMLHttpRequest(); - docXhr.open("GET", apiDocUrl, true); - docXhr.setRequestHeader("Content-Type", "application/json"); - docXhr.onreadystatechange = function () { - if (docXhr.readyState === 4 && docXhr.status === 200) { - const json = JSON.parse(docXhr.responseText); - const supportedClasses = json["hydra:supportedClass"]; - - let entities = {}; - const baseUrl = utils.parseBaseUrl(apiUrl); - - // gather the entities - supportedClasses.forEach(function (classData) { - // add entry point url - const entityName = classData["hydra:title"]; - let entryPoint = entryPoints[entityName.toLowerCase()]; - if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint; - classData["@entryPoint"] = entryPoint; - - entities[entityName] = classData; - }); - - const instance = new JSONLD(baseUrl, entities); - instances[apiUrl] = instance; - - // return the initialized JSONLD object - for (const fnc of successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance); - successFunctions[apiUrl] = []; - } else { - for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); - failureFunctions[apiUrl] = []; - } - }; - - docXhr.send(); - } else { - for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); - failureFunctions[apiUrl] = []; - } + if (window.VPUAuthToken !== undefined) { + JSONLD.doInitialization(apiUrl); + } else { + // window.VPUAuthToken will be set by vpu-auth-init event + window.addEventListener("vpu-auth-init", () => JSONLD.doInitialization(apiUrl)); + } + } + + static doInitialization(apiUrl) { + const xhr = new XMLHttpRequest(); + xhr.open("GET", apiUrl, true); + xhr.setRequestHeader('Authorization', 'Bearer ' + window.VPUAuthToken); + + xhr.onreadystatechange = function () { + if (xhr.readyState === 4 && xhr.status === 200) { + const json = JSON.parse(xhr.responseText); + + let entryPoints = {}; + for (let property in json) { + // for some reason the properties start with a lower case character + if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property]; + } + + // read the link header of the api response +// const utils = require("./utils"); + const links = utils.parseLinkHeader(this.getResponseHeader("link")); + + // get the hydra apiDocumentation url + const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"]; + + if (apiDocUrl !== undefined) { + // load the hydra apiDocumentation + const docXhr = new XMLHttpRequest(); + docXhr.open("GET", apiDocUrl, true); + docXhr.setRequestHeader("Content-Type", "application/json"); + docXhr.onreadystatechange = function () { + if (docXhr.readyState === 4 && docXhr.status === 200) { + const json = JSON.parse(docXhr.responseText); + const supportedClasses = json["hydra:supportedClass"]; + + let entities = {}; + const baseUrl = utils.parseBaseUrl(apiUrl); + + // gather the entities + supportedClasses.forEach(function (classData) { + // add entry point url + const entityName = classData["hydra:title"]; + let entryPoint = entryPoints[entityName.toLowerCase()]; + if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint; + classData["@entryPoint"] = entryPoint; + + entities[entityName] = classData; + }); + + const instance = new JSONLD(baseUrl, entities); + instances[apiUrl] = instance; + + // return the initialized JSONLD object + for (const fnc of successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance); + successFunctions[apiUrl] = []; + } else { + for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); + failureFunctions[apiUrl] = []; + } + }; + + docXhr.send(); } else { for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); failureFunctions[apiUrl] = []; } - }; + } else { + for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); + failureFunctions[apiUrl] = []; + } + }; - xhr.send(); - }); + xhr.send(); } static getInstance(apiUrl) { -- GitLab