Custom metric panel updates on Safari after switching multiplier and cost (#131)

This commit is contained in:
Frederic Jung 2026-10-28 19:38:55 +01:00
parent e0a02276a2
commit e3534cad4b
No known key found for this signature in database
3 changed files with 44 additions and 2 deletions

View file

@ -30,10 +30,10 @@ Object.assign(ACT,{
setPref:({kind,id,v})=>{if(!own(PREF,v))return;if(kind!=='t'&&kind!=='p'&&!(isIx(id)&&S.ixp[id]!==undefined))return;if(kind==='t'){const t=S.transit.find(x=>x.id===id);if(t)t.pref=v;}else if(kind==='p'){const x=S.pni.find(y=>y.id===id);if(x)x.pref=v;}else{S.ixPref=S.ixPref||{};S.ixPref[id]=v;}sfx('click');after();},
setMetric:({id,v})=>{if(v!=='custom'&&!own(MET,v))return;const l=S.links.find(x=>x.id===id);if(!l)return;const m=S.pairMet||(S.pairMet={}),k=edgeKey(l);
// Switching to custom starts from the current preset's factor, so nothing moves until the player edits it.
if(v==='custom'){if(!pairCm(k))m[k]={k:'f',v:MET[pairMet(k)].f};}else if(v==='normal')delete m[k];else m[k]=v;sfx('click');after();},
if(v==='custom'){if(!pairCm(k))m[k]={k:'f',v:MET[pairMet(k)].f};}else if(v==='normal')delete m[k];else m[k]=v;metBlur();sfx('click');after();},
metricMode:({id,k})=>{if(k!=='f'&&k!=='w')return;const l=S.links.find(x=>x.id===id);if(!l)return;const key=edgeKey(l),c=pairCm(key);if(!c||c.k===k)return;
// The cost stays what it was on this link, written the other way.
const w=pairW(key,l.km);S.pairMet[key]={k,v:clampM(k,k==='w'?w:w/(l.km+60))};sfx('click');after();},
const w=pairW(key,l.km);S.pairMet[key]={k,v:clampM(k,k==='w'?w:w/(l.km+60))};metBlur();sfx('click');after();},
metricSet:({id})=>{const l=S.links.find(x=>x.id===id),t=$('metIn');if(!l||!t)return;const key=edgeKey(l),c=pairCm(key);if(!c)return;const v=parseFloat(String(t.value).replace(',','.'));if(!Number.isFinite(v)){sfx('err');return;}
t.blur();S.pairMet[key]={k:c.k,v:clampM(c.k,v)};sfx('click');after();},
setStandby:({id,on})=>{const l=S.links.find(x=>x.id===id);if(!l)return;if(on)l.standby=true;else delete l.standby;sfx('click');after();},

View file

@ -67,6 +67,8 @@ function trafficSection(l){
if(u>=0.8&&egs.length){const d=R.egDesc[egs[0][0]];h+=`<p class="why">${tr('link_panels.biggest_feeder_egress_in_city_set',{egress:esc(d.lab),city:CITY[d.city].name})}</p>`;}
return h+`</section>`;
}
// Safari keeps the number field focused through a button tap, and renderPanel() skips a focused field: let go first (#131).
const metBlur=()=>{const t=$('metIn');if(t&&document.activeElement===t)t.blur();};
function metricSection(l){const key=edgeKey(l),cur=pairMet(key),pl=pairLinks(l.a,l.b),sb=!!l.standby,base=l.km+60,sel=k=>cur===k?'pink sm':'ghost sm';
let h=`<section><h3>${tr('common.routing_metric')}</h3><p class="lead">${tr('link_panels.your_routers_pick_the_lowest_cost_0160')}</p><div class="btns">${Object.keys(MET).map(k=>btn(MET[k].l,'setMetric',{id:l.id,v:k},sel(k))).join('')}${btn(tr('common.custom'),'setMetric',{id:l.id,v:'custom'},sel('custom'))}</div>`;
const cm=pairCm(key);

View file

@ -0,0 +1,40 @@
// #131 on Safari: the custom metric field keeps focus through a button tap, and the panel skips re-rendering a
// focused field, so switching between multiplier and cost (or to a preset) left the old panel on screen.
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const setup = (ev) => {
ev("ACT.start({});S.cash=1e9;S.lvl=4;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});ACT.buildPop({city:'ams'});UI.via=null;openLinkModal('fra','ams');ACT.buildLink({tier:1});closeModal()");
ev("window.L=S.links[0].id;ACT.selLink({id:L});renderAll();ACT.setMetric({id:L,v:'custom'});renderAll()");
};
test('#131 switching the custom metric mode re-renders even while the field has focus', () => {
const { ev, dom, errors } = load();
const d = dom.window.document;
setup(ev);
d.getElementById('metIn').focus();
assert.equal(d.activeElement.id, 'metIn');
ev("ACT.metricMode({id:L,k:'w'})");
assert.equal(d.getElementById('metIn').getAttribute('step'), '1', 'the field is now the exact cost');
assert.equal(+d.getElementById('metIn').value, ev("S.pairMet[edgeKey(S.links[0])].v"));
d.getElementById('metIn').focus();
ev("ACT.metricMode({id:L,k:'f'})");
assert.equal(d.getElementById('metIn').getAttribute('step'), '0.1', 'and back to a multiplier');
d.getElementById('metIn').focus();
ev("ACT.setMetric({id:L,v:'normal'})");
assert.equal(d.getElementById('metIn'), null, 'a preset hides the field');
assert.deepEqual(errors, []);
});
test('#131 the daily re-render still keeps a half-typed value', () => {
const { ev, dom, errors } = load();
const d = dom.window.document;
setup(ev);
const t = d.getElementById('metIn');
t.focus(); t.value = '3.5';
ev("S.day++;renderPanel()");
assert.equal(d.getElementById('metIn').value, '3.5');
assert.equal(d.activeElement.id, 'metIn');
assert.deepEqual(errors, []);
});