|
@@ -1,6 +1,6 @@
|
|
|
import { updateCurrentPageHtmlInHistoryStateForLaterBackButtonClicks, updateUrlAndStoreLatestHtmlForFutureBackButtons, whenTheBackOrForwardButtonIsClicked } from "./history"
|
|
|
import { getPretchedHtmlOr, prefetchHtml, storeThePrefetchedHtmlForWhenALinkIsClicked } from "./prefetch"
|
|
|
-import { extractDestinationFromLink, whenThisLinkIsClicked, whenThisLinkIsHovered } from "./links"
|
|
|
+import { createUrlObjectFromString, extractDestinationFromLink, whenThisLinkIsHoveredFor, whenThisLinkIsPressed } from "./links"
|
|
|
import { restoreScrollPosition, storeScrollInformationInHtmlBeforeNavigatingAway } from "./scroll"
|
|
|
import { putPersistantElementsBack, storePersistantElementsForLater } from "./persist"
|
|
|
import { finishAndHideProgressBar, showAndStartProgressBar } from "./bar"
|
|
@@ -14,52 +14,70 @@ let restoreScroll = true
|
|
|
let autofocus = false
|
|
|
|
|
|
export default function (Alpine) {
|
|
|
+ Alpine.navigate = (url) => {
|
|
|
+ navigateTo(
|
|
|
+ createUrlObjectFromString(url)
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
Alpine.directive('navigate', (el, { value, expression, modifiers }, { evaluateLater, cleanup }) => {
|
|
|
- let shouldPrefetch = modifiers.includes('prefetch')
|
|
|
+ // "persisted" elements will be picked up by .querySelector, not this callback...
|
|
|
+ if (value === 'persist') return
|
|
|
+
|
|
|
+ let shouldPrefetch = modifiers.includes('prefetch') && modifiers.includes('hover')
|
|
|
|
|
|
- shouldPrefetch && whenThisLinkIsHovered(el, () => {
|
|
|
- let forDestination = extractDestinationFromLink(el)
|
|
|
+ shouldPrefetch && whenThisLinkIsHoveredFor(el, 60, () => {
|
|
|
+ let destination = extractDestinationFromLink(el)
|
|
|
|
|
|
- prefetchHtml(forDestination, html => {
|
|
|
- storeThePrefetchedHtmlForWhenALinkIsClicked(html, forDestination)
|
|
|
+ prefetchHtml(destination, html => {
|
|
|
+ storeThePrefetchedHtmlForWhenALinkIsClicked(html, destination)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
- whenThisLinkIsClicked(el, () => {
|
|
|
- showProgressBar && showAndStartProgressBar()
|
|
|
+ whenThisLinkIsPressed(el, (whenItIsReleased) => {
|
|
|
+ let destination = extractDestinationFromLink(el)
|
|
|
+
|
|
|
+ prefetchHtml(destination, html => {
|
|
|
+ storeThePrefetchedHtmlForWhenALinkIsClicked(html, destination)
|
|
|
+ })
|
|
|
+
|
|
|
+ whenItIsReleased(() => {
|
|
|
+ navigateTo(destination)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ })
|
|
|
|
|
|
- let fromDestination = extractDestinationFromLink(el)
|
|
|
+ function navigateTo(destination) {
|
|
|
+ showProgressBar && showAndStartProgressBar()
|
|
|
|
|
|
- fetchHtmlOrUsePrefetchedHtml(fromDestination, html => {
|
|
|
- restoreScroll && storeScrollInformationInHtmlBeforeNavigatingAway()
|
|
|
+ fetchHtmlOrUsePrefetchedHtml(destination, html => {
|
|
|
+ restoreScroll && storeScrollInformationInHtmlBeforeNavigatingAway()
|
|
|
|
|
|
- showProgressBar && finishAndHideProgressBar()
|
|
|
+ showProgressBar && finishAndHideProgressBar()
|
|
|
|
|
|
- updateCurrentPageHtmlInHistoryStateForLaterBackButtonClicks()
|
|
|
+ updateCurrentPageHtmlInHistoryStateForLaterBackButtonClicks()
|
|
|
|
|
|
- preventAlpineFromPickingUpDomChanges(Alpine, andAfterAllThis => {
|
|
|
- enablePersist && storePersistantElementsForLater()
|
|
|
+ preventAlpineFromPickingUpDomChanges(Alpine, andAfterAllThis => {
|
|
|
+ enablePersist && storePersistantElementsForLater()
|
|
|
|
|
|
- swapCurrentPageWithNewHtml(html, () => {
|
|
|
- enablePersist && putPersistantElementsBack()
|
|
|
+ swapCurrentPageWithNewHtml(html, () => {
|
|
|
+ enablePersist && putPersistantElementsBack()
|
|
|
|
|
|
- restoreScroll && restoreScrollPosition()
|
|
|
+ restoreScroll && restoreScrollPosition()
|
|
|
|
|
|
- fireEventForOtherLibariesToHookInto()
|
|
|
+ fireEventForOtherLibariesToHookInto()
|
|
|
|
|
|
- updateUrlAndStoreLatestHtmlForFutureBackButtons(html, fromDestination)
|
|
|
+ updateUrlAndStoreLatestHtmlForFutureBackButtons(html, destination)
|
|
|
|
|
|
- andAfterAllThis(() => {
|
|
|
- autofocus && autofocusElementsWithTheAutofocusAttribute()
|
|
|
+ andAfterAllThis(() => {
|
|
|
+ autofocus && autofocusElementsWithTheAutofocusAttribute()
|
|
|
|
|
|
- nowInitializeAlpineOnTheNewPage(Alpine)
|
|
|
- })
|
|
|
+ nowInitializeAlpineOnTheNewPage(Alpine)
|
|
|
})
|
|
|
})
|
|
|
})
|
|
|
})
|
|
|
- })
|
|
|
-
|
|
|
+ }
|
|
|
|
|
|
whenTheBackOrForwardButtonIsClicked((html) => {
|
|
|
// @todo: see if there's a way to update the current HTML BEFORE
|