Skip to content

Commit

Permalink
Merge branch 'feat/link-in-new-tab' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30 committed Jul 1, 2022
2 parents da6b9bf + 8543c0e commit 2b29b96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"color": "red",
"links": [
{ "name": "Spotify", "url": "https://open.spotify.com/" },
{ "name": "YoutubeMusic", "url": "https://music.youtube.com/" },
{ "name": "YoutubeMusic", "url": "https://music.youtube.com/", "newTab": false },
{ "name": "Netflix", "url": "https://netflix.com" },
{ "name": "Youtube", "url": "https://youtube.com" }
]
Expand Down Expand Up @@ -35,6 +35,7 @@
]
}
],
"linkInNewTab": true,
"title": "startpage",
"searchEngine":"DuckDuckGo",
"user": "Deepjyoti",
Expand Down
29 changes: 27 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,14 @@ function parseAndCreate(jsonData) {

sqrs = jsonData["squares"]

globalNewTabEnabled = false
passedNewTab = jsonData["linkInNewTab"]
if (passedNewTab != undefined) {
globalNewTabEnabled = passedNewTab
}

sqrs.forEach((element, index) => {
sqr = createSqr(element, index)
sqr = createSqr(element, index, globalNewTabEnabled)
document.getElementById(otherContentId).appendChild(sqr)
})

Expand Down Expand Up @@ -320,7 +326,7 @@ function parseAndCreate(jsonData) {
extractQuickLinks(sqrs, jsonData["style"]);
}

function createSqr(sqrData, index) {
function createSqr(sqrData, index, globalNewTabEnabled) {
// Create a new square division with the passed element
name = sqrData["name"];
link = sqrData["url"];
Expand Down Expand Up @@ -352,10 +358,29 @@ function createSqr(sqrData, index) {
links.forEach(element => {
aName = element["name"]
aHref = element["url"]

// Idea is to give preference to
// link specific newTab field over the global
// one.
//
// As a fallback, the global value will be used.
isNewTab = globalNewTabEnabled
newTabValue = element["newTab"]

if (newTabValue != undefined) {
isNewTab = newTabValue
}

a = document.createElement("a")
attrHref = document.createAttribute("href")
attrHref.value = aHref

if (isNewTab) {
attrTarget = document.createAttribute("target")
attrTarget.value = "_blank"
a.setAttributeNode(attrTarget)
}

a.setAttributeNode(attrHref)

a.textContent = aName
Expand Down

0 comments on commit 2b29b96

Please sign in to comment.