so://phie

verbal

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

blob: f81c2c7b282b322b745ecf8d08242e7f56b17b44 (6908 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 VAuth from './page/auth.js';

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

11

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

13export const SWIPE_THRESHOLD = 70;

14export const TOKEN_NAME = 'verbal-token';

15

16export const state = {

17 app: null,

18 tab: 'search',

19 tabs: ['search', 'favorites', 'account'],

20 account: null,

21 devices: null,

22 token: null,

23 station: null,

24 authenticated: null,

25 open: false,

26 version: VERSION

27};

28

29(async () => {

30 state.locale = await http`get::/asset/json/locale.json`();

31 new Vue({

32 el: '#app',

33 data: {

34 state,

35 touch: null,

36 target: null

37 },

38 components: {

39 VMenu,

40 VSearch,

41 VFavorites,

42 VAccount,

43 VAuth,

44 VPlayer,

45 VPopup,

46 VDetail

47 },

48 mounted() {

49 document.body.style.display = '';

50 state.app = this;

51 this.init();

52 },

53 methods: {

54 async init() {

55 try {

56 state.account = await http`get::/api/account`();

57 state.devices = await http`get::/api/devices`();

58 state.token = localStorage.getItem(TOKEN_NAME);

59 }

60 catch(err) {

61 state.account = null;

62 state.devices = null;

63 state.token = null;

64 }

65 state.tab = 'search';

66 state.open = false;

67 state.station = null;

68 if(state.account === null) {

69 state.authenticated = false;

70 return;

71 }

72 state.authenticated = true;

73 document.addEventListener('scroll', this.onScroll);

74 document.addEventListener('wheel', this.onScroll);

75 document.addEventListener('touchmove', this.onScroll);

76 if('serviceWorker' in navigator) {

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

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

79 }

80 },

81 showError(error) {

82 this.$refs.popup.open({

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

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

85 actions: [

86 {

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

88 icon: 'message-report',

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

90 },

91 {

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

93 icon: 'arrow-right',

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

95 }

96 ]

97 });

98 },

99 updated() {

100 this.$refs.popup.open({

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

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

103 actions: [

104 {

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

106 icon: 'check',

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

108 }

109 ]

110 });

111 },

112 onScroll() {

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

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

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

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

117 }

118 }

119 }

120 },

121 onTouchStart(event) {

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

123 this.target = event.target;

124 },

125 onTouchEnd(event) {

126 const touch = event.changedTouches[0];

127 const diff = {

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

129 y: touch.pageY - this.touch.pageY

130 };

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

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

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

134 return;

135 }

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

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

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

139 }

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

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

142 }

143 }

144 }

145 },

146 computed: {

147 styles() {

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

149 const offset = window.innerWidth < 1200

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

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

152 return {

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

154 };

155 }

156 },

157 template: `

158 <div

159 v-if="state.authenticated !== null"

160 @touchstart="onTouchStart"

161 @touchend="onTouchEnd"

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

163 >

164 <v-auth v-if="!state.authenticated" :state="state"></v-auth>

165 <v-menu v-if="state.authenticated" :state="state"></v-menu>

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

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

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

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

170 </div>

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

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

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

174 </div>

175 `

176 });

177})();