Skip to content

Commit

Permalink
Merge pull request #25 from zipper/24-external-links
Browse files Browse the repository at this point in the history
fix: External links and multiple initializations of Collapsable
  • Loading branch information
zipper authored Sep 11, 2023
2 parents ed7ecc1 + 415cd6c commit 45c97ec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/Collapsable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ export class Collapsable {
const extLinks = document.querySelectorAll<HTMLAnchorElement>(options.externalLinks.selector)

extLinks.forEach((element) => {
const extLink = new CollapsableExtLink(this, element)
const collapsableItem = this.getItemById(element.hash.substring(1))

if (extLink) {
this.extLinks.push(extLink)
if (!collapsableItem) {
return
}

this.extLinks.push(new CollapsableExtLink(this, element, collapsableItem))
})
}

Expand Down
11 changes: 2 additions & 9 deletions src/CollapsableExtLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@ export class CollapsableExtLink {

private listener: EventListener | undefined

public constructor(collapsable: Collapsable, link: HTMLAnchorElement) {
public constructor(collapsable: Collapsable, link: HTMLAnchorElement, collapsableItem: CollapsableItem) {
this.collapsable = collapsable

if (link.tagName.toLowerCase() !== 'a') {
throw new Error(`Collapsable: External link has to be HTMLAnchorElement.'`)
}

const hash = link.getAttribute('href')?.substring(1)
const collapsableItem: CollapsableItem | undefined = this.collapsable.getItemById(hash)

if (!collapsableItem) {
throw new Error(`Collapsable: External link has no associated collapsable item.'`)
throw new Error('Collapsable: External link has to be HTMLAnchorElement.')
}

this.extLink = link
Expand Down

0 comments on commit 45c97ec

Please sign in to comment.