| 1 |
import type Swup from '../Swup.js'; |
| 2 |
import type { Options } from '../Swup.js'; |
| 3 |
import type { HistoryAction, HistoryDirection } from './navigate.js'; |
| 4 |
|
| 5 |
/** See below for the class Visit {} definition */ |
| 6 |
// export interface Visit {} |
| 7 |
|
| 8 |
export interface VisitFrom { |
| 9 |
/** The URL of the previous page */ |
| 10 |
url: string; |
| 11 |
/** The hash of the previous page */ |
| 12 |
hash?: string; |
| 13 |
} |
| 14 |
|
| 15 |
export interface VisitTo { |
| 16 |
/** The URL of the next page */ |
| 17 |
url: string; |
| 18 |
/** The hash of the next page */ |
| 19 |
hash?: string; |
| 20 |
/** The HTML content of the next page */ |
| 21 |
html?: string; |
| 22 |
/** The parsed document of the next page, available during visit */ |
| 23 |
document?: Document; |
| 24 |
} |
| 25 |
|
| 26 |
export interface VisitAnimation { |
| 27 |
/** Whether this visit is animated. Default: `true` */ |
| 28 |
animate: boolean; |
| 29 |
/** Whether to wait for the next page to load before starting the animation. Default: `false` */ |
| 30 |
wait: boolean; |
| 31 |
/** Name of a custom animation to run. */ |
| 32 |
name?: string; |
| 33 |
/** Whether this animation uses the native browser ViewTransition API. Default: `false` */ |
| 34 |
native: boolean; |
| 35 |
/** Elements on which to add animation classes. Default: `html` element */ |
| 36 |
scope: 'html' | 'containers' | string[]; |
| 37 |
/** Selector for detecting animation timing. Default: `[class*="transition-"]` */ |
| 38 |
selector: Options['animationSelector']; |
| 39 |
} |
| 40 |
|
| 41 |
export interface VisitScroll { |
| 42 |
/** Whether to reset the scroll position after the visit. Default: `true` */ |
| 43 |
reset: boolean; |
| 44 |
/** Anchor element to scroll to on the next page. */ |
| 45 |
target?: string | false; |
| 46 |
} |
| 47 |
|
| 48 |
export interface VisitTrigger { |
| 49 |
/** DOM element that triggered this visit. */ |
| 50 |
el?: Element; |
| 51 |
/** DOM event that triggered this visit. */ |
| 52 |
event?: Event; |
| 53 |
} |
| 54 |
|
| 55 |
export interface VisitCache { |
| 56 |
/** Whether this visit will try to load the requested page from cache. */ |
| 57 |
read: boolean; |
| 58 |
/** Whether this visit will save the loaded page in cache. */ |
| 59 |
write: boolean; |
| 60 |
} |
| 61 |
|
| 62 |
export interface VisitHistory { |
| 63 |
/** History action to perform: `push` for creating a new history entry, `replace` for replacing the current entry. Default: `push` */ |
| 64 |
action: HistoryAction; |
| 65 |
/** Whether this visit was triggered by a browser history navigation. */ |
| 66 |
popstate: boolean; |
| 67 |
/** The direction of travel in case of a browser history navigation: backward or forward. */ |
| 68 |
direction: HistoryDirection | undefined; |
| 69 |
} |
| 70 |
|
| 71 |
export interface VisitInitOptions { |
| 72 |
to: string; |
| 73 |
from?: string; |
| 74 |
hash?: string; |
| 75 |
el?: Element; |
| 76 |
event?: Event; |
| 77 |
} |
| 78 |
|
| 79 |
/** @internal */ |
| 80 |
export const VisitState = { |
| 81 |
CREATED: 1, |
| 82 |
QUEUED: 2, |
| 83 |
STARTED: 3, |
| 84 |
LEAVING: 4, |
| 85 |
LOADED: 5, |
| 86 |
ENTERING: 6, |
| 87 |
COMPLETED: 7, |
| 88 |
ABORTED: 8, |
| 89 |
FAILED: 9 |
| 90 |
} as const; |
| 91 |
|
| 92 |
/** @internal */ |
| 93 |
export type VisitState = (typeof VisitState)[keyof typeof VisitState]; |
| 94 |
|
| 95 |
/** An object holding details about the current visit. */ |
| 96 |
export class Visit { |
| 97 |
/** A unique ID to identify this visit */ |
| 98 |
id: number; |
| 99 |
/** The current state of this visit @internal */ |
| 100 |
state: VisitState; |
| 101 |
/** The previous page, about to leave */ |
| 102 |
from: VisitFrom; |
| 103 |
/** The next page, about to enter */ |
| 104 |
to: VisitTo; |
| 105 |
/** The content containers, about to be replaced */ |
| 106 |
containers: Options['containers']; |
| 107 |
/** Information about animated page transitions */ |
| 108 |
animation: VisitAnimation; |
| 109 |
/** What triggered this visit */ |
| 110 |
trigger: VisitTrigger; |
| 111 |
/** Cache behavior for this visit */ |
| 112 |
cache: VisitCache; |
| 113 |
/** Browser history behavior on this visit */ |
| 114 |
history: VisitHistory; |
| 115 |
/** Scroll behavior on this visit */ |
| 116 |
scroll: VisitScroll; |
| 117 |
/** User-defined metadata */ |
| 118 |
meta: Record<string, unknown>; |
| 119 |
|
| 120 |
constructor(swup: Swup, options: VisitInitOptions) { |
| 121 |
const { to, from, hash, el, event } = options; |
| 122 |
|
| 123 |
this.id = Math.random(); |
| 124 |
this.state = VisitState.CREATED; |
| 125 |
this.from = { url: from ?? swup.location.url, hash: swup.location.hash }; |
| 126 |
this.to = { url: to, hash }; |
| 127 |
this.containers = swup.options.containers; |
| 128 |
this.animation = { |
| 129 |
animate: true, |
| 130 |
wait: false, |
| 131 |
name: undefined, |
| 132 |
native: swup.options.native, |
| 133 |
scope: swup.options.animationScope, |
| 134 |
selector: swup.options.animationSelector |
| 135 |
}; |
| 136 |
this.trigger = { el, event }; |
| 137 |
this.cache = { |
| 138 |
read: swup.options.cache, |
| 139 |
write: swup.options.cache |
| 140 |
}; |
| 141 |
this.history = { |
| 142 |
action: 'push', |
| 143 |
popstate: false, |
| 144 |
direction: undefined |
| 145 |
}; |
| 146 |
this.scroll = { |
| 147 |
reset: true, |
| 148 |
target: undefined |
| 149 |
}; |
| 150 |
this.meta = {}; |
| 151 |
} |
| 152 |
|
| 153 |
/** @internal */ |
| 154 |
advance(state: VisitState) { |
| 155 |
if (this.state < state) { |
| 156 |
this.state = state; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** @internal */ |
| 161 |
abort() { |
| 162 |
this.state = VisitState.ABORTED; |
| 163 |
} |
| 164 |
|
| 165 |
/** Is this visit done, i.e. completed, failed, or aborted? */ |
| 166 |
get done(): boolean { |
| 167 |
return this.state >= VisitState.COMPLETED; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** Create a new visit object. */ |
| 172 |
export function createVisit(this: Swup, options: VisitInitOptions): Visit { |
| 173 |
return new Visit(this, options); |
| 174 |
} |