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