A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 967dfdfee0cf30569a9f932a4aa81eaa9c6629da (5912 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
state.authenticated = false;81
$route('@auth', { update: false });82
return;83
}84
state.authenticated = true;85
document.addEventListener('scroll', this.onScroll);86
document.addEventListener('wheel', this.onScroll);87
document.addEventListener('touchmove', this.onScroll);88
if('serviceWorker' in navigator) {89
navigator.serviceWorker.register('/worker.js');90
navigator.serviceWorker.addEventListener('controllerchange', this.updated);91
}92
$route(window.location.pathname, { update: false });93
},94
showError(error) {95
this.$refs.popup.open({96
title: $lang('global.error'),97
description: $lang('global.error.message', error),98
actions: [99
{100
title: $lang('global.report'),101
icon: 'message-report',102
handle: () => window.open('https://github.com/typable/verbal/issues')103
},104
{105
title: $lang('global.continue'),106
icon: 'arrow-right',107
handle: () => this.$refs.popup.close()108
}109
]110
});111
},112
updated() {113
this.$refs.popup.open({114
title: $lang('global.update'),115
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.',116
actions: [117
{118
title: $lang('global.continue'),119
icon: 'check',120
handle: () => window.location.reload()121
}122
]123
});124
},125
onPopState() {126
$route(window.location.pathname, { update: false });127
},128
onScroll() {129
if(state.tab === 'search') {130
if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {131
if(this.$refs.search.searching) {132
this.$refs.search.doSearchMore();133
}134
}135
}136
}137
},138
template: `139
<div140
v-if="state.authenticated !== null"141
class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh]"142
>143
<v-auth v-if="!state.authenticated" :state="state"></v-auth>144
<v-menu v-if="state.authenticated" :state="state"></v-menu>145
<div>146
<v-search v-if="state.authenticated" ref="search" :state="state"></v-search>147
<v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>148
<v-account v-if="state.authenticated" :state="state"></v-account>149
</div>150
<v-player ref="player" v-if="state.authenticated" :station="state.station"></v-player>151
<v-detail ref="detail" v-if="state.authenticated"></v-detail>152
<v-notfound v-if="state.authenticated" :state="state"></v-notfound>153
<v-popup ref="popup"></v-popup>154
</div>155
`156
});157
})();