|
|
@ -1,9 +1,18 @@ |
|
|
|
import { wireframeJSONToSVG } from 'roadmap-renderer'; |
|
|
|
import { wireframeJSONToSVG } from 'roadmap-renderer'; |
|
|
|
import Cookies from 'js-cookie'; |
|
|
|
import { |
|
|
|
import { TOKEN_COOKIE_NAME } from '../../lib/jwt.ts'; |
|
|
|
renderResourceProgress, |
|
|
|
import { httpGet } from '../../lib/http.ts'; |
|
|
|
ResourceType, |
|
|
|
|
|
|
|
} from '../../lib/resource-progress'; |
|
|
|
|
|
|
|
|
|
|
|
export class Renderer { |
|
|
|
export class Renderer { |
|
|
|
|
|
|
|
resourceId: string; |
|
|
|
|
|
|
|
resourceType: string; |
|
|
|
|
|
|
|
jsonUrl: string; |
|
|
|
|
|
|
|
loaderHTML: string | null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
containerId: string; |
|
|
|
|
|
|
|
loaderId: string; |
|
|
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
constructor() { |
|
|
|
this.resourceId = ''; |
|
|
|
this.resourceId = ''; |
|
|
|
this.resourceType = ''; |
|
|
|
this.resourceType = ''; |
|
|
@ -35,83 +44,64 @@ export class Renderer { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Clone it so we can use it later
|
|
|
|
// Clone it so we can use it later
|
|
|
|
this.loaderHTML = this.loaderEl.innerHTML; |
|
|
|
this.loaderHTML = this.loaderEl!.innerHTML; |
|
|
|
const dataset = this.containerEl.dataset; |
|
|
|
const dataset = this.containerEl.dataset; |
|
|
|
|
|
|
|
|
|
|
|
this.resourceType = dataset.resourceType; |
|
|
|
this.resourceType = dataset.resourceType!; |
|
|
|
this.resourceId = dataset.resourceId; |
|
|
|
this.resourceId = dataset.resourceId!; |
|
|
|
this.jsonUrl = dataset.jsonUrl; |
|
|
|
this.jsonUrl = dataset.jsonUrl!; |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async loadProgress() { |
|
|
|
|
|
|
|
const token = Cookies.get(TOKEN_COOKIE_NAME); |
|
|
|
|
|
|
|
if (!token) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { response, error } = await httpGet( |
|
|
|
|
|
|
|
`${import.meta.env.PUBLIC_API_URL}/v1-get-user-resource-progress`, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
resourceId: this.resourceId, |
|
|
|
|
|
|
|
resourceType: this.resourceType, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!response) { |
|
|
|
|
|
|
|
console.error(error); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { done } = response; |
|
|
|
|
|
|
|
done.forEach((topic) => { |
|
|
|
|
|
|
|
const topicEl = document.querySelector(`[data-group-id$="-${topic}"]`); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (topicEl) { |
|
|
|
|
|
|
|
topicEl.classList.add('done'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param { string } jsonUrl |
|
|
|
* @param { string } jsonUrl |
|
|
|
* @returns {Promise<SVGElement>} |
|
|
|
* @returns {Promise<SVGElement>} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
jsonToSvg(jsonUrl) { |
|
|
|
jsonToSvg(jsonUrl: string) { |
|
|
|
if (!jsonUrl) { |
|
|
|
if (!jsonUrl) { |
|
|
|
console.error('jsonUrl not defined in frontmatter'); |
|
|
|
console.error('jsonUrl not defined in frontmatter'); |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.containerEl.innerHTML = this.loaderHTML; |
|
|
|
if (!this.containerEl) { |
|
|
|
return Promise.all([ |
|
|
|
return null; |
|
|
|
fetch(jsonUrl) |
|
|
|
} |
|
|
|
.then((res) => { |
|
|
|
|
|
|
|
return res.json(); |
|
|
|
this.containerEl.innerHTML = this.loaderHTML!; |
|
|
|
}) |
|
|
|
|
|
|
|
.then((json) => { |
|
|
|
return fetch(jsonUrl) |
|
|
|
return wireframeJSONToSVG(json, { |
|
|
|
.then((res) => { |
|
|
|
fontURL: '/fonts/balsamiq.woff2', |
|
|
|
return res.json(); |
|
|
|
}); |
|
|
|
}) |
|
|
|
}) |
|
|
|
.then((json) => { |
|
|
|
.then((svg) => { |
|
|
|
return wireframeJSONToSVG(json, { |
|
|
|
this.containerEl.replaceChildren(svg); |
|
|
|
fontURL: '/fonts/balsamiq.woff2', |
|
|
|
}) |
|
|
|
}); |
|
|
|
.catch((error) => { |
|
|
|
}) |
|
|
|
const message = ` |
|
|
|
.then((svg) => { |
|
|
|
|
|
|
|
this.containerEl?.replaceChildren(svg); |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.then(() => { |
|
|
|
|
|
|
|
return renderResourceProgress( |
|
|
|
|
|
|
|
this.resourceType as ResourceType, |
|
|
|
|
|
|
|
this.resourceId |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.catch((error) => { |
|
|
|
|
|
|
|
if (!this.containerEl) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const message = ` |
|
|
|
<strong>There was an error.</strong><br> |
|
|
|
<strong>There was an error.</strong><br> |
|
|
|
|
|
|
|
|
|
|
|
Try loading the page again. or submit an issue on GitHub with following:<br><br> |
|
|
|
Try loading the page again. or submit an issue on GitHub with following:<br><br> |
|
|
|
|
|
|
|
|
|
|
|
${error.message} <br /> ${error.stack} |
|
|
|
${error.message} <br /> ${error.stack} |
|
|
|
`;
|
|
|
|
`;
|
|
|
|
|
|
|
|
this.containerEl.innerHTML = `<div class="error py-5 text-center text-red-600 mx-auto">${message}</div>`; |
|
|
|
this.containerEl.innerHTML = `<div class="error py-5 text-center text-red-600 mx-auto">${message}</div>`; |
|
|
|
}); |
|
|
|
}), |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.loadProgress(), |
|
|
|
|
|
|
|
]); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onDOMLoaded() { |
|
|
|
onDOMLoaded() { |
|
|
@ -129,16 +119,16 @@ export class Renderer { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switchRoadmap(newJsonUrl) { |
|
|
|
switchRoadmap(newJsonUrl: string) { |
|
|
|
const newJsonFileSlug = newJsonUrl.split('/').pop().replace('.json', ''); |
|
|
|
const newJsonFileSlug = newJsonUrl.split('/').pop()?.replace('.json', ''); |
|
|
|
|
|
|
|
|
|
|
|
// Update the URL and attach the new roadmap type
|
|
|
|
// Update the URL and attach the new roadmap type
|
|
|
|
if (window?.history?.pushState) { |
|
|
|
if (window?.history?.pushState) { |
|
|
|
const url = new URL(window.location); |
|
|
|
const url = new URL(window.location.href); |
|
|
|
const type = this.resourceType[0]; // r for roadmap, b for best-practices
|
|
|
|
const type = this.resourceType[0]; // r for roadmap, b for best-practices
|
|
|
|
|
|
|
|
|
|
|
|
url.searchParams.delete(type); |
|
|
|
url.searchParams.delete(type); |
|
|
|
url.searchParams.set(type, newJsonFileSlug); |
|
|
|
url.searchParams.set(type, newJsonFileSlug!); |
|
|
|
|
|
|
|
|
|
|
|
window.history.pushState(null, '', url.toString()); |
|
|
|
window.history.pushState(null, '', url.toString()); |
|
|
|
} |
|
|
|
} |
|
|
@ -154,13 +144,13 @@ export class Renderer { |
|
|
|
label: `${newJsonFileSlug}`, |
|
|
|
label: `${newJsonFileSlug}`, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this.jsonToSvg(newJsonUrl).then(() => { |
|
|
|
this.jsonToSvg(newJsonUrl)?.then(() => { |
|
|
|
this.containerEl.setAttribute('style', ''); |
|
|
|
this.containerEl?.setAttribute('style', ''); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
handleSvgClick(e) { |
|
|
|
handleSvgClick(e: any) { |
|
|
|
const targetGroup = e.target.closest('g') || {}; |
|
|
|
const targetGroup = e.target?.closest('g') || {}; |
|
|
|
const groupId = targetGroup.dataset ? targetGroup.dataset.groupId : ''; |
|
|
|
const groupId = targetGroup.dataset ? targetGroup.dataset.groupId : ''; |
|
|
|
if (!groupId) { |
|
|
|
if (!groupId) { |
|
|
|
return; |
|
|
|
return; |