This repository has been archived by the owner on Oct 9, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
app.vue
219 lines (185 loc) · 5.97 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<template>
<div id="app" :class="{ desktop: isDesktopApp }">
<div id="main" v-if="authenticated">
<hotkeys/>
<event-listeners/>
<app-header/>
<main-wrapper/>
<app-footer/>
<support-koel/>
<overlay ref="overlay"/>
</div>
<template v-else>
<div class="login-wrapper" @dblclick.self="triggerMaximize">
<login-form @loggedin="onUserLoggedIn"/>
</div>
</template>
<song-context-menu :songs="contextMenuSongs" ref="songContextMenu"/>
<album-context-menu :album="contextMenuAlbum" ref="albumContextMenu"/>
<artist-context-menu :artist="contextMenuArtist" ref="artistContextMenu"/>
</div>
</template>
<script lang="ts">
import Vue from 'vue'
import AppHeader from '@/components/layout/app-header.vue'
import AppFooter from '@/components/layout/app-footer/index.vue'
import EventListeners from '@/components/utils/event-listeners.vue'
import Hotkeys from '@/components/utils/hotkeys.vue'
import LoginForm from '@/components/auth/login-form.vue'
import MainWrapper from '@/components/layout/main-wrapper/index.vue'
import Overlay from '@/components/ui/overlay.vue'
import { eventBus, showOverlay, hideOverlay, $, app as appUtils } from '@/utils'
import { events } from '@/config'
import { sharedStore, favoriteStore, queueStore, preferenceStore as preferences } from '@/stores'
import { playback, socket, auth } from '@/services'
import { clickaway, droppable, focus } from '@/directives'
import { BaseContextMenu } from 'koel/types/ui'
export default Vue.extend({
components: {
Hotkeys,
AppHeader,
AppFooter,
MainWrapper,
Overlay,
LoginForm,
EventListeners,
SongContextMenu: () => import('@/components/song/context-menu.vue'),
AlbumContextMenu: () => import('@/components/album/context-menu.vue'),
ArtistContextMenu: () => import('@/components/artist/context-menu.vue'),
SupportKoel: () => import('@/components/meta/support-koel.vue')
},
data: () => ({
authenticated: false,
isDesktopApp: KOEL_ENV === 'app',
contextMenuSongs: [] as Song[],
contextMenuAlbum: null as unknown as Album,
contextMenuArtist: null as unknown as Artist
}),
async mounted (): Promise<void> {
// The app has just been initialized, check if we can get the user data with an already existing token
if (auth.hasToken()) {
this.authenticated = true
await this.init()
}
// Add an ugly mac/non-mac class for OS-targeting styles.
// I'm crying inside.
$.addClass(document.documentElement, navigator.userAgent.includes('Mac') ? 'mac' : 'non-mac')
},
created () {
eventBus.on(events.SONG_CONTEXT_MENU_REQUESTED, async (e: MouseEvent, songs: Song[]) => {
this.contextMenuSongs = ([] as Song[]).concat(songs)
await this.$nextTick()
;(this.$refs.songContextMenu as BaseContextMenu).open(e.pageY, e.pageX)
})
eventBus.on(events.ALBUM_CONTEXT_MENU_REQUESTED, async (e: MouseEvent, album: Album) => {
this.contextMenuAlbum = album
await this.$nextTick()
;(this.$refs.albumContextMenu as BaseContextMenu).open(e.pageY, e.pageX)
})
eventBus.on(events.ARTIST_CONTEXT_MENU_REQUESTED, async (e: MouseEvent, artist: Artist) => {
this.contextMenuArtist = artist
await this.$nextTick()
;(this.$refs.artistContextMenu as BaseContextMenu).open(e.pageY, e.pageX)
})
},
methods: {
async init (): Promise<void> {
showOverlay()
await socket.init()
// Make the most important HTTP request to get all necessary data from the server.
// Afterwards, init all mandatory stores and services.
try {
await sharedStore.init()
window.setTimeout(() => {
playback.init()
hideOverlay()
this.requestNotificationPermission()
window.addEventListener('beforeunload', (e: BeforeUnloadEvent): void => {
if (!preferences.confirmClosing) {
return
}
e.preventDefault()
e.returnValue = ''
})
this.subscribeToBroadcastEvents()
// Let all other components know we're ready.
eventBus.emit(events.KOEL_READY)
}, 100)
} catch (err) {
this.authenticated = false
throw err
}
},
/**
* Request for notification permission if it's not provided and the user is OK with notifications.
*/
requestNotificationPermission (): void {
if (window.Notification && preferences.notify && window.Notification.permission !== 'granted') {
window.Notification.requestPermission().then((result: string): void => {
preferences.notify = result === 'denied'
})
}
},
onUserLoggedIn (): void {
this.authenticated = true
this.init()
},
subscribeToBroadcastEvents (): void {
socket.listen(events.SOCKET_TOGGLE_FAVORITE, (): void => {
if (queueStore.current) {
favoriteStore.toggleOne(queueStore.current)
}
})
},
triggerMaximize: (): void => appUtils.triggerMaximize()
}
})
// …and the global directives
Vue.directive('koel-focus', focus)
Vue.directive('koel-clickaway', clickaway)
Vue.directive('koel-droppable', droppable)
</script>
<style lang="scss">
@import "~#/app.scss";
#dragGhost {
display: inline-block;
background: var(--color-green);
padding: .8rem;
border-radius: .2rem;
color: var(--color-text-primary);
font-family: var(--font-family);
font-size: 1rem;
font-weight: var(--font-weight-light);
position: fixed;
top: 0;
left: 0;
z-index: -1;
@media (hover: none) {
display: none;
}
}
#copyArea {
position: absolute;
left: -9999px;
width: 1px;
height: 1px;
bottom: 1px;
@media (hover: none) {
display: none;
}
}
#main, .login-wrapper {
display: flex;
height: 100vh;
flex-direction: column;
}
.login-wrapper {
@include vertical-center();
-webkit-app-region: drag;
user-select: none;
input, button {
-webkit-app-region: no-drag;
}
padding-bottom: 0;
}
</style>