-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
180 lines (149 loc) · 4.98 KB
/
app.js
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
/*
STATE
*/
import {Spotify} from './spotify.js';
import {UI} from './ui.js';
export const CUMULIO_PLAYLIST = '0GIFfPsuHdZUQGrGvKiXSm';
const spotify = new Spotify();
export const ui = new UI(spotify);
/*********** ADD DASHBOARD OPTIONS ************/
const dashboards = {
playlist: 'f3555bce-a874-4924-8d08-136169855807',
songInfo: 'e92c869c-2a94-406f-b18f-d691fd627d34',
};
const dashboardOptions = {
dashboardId: dashboards.playlist,
container: '#dashboard-container',
loader: {
background: '#111b31',
spinnerColor: '#f44069',
spinnerBackground: '#0d1425',
fontColor: '#ffffff'
}
};
/**********************************************/
/*
START
*/
window.onload = async () => {
spotify.spotifyParams = getHashParams();
if (!spotify.spotifyParams.access_token) return ui.setLoginStatus(false);
spotify.makeSpotifyRequest('https://api.spotify.com/v1/me', 'get')
.then(response => {
if (response.error) ui.setLoginStatus(false);
else ui.setLoginStatus(true, response);
});
openPageCumulioFavorites();
};
export const closeSongInfoModal = () => ui.closeSongInfoModal();
export const resetModalWidth = () => ui.resetModalWidth();
export const openPageCumulioPlaylist = async () => {
ui.openPage('Cumul.io playlist', 'cumulio-playlist');
const playlistEl = await ui.generatePlaylistSongList({id: CUMULIO_PLAYLIST, name: 'Cumul.io Playlist'});
const container = document.getElementById('playlists-list');
container.innerHTML = '';
container.append(playlistEl);
};
export const openPageMyPlaylists = async () => {
if (!spotify.user.loggedIn) return window.location.href = '/login';
ui.openPage('My Playlists', 'my-playlists');
const playlists = await spotify.getPlaylists();
const playlistsEl = document.getElementById('playlists-list');
playlistsEl.innerHTML = '';
const container = ui.generatePlaylistCards(playlists, window.openPagePlaylist);
playlistsEl.append(container);
};
export const openPagePlaylist = async (playlist) => {
ui.openPage(playlist.name || 'Playlist', 'my-playlists');
const playlistEl = await ui.generatePlaylistSongList(playlist);
const container = document.getElementById('playlists-list');
container.innerHTML = '';
container.append(playlistEl);
};
export const openPageCumulioFavorites = async () => {
ui.openPage('Cumul.io playlist visualized', 'cumulio-playlist-viz');
/**************** INTEGRATE DASHBOARD ****************/
listenToEvents();
loadDashboard(dashboards.playlist);
};
const loadDashboard = (id, container, key, token) => {
dashboardOptions.dashboardId = id;
dashboardOptions.container = container || '#dashboard-container';
if (key && token) {
dashboardOptions.key = key;
dashboardOptions.token = token;
}
Cumulio.addDashboard(dashboardOptions);
};
/*
CUMUL.IO FUNCTIONS
*/
/*********** LISTEN TO CUSTOM EVENTS AND ADD EXTRAS ************/
const getSong = (event) => {
let songName;
let songArtist;
let songId;
if (event.data.columns === undefined) {
songName = event.data.name.id.split('&id=')[0];
songId = event.data.name.id.split('&id=')[1];
}
else {
songName = event.data.columns[0].value;
songArtist = event.data.columns[1].value;
songId = event.data.columns[event.data.columns.length - 1].value;
}
return {id: songId, name: songName, artist: songArtist};
};
const listenToEvents = () => {
Cumulio.onCustomEvent(async (event) => {
const song = getSong(event);
if (event.data.event === 'add_to_playlist'){
await ui.addToPlaylistSelector(song.name, song.id);
}
else if (event.data.event === 'song_info') {
const token = await getDashboardAuthorizationToken({ songId: [song.id] });
loadDashboard(dashboards.songInfo, '#song-info-dashboard', token.id, token.token);
await ui.displaySongInfo(song);
}
});
};
const getDashboardAuthorizationToken = async (metadata) => {
try {
const body = {};
if (metadata && typeof metadata === 'object') {
Object.keys(metadata).forEach(key => {
body[key] = metadata[key];
});
}
/*
Make the call to the backend API, using the platform user access credentials in the header
to retrieve a dashboard authorization token for this user
*/
const response = await fetch('/authorization', {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' }
});
// Fetch the JSON result with the Cumul.io Authorization key & token
const responseData = await response.json();
return responseData;
}
catch (e) {
return { error: 'Could not retrieve dashboard authorization token.' };
}
};
/**************************************************************/
/*
HELPER FUNCTIONS
*/
function getHashParams() {
const hashParams = {};
let e;
const r = /([^&;=]+)=?([^&;]*)/g;
const q = window.location.hash.substring(1);
// eslint-disable-next-line no-cond-assign
while (e = r.exec(q)) {
hashParams[e[1]] = decodeURIComponent(e[2]);
}
return hashParams;
}