Files
iD/modules/operations/disconnect.js
Quincy Morgan a1af118f0e Ensure locales and presets are loaded before the UI loads (close #7406)
Consolidate localization behavior and init to a coreLocalizer function and singleton
Explicitly support `en-US` locale
Rename coreData to coreFileFetcher and export a singleton rather than using a property of coreContext
Add `apiConnections` property of coreContext to simplify adding a source switcher
Replace some init functions with re-callable, promise-supporting `ensureLoaded` functions
Make coreContext itself load the UI if a container has been specified at init time
Fix code tests
2020-03-31 12:23:31 -07:00

137 lines
4.3 KiB
JavaScript

import { t } from '../core/localizer';
import { actionDisconnect } from '../actions/disconnect';
import { behaviorOperation } from '../behavior/operation';
import { utilGetAllNodes } from '../util/index';
export function operationDisconnect(selectedIDs, context) {
var vertexIDs = [];
var wayIDs = [];
var otherIDs = [];
var actions = [];
selectedIDs.forEach(function(id) {
var entity = context.entity(id);
if (entity.geometry(context.graph()) === 'vertex') {
vertexIDs.push(id);
} else if (entity.type === 'way'){
wayIDs.push(id);
} else {
otherIDs.push(id);
}
});
var disconnectingWayID = vertexIDs.length === 0 && wayIDs.length === 1 && wayIDs[0];
var extent, nodes, coords;
if (disconnectingWayID) { // disconnecting a way
var way = context.entity(disconnectingWayID);
extent = way.extent(context.graph());
nodes = utilGetAllNodes([disconnectingWayID], context.graph());
coords = nodes.map(function(n) { return n.loc; });
way.nodes.forEach(function(vertexID) {
var action = actionDisconnect(vertexID).limitWays(wayIDs);
if (action.disabled(context.graph()) !== 'not_connected') {
actions.push(action);
}
});
} else { // disconnecting a vertex
vertexIDs.forEach(function(vertexID) {
var action = actionDisconnect(vertexID);
if (wayIDs.length > 0) {
var waysIDsForVertex = wayIDs.filter(function(wayID) {
var way = context.entity(wayID);
return way.nodes.indexOf(vertexID) !== -1;
});
action.limitWays(waysIDsForVertex);
}
actions.push(action);
});
}
var operation = function() {
context.perform(function(graph) {
return actions.reduce(function(graph, action) { return action(graph); }, graph);
}, operation.annotation());
context.validator().validate();
};
operation.available = function() {
if (actions.length === 0) return false;
if (otherIDs.length !== 0) return false;
if (vertexIDs.length !== 0 && wayIDs.length !== 0 && !wayIDs.every(function(wayID) {
return vertexIDs.some(function(vertexID) {
var way = context.entity(wayID);
return way.nodes.indexOf(vertexID) !== -1;
});
})) return false;
return true;
};
operation.disabled = function() {
var reason;
for (var actionIndex in actions) {
reason = actions[actionIndex].disabled(context.graph());
if (reason) return reason;
}
if (disconnectingWayID && extent.percentContainedIn(context.map().extent()) < 0.8) {
return 'too_large.single';
} else if (disconnectingWayID && someMissing()) {
return 'not_downloaded';
} else if (selectedIDs.some(context.hasHiddenConnections)) {
return 'connected_to_hidden';
}
return false;
function someMissing() {
if (context.inIntro()) return false;
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();
if (disable) {
return t('operations.disconnect.' + disable);
}
if (disconnectingWayID) {
return t('operations.disconnect.' + context.graph().geometry(disconnectingWayID) + '.description');
}
return t('operations.disconnect.description');
};
operation.annotation = function() {
return t('operations.disconnect.annotation');
};
operation.id = 'disconnect';
operation.keys = [t('operations.disconnect.key')];
operation.title = t('operations.disconnect.title');
operation.behavior = behaviorOperation(context).which(operation);
return operation;
}