Variable cleanups in behavior and modes

This commit is contained in:
Bryan Housel
2018-09-06 09:50:26 -04:00
parent 10c995552a
commit eba115803a
11 changed files with 106 additions and 102 deletions

View File

@@ -6,8 +6,8 @@ import { utilRebind } from '../util/rebind';
export function behaviorAddWay(context) {
var dispatch = d3_dispatch('start', 'startFromWay', 'startFromNode'),
draw = behaviorDraw(context);
var dispatch = d3_dispatch('start', 'startFromWay', 'startFromNode');
var draw = behaviorDraw(context);
var addWay = function(surface) {
draw.on('click', function() { dispatch.apply('start', this, arguments); })

View File

@@ -11,14 +11,14 @@ import { timer as d3_timer } from 'd3-timer';
export function behaviorBreathe() {
var duration = 800,
steps = 4,
selector = '.selected.shadow, .selected .shadow',
selected = d3_select(null),
classed = '',
params = {},
done = false,
timer;
var duration = 800;
var steps = 4;
var selector = '.selected.shadow, .selected .shadow';
var _selected = d3_select(null);
var _classed = '';
var _params = {};
var _done = false;
var _timer;
function ratchetyInterpolator(a, b, steps, units) {
@@ -49,30 +49,30 @@ export function behaviorBreathe() {
transition
.styleTween('stroke-opacity', function(d) {
return ratchetyInterpolator(
params[d.id][toFrom].opacity,
params[d.id][fromTo].opacity,
_params[d.id][toFrom].opacity,
_params[d.id][fromTo].opacity,
steps
);
})
.styleTween('stroke-width', function(d) {
return ratchetyInterpolator(
params[d.id][toFrom].width,
params[d.id][fromTo].width,
_params[d.id][toFrom].width,
_params[d.id][fromTo].width,
steps,
'px'
);
})
.styleTween('fill-opacity', function(d) {
return ratchetyInterpolator(
params[d.id][toFrom].opacity,
params[d.id][fromTo].opacity,
_params[d.id][toFrom].opacity,
_params[d.id][fromTo].opacity,
steps
);
})
.styleTween('r', function(d) {
return ratchetyInterpolator(
params[d.id][toFrom].width,
params[d.id][fromTo].width,
_params[d.id][toFrom].width,
_params[d.id][fromTo].width,
steps,
'px'
);
@@ -84,10 +84,11 @@ export function behaviorBreathe() {
selection
.call(reset)
.each(function(d) {
var s = d3_select(this),
tag = s.node().tagName,
p = {'from': {}, 'to': {}},
opacity, width;
var s = d3_select(this);
var tag = s.node().tagName;
var p = {'from': {}, 'to': {}};
var opacity;
var width;
// determine base opacity and width
if (tag === 'circle') {
@@ -104,28 +105,28 @@ export function behaviorBreathe() {
p.to.opacity = opacity * 1.25;
p.from.width = width * 0.7;
p.to.width = width * (tag === 'circle' ? 1.5 : 1);
params[d.id] = p;
_params[d.id] = p;
});
}
function run(surface, fromTo) {
var toFrom = (fromTo === 'from' ? 'to' : 'from'),
currSelected = surface.selectAll(selector),
currClassed = surface.attr('class');
var toFrom = (fromTo === 'from' ? 'to' : 'from');
var currSelected = surface.selectAll(selector);
var currClassed = surface.attr('class');
if (done || currSelected.empty()) {
selected.call(reset);
if (_done || currSelected.empty()) {
_selected.call(reset);
return;
}
if (!_isEqual(currSelected.data(), selected.data()) || currClassed !== classed) {
selected.call(reset);
classed = currClassed;
selected = currSelected.call(calcAnimationParams);
if (!_isEqual(currSelected.data(), _selected.data()) || currClassed !== _classed) {
_selected.call(reset);
_classed = currClassed;
_selected = currSelected.call(calcAnimationParams);
}
selected
_selected
.transition()
.duration(duration)
.call(setAnimationParams, fromTo)
@@ -136,26 +137,26 @@ export function behaviorBreathe() {
var breathe = function(surface) {
done = false;
timer = d3_timer(function() {
_done = false;
_timer = d3_timer(function() {
// wait for elements to actually become selected
if (surface.selectAll(selector).empty()) {
return false;
}
surface.call(run, 'from');
timer.stop();
_timer.stop();
return true;
}, 20);
};
breathe.off = function() {
done = true;
if (timer) {
timer.stop();
_done = true;
if (_timer) {
_timer.stop();
}
selected
_selected
.interrupt()
.call(reset);
};

View File

@@ -23,8 +23,8 @@ export function behaviorCopy(context) {
function getDescendants(id, graph, descendants) {
var entity = graph.entity(id),
i, children;
var entity = graph.entity(id);
var children;
descendants = descendants || {};
@@ -36,7 +36,7 @@ export function behaviorCopy(context) {
children = [];
}
for (i = 0; i < children.length; i++) {
for (var i = 0; i < children.length; i++) {
if (!descendants[children[i]]) {
descendants[children[i]] = true;
descendants = getDescendants(children[i], graph, descendants);
@@ -50,11 +50,12 @@ export function behaviorCopy(context) {
function doCopy() {
if (!getSelectionText()) d3_event.preventDefault();
var graph = context.graph(),
selected = groupEntities(context.selectedIDs(), graph),
canCopy = [],
skip = {},
i, entity;
var graph = context.graph();
var selected = groupEntities(context.selectedIDs(), graph);
var canCopy = [];
var skip = {};
var entity;
var i;
for (i = 0; i < selected.relation.length; i++) {
entity = selected.relation[i];

View File

@@ -120,7 +120,7 @@ export function behaviorDrawWay(context, wayId, index, mode, startGraph) {
for (var i = 0; i < parents.length; i++) {
var parent = parents[i];
var nodes = parent.nodes.map(function(nodeID) { return graph.entity(nodeID); });
if (origWay.isClosed()) { // Check if Area
if (finishDraw) {
nodes.splice(-2, 1);

View File

@@ -54,9 +54,9 @@ export function behaviorLasso(context) {
function lassoed() {
if (!lasso) return [];
var graph = context.graph(),
bounds = lasso.extent().map(context.projection.invert),
extent = geoExtent(normalize(bounds[0], bounds[1]));
var graph = context.graph();
var bounds = lasso.extent().map(context.projection.invert);
var extent = geoExtent(normalize(bounds[0], bounds[1]));
return _map(context.intersects(extent).filter(function(entity) {
return entity.type === 'node' &&

View File

@@ -9,7 +9,8 @@ import { uiFlash } from '../ui';
/* Creates a keybinding behavior for an operation */
export function behaviorOperation() {
var _operation, keybinding;
var keybinding;
var _operation;
var behavior = function () {
if (_operation && _operation.available()) {

View File

@@ -15,8 +15,9 @@ import {
import {
geoExtent,
geoPointInPolygon
} from '../geo/';
geoPointInPolygon,
geoVecSubtract
} from '../geo';
import { modeMove } from '../modes';
import { uiCmd } from '../ui';
@@ -29,17 +30,17 @@ export function behaviorPaste(context) {
function doPaste() {
d3_event.preventDefault();
var baseGraph = context.graph(),
mouse = context.mouse(),
projection = context.projection,
viewport = geoExtent(projection.clipExtent()).polygon();
var baseGraph = context.graph();
var mouse = context.mouse();
var projection = context.projection;
var viewport = geoExtent(projection.clipExtent()).polygon();
if (!geoPointInPolygon(mouse, viewport)) return;
var extent = geoExtent(),
oldIDs = context.copyIDs(),
oldGraph = context.copyGraph(),
newIDs = [];
var extent = geoExtent();
var oldIDs = context.copyIDs();
var oldGraph = context.copyGraph();
var newIDs = [];
if (!oldIDs.length) return;
@@ -49,14 +50,14 @@ export function behaviorPaste(context) {
var copies = action.copies();
var originals = _invert(_mapValues(copies, 'id'));
for (var id in copies) {
var oldEntity = oldGraph.entity(id),
newEntity = copies[id];
var oldEntity = oldGraph.entity(id);
var newEntity = copies[id];
extent._extend(oldEntity.extent(oldGraph));
// Exclude child nodes from newIDs if their parent way was also copied.
var parents = context.graph().parentWays(newEntity),
parentCopied = false;
var parents = context.graph().parentWays(newEntity);
var parentCopied = false;
for (var i = 0; i < parents.length; i++) {
if (originals[parents[i].id]) {
parentCopied = true;
@@ -70,8 +71,8 @@ export function behaviorPaste(context) {
}
// Put pasted objects where mouse pointer is..
var center = projection(extent.center()),
delta = [ mouse[0] - center[0], mouse[1] - center[1] ];
var center = projection(extent.center());
var delta = geoVecSubtract(mouse, center);
context.perform(actionMove(newIDs, delta, projection));
context.enter(modeMove(context, newIDs, baseGraph));

View File

@@ -8,15 +8,15 @@ import { utilGetDimensions } from '../util/dimensions';
export function behaviorTail() {
var text,
container,
xmargin = 25,
tooltipSize = [0, 0],
selectionSize = [0, 0];
var container;
var xmargin = 25;
var tooltipSize = [0, 0];
var selectionSize = [0, 0];
var _text;
function tail(selection) {
if (!text) return;
if (!_text) return;
d3_select(window)
.on('resize.tail', function() { selectionSize = utilGetDimensions(selection); });
@@ -27,7 +27,7 @@ export function behaviorTail() {
.attr('class', 'tail tooltip-inner');
container.append('div')
.text(text);
.text(_text);
selection
.on('mousemove.tail', mousemove)
@@ -72,7 +72,7 @@ export function behaviorTail() {
tail.off = function(selection) {
if (!text) return;
if (!_text) return;
container
.on('mousemove.tail', null)
@@ -88,9 +88,9 @@ export function behaviorTail() {
};
tail.text = function(_) {
if (!arguments.length) return text;
text = _;
tail.text = function(val) {
if (!arguments.length) return _text;
_text = val;
return tail;
};

View File

@@ -20,11 +20,12 @@ export function modeAddArea(context) {
};
var behavior = behaviorAddWay(context)
.tail(t('modes.add_area.tail'))
.on('start', start)
.on('startFromWay', startFromWay)
.on('startFromNode', startFromNode),
defaultTags = { area: 'yes' };
.tail(t('modes.add_area.tail'))
.on('start', start)
.on('startFromWay', startFromWay)
.on('startFromNode', startFromNode);
var defaultTags = { area: 'yes' };
function actionClose(wayId) {
@@ -35,9 +36,9 @@ export function modeAddArea(context) {
function start(loc) {
var startGraph = context.graph(),
node = osmNode({ loc: loc }),
way = osmWay({ tags: defaultTags });
var startGraph = context.graph();
var node = osmNode({ loc: loc });
var way = osmWay({ tags: defaultTags });
context.perform(
actionAddEntity(node),
@@ -51,9 +52,9 @@ export function modeAddArea(context) {
function startFromWay(loc, edge) {
var startGraph = context.graph(),
node = osmNode({ loc: loc }),
way = osmWay({ tags: defaultTags });
var startGraph = context.graph();
var node = osmNode({ loc: loc });
var way = osmWay({ tags: defaultTags });
context.perform(
actionAddEntity(node),
@@ -68,8 +69,8 @@ export function modeAddArea(context) {
function startFromNode(node) {
var startGraph = context.graph(),
way = osmWay({ tags: defaultTags });
var startGraph = context.graph();
var way = osmWay({ tags: defaultTags });
context.perform(
actionAddEntity(way),

View File

@@ -27,9 +27,9 @@ export function modeAddLine(context) {
function start(loc) {
var startGraph = context.graph(),
node = osmNode({ loc: loc }),
way = osmWay();
var startGraph = context.graph();
var node = osmNode({ loc: loc });
var way = osmWay();
context.perform(
actionAddEntity(node),
@@ -42,9 +42,9 @@ export function modeAddLine(context) {
function startFromWay(loc, edge) {
var startGraph = context.graph(),
node = osmNode({ loc: loc }),
way = osmWay();
var startGraph = context.graph();
var node = osmNode({ loc: loc });
var way = osmWay();
context.perform(
actionAddEntity(node),
@@ -58,8 +58,8 @@ export function modeAddLine(context) {
function startFromNode(node) {
var startGraph = context.graph(),
way = osmWay();
var startGraph = context.graph();
var way = osmWay();
context.perform(
actionAddEntity(way),

View File

@@ -6,7 +6,6 @@ import { services } from '../services';
export function modeAddNote(context) {
var mode = {
id: 'add-note',
button: 'note',