A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 980f9734cbd13b48055a44ad69d48ba46d322fae (6608 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';9
import VDetail from './component/detail.js';11
export const VERSION = '{{version}}';12
export 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
VPopup,44
VDetail45
},46
mounted() {47
document.body.style.display = '';48
state.app = this;49
this.init();50
},51
methods: {52
async init() {53
try {54
state.account = await http`get::/api/account`();55
state.devices = await http`get::/api/devices`();56
state.token = localStorage.getItem(TOKEN_NAME);57
}58
catch(error) {59
// ignore60
}61
if(state.account === null) {62
this.$refs.popup.open({63
title: $lang('global.brand'),64
description: $lang('global.slogan') + $lang('global.upcoming')65
});66
return;67
}68
state.authenticated = true;69
document.addEventListener('scroll', this.onScroll);70
document.addEventListener('wheel', this.onScroll);71
if('serviceWorker' in navigator) {72
navigator.serviceWorker.register('/worker.js');73
navigator.serviceWorker.addEventListener('controllerchange', this.updated);74
}75
},76
showError(error) {77
this.$refs.popup.open({78
title: $lang('global.error'),79
description: $lang('global.error.message', error),80
actions: [81
{82
title: $lang('global.report'),83
icon: 'message-report',84
handle: () => window.open('https://github.com/typable/verbal/issues')85
},86
{87
title: $lang('global.continue'),88
icon: 'arrow-right',89
handle: () => this.$refs.popup.close()90
}91
]92
});93
},94
updated() {95
this.$refs.popup.open({96
title: $lang('global.update'),97
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.',98
actions: [99
{100
title: $lang('global.continue'),101
icon: 'check',102
handle: () => window.location.reload()103
}104
]105
});106
},107
onScroll() {108
if(state.tab === 'search') {109
if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {110
if(this.$refs.search.searching) {111
this.$refs.search.doSearchMore();112
}113
}114
}115
},116
onTouchStart(event) {117
this.touch = event.changedTouches[0];118
this.target = event.target;119
},120
onTouchEnd(event) {121
const touch = event.changedTouches[0];122
const diff = {123
x: touch.pageX - this.touch.pageX,124
y: touch.pageY - this.touch.pageY125
};126
const angle = Math.atan(diff.x / diff.y) * (180 / Math.PI);127
const index = state.tabs.indexOf(state.tab);128
if(state.open === true) {129
return;130
}131
if(Math.abs(diff.x) >= SWIPE_THRESHOLD) {132
if(diff.x > 0 && index > 0 && (angle <= -60 || angle >= 60)) {133
state.tab = state.tabs[index - 1];134
}135
if(diff.x < 0 && index < state.tabs.length - 1 && (angle <= -60 || angle >= 60)) {136
state.tab = state.tabs[index + 1];137
}138
}139
}140
},141
computed: {142
styles() {143
const index = state.tabs.indexOf(state.tab);144
const offset = window.innerWidth < 1200145
? `${index * -100}vw`146
: `${index * -1200}px`;147
return {148
'transform': `translate(${offset}, 0)`149
};150
}151
},152
template: `153
<div154
@touchstart="onTouchStart"155
@touchend="onTouchEnd"156
class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh] max-h-[100vh] overflow-hidden"157
>158
<v-menu :state="state"></v-menu>159
<div class="flex flex-1 gap-[32px] sm:gap-[5rem] overflow-y-clip transition-transform md:transition-none" :style="styles">160
<v-search v-if="state.authenticated" ref="search" :state="state"></v-search>161
<v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>162
<v-account v-if="state.authenticated" :state="state"></v-account>163
</div>164
<v-player ref="player" v-if="state.authenticated" :station="state.station"></v-player>165
<v-detail ref="detail" v-if="state.authenticated" :station="state.station"></v-detail>166
<v-popup ref="popup"></v-popup>167
</div>168
`169
});170
})();