From e3775844c83e86afcf0d29bae73f72fe428182fd Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Wed, 16 Jan 2013 12:52:06 -0500 Subject: [PATCH] Refactor lines, keep non-scoped fns out of scope --- js/id/svg/lines.js | 54 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/js/id/svg/lines.js b/js/id/svg/lines.js index 925d8c968..02f33a707 100644 --- a/js/id/svg/lines.js +++ b/js/id/svg/lines.js @@ -1,16 +1,38 @@ iD.svg.Lines = function() { - var arrowtext = '►\u3000\u3000', - alength; + var arrowtext = '►\u3000\u3000', + alength; + + function drawPaths(group, lines, filter, classes, lineString) { + var paths = group.selectAll('path') + .filter(filter) + .data(lines, iD.Entity.key); + + paths.enter() + .append('path') + .attr('class', classes); + + paths + .order() + .attr('d', lineString) + .call(iD.svg.TagClasses()); + + paths.exit() + .remove(); + + return paths; + } return function(surface, graph, entities, filter, projection) { + if (!alength) { var arrow = surface.append('text').text(arrowtext); alength = arrow.node().getComputedTextLength(); arrow.remove(); } - var lines = []; + var lines = [], + lineStrings = {}; for (var i = 0; i < entities.length; i++) { var entity = entities[i]; @@ -19,8 +41,6 @@ iD.svg.Lines = function() { } } - var lineStrings = {}; - function lineString(entity) { if (lineStrings[entity.id] !== undefined) { return lineStrings[entity.id]; @@ -31,32 +51,12 @@ iD.svg.Lines = function() { 'M' + nodes.map(iD.svg.RoundProjection(projection)).join('L')); } - function drawPaths(group, lines, filter, classes) { - var paths = group.selectAll('path') - .filter(filter) - .data(lines, iD.Entity.key); - - paths.enter() - .append('path') - .attr('class', classes); - - paths - .order() - .attr('d', lineString) - .call(iD.svg.TagClasses()); - - paths.exit() - .remove(); - - return paths; - } - var casing = surface.select('.layer-casing'), stroke = surface.select('.layer-stroke'), defs = surface.select('defs'), text = surface.select('.layer-text'), - casings = drawPaths(casing, lines, filter, 'way line casing'), - strokes = drawPaths(stroke, lines, filter, 'way line stroke'); + casings = drawPaths(casing, lines, filter, 'way line casing', lineString), + strokes = drawPaths(stroke, lines, filter, 'way line stroke', lineString); // Determine the lengths of oneway paths var lengths = {},