Skip to content

Commit

Permalink
formatting for feature properties
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddewie committed Dec 15, 2024
1 parent 80488fa commit b73cc60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 12 additions & 2 deletions proxy/js/features.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,29 @@ const railwayLineFeatures = {
electrification_state: {
name: 'Electrification',
},
// TODO format with 2 digits and Hz
frequency: {
name: 'Frequency',
format: {
template: '%.2d Hz',
},
},
// TODO format with V
voltage: {
name: 'Voltage',
format: {
template: '%d V',
},
},
future_frequency: {
name: 'Future frequency',
format: {
template: '%.2d Hz',
},
},
future_voltage: {
name: 'Future voltage',
format: {
template: '%d V',
},
},
gauge_label: {
name: 'Gauge',
Expand Down
12 changes: 11 additions & 1 deletion proxy/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,18 @@ function popupContent(feature) {
const featureType = featureContent && featureContent.type || 'point';
const osmType = featureType === 'point' ? 'node' : 'way';

const formatPropertyValue = (value, format) => {
if (!format) {
return String(value);
} else if (format.template) {
return format.template.replace('%s', () => String(value)).replace(/%(\.(\d+))?d/, (_1, _2, decimals) => value.toFixed(Number(decimals)));
} else {
return String(value);
}
}

const propertyValues = Object.entries(featureCatalog.properties || {})
.map(([property, {name}]) => properties[property] ? `<span class="badge rounded-pill text-bg-light">${name}${properties[property] === true ? '' : `: <span class="text-monospace">${properties[property]}`}</span></span>` : '')
.map(([property, {name, format}]) => (properties[property] !== undefined && properties[property] !== null && properties[property] !== '' && properties[property] !== false) ? `<span class="badge rounded-pill text-bg-light">${name}${properties[property] === true ? '' : `: <span class="text-monospace">${formatPropertyValue(properties[property], format)}`}</span></span>` : '')
.filter(it => it)
.join('')

Expand Down

0 comments on commit b73cc60

Please sign in to comment.