-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
157 lines (132 loc) · 4.53 KB
/
app.js
File metadata and controls
157 lines (132 loc) · 4.53 KB
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
// ============================================================================
// FIREBASE CONFIGURATION
// ============================================================================
const FIREBASE_CONFIG = {
apiKey: "AIzaSyCKqmjZUw-oUw2rCNag8XVRQc2S-fPcz8w",
authDomain: "jammingjs-52efa.firebaseapp.com",
databaseURL: "https://jammingjs-52efa.firebaseio.com",
projectId: "jammingjs-52efa",
storageBucket: "jammingjs-52efa.appspot.com",
messagingSenderId: "690797988767"
};
// ============================================================================
// FIREBASE INITIALIZATION
// ============================================================================
function initializeFirebase() {
if (typeof firebase === "undefined") {
console.error("Firebase SDK not loaded");
return null;
}
try {
firebase.initializeApp(FIREBASE_CONFIG);
return firebase.database().ref().child("composition");
} catch (error) {
console.error("Error initializing Firebase:", error);
return null;
}
}
// ============================================================================
// SONG LIST MANAGER
// ============================================================================
class SongListManager {
constructor(compositionManager) {
this.compositionManager = compositionManager;
this.songListDiv = document.getElementById("songList");
this.songList = null;
this.dbRef = null;
this.initialize();
}
initialize() {
if (!this.songListDiv) {
console.error("Song list container not found");
return;
}
this.createSongList();
this.setupFirebaseListener();
}
createSongList() {
// Clear existing list
this.songListDiv.innerHTML = "<h2>Firebase songs</h2><br>";
// Create new list
this.songList = document.createElement("ul");
this.songList.style.listStyleType = "none";
this.songList.id = "songListItems";
this.songListDiv.appendChild(this.songList);
}
setupFirebaseListener() {
this.dbRef = initializeFirebase();
if (!this.dbRef) {
this.showError("Firebase not available");
return;
}
this.dbRef.on("value", (snapshot) => {
this.handleSnapshot(snapshot);
});
}
handleSnapshot(snapshot) {
// Clear existing songs
this.songList.innerHTML = "";
const numOfSongs = snapshot.numChildren();
if (numOfSongs === 0) {
this.showEmptyState();
return;
}
const songs = snapshot.val();
Object.entries(songs).forEach(([songId, songData]) => {
this.createSongButton(songId, songData.song);
});
}
createSongButton(songId, songData) {
const listItem = document.createElement("li");
const songButton = document.createElement("button");
songButton.className = "songs";
songButton.textContent = songId;
const lineBreak = document.createElement("br");
songButton.appendChild(lineBreak);
songButton.addEventListener("click", () => {
this.loadSong(songData);
});
listItem.appendChild(songButton);
this.songList.appendChild(listItem);
}
loadSong(songData) {
try {
const compositionData = JSON.parse(songData);
if (!Array.isArray(compositionData)) {
throw new Error("Invalid composition format");
}
// loadComposition automatically starts playback after loading
this.compositionManager.loadComposition(compositionData);
} catch (error) {
console.error("Error loading song:", error);
alert("Error loading song. Please check the data format.");
}
}
showEmptyState() {
const emptyMessage = document.createElement("li");
emptyMessage.textContent = "No songs available";
emptyMessage.style.fontStyle = "italic";
emptyMessage.style.color = "#999";
this.songList.appendChild(emptyMessage);
}
showError(message) {
const errorMessage = document.createElement("li");
errorMessage.textContent = message;
errorMessage.style.color = "#f00";
this.songList.appendChild(errorMessage);
}
}
// ============================================================================
// INITIALIZATION
// ============================================================================
// Wait for DOM and index.js to be loaded
document.addEventListener("DOMContentLoaded", () => {
// Wait a bit for index.js to initialize compositionManager
setTimeout(() => {
if (typeof window.compositionManager !== "undefined") {
new SongListManager(window.compositionManager);
} else {
console.error("CompositionManager not found. Make sure index.js is loaded first.");
}
}, 100);
});