'use strict'; /* * Cargo pickers shared by the trade and end game screens (window.CargoUI): one slider + stepper + number box per resource. * The server decides everything (capacity per link, stock, links); this only keeps the numbers inside what the POP holds. */ (function () { const { h } = UI; const D = UpstreamData; const num = (n) => App.fmt.num(n); // One row: label, "/ have", slider, -/+ buttons, number box, "all". onChange(v) gets the clamped integer. function pickRow(label, have, cur, onChange, sub) { have = Math.max(0, Math.floor(have)); const range = h('input', { type: 'range', min: '0', max: String(have), step: '1', value: String(cur), 'aria-label': label }); const numIn = h('input', { type: 'number', inputmode: 'numeric', min: '0', max: String(have), value: String(cur), class: 'mil-num', 'aria-label': label }); const set = (v) => { v = Math.max(0, Math.min(have, Math.floor(Number(v)) || 0)); range.value = String(v); numIn.value = String(v); onChange(v); }; range.addEventListener('input', () => set(range.value)); numIn.addEventListener('input', () => set(numIn.value)); return h('div', { class: 'mil-pick' }, h('div', { class: 'row' }, h('span', null, h('b', { text: label }), h('span', { class: 'dimmer', text: ' / ' + num(have) })), sub || null), h('div', { class: 'pick-ctl' }, h('button', { class: 'btn small', type: 'button', 'aria-label': '-1', onclick: () => set(Number(numIn.value) - 1) }, '−'), range, h('button', { class: 'btn small', type: 'button', 'aria-label': '+1', onclick: () => set(Number(numIn.value) + 1) }, '+'), numIn, h('button', { class: 'btn small', type: 'button', onclick: () => set(have) }, App.t('send.all')))); } // Pickers for fiber / hardware / capital writing into `cargo` ({res: n}, zero entries removed). have = { fiber, hardware, capital }. function cargoPickers(cargo, have, onChange) { return D.RESOURCES.map((r) => pickRow(App.resName(r), Math.floor(have[r] || 0), cargo[r] || 0, (v) => { if (v > 0) cargo[r] = v; else delete cargo[r]; if (onChange) onChange(); })); } const cargoTotal = (cargo) => Object.keys(cargo).reduce((s, k) => s + (cargo[k] || 0), 0); window.CargoUI = { pickRow, cargoPickers, cargoTotal }; }());