modules/operations/circularize.js (66 lines of code) (raw):

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(situation) { if (selectedIDs.length !== 1 || entity.type !== 'way' || new Set(entity.nodes).size <= 1) return false; if (situation === 'toolbar' && action.disabled(context.graph())) return false; return true; }; // 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() { 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(); 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; }