mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-24 00:54:03 +02:00
Trying out simpler outdated_tags code, with tag diff
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { t } from '../util/locale';
|
||||
import { actionUpgradeTags, actionChangeTags, actionChangePreset } from '../actions';
|
||||
import { utilDisplayLabel } from '../util';
|
||||
import { utilArrayUnion, utilDisplayLabel } from '../util';
|
||||
import { validationIssue, validationIssueFix } from '../core/validator';
|
||||
|
||||
|
||||
@@ -8,66 +8,79 @@ export function validationOutdatedTags() {
|
||||
var type = 'outdated_tags';
|
||||
|
||||
|
||||
function missingRecommendedTags(entity, context, graph) {
|
||||
var preset = context.presets().match(entity, graph);
|
||||
if (!preset.isFallback() && preset.tags !== preset.addTags) {
|
||||
return Object.keys(preset.addTags).reduce(function(obj, key) {
|
||||
if (!entity.tags[key]) {
|
||||
obj[key] = preset.addTags[key];
|
||||
}
|
||||
return obj;
|
||||
}, {});
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
var validation = function checkOutdatedTags(entity, context) {
|
||||
var replacementPresetID = context.presets().match(entity, context.graph()).replacement;
|
||||
var deprecatedTagsArray = entity.deprecatedTags();
|
||||
var missingTags = missingRecommendedTags(entity, context, context.graph());
|
||||
var graph = context.graph();
|
||||
var oldTags = Object.assign({}, entity.tags); // shallow copy
|
||||
var preset = context.presets().match(entity, graph);
|
||||
|
||||
// upgrade preset..
|
||||
if (preset.replacement) {
|
||||
var newPreset = context.presets().item(preset.replacement);
|
||||
graph = actionChangePreset(entity.id, preset, newPreset, true)(graph); // true = skip field defaults
|
||||
entity = graph.entity(entity.id);
|
||||
preset = newPreset;
|
||||
}
|
||||
|
||||
// upgrade tags..
|
||||
var deprecatedTags = entity.deprecatedTags();
|
||||
if (deprecatedTags.length) {
|
||||
deprecatedTags.forEach(function(tag) {
|
||||
graph = actionUpgradeTags(entity.id, tag.old, tag.replace)(graph);
|
||||
});
|
||||
entity = graph.entity(entity.id);
|
||||
}
|
||||
|
||||
// add missing addTags..
|
||||
var newTags = Object.assign({}, entity.tags); // shallow copy
|
||||
if (preset.tags !== preset.addTags) {
|
||||
Object.keys(preset.addTags).forEach(function(k) {
|
||||
if (!newTags[k]) {
|
||||
newTags[k] = preset.addTags[k];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// determine diff
|
||||
var keys = utilArrayUnion(Object.keys(oldTags), Object.keys(newTags)).sort();
|
||||
var tagDiff = [];
|
||||
keys.forEach(function(k) {
|
||||
var oldVal = oldTags[k];
|
||||
var newVal = newTags[k];
|
||||
|
||||
if (oldVal && (!newVal || newVal !== oldVal)) {
|
||||
tagDiff.push('- ' + k + '=' + oldVal);
|
||||
}
|
||||
if (newVal && (!oldVal || newVal !== oldVal)) {
|
||||
tagDiff.push('+ ' + k + '=' + newVal);
|
||||
}
|
||||
});
|
||||
|
||||
if (!tagDiff.length) return [];
|
||||
|
||||
|
||||
if (!replacementPresetID && deprecatedTagsArray.length === 0 &&
|
||||
Object.keys(missingTags).length === 0) return [];
|
||||
// debugging (sorta)
|
||||
var tooltip = '<pre>\n' + tagDiff.join('\n') + '</pre>';
|
||||
|
||||
|
||||
return [new validationIssue({
|
||||
type: type,
|
||||
severity: 'warning',
|
||||
message: t('issues.outdated_tags.message', { feature: utilDisplayLabel(entity, context) }),
|
||||
tooltip: t('issues.outdated_tags.tip'),
|
||||
tooltip: tooltip, // t('issues.outdated_tags.tip'),
|
||||
entities: [entity],
|
||||
info: {
|
||||
deprecatedTagsArray: deprecatedTagsArray,
|
||||
replacementPresetID: replacementPresetID
|
||||
tagDiff: tagDiff,
|
||||
newTags: newTags
|
||||
},
|
||||
fixes: [
|
||||
new validationIssueFix({
|
||||
auto: true,
|
||||
title: t('issues.fix.upgrade_tags.title'),
|
||||
onClick: function() {
|
||||
var replacementPresetID = this.issue.info.replacementPresetID;
|
||||
var replacementPreset = replacementPresetID && context.presets().item(replacementPresetID);
|
||||
var deprecatedTagsArray = this.issue.info.deprecatedTagsArray;
|
||||
var entityID = this.issue.entities[0].id;
|
||||
var newTags = this.issue.info.newTags;
|
||||
context.perform(
|
||||
function(graph) {
|
||||
if (replacementPreset) {
|
||||
var oldPreset = context.presets().match(graph.entity(entityID), context.graph());
|
||||
graph = actionChangePreset(entityID, oldPreset, replacementPreset, true /* skip field defaults */)(graph);
|
||||
deprecatedTagsArray = graph.entity(entityID).deprecatedTags();
|
||||
}
|
||||
deprecatedTagsArray.forEach(function(deprecatedTags) {
|
||||
graph = actionUpgradeTags(entityID, deprecatedTags.old, deprecatedTags.replace)(graph);
|
||||
});
|
||||
var missingTags = missingRecommendedTags(graph.entity(entityID), context, graph);
|
||||
var tags = Object.assign({}, graph.entity(entityID).tags); // shallow copy
|
||||
for (var key in missingTags) {
|
||||
tags[key] = missingTags[key];
|
||||
}
|
||||
graph = actionChangeTags(entityID, tags)(graph);
|
||||
return graph;
|
||||
},
|
||||
actionChangeTags(entityID, newTags),
|
||||
t('issues.fix.upgrade_tags.annotation')
|
||||
);
|
||||
}
|
||||
@@ -76,6 +89,7 @@ export function validationOutdatedTags() {
|
||||
})];
|
||||
};
|
||||
|
||||
|
||||
validation.type = type;
|
||||
|
||||
return validation;
|
||||
|
||||
Reference in New Issue
Block a user