summaryrefslogtreecommitdiff
path: root/examples/view-transitions/src/scripts/utils.js
blob: 5b7f78535477cd8bc612e14b71316c764f422d0b (plain) (blame)
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
export function getNavigationType(fromPath, toPath) {
	if (fromPath.startsWith('/movies') && toPath === '/') {
		return 'movie-to-home';
	}

	if (fromPath === '/tv' && toPath.startsWith('/tv/')) {
		return 'tv-to-show';
	}

	if (fromPath === '/' && toPath.startsWith('/movies')) {
		return 'home-to-movie';
	}

	if (fromPath.startsWith('/tv/') && toPath === '/tv') {
		return 'show-to-tv';
	}

	if (
		(fromPath.startsWith('/movies') || fromPath.startsWith('/tv')) &&
		toPath.startsWith('/people')
	) {
		return 'movie-to-person';
	}

	if (
		fromPath.startsWith('/people') &&
		(toPath.startsWith('/movies') || toPath.startsWith('/tv/'))
	) {
		return 'person-to-movie';
	}

	return 'other';
}

export function isBackNavigation(navigateEvent) {
	if (navigateEvent.navigationType === 'push' || navigateEvent.navigationType === 'replace') {
		return false;
	}
	if (
		navigateEvent.destination.index !== -1 &&
		navigateEvent.destination.index < navigation.currentEntry.index
	) {
		return true;
	}
	return false;
}

export function shouldNotIntercept(navigationEvent) {
	return (
		navigationEvent.canIntercept === false ||
		// If this is just a hashChange,
		// just let the browser handle scrolling to the content.
		navigationEvent.hashChange ||
		// If this is a download,
		// let the browser perform the download.
		navigationEvent.downloadRequest ||
		// If this is a form submission,
		// let that go to the server.
		navigationEvent.formData
	);
}

export function useTvFragment(navigateEvent) {
	const toUrl = new URL(navigateEvent.destination.url);
	const toPath = toUrl.pathname;

	return toPath.startsWith('/tv');
}

export function getPathId(path) {
	return path.split('/')[2];
}

export function updateTheDOMSomehow(data) {
	document.getElementById('content').innerHTML = data;
}