From 579d6325636938443f34837fc05dd856db65539f Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 31 Jan 2013 17:19:01 -0500 Subject: [PATCH] Hoist functions up a scope --- js/id/modes/add_area.js | 120 +++++++++++++++++------------------ js/id/modes/add_line.js | 134 +++++++++++++++++++-------------------- js/id/modes/add_point.js | 62 +++++++++--------- 3 files changed, 155 insertions(+), 161 deletions(-) diff --git a/js/id/modes/add_area.js b/js/id/modes/add_area.js index ae2e008ee..0a9496bb6 100644 --- a/js/id/modes/add_area.js +++ b/js/id/modes/add_area.js @@ -7,71 +7,69 @@ iD.modes.AddArea = function(context) { key: t('modes.add_area.key') }; - var behavior, - defaultTags = {area: 'yes'}; - - mode.enter = function() { - function start(loc) { - var graph = context.graph(), - node = iD.Node({loc: loc}), - way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddEntity(node), - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id), - iD.actions.AddVertex(way.id, node.id)); - - context.enter(iD.modes.DrawArea(context, way.id, graph)); - } - - function startFromWay(other, loc, index) { - var graph = context.graph(), - node = iD.Node({loc: loc}), - way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddEntity(node), - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id), - iD.actions.AddVertex(way.id, node.id), - iD.actions.AddVertex(other.id, node.id, index)); - - context.enter(iD.modes.DrawArea(context, way.id, graph)); - } - - function startFromNode(node) { - var graph = context.graph(), - way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id), - iD.actions.AddVertex(way.id, node.id)); - - context.enter(iD.modes.DrawArea(context, way.id, graph)); - } - - function startFromMidpoint(midpoint) { - var graph = context.graph(), - node = iD.Node(), - way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddMidpoint(midpoint, node), - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id), - iD.actions.AddVertex(way.id, node.id)); - - context.enter(iD.modes.DrawArea(context, way.id, graph)); - } - - behavior = iD.behavior.AddWay(context) + var behavior = iD.behavior.AddWay(context) .on('start', start) .on('startFromWay', startFromWay) .on('startFromNode', startFromNode) - .on('startFromMidpoint', startFromMidpoint); + .on('startFromMidpoint', startFromMidpoint), + defaultTags = {area: 'yes'}; + function start(loc) { + var graph = context.graph(), + node = iD.Node({loc: loc}), + way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddEntity(node), + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id), + iD.actions.AddVertex(way.id, node.id)); + + context.enter(iD.modes.DrawArea(context, way.id, graph)); + } + + function startFromWay(other, loc, index) { + var graph = context.graph(), + node = iD.Node({loc: loc}), + way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddEntity(node), + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id), + iD.actions.AddVertex(way.id, node.id), + iD.actions.AddVertex(other.id, node.id, index)); + + context.enter(iD.modes.DrawArea(context, way.id, graph)); + } + + function startFromNode(node) { + var graph = context.graph(), + way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id), + iD.actions.AddVertex(way.id, node.id)); + + context.enter(iD.modes.DrawArea(context, way.id, graph)); + } + + function startFromMidpoint(midpoint) { + var graph = context.graph(), + node = iD.Node(), + way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddMidpoint(midpoint, node), + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id), + iD.actions.AddVertex(way.id, node.id)); + + context.enter(iD.modes.DrawArea(context, way.id, graph)); + } + + mode.enter = function() { context.install(behavior); context.tail(t('modes.add_area.tail')); }; diff --git a/js/id/modes/add_line.js b/js/id/modes/add_line.js index 79c5f3031..7845e6e6f 100644 --- a/js/id/modes/add_line.js +++ b/js/id/modes/add_line.js @@ -7,78 +7,76 @@ iD.modes.AddLine = function(context) { key: t('modes.add_line.key') }; - var behavior, - defaultTags = {highway: 'residential'}; - - mode.enter = function() { - function start(loc) { - var graph = context.graph(), - node = iD.Node({loc: loc}), - way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddEntity(node), - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id)); - - context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); - } - - function startFromWay(other, loc, index) { - var graph = context.graph(), - node = iD.Node({loc: loc}), - way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddEntity(node), - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id), - iD.actions.AddVertex(other.id, node.id, index)); - - context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); - } - - function startFromNode(node) { - var graph = context.graph(), - parent = graph.parentWays(node)[0], - isLine = parent && parent.geometry(graph) === 'line'; - - if (isLine && parent.first() === node.id) { - context.enter(iD.modes.DrawLine(context, parent.id, 'backward', graph)); - - } else if (isLine && parent.last() === node.id) { - context.enter(iD.modes.DrawLine(context, parent.id, 'forward', graph)); - - } else { - var way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id)); - - context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); - } - } - - function startFromMidpoint(midpoint) { - var graph = context.graph(), - node = iD.Node(), - way = iD.Way({tags: defaultTags}); - - context.perform( - iD.actions.AddMidpoint(midpoint, node), - iD.actions.AddEntity(way), - iD.actions.AddVertex(way.id, node.id)); - - context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); - } - - behavior = iD.behavior.AddWay(context) + var behavior = iD.behavior.AddWay(context) .on('start', start) .on('startFromWay', startFromWay) .on('startFromNode', startFromNode) - .on('startFromMidpoint', startFromMidpoint); + .on('startFromMidpoint', startFromMidpoint), + defaultTags = {highway: 'residential'}; + function start(loc) { + var graph = context.graph(), + node = iD.Node({loc: loc}), + way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddEntity(node), + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id)); + + context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); + } + + function startFromWay(other, loc, index) { + var graph = context.graph(), + node = iD.Node({loc: loc}), + way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddEntity(node), + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id), + iD.actions.AddVertex(other.id, node.id, index)); + + context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); + } + + function startFromNode(node) { + var graph = context.graph(), + parent = graph.parentWays(node)[0], + isLine = parent && parent.geometry(graph) === 'line'; + + if (isLine && parent.first() === node.id) { + context.enter(iD.modes.DrawLine(context, parent.id, 'backward', graph)); + + } else if (isLine && parent.last() === node.id) { + context.enter(iD.modes.DrawLine(context, parent.id, 'forward', graph)); + + } else { + var way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id)); + + context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); + } + } + + function startFromMidpoint(midpoint) { + var graph = context.graph(), + node = iD.Node(), + way = iD.Way({tags: defaultTags}); + + context.perform( + iD.actions.AddMidpoint(midpoint, node), + iD.actions.AddEntity(way), + iD.actions.AddVertex(way.id, node.id)); + + context.enter(iD.modes.DrawLine(context, way.id, 'forward', graph)); + } + + mode.enter = function() { context.install(behavior); context.tail(t('modes.add_line.tail')); }; diff --git a/js/id/modes/add_point.js b/js/id/modes/add_point.js index c3dc7f075..5a24dad25 100644 --- a/js/id/modes/add_point.js +++ b/js/id/modes/add_point.js @@ -6,46 +6,44 @@ iD.modes.AddPoint = function(context) { key: t('modes.add_point.key') }; - var behavior; + var behavior = iD.behavior.Draw(context) + .on('click', add) + .on('clickWay', addWay) + .on('clickNode', addNode) + .on('clickMidpoint', addNode) + .on('cancel', cancel) + .on('finish', cancel); + + function add(loc) { + var node = iD.Node({loc: loc}); + + context.perform( + iD.actions.AddEntity(node), + t('operations.add.annotation.point')); + + context.enter(iD.modes.Select(context, [node.id], true)); + } + + function addWay(way, loc, index) { + add(loc); + } + + function addNode(node) { + add(node.loc); + } + + function cancel() { + context.enter(iD.modes.Browse(context)); + } mode.enter = function() { - function add(loc) { - var node = iD.Node({loc: loc}); - - context.perform( - iD.actions.AddEntity(node), - t('operations.add.annotation.point')); - - context.enter(iD.modes.Select(context, [node.id], true)); - } - - function addWay(way, loc, index) { - add(loc); - } - - function addNode(node) { - add(node.loc); - } - - function cancel() { - context.enter(iD.modes.Browse(context)); - } - - behavior = iD.behavior.Draw(context) - .on('click', add) - .on('clickWay', addWay) - .on('clickNode', addNode) - .on('clickMidpoint', addNode) - .on('cancel', cancel) - .on('finish', cancel); - context.install(behavior); context.tail(t('modes.add_point.tail')); }; mode.exit = function() { - context.tail(false); context.uninstall(behavior); + context.tail(false); }; return mode;