A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: d2f18a01971009c5b2fc1bdddb6f01d033935644 (7582 bytes)[doc][blame][raw]
1
import Vue from '/asset/lib/vue.min.js';2
import {http, $lang} 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';10
const VERSION = '{{version}}';11
const SWIPE_THRESHOLD = 70;13
export const TOKEN_NAME = 'verbal-token';15
export const state = {16
app: null,17
tab: 'search',18
tabs: ['search', 'favorites', 'account'],19
account: null,20
devices: null,21
token: null,22
station: null,23
authenticated: false,24
open: false,25
version: VERSION26
};28
(async () => {29
state.locale = await http`get::/asset/json/locale.json`();30
new Vue({31
el: '#app',32
data: {33
state,34
touch: null,35
target: null36
},37
components: {38
VMenu,39
VSearch,40
VFavorites,41
VAccount,42
VPlayer,43
VPopup44
},45
mounted() {46
document.body.style.display = '';47
state.app = this;48
this.init();49
},50
methods: {51
async init() {52
try {53
state.account = await http`get::/api/account`();54
state.devices = await http`get::/api/devices`();55
state.token = localStorage.getItem(TOKEN_NAME);56
}57
catch(error) {58
// ignore59
}60
if(state.account === null) {61
this.$refs.popup.open({62
title: $lang('global.welcome'),63
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.',64
actions: [65
{66
icon: 'check',67
title: $lang('global.continue'),68
handle: () => {69
localStorage.setItem('verbal-token', 'test-token');70
window.location.reload();71
}72
}73
]74
});75
return;76
}77
state.authenticated = true;78
document.addEventListener('scroll', this.onScroll);79
document.addEventListener('wheel', this.onScroll);80
if('serviceWorker' in navigator) {81
navigator.serviceWorker.register('/worker.js');82
navigator.serviceWorker.addEventListener('controllerchange', this.updated);83
}84
},85
showError(error) {86
this.$refs.popup.open({87
title: $lang('global.error'),88
description: $lang('global.error.message', error),89
actions: [90
{91
title: $lang('global.report'),92
icon: 'message-report',93
handle: () => window.open('https://github.com/typable/verbal/issues')94
},95
{96
title: $lang('global.continue'),97
icon: 'arrow-right',98
handle: () => this.$refs.popup.close()99
}100
]101
});102
},103
updated() {104
this.$refs.popup.open({105
title: $lang('global.update'),106
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.',107
actions: [108
{109
title: $lang('global.continue'),110
icon: 'check',111
handle: () => window.location.reload()112
}113
]114
});115
},116
onScroll() {117
if(state.tab === 'search') {118
if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {119
if(this.$refs.search.searching) {120
this.$refs.search.doSearchMore();121
}122
}123
}124
},125
onTouchStart(event) {126
this.touch = event.changedTouches[0];127
this.target = event.target;128
},129
onTouchEnd(event) {130
const touch = event.changedTouches[0];131
const diff = {132
x: touch.pageX - this.touch.pageX,133
y: touch.pageY - this.touch.pageY134
};135
const angle = Math.atan(diff.x / diff.y) * (180 / Math.PI);136
const index = state.tabs.indexOf(state.tab);137
if(state.open) {138
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {139
if(diff.y > 0 && angle >= -30 && angle <= 30) {140
state.open = false;141
}142
}143
return;144
}145
if(Math.abs(diff.x) >= SWIPE_THRESHOLD) {146
if(diff.x > 0 && index > 0 && (angle <= -60 || angle >= 60)) {147
state.tab = state.tabs[index - 1];148
}149
if(diff.x < 0 && index < state.tabs.length - 1 && (angle <= -60 || angle >= 60)) {150
state.tab = state.tabs[index + 1];151
}152
}153
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {154
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {155
if(!state.open && this.target === this.$refs.player.$refs.button) {156
if(diff.y < 0 && angle >= -30 && angle <= 30) {157
state.open = true;158
}159
}160
}161
}162
}163
},164
computed: {165
styles() {166
const index = state.tabs.indexOf(state.tab);167
const offset = window.innerWidth < 1200168
? `${index * -100}vw`169
: `${index * -1200}px`;170
return {171
'transform': `translate(${offset}, 0)`172
};173
}174
},175
template: `176
<div177
@touchstart="onTouchStart"178
@touchend="onTouchEnd"179
class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh] max-h-[100vh] overflow-hidden"180
>181
<v-menu :state="state"></v-menu>182
<div class="flex flex-1 gap-[32px] sm:gap-[5rem] overflow-y-clip transition-transform md:transition-none" :style="styles">183
<v-search v-if="state.authenticated" ref="search" :state="state"></v-search>184
<v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>185
<v-account v-if="state.authenticated" :state="state"></v-account>186
</div>187
<v-player ref="player" v-if="state.authenticated" :station="state.station"></v-player>188
<v-popup ref="popup"></v-popup>189
</div>190
`191
});192
})();