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