diff options
Diffstat (limited to 'node_modules/swup/src/helpers')
| -rwxr-xr-x | node_modules/swup/src/helpers/Location.ts | 38 | ||||
| -rwxr-xr-x | node_modules/swup/src/helpers/classify.ts | 12 | ||||
| -rwxr-xr-x | node_modules/swup/src/helpers/delegateEvent.ts | 27 | ||||
| -rwxr-xr-x | node_modules/swup/src/helpers/getCurrentUrl.ts | 4 | ||||
| -rwxr-xr-x | node_modules/swup/src/helpers/history.ts | 37 | ||||
| -rw-r--r-- | node_modules/swup/src/helpers/matchPath.ts | 23 |
6 files changed, 141 insertions, 0 deletions
diff --git a/node_modules/swup/src/helpers/Location.ts b/node_modules/swup/src/helpers/Location.ts new file mode 100755 index 00000000..aca9d6b6 --- /dev/null +++ b/node_modules/swup/src/helpers/Location.ts @@ -0,0 +1,38 @@ +/** + * A helper for creating a Location from either an element + * or a URL object/string + * + */ +export class Location extends URL { + constructor(url: URL | string, base: string = document.baseURI) { + super(url.toString(), base); + // Fix Safari bug with extending native classes + Object.setPrototypeOf(this, Location.prototype); + } + + /** + * The full local path including query params. + */ + get url(): string { + return this.pathname + this.search; + } + + /** + * Instantiate a Location from an element's href attribute + * @param el + * @returns new Location instance + */ + static fromElement(el: Element): Location { + const href = el.getAttribute('href') || el.getAttribute('xlink:href') || ''; + return new Location(href); + } + + /** + * Instantiate a Location from a URL object or string + * @param url + * @returns new Location instance + */ + static fromUrl(url: URL | string): Location { + return new Location(url); + } +} diff --git a/node_modules/swup/src/helpers/classify.ts b/node_modules/swup/src/helpers/classify.ts new file mode 100755 index 00000000..3d9ecec6 --- /dev/null +++ b/node_modules/swup/src/helpers/classify.ts @@ -0,0 +1,12 @@ +/** Turn a string into a slug by lowercasing and replacing whitespace. */ +export const classify = (text: string, fallback?: string): string => { + const output = String(text) + .toLowerCase() + // .normalize('NFD') // split an accented letter in the base letter and the acent + // .replace(/[\u0300-\u036f]/g, '') // remove all previously split accents + .replace(/[\s/_.]+/g, '-') // replace spaces and _./ with '-' + .replace(/[^\w-]+/g, '') // remove all non-word chars + .replace(/--+/g, '-') // replace repeating '-' with single '-' + .replace(/^-+|-+$/g, ''); // trim '-' from edges + return output || fallback || ''; +}; diff --git a/node_modules/swup/src/helpers/delegateEvent.ts b/node_modules/swup/src/helpers/delegateEvent.ts new file mode 100755 index 00000000..eec44f50 --- /dev/null +++ b/node_modules/swup/src/helpers/delegateEvent.ts @@ -0,0 +1,27 @@ +import delegate, { + type DelegateEventHandler, + type DelegateOptions, + type EventType +} from 'delegate-it'; +import type { ParseSelector } from 'typed-query-selector/parser.js'; + +export type DelegateEventUnsubscribe = { + destroy: () => void; +}; + +/** Register a delegated event listener. */ +export const delegateEvent = < + Selector extends string, + TElement extends Element = ParseSelector<Selector, HTMLElement>, + TEvent extends EventType = EventType +>( + selector: Selector, + type: TEvent, + callback: DelegateEventHandler<GlobalEventHandlersEventMap[TEvent], TElement>, + options?: DelegateOptions +): DelegateEventUnsubscribe => { + const controller = new AbortController(); + options = { ...options, signal: controller.signal }; + delegate<Selector, TElement, TEvent>(selector, type, callback, options); + return { destroy: () => controller.abort() }; +}; diff --git a/node_modules/swup/src/helpers/getCurrentUrl.ts b/node_modules/swup/src/helpers/getCurrentUrl.ts new file mode 100755 index 00000000..ab0f0d57 --- /dev/null +++ b/node_modules/swup/src/helpers/getCurrentUrl.ts @@ -0,0 +1,4 @@ +/** Get the current page URL: path name + query params. Optionally including hash. */ +export const getCurrentUrl = ({ hash }: { hash?: boolean } = {}): string => { + return window.location.pathname + window.location.search + (hash ? window.location.hash : ''); +}; diff --git a/node_modules/swup/src/helpers/history.ts b/node_modules/swup/src/helpers/history.ts new file mode 100755 index 00000000..e7ea2f6c --- /dev/null +++ b/node_modules/swup/src/helpers/history.ts @@ -0,0 +1,37 @@ +import { getCurrentUrl } from './getCurrentUrl.js'; + +export interface HistoryState { + url: string; + source: 'swup'; + random: number; + index?: number; + [key: string]: unknown; +} + +type HistoryData = Record<string, unknown>; + +/** Create a new history record with a custom swup identifier. */ +export const createHistoryRecord = (url: string, data: HistoryData = {}): void => { + url = url || getCurrentUrl({ hash: true }); + const state: HistoryState = { + url, + random: Math.random(), + source: 'swup', + ...data + }; + window.history.pushState(state, '', url); +}; + +/** Update the current history record with a custom swup identifier. */ +export const updateHistoryRecord = (url: string | null = null, data: HistoryData = {}): void => { + url = url || getCurrentUrl({ hash: true }); + const currentState = (window.history.state as HistoryState) || {}; + const state: HistoryState = { + ...currentState, + url, + random: Math.random(), + source: 'swup', + ...data + }; + window.history.replaceState(state, '', url); +}; diff --git a/node_modules/swup/src/helpers/matchPath.ts b/node_modules/swup/src/helpers/matchPath.ts new file mode 100644 index 00000000..e1332911 --- /dev/null +++ b/node_modules/swup/src/helpers/matchPath.ts @@ -0,0 +1,23 @@ +import { match } from 'path-to-regexp'; + +import type { + Path, + ParseOptions, + TokensToRegexpOptions, + RegexpToFunctionOptions, + MatchFunction +} from 'path-to-regexp'; + +export { type Path }; + +/** Create a match function from a path pattern that checks if a URLs matches it. */ +export const matchPath = <P extends object = object>( + path: Path, + options?: ParseOptions & TokensToRegexpOptions & RegexpToFunctionOptions +): MatchFunction<P> => { + try { + return match<P>(path, options); + } catch (error) { + throw new Error(`[swup] Error parsing path "${String(path)}":\n${String(error)}`); + } +}; |
