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