-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
163 lines (144 loc) · 4.5 KB
/
loader.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
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
var items;
getJSON('examples/examples.json',
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
items = document.getElementById("myDropdown")
items.innerHTML = "";
examples = data['examples'] || [];
var entry;
var title;
examples.forEach(function(ex) {
entry = document.createElement("a", "href=");
title = document.createTextNode(ex['name']);
entry.appendChild(title);
entry.onclick = function() {
// alert(JSON.stringify(ex));
// items.parentElement.innerHTML = "";
document.getElementById("dropdown").innerHTML =ex['name'];
$a.setAttribute("autoplay","false");
// window.location.hash=ex['name'];
history.pushState("", document.title, window.location.pathname);
loadex(ex);
};
items.appendChild(entry);
});
// entry = document.createElement("a", "href=");
// title = document.createTextNode("install SW");
// entry.appendChild(title);
// entry.onclick = function() { registerSW();};
// items.appendChild(entry);
entry = document.createElement("a", "href=");
title = document.createTextNode("clear serviceWorker");
entry.appendChild(title);
entry.onclick = function() { removeSW();alert("Service Worker has been reset!");location.reload(true);
};
items.appendChild(entry);
// entry = document.createElement("a", "href=");
// title = document.createTextNode("Reset serviceWorker");
// entry.appendChild(title);
// entry.onclick = function() { removeSW();registerSW();alert("Service Worker has been reset!");location.reload(true); };
// items.appendChild(entry);
if(window.location.hash==="#hogs") {
loadex(data['examples'][5]);
$a.play();
// Fragment exists
// alert("hi"+window.location.hash);
} else {
loadex(data['examples'][0]);
// alert("bye");
}
// loadex(data['examples'][0]);
// loadex(data['examples'][1]);
}
});
var $a = document.getElementById("audio");
var bpm;
var offset;
var audio_duration;
var beatlen;
var measure;
var measures;
var lines;
function doMath() {
beatlen = 1.0 * bpm / (60.0);
measure = beatlen * 4.0 / 3.0;
measures = audio_duration / measure;
lines = Math.floor((audio_duration + offset) / measure);
if(offset<0.0){lines++;;}
}
function loadex(ex) {
var curdir;
var drawspec = false;
getJSON(ex['loc'] + ex['align'],
function(err, data) {
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
curdir = ex['loc'];
bpm = ex['bpm'];
offset = ex['offset'];
audio_duration = ex['audio_duration'];
drawspec = ex['images'];
doMath();
// console.log(lines);
render(data, ex);
}
});
$a.src = ex['loc'] + ex['sound'];
if(window.location.hash==="#hogs") {
$a.setAttribute("autoplay","true");}
$a.load();
// if(window.location.hash==="hogs") {
// $a.play();
// }
}
registerSW();
function registerSW(){
if('serviceWorker' in navigator) {
navigator.serviceWorker
.register('sw.js')
.then(function() { console.log('Service Worker Registered'); });
}
}
function removeSW(){
navigator.serviceWorker.getRegistrations().then(function(registrations) {
for (let registration of registrations) {
registration.unregister()
}
});
console.log("removed");
}