annotate light9/ascoltami/main.ts @ 2050:7ed414bdaab9

wip porting asco to TS and not-jquery
author drewp@bigasterisk.com
date Wed, 11 May 2022 00:07:13 -0700
parents
children b7a3dff5514d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2050
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
1 async function onLoad() {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
2 const config = await (await fetch('config')).json();
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
3 const times = config.times;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
4 document.title = document.title.replace('{{host}}', config.host);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
5 const h1 =document.querySelector('h1')!
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
6 h1.innerText = h1?.innerText.replace('{{host}}', config.host)
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
7 /*
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
8 $("#nav").text(navigator.userAgent);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
9 var updateFreq = (navigator.userAgent.indexOf("Linux") != -1) ? 10 : 2;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
10 if (navigator.userAgent.match(/Windows NT/)) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
11 // helper laptop
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
12 updateFreq = 10;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
13 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
14 $("#updateReq").text(updateFreq);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
15
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
16 var times = {% raw times %};
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
17
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
18 var currentDuration = 0;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
19 var currentHighlightedSong = "";
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
20 var lastPlaying;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
21 function updateCurrent(doneCallback) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
22 $.getJSON("time", {}, function (data, status) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
23 $("#currentSong").text(data.song);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
24 if (data.song != currentHighlightedSong) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
25 showCurrentSong(data.song);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
26 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
27 $("#currentTime").text(data.t.toFixed(1));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
28 $("#leftTime").text((data.duration - data.t).toFixed(1));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
29 $("#leftAutoStopTime").text(
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
30 Math.max(0, data.duration - times.post - data.t).toFixed(1));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
31 $("#states").text(JSON.stringify(data.state));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
32 currentDuration = data.duration;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
33 $("#timeSlider").slider({value: data.t,
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
34 max: data.duration});
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
35 if (data.playing != lastPlaying) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
36 $(".playMode").removeClass("active");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
37 $(data.playing ? "#cmd-play" : "#cmd-stop").addClass("active");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
38 lastPlaying = data.playing;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
39 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
40 $("#next").text(data.next);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
41 doneCallback();
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
42 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
43 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
44 function showCurrentSong(uri) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
45 $(".songs div").each(function (i, row) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
46 row = $(row);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
47 if (row.find("button").data("uri") == uri) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
48 row.addClass("currentSong");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
49 } else {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
50 row.removeClass("currentSong");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
51 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
52 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
53 currentHighlightedSong = uri;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
54 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
55 $.getJSON("songs", {}, function (data, status) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
56 $.each(data.songs, function (i, song) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
57 var button = $("<button>");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
58 // link is just for dragging, not clicking
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
59 var link = $("<a>");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
60 link.append($("<span>").addClass("num").text(song.label.slice(0,2)));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
61 link.append($("<span>").addClass("song-name").text(song.label.slice(2).trim()));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
62 link.attr("href", song.uri);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
63 link.click(function () { button.click(); return false; });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
64 button.append(link);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
65 button.data(song);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
66 button.click(function () {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
67 $.post("song", button.data("uri"),
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
68 function (data, textStatus, xhr) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
69 showCurrentSong(song.uri);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
70 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
71 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
72 $(".songs").append($("<div>").append(button));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
73 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
74 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
75
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
76 var tojs = JSON.stringify;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
77
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
78 $(document).keypress(function (ev) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
79
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
80 if (ev.which == 115) { $("#cmd-stop").click(); return false; }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
81 if (ev.which == 112) { $("#cmd-play").click(); return false; }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
82 if (ev.which == 105) { $("#cmd-intro").click(); return false; }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
83 if (ev.which == 116) { $("#cmd-post").click(); return false; }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
84
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
85 if (ev.key == 'g') {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
86 $("#cmd-go").click();
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
87 return false;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
88 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
89 return true;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
90 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
91
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
92 $("#cmd-stop").click(function () { $.post("time", tojs({pause: true})); });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
93 $("#cmd-play").click(function () { $.post("time", tojs({resume: true})); });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
94 $("#cmd-intro").click(function () {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
95 $.post("time", tojs({t: times.intro, resume: true}))
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
96 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
97 $("#cmd-post").click(function () {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
98 $.post("time", tojs({t: currentDuration - times.post, resume: true}))
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
99 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
100 $("#cmd-go").click(function () {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
101 $.post("go");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
102 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
103 $("#cmd-out0").click(function () { $.post("output", tojs({sink: "0"})); })
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
104 $("#cmd-out1").click(function () { $.post("output", tojs({sink: "1"})); })
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
105
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
106 var pendingSlide = false;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
107 $("#timeSlider").slider({
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
108 step: .01,
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
109 slide: function (event, ui) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
110 if (pendingSlide) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
111 return;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
112 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
113 pendingSlide = true;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
114 $.post("time", '{"t" : '+ui.value+'}',
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
115 function (data, status, xhr) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
116 pendingSlide = false;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
117 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
118 },
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
119 });
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
120
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
121 var raf = window.requestAnimationFrame ||
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
122 window.mozRequestAnimationFrame ||
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
123 window.webkitRequestAnimationFrame;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
124
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
125 var recentUpdates = [];
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
126 function onUpdate() {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
127 recentUpdates.push(+new Date());
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
128 recentUpdates = recentUpdates.slice(Math.max(recentUpdates.length - 5, 0));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
129 refreshUpdateFreqs();
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
130 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
131
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
132 function refreshUpdateFreqs() {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
133 if (recentUpdates.length > 1) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
134 if (+new Date() - recentUpdates[recentUpdates.length - 1] > 1000) {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
135 $("#updateActual").text("(stalled)");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
136 $(".dimStalled").addClass("stalled");
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
137 return;
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
138 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
139
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
140 var avgMs = (recentUpdates[recentUpdates.length - 1] - recentUpdates[0]) / (recentUpdates.length - 1);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
141 $("#updateActual").text(Math.round(1000 / avgMs));
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
142 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
143 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
144 setInterval(refreshUpdateFreqs, 2000);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
145
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
146 function updateLoop() {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
147 var whenDone = function () {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
148 setTimeout(function () {
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
149 raf(updateLoop);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
150 }, 1000 / updateFreq);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
151 };
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
152 onUpdate();
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
153 updateCurrent(whenDone);
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
154 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
155 updateLoop();
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
156 */
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
157 }
7ed414bdaab9 wip porting asco to TS and not-jquery
drewp@bigasterisk.com
parents:
diff changeset
158 onLoad()