koel/resources/assets/js/router.js

125 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import isMobile from 'ismobilejs'
2016-07-10 18:05:18 +00:00
2016-11-26 03:25:35 +00:00
import { loadMainView } from './utils'
import { artistStore, albumStore, songStore, queueStore, playlistStore, userStore } from './stores'
import { playback } from './services'
2016-07-10 17:55:20 +00:00
export default {
routes: {
2016-11-26 03:25:35 +00:00
'/home' () {
loadMainView('home')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/queue' () {
loadMainView('queue')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/songs' () {
loadMainView('songs')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/albums' () {
loadMainView('albums')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/album/(\\d+)' (id) {
const album = albumStore.byId(~~id)
2016-07-10 17:55:20 +00:00
if (album) {
2016-11-26 03:25:35 +00:00
loadMainView('album', album)
2016-07-10 17:55:20 +00:00
}
},
2016-11-26 03:25:35 +00:00
'/artists' () {
loadMainView('artists')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/artist/(\\d+)' (id) {
const artist = artistStore.byId(~~id)
2016-07-10 17:55:20 +00:00
if (artist) {
2016-11-26 03:25:35 +00:00
loadMainView('artist', artist)
2016-07-10 17:55:20 +00:00
}
},
2016-11-26 03:25:35 +00:00
'/favorites' () {
loadMainView('favorites')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/playlist/(\\d+)' (id) {
const playlist = playlistStore.byId(~~id)
2016-07-10 17:55:20 +00:00
if (playlist) {
2016-11-26 03:25:35 +00:00
loadMainView('playlist', playlist)
2016-07-10 17:55:20 +00:00
}
},
2016-11-26 03:25:35 +00:00
'/settings' () {
userStore.current.is_admin && loadMainView('settings')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/users' () {
userStore.current.is_admin && loadMainView('users')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/profile' () {
loadMainView('profile')
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/login' () {
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
'/song/([a-z0-9]{32})' (id) {
const song = songStore.byId(id)
if (!song) return
2016-07-10 18:05:18 +00:00
if (isMobile.apple.device) {
// Mobile Safari doesn't allow autoplay, so we just queue.
2016-11-26 03:25:35 +00:00
queueStore.queue(song)
loadMainView('queue')
2016-07-10 18:05:18 +00:00
} else {
2016-11-26 03:25:35 +00:00
playback.queueAndPlay(song)
2016-07-10 18:05:18 +00:00
}
2016-07-10 17:55:20 +00:00
},
2016-07-30 15:32:17 +00:00
2016-11-26 03:25:35 +00:00
'/youtube' () {
loadMainView('youtubePlayer')
}
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
init () {
this.loadState()
window.addEventListener('popstate', () => this.loadState(), true)
2016-07-10 17:55:20 +00:00
},
2016-11-26 03:25:35 +00:00
loadState () {
2016-07-10 17:55:20 +00:00
if (!window.location.hash) {
2016-11-26 03:25:35 +00:00
return this.go('home')
2016-07-10 17:55:20 +00:00
}
Object.keys(this.routes).forEach(route => {
2016-11-26 03:25:35 +00:00
const matches = window.location.hash.match(new RegExp(`^#!${route}$`))
2016-07-10 17:55:20 +00:00
if (matches) {
2016-11-26 03:25:35 +00:00
const [, ...params] = matches
this.routes[route](...params)
return false
2016-07-10 17:55:20 +00:00
}
2016-11-26 03:25:35 +00:00
})
2016-07-10 17:55:20 +00:00
},
/**
2016-07-11 01:41:35 +00:00
* Navigate to a (relative, hashed) path.
2016-07-10 17:55:20 +00:00
*
* @param {String} path
*/
2016-11-26 03:25:35 +00:00
go (path) {
2016-07-10 17:55:20 +00:00
if (path[0] !== '/') {
2016-11-26 03:25:35 +00:00
path = `/${path}`
2016-07-10 17:55:20 +00:00
}
2016-07-11 01:41:35 +00:00
if (path.indexOf('/#!') !== 0) {
2016-11-26 03:25:35 +00:00
path = `/#!${path}`
2016-07-11 01:41:35 +00:00
}
2016-11-26 03:25:35 +00:00
document.location.href = `${document.location.origin}${path}`
}
}