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