From e3534cad4b1a4d90ad6fcfc0cc1455569aa24202 Mon Sep 17 00:00:00 2001 From: Frederic Jung Date: Wed, 28 Oct 2026 19:38:55 +0100 Subject: [PATCH] Custom metric panel updates on Safari after switching multiplier and cost (#131) --- src/game/90-actions/60-links.js | 4 ++-- src/game/93-ui/70-link-panels.js | 2 ++ tests/metric-panel.test.js | 40 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/metric-panel.test.js diff --git a/src/game/90-actions/60-links.js b/src/game/90-actions/60-links.js index d9b1000..7c046d0 100644 --- a/src/game/90-actions/60-links.js +++ b/src/game/90-actions/60-links.js @@ -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();}, diff --git a/src/game/93-ui/70-link-panels.js b/src/game/93-ui/70-link-panels.js index 4633d68..85a3af8 100644 --- a/src/game/93-ui/70-link-panels.js +++ b/src/game/93-ui/70-link-panels.js @@ -67,6 +67,8 @@ function trafficSection(l){ if(u>=0.8&&egs.length){const d=R.egDesc[egs[0][0]];h+=`

${tr('link_panels.biggest_feeder_egress_in_city_set',{egress:esc(d.lab),city:CITY[d.city].name})}

`;} return h+``; } +// 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=`

${tr('common.routing_metric')}

${tr('link_panels.your_routers_pick_the_lowest_cost_0160')}

${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'))}
`; const cm=pairCm(key); diff --git a/tests/metric-panel.test.js b/tests/metric-panel.test.js new file mode 100644 index 0000000..fdf2d5a --- /dev/null +++ b/tests/metric-panel.test.js @@ -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, []); +});