2026-11-08 10:23:25 +00:00
let redirectsTold = "" ;
2026-11-06 08:28:04 +00:00
function adoptSettings ( next ) {
settings = next ;
settingsSig = JSON . stringify ( next );
if ( space . edits === space . saved ) {
space . views = { graph : {... next . space . graph }, sky : {... next . space . sky }};
space . glides = { graph : null , sky : null };
2026-11-07 15:03:14 +00:00
refresh3d ();
2026-11-06 08:28:04 +00:00
}
sky . mode = "" ;
drawSky ();
syncMusic ();
syncRadio ();
musicButton ();
2026-11-11 23:39:17 +00:00
syncBubbles ();
2026-11-06 08:28:04 +00:00
}
async function saveSettings ( next ) {
2026-11-08 17:26:51 +00:00
const signed = await signedProof ( "/api/settings/sign" , next , "Settings" );
2026-11-07 19:59:47 +00:00
if ( ! signed ) {
return false ;
}
const result = await sendJson ( "/api/settings" , {... next , proof : signed . proof });
2026-11-06 08:28:04 +00:00
if ( ! result . ok || ! result . data ) {
2026-11-08 17:26:51 +00:00
toast ( "Settings not saved" , [ ` ${ result . status } ${ result . text } ` . trim ()], false );
2026-11-06 08:28:04 +00:00
return false ;
}
2026-11-08 10:23:25 +00:00
const { settings : saved , problems } = result . data ;
const unresolved = problems . join ( "\n" );
if ( unresolved && unresolved !== redirectsTold ) {
2026-11-08 17:26:51 +00:00
toast ( "Redirect not resolved" , [... problems , "These stations load only from their own address for now; saving after ten minutes tries again." ], false );
2026-11-08 10:23:25 +00:00
}
redirectsTold = unresolved ;
const fresh = [... new Set ( saved . streams . flatMap (( stream ) => Ambience . stationOrigins ( stream )))]. filter (( origin ) => ! pageMedia . has ( origin ));
adoptSettings ( saved );
2026-11-06 08:28:04 +00:00
if ( fresh . length ) {
2026-11-08 17:26:51 +00:00
toast ( "New station" , [ "The page reloads so the browser may play it. If the browser then blocks playback, a click starts the station." , `New for this page: ${ fresh . join ( ", " ) } ` ], true );
2026-11-06 08:28:04 +00:00
window . setTimeout (() => window . location . reload (), 1500 );
}
return true ;
}
function streamRow ( stream , rows , list ) {
const field = ( attrs ) => h ( "input" , { class : "field" , required : true , ... attrs });
2026-11-08 17:26:51 +00:00
const name = field ({ type : "text" , value : stream . name , placeholder : "Station name" , maxlength : 120 });
2026-11-06 08:28:04 +00:00
const url = field ({ type : "url" , value : stream . url , placeholder : "https://…" });
const from = field ({ type : "time" , value : stream . from });
const to = field ({ type : "time" , value : stream . to === "24:00" ? "00:00" : stream . to });
const on = h ( "input" , { type : "checkbox" , checked : stream . on });
const days = WEEKDAYS . map (( label , index ) => h ( "input" , { type : "checkbox" , checked : stream . days . includes ( index ), "aria-label" : label }));
const entry = { read : () => ({
name : name . value . trim (),
url : url . value . trim (),
from : from . value ,
to : to . value === "00:00" && stream . to === "24:00" ? "24:00" : to . value ,
days : days . flatMap (( box , index ) => ( box . checked ? [ index ] : [])),
on : on . checked ,
2026-11-08 10:23:25 +00:00
via : stream . via ,
2026-11-06 08:28:04 +00:00
})};
entry . row = h ( "div" , { class : "stream-row" },
2026-11-08 17:26:51 +00:00
h ( "div" , { class : "grid" }, h ( "label" , {}, "Name" , name ), h ( "label" , {}, "Stream URL" , url ), h ( "label" , {}, "from" , from ), h ( "label" , {}, "to" , to )),
h ( "div" , { class : "days" }, days . map (( box , index ) => h ( "label" , { class : "check" }, box , WEEKDAYS [ index ])), h ( "label" , { class : "check" }, on , "active" ), h ( "span" , { class : "spacer" }),
btn ( "Remove" , "btn small danger" , () => {
2026-11-06 08:28:04 +00:00
rows . splice ( rows . indexOf ( entry ), 1 );
entry . row . remove ();
})));
rows . push ( entry );
list . append ( entry . row );
}
2026-11-06 22:20:06 +00:00
function ambience () {
2026-11-07 15:03:14 +00:00
const tool = ( id , name , label , title , run ) => h ( "button" , { type : "button" , id , class : "btn small icon-only" , ... named ( label , title ), on : { click : run }}, icon ( name ));
2026-11-08 17:26:51 +00:00
const section = h ( "section" , {}, h ( "h3" , {}, "Ambience" ), h ( "div" , { class : "toolbar" },
tool ( "sky-look" , "bi-stars" , "Sky" , "Look at the sky: dragging turns the view (key h)" , () => {
2026-11-06 22:20:06 +00:00
closeSheet ();
skyLook ( true );
}),
2026-11-08 17:26:51 +00:00
tool ( "music-toggle" , "bi-music-note-beamed" , "Music" , "Music: generative music on or off" , toggleMusic ),
tool ( "leitstand" , "bi-display" , "Control room" , "Control room: full screen with large type" , toggleLeitstand ),
tool ( "notify" , "bi-bell" , "Notifications" , "Notifications: allow the browser's desktop notifications" , askNotify )));
2026-11-06 22:20:06 +00:00
window . setTimeout (() => {
musicButton ();
notifyLabel ();
}, 0 );
return section ;
}
2026-11-13 10:48:13 +00:00
function stationsSheet () {
const list = h ( "div" , { class : "mini" });
const rows = [];
settings . streams . forEach (( stream ) => streamRow ( stream , rows , list ));
const form = h ( "form" , { class : "settings" , "aria-label" : "Stations" },
h ( "section" , {}, h ( "h3" , {}, "Internet radio" ), h ( "p" , { class : "muted" }, "Internet radio at fixed times with a 3 s fade in and out; the music pauses meanwhile. A new station plays around the clock until you change its times; in the radio panel you start and stop it by hand. The page may load sound only from these addresses; your passkey signs a new address. If a signed station redirects, the server resolves the redirect when you save a new station or a changed address and releases its target; the note after saving names it." ),
list , h ( "div" , {}, btn ( "+ Station" , "btn small soft" , () => streamRow ({ name : "" , url : "" , from : "00:00" , to : "24:00" , days : [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ], on : true , via : []}, rows , list )))),
h ( "div" , { class : "sheet-foot" }, h ( "span" , { class : "spacer" }), btn ( "Cancel" , "btn ghost" , closeSheet ), h ( "button" , { type : "submit" , class : "btn primary" }, "Save" )));
2026-11-09 22:22:46 +00:00
form . addEventListener ( "submit" , async ( event ) => {
event . preventDefault ();
context ();
2026-11-13 10:48:13 +00:00
if ( await saveSettings ({... settings , space : savedSpace (), streams : rows . map (( entry ) => entry . read ())})) {
closeSheet ();
toast ( "Stations saved" , [], true );
2026-11-09 22:22:46 +00:00
}
});
2026-11-13 10:48:13 +00:00
openSheet ( "Stations" , [ form ], false );
2026-11-09 22:22:46 +00:00
}
2026-11-06 08:28:04 +00:00
function settingsSheet () {
const number = ( value , min , max , label ) => h ( "input" , { class : "field" , type : "number" , min , max , step : "any" , value , required : true , "aria-label" : label });
2026-11-08 17:26:51 +00:00
const latitude = number ( settings . latitude , - 90 , 90 , "Latitude" );
const longitude = number ( settings . longitude , - 180 , 180 , "Longitude" );
2026-11-06 08:28:04 +00:00
const theme = h ( "select" , { class : "field" }, THEME_CHOICES . map (([ id , label ]) => h ( "option" , { value : id , selected : settings . theme === id }, label )));
const colors = Object . fromEntries ( COLOR_FIELDS . map (([ id ]) => [ id , h ( "input" , { type : "color" , value : settings . colors [ id ]})]));
const custom = h ( "div" , { class : "grid" }, COLOR_FIELDS . map (([ id , label ]) => h ( "label" , {}, label , colors [ id ])));
const verdict = h ( "p" , { class : "muted" , "aria-live" : "polite" });
const judge = () => {
custom . hidden = theme . value !== "custom" ;
verdict . hidden = theme . value !== "custom" ;
const panel = Ambience . THEMES [ Ambience . modeOf ( colors . top . value , colors . bottom . value )]. panel ;
const ratio = Ambience . contrast ( Ambience . hex ( colors . accent . value ), Ambience . hex ( panel ));
2026-11-08 17:26:51 +00:00
verdict . textContent = `Accent on surfaces: contrast ${ ratio . toFixed ( 1 ) } : 1 ${ ratio < 3 ? "; too weak, pick a stronger accent" : "" } ` ;
2026-11-06 08:28:04 +00:00
};
theme . addEventListener ( "change" , judge );
Object . values ( colors ). forEach (( input ) => input . addEventListener ( "input" , judge ));
judge ();
const musicOn = h ( "input" , { type : "checkbox" , checked : settings . music . on });
2026-11-08 17:26:51 +00:00
const volume = h ( "input" , { type : "range" , min : 0 , max : 1 , step : 0.05 , value : settings . music . volume , "aria-label" : "Overall volume" });
2026-11-06 08:28:04 +00:00
const notify = h ( "input" , { type : "checkbox" , checked : settings . notify });
const form = h ( "form" , { class : "settings" },
2026-11-06 22:20:06 +00:00
ambience (),
2026-11-08 17:26:51 +00:00
h ( "section" , {}, h ( "h3" , {}, "Location" ), h ( "p" , { class : "muted" }, "Sun, moon and stars stand where you see them from here, looking south; under 🔭 Sky (key h) you turn the view. Decimal degrees, north and east positive." ),
h ( "div" , { class : "grid" }, h ( "label" , {}, "Latitude" , latitude ), h ( "label" , {}, "Longitude" , longitude ))),
h ( "section" , {}, h ( "h3" , {}, "Appearance" ), h ( "label" , {}, "Colors" , theme ), custom , verdict ),
h ( "section" , {}, h ( "h3" , {}, "Music" ), h ( "p" , { class : "muted" }, "Generative ambient music, made offline: calm with an empty inbox, a little tenser when something waits, quieter under messages." ),
h ( "div" , { class : "grid" }, h ( "label" , { class : "check" }, musicOn , "Music on" ), h ( "label" , {}, "Overall volume (music and radio)" , volume ))),
2026-11-13 10:48:13 +00:00
h ( "section" , {}, h ( "h3" , {}, "Notifications" ), h ( "p" , { class : "muted" }, "Mode, tones, squelch, the speech threshold, reading aloud and the stations sit in the radio panel behind the radio button in the header." ),
2026-11-09 22:22:46 +00:00
h ( "label" , { class : "check" }, notify , "Show desktop notifications when the browser allows them" )),
2026-11-06 08:28:04 +00:00
passkeySection (),
2026-11-08 17:26:51 +00:00
h ( "div" , { class : "sheet-foot" }, h ( "span" , { class : "spacer" }), btn ( "Cancel" , "btn ghost" , closeSheet ), h ( "button" , { type : "submit" , class : "btn primary" }, "Save" )));
2026-11-06 08:28:04 +00:00
form . addEventListener ( "submit" , async ( event ) => {
event . preventDefault ();
context ();
const next = {
latitude : Number ( latitude . value ),
longitude : Number ( longitude . value ),
theme : theme . value ,
colors : Object . fromEntries ( COLOR_FIELDS . map (([ id ]) => [ id , colors [ id ]. value ])),
music : { on : musicOn . checked , volume : Number ( volume . value )},
notify : notify . checked ,
2026-11-13 10:48:13 +00:00
streams : settings . streams ,
2026-11-09 22:22:46 +00:00
speech : settings . speech ,
2026-11-06 08:28:04 +00:00
space : savedSpace (),
2026-11-09 06:53:55 +00:00
estimation : settings . estimation ,
2026-11-09 22:22:46 +00:00
trend : settings . trend ,
2026-11-11 23:39:17 +00:00
bubbles : settings . bubbles ,
2026-11-06 08:28:04 +00:00
};
if ( await saveSettings ( next )) {
closeSheet ();
2026-11-08 17:26:51 +00:00
toast ( "Settings saved" , [], true );
2026-11-06 08:28:04 +00:00
}
});
2026-11-08 17:26:51 +00:00
openSheet ( "Settings and ambience" , [ form ], false );
2026-11-06 08:28:04 +00:00
}