annotate light9/ascoltami/main.ts @ 2114:758ce4dfbd2f

asco layout
author drewp@bigasterisk.com
date Thu, 02 Jun 2022 19:21:46 +0000
parents 16c7dd543250
children c4427fd59306
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
1 function byId(id: string): HTMLElement {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
2 return document.getElementById(id)!;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
3 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
4
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
5 async function onLoad() {
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
6 const config = await (await fetch("api/config")).json();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
7 const times = config.times;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
8 document.title = document.title.replace("{{host}}", config.host);
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
9 const h1 = document.querySelector("h1")!;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
10 h1.innerText = h1.innerText.replace("{{host}}", config.host);
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
11
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
12 byId("nav").innerText = navigator.userAgent;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
13 var updateFreq = navigator.userAgent.indexOf("Linux") != -1 ? 10 : 2;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
14 if (navigator.userAgent.match(/Windows NT/)) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
15 // helper laptop
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
16 updateFreq = 10;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
17 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
18 byId("updateReq").innerText = "" + updateFreq;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
19
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
20 let currentDuration = 0;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
21 let currentHighlightedSong = "";
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
22 let lastPlaying = false;
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
23
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
24 async function updateCurrent() {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
25 const data = await (await fetch("api/time")).json();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
26 byId("currentSong").innerText = data.song;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
27 if (data.song != currentHighlightedSong) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
28 showCurrentSong(data.song);
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
29 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
30 byId("currentTime").innerText = data.t.toFixed(1);
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
31 byId("leftTime").innerText = (data.duration - data.t).toFixed(1);
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
32 byId("leftAutoStopTime").innerText = Math.max(
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
33 0,
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
34 data.duration - times.post - data.t
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
35 ).toFixed(1);
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
36 byId("states").innerText = JSON.stringify(data.state);
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
37 currentDuration = data.duration;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
38 // document.querySelector("#timeSlider").slider({ value: data.t, max: data.duration });
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
39 if (data.playing != lastPlaying) {
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
40 document
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
41 .querySelectorAll(".playMode")
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
42 .forEach((e: Element) => e.classList.remove("active"));
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
43 byId(data.playing ? "cmd-play" : "cmd-stop").classList.add("active");
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
44 lastPlaying = data.playing;
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
45 }
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
46 byId("next").innerText = data.next;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
47 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
48
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
49 function showCurrentSong(uri: string) {
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
50 document
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
51 .querySelectorAll(".songs div")
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
52 .forEach((row: Element, i: number) => {
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
53 if (row.querySelector("button")!.dataset.uri == uri) {
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
54 row.classList.add("currentSong");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
55 } else {
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
56 row.classList.remove("currentSong");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
57 }
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
58 });
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
59 currentHighlightedSong = uri;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
60 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
61
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
62 const data = await (await fetch("api/songs")).json();
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
63 data.songs.forEach((song) => {
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
64 const button = document.createElement("button");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
65 // link is just for dragging, not clicking
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
66 const link = document.createElement("a");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
67 const n = document.createElement("span");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
68 n.classList.add("num");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
69 n.innerText = song.label.slice(0, 2);
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
70 link.appendChild(n);
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
71
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
72 const sn = document.createElement("span");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
73 sn.classList.add("song-name");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
74 sn.innerText = song.label.slice(2).trim();
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
75 link.appendChild(sn);
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
76 link.setAttribute("href", song.uri);
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
77 link.addEventListener("click", (ev) => {
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
78 ev.stopPropagation();
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
79 button.click();
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
80 });
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
81 button.appendChild(link);
2114
758ce4dfbd2f asco layout
drewp@bigasterisk.com
parents: 2058
diff changeset
82 button.dataset.uri = song.uri;
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
83 button.addEventListener("click", async (ev) => {
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
84 await fetch("api/song", { method: "POST", body: song.uri });
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
85 showCurrentSong(song.uri);
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
86 });
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
87 const dv = document.createElement("div");
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
88 dv.appendChild(button);
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
89 document.querySelector(".songs").appendChild(dv);
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
90 });
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
91
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
92 document.addEventListener("keypress", (ev) => {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
93 if (ev.which == 115) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
94 byId("cmd-stop").click();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
95 return false;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
96 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
97 if (ev.which == 112) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
98 byId("cmd-play").click();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
99 return false;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
100 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
101 if (ev.which == 105) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
102 byId("cmd-intro").click();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
103 return false;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
104 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
105 if (ev.which == 116) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
106 byId("cmd-post").click();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
107 return false;
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
108 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
109
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
110 if (ev.key == "g") {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
111 byId("cmd-go").click();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
112 return false;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
113 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
114 return true;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
115 });
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
116
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
117 async function postJson(url: string, jsBody: Object) {
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
118 return fetch(url, {
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
119 method: "POST",
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
120 headers: { "Content-Type": "applcation/json" },
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
121 body: JSON.stringify(jsBody),
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
122 });
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
123 }
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
124
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
125 byId("cmd-stop").addEventListener("click", (ev: Event) =>
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
126 postJson("api/time", { pause: true })
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
127 );
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
128 byId("cmd-play").addEventListener("click", (ev: Event) =>
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
129 postJson("api/time", { resume: true })
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
130 );
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
131 byId("cmd-intro").addEventListener("click", (ev: Event) =>
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
132 postJson("api/time", { t: times.intro, resume: true })
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
133 );
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
134 byId("cmd-post").addEventListener("click", (ev: Event) =>
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
135 postJson("api/time", { t: currentDuration - times.post, resume: true })
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
136 );
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
137 byId("cmd-go").addEventListener("click", (ev: Event) =>
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
138 postJson("api/go", {})
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
139 );
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
140
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
141 // var pendingSlide = false;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
142 // $("#timeSlider").slider({
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
143 // step: 0.01,
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
144 // slide: function (event, ui) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
145 // if (pendingSlide) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
146 // return;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
147 // }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
148 // pendingSlide = true;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
149 // $.post("time", '{"t" : ' + ui.value + "}", function (data, status, xhr) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
150 // pendingSlide = false;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
151 // });
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
152 // },
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
153 // });
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
154
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
155 let recentUpdates: Array<number> = [];
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
156 function onUpdate() {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
157 recentUpdates.push(+new Date());
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
158 recentUpdates = recentUpdates.slice(Math.max(recentUpdates.length - 5, 0));
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
159 refreshUpdateFreqs();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
160 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
161
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
162 function refreshUpdateFreqs() {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
163 if (recentUpdates.length > 1) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
164 if (+new Date() - recentUpdates[recentUpdates.length - 1] > 1000) {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
165 byId("updateActual").innerText = "(stalled)";
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
166 document
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
167 .querySelectorAll(".dimStalled")
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
168 .forEach((el) => el.classList.add("stalled"));
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
169 return;
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
170 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
171
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
172 var avgMs =
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
173 (recentUpdates[recentUpdates.length - 1] - recentUpdates[0]) /
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
174 (recentUpdates.length - 1);
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
175 byId("updateActual").innerText = "" + Math.round(1000 / avgMs);
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
176 }
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
177 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
178 setInterval(refreshUpdateFreqs, 2000);
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
179
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
180 async function updateLoop() {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
181 var whenDone = function () {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
182 setTimeout(function () {
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
183 requestAnimationFrame(updateLoop);
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
184 }, 1000 / updateFreq);
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
185 };
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
186 onUpdate();
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
187 await updateCurrent();
2058
16c7dd543250 asco web: fix more porting bugs. UI might work (except for time slider)
drewp@bigasterisk.com
parents: 2053
diff changeset
188 whenDone();
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
189 }
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
190 updateLoop();
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
191 }
2053
b7a3dff5514d rough and untested port of asco from jquery to vanilla
drewp@bigasterisk.com
parents: 2050
diff changeset
192 onLoad();