koel/resources/assets/js/router.ts

87 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import isMobile from 'ismobilejs'
2022-05-14 18:49:45 +00:00
import { loadMainView, use } from '@/utils'
import { albumStore, artistStore, playlistStore, queueStore, songStore, userStore } from '@/stores'
import { playbackService } from '@/services'
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
class Router {
routes: Record<string, Closure>
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
constructor () {
this.routes = {
'/home': () => loadMainView('Home'),
'/queue': () => loadMainView('Queue'),
'/songs': () => loadMainView('Songs'),
'/albums': () => loadMainView('Albums'),
'/artists': () => loadMainView('Artists'),
'/favorites': () => loadMainView('Favorites'),
'/recently-played': () => loadMainView('RecentlyPlayed'),
'/search': () => loadMainView('Search.Excerpt'),
'/search/songs/(.+)': (q: string) => loadMainView('Search.Songs', q),
'/upload': () => userStore.current.is_admin && loadMainView('Upload'),
'/settings': () => userStore.current.is_admin && loadMainView('Settings'),
'/users': () => userStore.current.is_admin && loadMainView('Users'),
'/youtube': () => loadMainView('YouTube'),
'/visualizer': () => loadMainView('Visualizer'),
'/profile': () => loadMainView('Profile'),
2022-06-10 10:47:46 +00:00
'/album/(\\d+)': async (id: number) => loadMainView('Album', await albumStore.resolve(id)),
'/artist/(\\d+)': async (id: number) => loadMainView('Artist', await artistStore.resolve(id)),
2022-05-14 23:37:16 +00:00
'/playlist/(\\d+)': (id: number) => use(playlistStore.byId(~~id), playlist => loadMainView('Playlist', playlist)),
2022-06-10 10:47:46 +00:00
'/song/([a-z0-9]{32})': async (id: string) => {
2022-07-05 21:43:35 +00:00
const song = await songStore.resolve(id)
2022-06-10 10:47:46 +00:00
if (!song) {
this.go('home')
return
}
2022-05-14 23:37:16 +00:00
if (isMobile.apple.device) {
// Mobile Safari doesn't allow autoplay, so we just queue.
queueStore.queue(song)
loadMainView('Queue')
} else {
await playbackService.queueAndPlay([song])
}
2022-06-10 10:47:46 +00:00
}
2022-05-14 23:37:16 +00:00
}
2022-04-15 14:24:30 +00:00
2022-05-14 23:37:16 +00:00
window.addEventListener('popstate', () => this.resolveRoute(), true)
}
public resolveRoute () {
2022-04-15 14:24:30 +00:00
if (!window.location.hash) {
return this.go('home')
}
2022-04-15 17:00:08 +00:00
Object.keys(this.routes).forEach(route => {
2022-04-15 14:24:30 +00:00
const matches = window.location.hash.match(new RegExp(`^#!${route}$`))
if (matches) {
const [, ...params] = matches
this.routes[route](...params)
}
})
2022-05-14 23:37:16 +00:00
}
2022-04-15 14:24:30 +00:00
/**
* Navigate to a (relative, hash-bang'ed) path.
*/
2022-05-14 23:37:16 +00:00
public go (path: string | number) {
2022-04-15 14:24:30 +00:00
if (typeof path === 'number') {
window.history.go(path)
return
}
2022-05-14 18:49:45 +00:00
if (!path.startsWith('/')) {
2022-04-15 14:24:30 +00:00
path = `/${path}`
}
2022-05-14 18:49:45 +00:00
if (!path.startsWith('/#!')) {
2022-04-15 14:24:30 +00:00
path = `/#!${path}`
}
path = path.substring(1, path.length)
document.location.href = `${document.location.origin}${document.location.pathname}${path}`
}
}
2022-05-14 23:37:16 +00:00
export default new Router()