All files pretty.ts

95.24% Statements 80/84
80.77% Branches 21/26
83.33% Functions 10/12
95.77% Lines 68/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 1591x                           1x     28x 28x 9x 19x   19x     28x           1x 14x     14x 9x     14x     1x 14x   14x   14x 8x 8x     6x     1x 1x 4x 4x 4x         1x 7x 7x 7x 6x             30x     15x     15x   15x 15x 8x 8x   16x 14x 14x     8x       15x 15x 5x   15x 4x 4x   4x     4x   12x 9x       4x     15x 15x   15x     1x         1x             1x 4x   4x 4x   4x 4x   1x       1x 1x   2x         1x  
import { app, Route, ROUTER_404_EVENT, ROUTER_EVENT } from "apprun";
 
export interface IPrettyRoute extends Route {
	linkClick: (event: MouseEvent) => void;
}
 
export interface IPrettyRouteDynamicSegments {
	[route: string]: string;
}
 
export interface IPrettyRouteQueries {
	[field: string]: string;
}
 
const dynamicRoutes: IPrettyRouteDynamicSegments = {};
 
function decomposeDynamicRoute(route: string): {staticSegment: string, dynamicSegment: string } {
	let pos = route.search(/[:\?]/);
	if(pos === -1) {
		pos = route.length;
	} else Eif(route[pos] === ":") {
		// Need to capture the "/"" before the ":". It must exist to be a well formed path element.
		pos = pos - 1;
	}
 
	return {
		dynamicSegment: route.substring(pos),
		staticSegment: route.substring(0, pos),
	};
}
 
export function addDynamicRoute(route: string): string {
	const {staticSegment, dynamicSegment} = decomposeDynamicRoute(route);
 
	// Just in case someone passes in a static route - ignore it.
	if(dynamicSegment) {
		dynamicRoutes[staticSegment] = dynamicSegment;
	}
 
	return staticSegment;
}
 
export function removeDynamicRoute(route: string): string | undefined {
	const {staticSegment, dynamicSegment} = decomposeDynamicRoute(route);
 
	const old = dynamicRoutes[staticSegment];
 
	if(old) {
		delete dynamicRoutes[staticSegment];
		return staticSegment;
	}
 
	return undefined;
}
 
let prettyLinkQuerySeparator = "&";
export function setPrettyLinkQuerySeparator(newSeparator: string): string {
	const old = prettyLinkQuerySeparator;
	prettyLinkQuerySeparator = newSeparator;
	return old;
}
 
// Handle automatically setting the pretty link onclick handler
// via the $prettylink directive
const prettyClickDirective: string = "$prettylink";
app.on("$", ({key, props}) => {
		Eif (key === prettyClickDirective) {
			if(typeof props[key] === "boolean") {
				props.onclick = linkClick;
			}
		}
	});
 
// A router function that handles "pretty links" (i.e. non hash based urls)
// dynamic segements, query strings, and html5 history.
function prettyLinkRouter(url: string, Epopstate: boolean = false): void {
	// Adjust url in case there is a dynamic portion such as a query or
	// dynamic segment.
	const origUrl = url;
 
	// Any queries?
	const queries: IPrettyRouteQueries = {};
 
	const queryPos = url.search(/\?/);
	if(queryPos >= 0) {
		const queryStr = url.substring(queryPos + 1);
		const queryStrings = queryStr.split(prettyLinkQuerySeparator);
 
		for(const query of queryStrings) {
			const [field, value] = query.split("=");
			queries[field] = value;
		}
 
		url = url.substring(0, queryPos);
	}
 
	// Any dynamic segements?
	const dynamic: IPrettyRouteDynamicSegments = {};
	const matchingDynamicRoute = Object.keys(dynamicRoutes).find((route) => {
		return url.startsWith(route + "/");
	});
	if(matchingDynamicRoute) {
		const urlDynamicParams = url.substring(matchingDynamicRoute.length).split("/");
		const dynamicParams = dynamicRoutes[matchingDynamicRoute].split("/");
 
		console.assert(dynamicParams.length === urlDynamicParams.length, `expected dynamic and actual dynamic length don't match ${dynamicParams.length} vs. ${urlDynamicParams.length}`);
 
		// First param will always be a "", so ignore it.
		for(let i = 1; i < dynamicParams.length; ++i) {
			// Is this parameter in the dynamic section actually dynamic?
			if(dynamicParams[i][0] === ":") {
				dynamic[dynamicParams[i].substring(1)] = urlDynamicParams[i];
			}
		}
 
		url = matchingDynamicRoute;
	}
 
	app.run(url, dynamic, queries) || app.run(ROUTER_404_EVENT, url, dynamic, queries);
	app.run(ROUTER_EVENT, url, dynamic, queries);
 
	Eif(!popstate) history.pushState({}, "", origUrl);
}
 
const prettyLinkRouterPopstateHandler = (_event: PopStateEvent) => {
	// app.route cannot be undefined/null as it is assigned at the end of this module.
	app.route!(location.href.replace(location.origin, ""), true);
};
 
document.addEventListener("DOMContentLoaded", () => {
	window.onpopstate = prettyLinkRouterPopstateHandler;
 
	prettyLinkRouter(location.href.replace(location.origin, ""));
});
 
// linkClick method to be used as an onclick event handler.
const linkClick = (event: MouseEvent) => {
	event.preventDefault();
 
	const currentTarget: HTMLBaseElement = event.currentTarget as HTMLBaseElement;
	const href: string = currentTarget.href;
 
	Iif(!href) console.error("linkClick invoked on target with no href!", event.currentTarget);
	else app.run("route", href.replace(location.origin, ""));
};
prettyLinkRouter.linkClick = linkClick;
 
// selectors is a standard DOMString so you can select multiple selectors separated by
// commas to come up with any scheme to identify all your links.
export function addPrettyLinkHandlers(selectors: string): void {
	document.querySelectorAll(selectors)
		.forEach((link: Element) => {
			(link as HTMLElement).onclick = linkClick;
		});
}
 
// Overwrite AppRun's default router which only supports hash links.
app.route = prettyLinkRouter;