so://phie

verbal

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

blob: a4d4506db21e5b63342e3482a709d8580988d003 (6902 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 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 VAuth,

43 VPlayer,

44 VPopup,

45 VDetail

46 },

47 mounted() {

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

49 state.app = this;

50 this.init();

51 },

52 methods: {

53 async init() {

54 try {

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

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

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

58 }

59 catch(err) {

60 state.account = null;

61 state.devices = null;

62 state.token = null;

63 }

64 state.tab = 'search';

65 state.station = null;

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

67 state.authenticated = false;

68 return;

69 }

70 state.authenticated = true;

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

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

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

74 if('serviceWorker' in navigator) {

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

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

77 }

78 },

79 showError(error) {

80 this.$refs.popup.open({

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

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

83 actions: [

84 {

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

86 icon: 'message-report',

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

88 },

89 {

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

91 icon: 'arrow-right',

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

93 }

94 ]

95 });

96 },

97 updated() {

98 this.$refs.popup.open({

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

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

101 actions: [

102 {

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

104 icon: 'check',

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

106 }

107 ]

108 });

109 },

110 onScroll() {

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

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

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

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

115 }

116 }

117 }

118 },

119 onTouchStart(event) {

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

121 this.target = event.target;

122 },

123 onTouchEnd(event) {

124 const touch = event.changedTouches[0];

125 const diff = {

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

127 y: touch.pageY - this.touch.pageY

128 };

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

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

131 if(this.$refs.player.open === true || this.$refs.detail.open === true) {

132 return;

133 }

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

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

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

137 }

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

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

140 }

141 }

142 }

143 },

144 computed: {

145 styles() {

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

147 const offset = window.innerWidth < 1200

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

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

150 return {

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

152 };

153 }

154 },

155 template: `

156 <div

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

158 @touchstart="onTouchStart"

159 @touchend="onTouchEnd"

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

161 >

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

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

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

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

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

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

168 </div>

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

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

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

172 </div>

173 `

174 });

175})();