Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Extract utils functions to utils.js
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminaaron committed May 23, 2024
1 parent 662c943 commit 185ad29
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 87 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
node_modules/
package-lock.json

public/*
!public/index.html
public/assets/
86 changes: 1 addition & 85 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title>FörderFunke</title>
<script src="./utils.js"></script>
<script src="./assets/bundle.js"></script>
<link rel="stylesheet" href="./assets/choices.min.css" />
<script src="./assets/choices.min.js"></script>
Expand Down Expand Up @@ -414,91 +415,6 @@ <h3>Report</h3>
}

run()

// ----- utils ----

async function checkForNewRepoCommits() {
console.log("Checking for updates, old commit:", latestRPsRepoCommit)
let checkLatestRPsRepoCommit = await fetchAsset("latest-rps-repo-commit.txt")
// TODO also check foerderfunke-webapp latest commit
if (checkLatestRPsRepoCommit === latestRPsRepoCommit) return
console.log("Update available, new commit:", checkLatestRPsRepoCommit)
latestRPsRepoCommit = checkLatestRPsRepoCommit
document.getElementById("update-banner").style.display = "block"
let eligibleRPsBefore = [... eligibleRPs]
await parseTurtleFiles()
await update()
let newEligibleRPs = eligibleRPs.filter(rp => !eligibleRPsBefore.includes(rp))
if (newEligibleRPs.length > 0) {
document.title = `(${newEligibleRPs.length}) ${document.title}`
alert("New eligible requirement profiles available: " + newEligibleRPs.map(rpUri => getRpTitle(rpUri)).join(", "))
}
}

function dfShortUriToLabel(key) {
return metadata.df[expandShortUri(key)]?.label ?? key
}

function expandShortUri(uri) {
return uri.startsWith("ff:") ? "https://foerderfunke.org/default#" + uri.slice(3) : uri
}

function shortenLongUri(uri) {
return uri.startsWith("http") ? "ff:" + uri.split("#")[1] : uri
}

function buildRowAndColumns(table) {
let tr = document.createElement("tr")
table.appendChild(tr)
let tds = []
for (let i = 0; i < maxDepth + 3; i++) {
let td = document.createElement("td")
tr.appendChild(td)
tds.push(td)
}
return tds
}

function getRpTitle(rpUri) {
return metadata.rp[rpUri]?.title ?? rpUri
}

async function fetchAsset(relPath) {
const response = await fetch("assets/" + relPath, {
method: "GET",
cache: "reload"
})
return await response.text()
}

async function clearUserProfile() {
localStorage.setItem("userProfile", JSON.stringify(EMPTY_PROFILE))
userProfile = JSON.parse(localStorage.getItem("userProfile"))
await update()
}

async function importUserProfileTurtle() {
alert("TODO")
// TODO
}

async function downloadUserProfileTurtle() {
let userProfileTurtle = await MatchingEngine.convertUserProfileToTurtle(userProfile)
let blob = new Blob([userProfileTurtle], {type: "text/turtle"})
let url = URL.createObjectURL(blob)
let a = document.createElement("a")
a.href = url
a.download = "user-profile.ttl"
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
}

async function showUserProfileTurtle() {
let userProfileTurtle = await MatchingEngine.convertUserProfileToTurtle(userProfile)
alert(userProfileTurtle)
}
</script>
</body>
</html>
83 changes: 83 additions & 0 deletions public/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

async function checkForNewRepoCommits() {
console.log("Checking for updates, old commit:", latestRPsRepoCommit)
let checkLatestRPsRepoCommit = await fetchAsset("latest-rps-repo-commit.txt")
// TODO also check foerderfunke-webapp latest commit
if (checkLatestRPsRepoCommit === latestRPsRepoCommit) return
console.log("Update available, new commit:", checkLatestRPsRepoCommit)
latestRPsRepoCommit = checkLatestRPsRepoCommit
document.getElementById("update-banner").style.display = "block"
let eligibleRPsBefore = [... eligibleRPs]
await parseTurtleFiles()
await update()
let newEligibleRPs = eligibleRPs.filter(rp => !eligibleRPsBefore.includes(rp))
if (newEligibleRPs.length > 0) {
document.title = `(${newEligibleRPs.length}) ${document.title}`
alert("New eligible requirement profiles available: " + newEligibleRPs.map(rpUri => getRpTitle(rpUri)).join(", "))
}
}

function dfShortUriToLabel(key) {
return metadata.df[expandShortUri(key)]?.label ?? key
}

function expandShortUri(uri) {
return uri.startsWith("ff:") ? "https://foerderfunke.org/default#" + uri.slice(3) : uri
}

function shortenLongUri(uri) {
return uri.startsWith("http") ? "ff:" + uri.split("#")[1] : uri
}

function buildRowAndColumns(table) {
let tr = document.createElement("tr")
table.appendChild(tr)
let tds = []
for (let i = 0; i < maxDepth + 3; i++) {
let td = document.createElement("td")
tr.appendChild(td)
tds.push(td)
}
return tds
}

function getRpTitle(rpUri) {
return metadata.rp[rpUri]?.title ?? rpUri
}

async function fetchAsset(relPath) {
const response = await fetch("assets/" + relPath, {
method: "GET",
cache: "reload"
})
return await response.text()
}

async function clearUserProfile() {
localStorage.setItem("userProfile", JSON.stringify(EMPTY_PROFILE))
userProfile = JSON.parse(localStorage.getItem("userProfile"))
await update()
}

async function importUserProfileTurtle() {
alert("TODO")
// TODO
}

async function downloadUserProfileTurtle() {
let userProfileTurtle = await MatchingEngine.convertUserProfileToTurtle(userProfile)
let blob = new Blob([userProfileTurtle], {type: "text/turtle"})
let url = URL.createObjectURL(blob)
let a = document.createElement("a")
a.href = url
a.download = "user-profile.ttl"
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
}

async function showUserProfileTurtle() {
let userProfileTurtle = await MatchingEngine.convertUserProfileToTurtle(userProfile)
alert(userProfileTurtle)
}

0 comments on commit 185ad29

Please sign in to comment.