-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae68667
commit 96b477a
Showing
3 changed files
with
44 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,18 @@ | ||
|
||
let handleLinkClick = () => {} | ||
let handleLinkHover = () => {} | ||
export function whenThisLinkIsClicked(el, callback) { | ||
el.addEventListener('click', e => { | ||
e.preventDefault() | ||
|
||
export function whenALinkIsClicked(callback) { | ||
handleLinkClick = callback | ||
|
||
initializeLinksForClicking() | ||
callback(el) | ||
}) | ||
} | ||
|
||
export function whenALinkIsHovered(callback) { | ||
handleLinkHover = callback | ||
|
||
initializeLinksForHovering() | ||
export function whenThisLinkIsHovered(el, callback) { | ||
el.addEventListener('mouseenter', e => { | ||
callback(e) | ||
}) | ||
} | ||
|
||
export function extractDestinationFromLink(linkEl) { | ||
return new URL(linkEl.getAttribute('href'), document.baseURI) | ||
} | ||
|
||
export function hijackNewLinksOnThePage() { | ||
initializeLinksForClicking() | ||
initializeLinksForHovering() | ||
} | ||
|
||
function initializeLinksForClicking() { | ||
getLinks().forEach(el => { | ||
el.addEventListener('click', e => { | ||
e.preventDefault() | ||
|
||
handleLinkClick(el) | ||
}) | ||
}) | ||
} | ||
|
||
function initializeLinksForHovering() { | ||
getLinks() | ||
.filter(i => i.hasAttribute('wire:navigate.prefetch')) | ||
.forEach(el => { | ||
el.addEventListener('mouseenter', e => { | ||
handleLinkHover(el) | ||
}) | ||
}) | ||
} | ||
|
||
function getLinks() { | ||
return Array.from(document.links) | ||
.filter(i => i.hasAttribute('wire:navigate') | ||
|| i.hasAttribute('wire:navigate.prefetch')) | ||
} | ||
|