2022-04-15 14:24:30 +00:00
|
|
|
import isMobile from 'ismobilejs'
|
|
|
|
|
|
|
|
import { loadMainView } from './utils'
|
2022-04-15 17:00:08 +00:00
|
|
|
import { albumStore, artistStore, playlistStore, queueStore, songStore, userStore } from './stores'
|
2022-04-24 08:50:45 +00:00
|
|
|
import { playbackService } from './services'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { use } from '@/utils'
|
|
|
|
|
|
|
|
const router = {
|
|
|
|
routes: {
|
2022-04-15 17:00:08 +00:00
|
|
|
'/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'),
|
2022-04-15 14:24:30 +00:00
|
|
|
'/search/songs/(.+)': (q: string) => loadMainView('Search.Songs', q),
|
2022-04-15 17:00:08 +00:00
|
|
|
'/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'),
|
|
|
|
'/album/(\\d+)': (id: number) => use(albumStore.byId(~~id)!, album => loadMainView('Album', album)),
|
|
|
|
'/artist/(\\d+)': (id: number) => use(artistStore.byId(~~id)!, artist => loadMainView('Artist', artist)),
|
|
|
|
'/playlist/(\\d+)': (id: number) => use(playlistStore.byId(~~id)!, playlist => loadMainView('Playlist', playlist)),
|
|
|
|
'/song/([a-z0-9]{32})': (id: string) => use(songStore.byId(id)!, song => {
|
2022-04-15 14:24:30 +00:00
|
|
|
if (isMobile.apple.device) {
|
|
|
|
// Mobile Safari doesn't allow autoplay, so we just queue.
|
|
|
|
queueStore.queue(song)
|
|
|
|
loadMainView('Queue')
|
|
|
|
} else {
|
2022-04-24 08:50:45 +00:00
|
|
|
playbackService.queueAndPlay([song])
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
})
|
2022-05-05 15:30:10 +00:00
|
|
|
} as { [path: string]: Closure },
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
init () {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.loadState()
|
2022-04-15 17:00:08 +00:00
|
|
|
window.addEventListener('popstate', () => this.loadState(), true)
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-04-15 17:00:08 +00:00
|
|
|
loadState () {
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to a (relative, hash-bang'ed) path.
|
|
|
|
*/
|
2022-04-15 17:00:08 +00:00
|
|
|
go: (path: string | number) => {
|
2022-04-15 14:24:30 +00:00
|
|
|
if (window.__UNIT_TESTING__) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof path === 'number') {
|
|
|
|
window.history.go(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path[0] !== '/') {
|
|
|
|
path = `/${path}`
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.indexOf('/#!') !== 0) {
|
|
|
|
path = `/#!${path}`
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path.substring(1, path.length)
|
|
|
|
document.location.href = `${document.location.origin}${document.location.pathname}${path}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default router
|