A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: e943dd393a26f67d200aa102facf5a7de35c5153 (6412 bytes)[doc][blame][raw]
1
import Vue, {reactive} from '/asset/lib/vue.min.js';2
import {http, $lang, $route} from './utils.js';3
import VMenu from './component/menu.js';4
import VPlayer from './component/player.js';5
import VPopup from './component/popup.js';6
import VSearch from './page/search.js';7
import VFavorites from './page/favorites.js';8
import VAccount from './page/account.js';9
import VNotfound from './page/notfound.js';10
import VAuth from './page/auth.js';11
import VProfile from './page/profile.js';12
import VDetail from './component/detail.js';14
export const VERSION = '{{version}}';15
export const SWIPE_THRESHOLD = 70;16
export const TOKEN_NAME = 'verbal-token';18
export const ROUTES = {19
'^\/$': 'search',20
'^\/search$': 'search',21
'^\/favorites$': 'favorites',22
'^\/account$': 'account',23
'^\/station\/(\\d+)$': 'detail',24
'^@not-found$': 'not-found',25
'^\/auth$': 'auth',26
'^\/profile$': 'profile',27
'^\/profile/(\\d+)$': 'profile'28
};30
export const state = reactive({31
app: null,32
tab: 'search',33
tabs: ['search', 'favorites', 'account'],34
account: null,35
devices: null,36
token: null,37
station: null,38
authenticated: null,39
version: VERSION40
});42
(async () => {43
state.locale = await http`get::/asset/json/locale.json`();44
new Vue({45
el: '#app',46
data: {47
state,48
touch: null,49
target: null50
},51
components: {52
VMenu,53
VSearch,54
VFavorites,55
VAccount,56
VNotfound,57
VAuth,58
VPlayer,59
VPopup,60
VDetail,61
VProfile62
},63
mounted() {64
document.body.style.display = '';65
state.app = this;66
this.init();67
window.addEventListener('popstate', this.onPopState);68
},69
methods: {70
async init() {71
try {72
state.account = await http`get::/api/account`();73
state.devices = await http`get::/api/devices`();74
state.token = localStorage.getItem(TOKEN_NAME);75
}76
catch(err) {77
state.account = null;78
state.devices = null;79
state.token = null;80
}81
state.tab = 'search';82
state.station = null;83
if(state.account === null) {84
localStorage.setItem(TOKEN_NAME, 'guest-token');85
state.account = await http`get::/api/account`();86
state.devices = await http`get::/api/devices`();87
state.token = localStorage.getItem(TOKEN_NAME);88
}89
state.authenticated = state.token !== 'guest-token';90
if(!state.authenticated) {91
const navigatorLanguage = navigator?.language;92
let language = 'en';93
if(/^de-?/.test(navigatorLanguage)) {94
language = 'de';95
}96
state.account.language = language;97
}98
document.addEventListener('scroll', this.onScroll);99
document.addEventListener('wheel', this.onScroll);100
document.addEventListener('touchmove', this.onScroll);101
if('serviceWorker' in navigator) {102
navigator.serviceWorker.register('/worker.js');103
navigator.serviceWorker.addEventListener('controllerchange', this.updated);104
}105
$route(window.location.pathname, { update: false });106
},107
showError(error) {108
this.$refs.popup.open({109
title: $lang('global.error'),110
description: $lang('global.error.message', error),111
actions: [112
{113
title: $lang('global.report'),114
icon: 'message-report',115
handle: () => window.open('https://github.com/typable/verbal/issues')116
},117
{118
title: $lang('global.continue'),119
icon: 'arrow-right',120
handle: () => this.$refs.popup.close()121
}122
]123
});124
},125
updated() {126
this.$refs.popup.open({127
title: $lang('global.update'),128
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.',129
actions: [130
{131
title: $lang('global.continue'),132
icon: 'check',133
handle: () => window.location.reload()134
}135
]136
});137
},138
onPopState() {139
$route(window.location.pathname, { update: false });140
},141
onScroll() {142
if(state.tab === 'search') {143
if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {144
if(this.$refs.search.searching) {145
this.$refs.search.doSearchMore();146
}147
}148
}149
}150
},151
template: `152
<div153
v-if="state.authenticated !== null"154
class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh]"155
>156
<v-menu :state="state"></v-menu>157
<v-auth :state="state"></v-auth>158
<div>159
<v-search ref="search" :state="state"></v-search>160
<v-favorites ref="favorites" :state="state"></v-favorites>161
<v-account :state="state"></v-account>162
<v-profile ref="profile" :state="state"></v-profile>163
</div>164
<v-player ref="player" :station="state.station"></v-player>165
<v-detail ref="detail"></v-detail>166
<v-notfound :state="state"></v-notfound>167
<v-popup ref="popup"></v-popup>168
</div>169
`170
});171
})();