Files
iD/modules/operations/circularize.js
T
Bryan Housel 966f5c3586 Add explicit validation calls to each operation
This is cleaner than adding hooks to transitionable actions, or performing validation
on every history change.  We want validation to run after each operation, but not
while the user is drawing lines or typing in fields.
2019-05-06 14:49:46 -04:00

83 lines
2.5 KiB
JavaScript

import { t } from '../util/locale';
import { actionCircularize } from '../actions/circularize';
import { behaviorOperation } from '../behavior/operation';
import { utilGetAllNodes } from '../util';
export function operationCircularize(selectedIDs, context) {
var entityID = selectedIDs[0];
var entity = context.entity(entityID);
var extent = entity.extent(context.graph());
var geometry = context.geometry(entityID);
var action = actionCircularize(entityID, context.projection);
var nodes = utilGetAllNodes(selectedIDs, context.graph());
var coords = nodes.map(function(n) { return n.loc; });
var operation = function() {
context.perform(action, operation.annotation());
window.setTimeout(function() {
context.validator().validate();
}, 300); // after any transition
};
operation.available = function() {
return selectedIDs.length === 1 &&
entity.type === 'way' &&
new Set(entity.nodes).size > 1;
};
// don't cache this because the visible extent could change
operation.disabled = function() {
var actionDisabled = action.disabled(context.graph());
if (actionDisabled) {
return actionDisabled;
} else if (extent.percentContainedIn(context.extent()) < 0.8) {
return 'too_large';
} else if (someMissing()) {
return 'not_downloaded';
} else if (selectedIDs.some(context.hasHiddenConnections)) {
return 'connected_to_hidden';
}
return false;
function someMissing() {
var osm = context.connection();
if (osm) {
var missing = coords.filter(function(loc) { return !osm.isDataLoaded(loc); });
if (missing.length) {
missing.forEach(function(loc) { context.loadTileAtLoc(loc); });
return true;
}
}
return false;
}
};
operation.tooltip = function() {
var disable = operation.disabled();
return disable ?
t('operations.circularize.' + disable) :
t('operations.circularize.description.' + geometry);
};
operation.annotation = function() {
return t('operations.circularize.annotation.' + geometry);
};
operation.id = 'circularize';
operation.keys = [t('operations.circularize.key')];
operation.title = t('operations.circularize.title');
operation.behavior = behaviorOperation(context).which(operation);
return operation;
}