Files
iD/modules/validations/generic_name.js
Quincy Morgan 375779882d Add ability to ignore warnings
Replace issue's array of entities with array of entity IDs
Improve issue ID hashing
2019-04-29 17:52:32 -07:00

97 lines
3.1 KiB
JavaScript

import { filters } from 'name-suggestion-index';
import { t } from '../util/locale';
import { utilPreset } from '../util';
import { validationIssue, validationIssueFix } from '../core/validation';
import { actionChangeTags } from '../actions/change_tags';
export function validationGenericName() {
var type = 'generic_name';
// known list of generic names (e.g. "bar")
var discardNamesRegexes = filters.discardNames.map(function(discardName) {
return new RegExp(discardName, 'i');
});
var keysToTestForGenericValues = ['amenity', 'building', 'leisure', 'man_made', 'shop', 'tourism'];
function isGenericName(entity) {
var name = entity.tags.name;
if (!name) return false;
name = name.toLowerCase();
var i, key, val;
// test if the name is just the key or tag value (e.g. "park")
for (i = 0; i < keysToTestForGenericValues.length; i++) {
key = keysToTestForGenericValues[i];
val = entity.tags[key];
if (val) {
val = val.toLowerCase();
if (key === name ||
val === name ||
key.replace(/\_/g, ' ') === name ||
val.replace(/\_/g, ' ') === name) {
return entity.tags.name;
}
}
}
// test if the name is otherwise generic
for (i = 0; i < discardNamesRegexes.length; i++) {
if (discardNamesRegexes[i].test(name)) {
return entity.tags.name;
}
}
return false;
}
var validation = function checkGenericName(entity, context) {
var generic = isGenericName(entity);
if (!generic) return [];
var preset = utilPreset(entity, context);
return [new validationIssue({
type: type,
severity: 'warning',
message: t('issues.generic_name.message', {feature: preset.name(), name: generic}),
reference: showReference,
entityIds: [entity.id],
hash: generic,
fixes: [
new validationIssueFix({
icon: 'iD-operation-delete',
title: t('issues.fix.remove_generic_name.title'),
onClick: function() {
var entityId = this.issue.entityIds[0];
var entity = context.entity(entityId);
var tags = Object.assign({}, entity.tags); // shallow copy
delete tags.name;
context.perform(
actionChangeTags(entityId, tags),
t('issues.fix.remove_generic_name.annotation')
);
}
})
]
})];
function showReference(selection) {
selection.selectAll('.issue-reference')
.data([0])
.enter()
.append('div')
.attr('class', 'issue-reference')
.text(t('issues.generic_name.reference'));
}
};
validation.type = type;
return validation;
}