so://phie

verbal

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

blob: a6bd3ef1263787a446620a9648c86e8ea0527465 (6439 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';

9

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

11export const SWIPE_THRESHOLD = 70;

12export const TOKEN_NAME = 'verbal-token';

13

14export const state = {

15 app: null,

16 tab: 'search',

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

18 account: null,

19 devices: null,

20 token: null,

21 station: null,

22 authenticated: false,

23 open: false,

24 version: VERSION

25};

26

27(async () => {

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

29 new Vue({

30 el: '#app',

31 data: {

32 state,

33 touch: null,

34 target: null

35 },

36 components: {

37 VMenu,

38 VSearch,

39 VFavorites,

40 VAccount,

41 VPlayer,

42 VPopup

43 },

44 mounted() {

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

46 state.app = this;

47 this.init();

48 },

49 methods: {

50 async init() {

51 try {

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

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

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

55 }

56 catch(error) {

57 // ignore

58 }

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

60 this.$refs.popup.open({

61 title: $lang('global.brand'),

62 description: $lang('global.slogan') + $lang('global.upcoming')

63 });

64 return;

65 }

66 state.authenticated = true;

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

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

69 if('serviceWorker' in navigator) {

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

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

72 }

73 },

74 showError(error) {

75 this.$refs.popup.open({

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

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

78 actions: [

79 {

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

81 icon: 'message-report',

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

83 },

84 {

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

86 icon: 'arrow-right',

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

88 }

89 ]

90 });

91 },

92 updated() {

93 this.$refs.popup.open({

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

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

96 actions: [

97 {

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

99 icon: 'check',

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

101 }

102 ]

103 });

104 },

105 onScroll() {

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

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

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

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

110 }

111 }

112 }

113 },

114 onTouchStart(event) {

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

116 this.target = event.target;

117 },

118 onTouchEnd(event) {

119 const touch = event.changedTouches[0];

120 const diff = {

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

122 y: touch.pageY - this.touch.pageY

123 };

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

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

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

127 return;

128 }

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

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

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

132 }

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

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

135 }

136 }

137 }

138 },

139 computed: {

140 styles() {

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

142 const offset = window.innerWidth < 1200

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

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

145 return {

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

147 };

148 }

149 },

150 template: `

151 <div

152 @touchstart="onTouchStart"

153 @touchend="onTouchEnd"

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

155 >

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

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

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

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

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

161 </div>

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

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

164 </div>

165 `

166 });

167})();