Skip to content

Commit

Permalink
Add utilIsColorValid to check whether a color can be shown
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Nov 18, 2023
1 parent 8e971ba commit 9e32b4a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
12 changes: 9 additions & 3 deletions modules/ui/feature_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { osmEntity } from '../osm/entity';
import { uiIcon } from './icon';
import { uiCmd } from './cmd';

import { utilHighlightEntities, utilNoAuto } from '../util';
import { utilHighlightEntities, utilIsColorValid, utilNoAuto } from '../util';


export function uiFeatureList(context) {
Expand Down Expand Up @@ -303,8 +303,8 @@ export function uiFeatureList(context) {
label
.append('span')
.attr('class', 'entity-name')
.classed('has-color', d => d.entity && d.entity.type === 'relation' && d.entity.tags.colour)
.style('border-color', d => d.entity && d.entity.type === 'relation' && d.entity.tags.colour)
.classed('has-color', d => !!_getColor(d.entity))
.style('border-color', d => _getColor(d.entity))
.text(d => d.name);

enter
Expand All @@ -319,6 +319,12 @@ export function uiFeatureList(context) {
}


function _getColor(entity) {
const val = entity?.type === 'relation' && entity?.tags.colour;
return (val && utilIsColorValid(val)) ? val : null;
}


function mouseover(d3_event, d) {
if (!d.id || d.id === -1) return;
utilHighlightEntities([d.id], true, context);
Expand Down
12 changes: 9 additions & 3 deletions modules/ui/sections/raw_member_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { osmEntity } from '../../osm';
import { uiIcon } from '../icon';
import { uiCombobox } from '../combobox';
import { uiSection } from '../section';
import { utilHighlightEntities, utilNoAuto } from '../../util';
import { utilHighlightEntities, utilIsColorValid, utilNoAuto } from '../../util';

const MAX_MEMBERS = 1000;

Expand Down Expand Up @@ -194,8 +194,8 @@ export function uiSectionRawMemberEditor(context) {
labelLink
.append('span')
.attr('class', 'member-entity-name')
.classed('has-color', d => d.member.type === 'relation' && d.member.tags.colour)
.style('border-color', d => d.member.type === 'relation' && d.member.tags.colour)
.classed('has-color', d => !!_getColor(d.member))
.style('border-color', d => _getColor(d.member))
.text(d => (d.member ? l10n.displayName(d.member.tags) : ''));

label
Expand Down Expand Up @@ -335,6 +335,12 @@ export function uiSectionRawMemberEditor(context) {
};


function _getColor(entity) {
const val = entity?.type === 'relation' && entity?.tags.colour;
return (val && utilIsColorValid(val)) ? val : null;
}


function _bindCombo(d, i, nodes) {
let row = d3_select(nodes[i]);
let role = row.selectAll('input.member-role');
Expand Down
17 changes: 12 additions & 5 deletions modules/ui/sections/raw_membership_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { uiIcon } from '../icon';
import { uiCombobox } from '../combobox';
import { uiSection } from '../section';
import { uiTooltip } from '../tooltip';
import { utilNoAuto, utilHighlightEntities } from '../../util';
import { utilNoAuto, utilIsColorValid, utilHighlightEntities } from '../../util';

const MAX_MEMBERSHIPS = 1000;

Expand Down Expand Up @@ -134,6 +134,12 @@ export function uiSectionRawMembershipEditor(context) {
}


function _getColor(entity) {
const val = entity?.type === 'relation' && entity?.tags.colour;
return (val && utilIsColorValid(val)) ? val : null;
}


function changeRole(d3_event, d) {
if (d === 0) return; // called on newrow (shouldn't happen)
if (_inChange) return; // avoid accidental recursive call iD#5731
Expand Down Expand Up @@ -245,15 +251,16 @@ export function uiSectionRawMembershipEditor(context) {
var matched = presets.match(entity, graph);
var presetName = (matched && matched.name()) || l10n.t('inspector.relation');
var entityName = l10n.displayName(entity.tags) || '';
var color = _getColor(entity);

return selection => {
selection
.append('b')
.text(presetName + ' ');
selection
.append('span')
.classed('has-color', entity.tags.colour)
.style('border-color', entity.tags.colour)
.classed('has-color', !!color)
.style('border-color', color)
.text(entityName);
};
}
Expand Down Expand Up @@ -362,8 +369,8 @@ export function uiSectionRawMembershipEditor(context) {
labelLink
.append('span')
.attr('class', 'member-entity-name')
.classed('has-color', d => d.relation.tags.colour)
.style('border-color', d => d.relation.tags.colour)
.classed('has-color', d => !!_getColor(d.relation))
.style('border-color', d => _getColor(d.relation))
.text(function(d) {
const matched = presets.match(d.relation, editor.staging.graph);
// hide the network from the name if there is NSI match
Expand Down
1 change: 1 addition & 0 deletions modules/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { utilFastMouse } from './util';
export { utilFetchResponse, FetchError } from './fetch_response';
export { utilFunctor } from './util';
export { utilGetSetValue } from './get_set_value';
export { utilIsColorValid } from './util';
export { utilHighlightEntities } from './util';
export { utilKeybinding } from './keybinding';
export { utilNoAuto } from './util';
Expand Down
15 changes: 15 additions & 0 deletions modules/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ export function utilHighlightEntities(ids, highlighted, context) {
}


// Returns whether value looks like a valid color we can display
export function utilIsColorValid(value) {
// OSM only supports hex or named colors
if (!value.match(/^(#([0-9a-fA-F]{3}){1,2}|\w+)$/)) {
return false;
}
// see https://stackoverflow.com/a/68217760/1627467
if (!CSS.supports('color', value) || ['unset', 'inherit', 'initial', 'revert'].includes(value)) {
return false;
}

return true;
}


// `utilSetTransform`
// Applies a CSS transformation to the given selection
export function utilSetTransform(selection, x, y, scale, rotate) {
Expand Down

0 comments on commit 9e32b4a

Please sign in to comment.