so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 95bebe186c3a0a5efbc488cde950fb2cfd734907 (6654 bytes)[doc][blame][raw]

1import Vue from '/asset/lib/vue.min.js';

2import {http, $lang} from './utils.js';

3import VMenu from './component/menu.js';

4import VPlayer from './component/player.js';

5import VPopup from './component/popup.js';

6import VSearch from './page/search.js';

7import VFavorites from './page/favorites.js';

8import VAccount from './page/account.js';

9import VDetail from './component/detail.js';

10

11export const VERSION = '{{version}}';

12export const SWIPE_THRESHOLD = 70;

13export const TOKEN_NAME = 'verbal-token';

14

15export 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: VERSION

26};

27

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: null

36 },

37 components: {

38 VMenu,

39 VSearch,

40 VFavorites,

41 VAccount,

42 VPlayer,

43 VPopup,

44 VDetail

45 },

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 // ignore

60 }

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 document.addEventListener('touchmove', this.onScroll);

72 if('serviceWorker' in navigator) {

73 navigator.serviceWorker.register('/worker.js');

74 navigator.serviceWorker.addEventListener('controllerchange', this.updated);

75 }

76 },

77 showError(error) {

78 this.$refs.popup.open({

79 title: $lang('global.error'),

80 description: $lang('global.error.message', error),

81 actions: [

82 {

83 title: $lang('global.report'),

84 icon: 'message-report',

85 handle: () => window.open('https://github.com/typable/verbal/issues')

86 },

87 {

88 title: $lang('global.continue'),

89 icon: 'arrow-right',

90 handle: () => this.$refs.popup.close()

91 }

92 ]

93 });

94 },

95 updated() {

96 this.$refs.popup.open({

97 title: $lang('global.update'),

98 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.',

99 actions: [

100 {

101 title: $lang('global.continue'),

102 icon: 'check',

103 handle: () => window.location.reload()

104 }

105 ]

106 });

107 },

108 onScroll() {

109 if(state.tab === 'search') {

110 if(window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 200) {

111 if(this.$refs.search.searching) {

112 this.$refs.search.doSearchMore();

113 }

114 }

115 }

116 },

117 onTouchStart(event) {

118 this.touch = event.changedTouches[0];

119 this.target = event.target;

120 },

121 onTouchEnd(event) {

122 const touch = event.changedTouches[0];

123 const diff = {

124 x: touch.pageX - this.touch.pageX,

125 y: touch.pageY - this.touch.pageY

126 };

127 const angle = Math.atan(diff.x / diff.y) * (180 / Math.PI);

128 const index = state.tabs.indexOf(state.tab);

129 if(state.open === true) {

130 return;

131 }

132 if(Math.abs(diff.x) >= SWIPE_THRESHOLD) {

133 if(diff.x > 0 && index > 0 && (angle <= -60 || angle >= 60)) {

134 state.tab = state.tabs[index - 1];

135 }

136 if(diff.x < 0 && index < state.tabs.length - 1 && (angle <= -60 || angle >= 60)) {

137 state.tab = state.tabs[index + 1];

138 }

139 }

140 }

141 },

142 computed: {

143 styles() {

144 const index = state.tabs.indexOf(state.tab);

145 const offset = window.innerWidth < 1200

146 ? `${index * -100}vw`

147 : `${index * -1200}px`;

148 return {

149 'transform': `translate(${offset}, 0)`

150 };

151 }

152 },

153 template: `

154 <div

155 @touchstart="onTouchStart"

156 @touchend="onTouchEnd"

157 class="px-4 sm:px-10 max-w-[1200px] mx-auto flex flex-col min-h-[100vh] max-h-[100vh] overflow-hidden"

158 >

159 <v-menu :state="state"></v-menu>

160 <div class="flex flex-1 gap-[32px] sm:gap-[5rem] overflow-y-clip transition-transform md:transition-none" :style="styles">

161 <v-search v-if="state.authenticated" ref="search" :state="state"></v-search>

162 <v-favorites v-if="state.authenticated" ref="favorites" :state="state"></v-favorites>

163 <v-account v-if="state.authenticated" :state="state"></v-account>

164 </div>

165 <v-player ref="player" v-if="state.authenticated" :station="state.station"></v-player>

166 <v-detail ref="detail" v-if="state.authenticated"></v-detail>

167 <v-popup ref="popup"></v-popup>

168 </div>

169 `

170 });

171})();