A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 8de3accabd3f742c53fdfe24af45cc31155dff3a (7082 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.brand'),63
description: $lang('global.slogan') + $lang('global.upcoming')64
});65
return;66
}67
state.authenticated = true;68
document.addEventListener('scroll', this.onScroll);69
document.addEventListener('wheel', this.onScroll);70
if('serviceWorker' in navigator) {71
navigator.serviceWorker.register('/worker.js');72
navigator.serviceWorker.addEventListener('controllerchange', this.updated);73
}74
},75
showError(error) {76
this.$refs.popup.open({77
title: $lang('global.error'),78
description: $lang('global.error.message', error),79
actions: [80
{81
title: $lang('global.report'),82
icon: 'message-report',83
handle: () => window.open('https://github.com/typable/verbal/issues')84
},85
{86
title: $lang('global.continue'),87
icon: 'arrow-right',88
handle: () => this.$refs.popup.close()89
}90
]91
});92
},93
updated() {94
this.$refs.popup.open({95
title: $lang('global.update'),96
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.',97
actions: [98
{99
title: $lang('global.continue'),100
icon: 'check',101
handle: () => window.location.reload()102
}103
]104
});105
},106
onScroll() {107
if(state.tab === 'search') {108
if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {109
if(this.$refs.search.searching) {110
this.$refs.search.doSearchMore();111
}112
}113
}114
},115
onTouchStart(event) {116
this.touch = event.changedTouches[0];117
this.target = event.target;118
},119
onTouchEnd(event) {120
const touch = event.changedTouches[0];121
const diff = {122
x: touch.pageX - this.touch.pageX,123
y: touch.pageY - this.touch.pageY124
};125
const angle = Math.atan(diff.x / diff.y) * (180 / Math.PI);126
const index = state.tabs.indexOf(state.tab);127
if(state.open) {128
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {129
if(diff.y > 0 && angle >= -30 && angle <= 30) {130
state.open = false;131
}132
}133
return;134
}135
if(Math.abs(diff.x) >= SWIPE_THRESHOLD) {136
if(diff.x > 0 && index > 0 && (angle <= -60 || angle >= 60)) {137
state.tab = state.tabs[index - 1];138
}139
if(diff.x < 0 && index < state.tabs.length - 1 && (angle <= -60 || angle >= 60)) {140
state.tab = state.tabs[index + 1];141
}142
}143
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {144
if(Math.abs(diff.y) >= SWIPE_THRESHOLD) {145
if(!state.open && this.target === this.$refs.player.$refs.button) {146
if(diff.y < 0 && angle >= -30 && angle <= 30) {147
state.open = true;148
}149
}150
}151
}152
}153
},154
computed: {155
styles() {156
const index = state.tabs.indexOf(state.tab);157
const offset = window.innerWidth < 1200158
? `${index * -100}vw`159
: `${index * -1200}px`;160
return {161
'transform': `translate(${offset}, 0)`162
};163
}164
},165
template: `166
<div167
@touchstart="onTouchStart"168
@touchend="onTouchEnd"169
class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh] max-h-[100vh] overflow-hidden"170
>171
<v-menu :state="state"></v-menu>172
<div class="flex flex-1 gap-[32px] sm:gap-[5rem] overflow-y-clip transition-transform md:transition-none" :style="styles">173
<v-search v-if="state.authenticated" ref="search" :state="state"></v-search>174
<v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>175
<v-account v-if="state.authenticated" :state="state"></v-account>176
</div>177
<v-player ref="player" v-if="state.authenticated" :station="state.station"></v-player>178
<v-popup ref="popup"></v-popup>179
</div>180
`181
});182
})();