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