From c1a87ed8b61aa71af0391f3fa97d8c4cb6921d5d Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Wed, 28 Nov 2012 13:59:28 -0500 Subject: [PATCH] Work on road drawing, index ways on demand --- js/id/actions/modes.js | 75 ++++++++++++++++++++---------------------- js/id/graph/entity.js | 2 +- js/id/graph/graph.js | 34 +++++++++---------- js/id/id.js | 2 +- js/id/renderer/map.js | 2 +- 5 files changed, 56 insertions(+), 59 deletions(-) diff --git a/js/id/actions/modes.js b/js/id/actions/modes.js index 17302e165..3e56ace86 100644 --- a/js/id/actions/modes.js +++ b/js/id/actions/modes.js @@ -63,9 +63,8 @@ iD.modes.AddRoad = { .append('g').attr('id', 'addroad'); teaser.append('circle') - .attr('class', 'handle') - .style('pointer-events', 'none') - .attr('r', 3); + .attr({ 'class': 'handle', r: 3 }) + .style('pointer-events', 'none'); surface.on('mousemove.addroad', function() { teaser.attr('transform', function() { @@ -75,9 +74,9 @@ iD.modes.AddRoad = { }); surface.on('click.addroad', function() { - var t = d3.select(d3.event.target); - var node; - var way = this.way(); + var t = d3.select(d3.event.target), + node, + way = this.way(); // connect a way to an existing way if (t.data() && t.data()[0] && t.data()[0].type === 'node') { @@ -87,10 +86,11 @@ iD.modes.AddRoad = { d3.mouse(surface.node()))); } + this.map.perform(iD.actions.startWay(way)); way.nodes.push(node.id); this.map.perform(iD.actions.changeWayNodes(way, node)); this.map.selectClick(way); - this.controller.enter(iD.modes.DrawRoad(way)); + this.controller.enter(iD.modes.DrawRoad(way.id)); }.bind(this)); d3.select(document).on('keydown.addroad', function() { @@ -105,48 +105,45 @@ iD.modes.AddRoad = { } }; -iD.modes.DrawRoad = function(way) { +iD.modes.DrawRoad = function(way_id) { return { enter: function() { - var surface = this.map.surface; - var lastNode = this.map.history.graph().entity(way.nodes[way.nodes.length - 1]); - this.nextnode = iD.modes._node([lastNode.lon, lastNode.lat]); - way.nodes.push(this.nextnode.id); - this.map.perform(iD.actions.changeWayNodes(way, this.nextnode)); + var surface = this.map.surface, + + nextnode = iD.modes._node([NaN, NaN]); + var nextnode_id = nextnode.id; + + var way = this.map.history.graph().entity(way_id); + way.nodes.push(nextnode_id); + this.map.perform(iD.actions.changeWayNodes(way, nextnode)); surface.on('mousemove.drawroad', function() { var ll = this.map.projection.invert(d3.mouse(surface.node())); - this.map.history.replace(iD.actions.move(this.nextnode, ll)); - this.map.update(); + var way = this.map.history.graph().entity(way_id); + var node = iD.Entity(this.map.history.graph().entity(nextnode_id), { + lon: ll[0], + lat: ll[1] + }); + this.map.history.replace(iD.actions.changeWayNodes(way, node)); + var only = iD.Util.trueObj([way.id].concat(_.pluck(way.nodes, 'id'))); + this.map.redraw(only); }.bind(this)); surface.on('click.drawroad', function() { - if (this._doubleTime) { - window.clearTimeout(this._doubleTime); - this._doubleTime = null; + var t = d3.select(d3.event.target); + d3.event.stopPropagation(); + // connect a way to an existing way + if (t.data() && t.data()[0] && t.data()[0].type === 'node') { + node = t.data()[0]; } else { - // forgive me - var that = this; - this._doubleTime = window.setTimeout(function(e) { - return function() { - d3.event = e; - var t = d3.select(d3.event.target); - d3.event.stopPropagation(); - // connect a way to an existing way - if (t.data() && t.data()[0] && t.data()[0].type === 'node') { - node = t.data()[0]; - } else { - node = iD.modes._node(that.map.projection.invert( - d3.mouse(surface.node()))); - } - way.nodes.pop(); - way.nodes.push(node.id); - that.map.perform(iD.actions.changeWayNodes(way, node)); - way.nodes = way.nodes.slice(); - that.controller.enter(iD.modes.DrawRoad(way)); - }; - }(_.clone(d3.event)), 150); + node = iD.modes._node(this.map.projection.invert( + d3.mouse(surface.node()))); } + way.nodes.pop(); + way.nodes.push(node.id); + this.map.perform(iD.actions.changeWayNodes(way, node)); + way.nodes = way.nodes.slice(); + this.controller.enter(iD.modes.DrawRoad(way)); }.bind(this)); surface.on('dblclick.drawroad', function() { diff --git a/js/id/graph/entity.js b/js/id/graph/entity.js index 766cf8ab2..1c908cb65 100644 --- a/js/id/graph/entity.js +++ b/js/id/graph/entity.js @@ -38,7 +38,7 @@ iD.Node = function (attrs) { }; iD.Way = function (attrs) { - return iD.Entity(_.extend({}, attrs, {type: 'way'})); + return iD.Entity(_.extend({}, attrs, {type: 'way', nodes: []})); }; iD.Relation = function (attrs) { diff --git a/js/id/graph/graph.js b/js/id/graph/graph.js index 96d28a8ed..952bb7e76 100644 --- a/js/id/graph/graph.js +++ b/js/id/graph/graph.js @@ -4,22 +4,6 @@ iD.Graph = function(entities, annotation) { this.entities = entities || {}; this.annotation = annotation; - for (var id in this.entities) { - // TODO: update extents that need it. - if (this.entities[id].type === 'way' && !this.entities[id]._extent) { - // top left, bottom right - var extent = [[-Infinity, Infinity], [Infinity, -Infinity]]; - var w = this.fetch(id); - for (var j = 0, l = w.nodes.length; j < l; j++) { - if (w.nodes[j].lon > extent[0][0]) extent[0][0] = w.nodes[j].lon; - if (w.nodes[j].lon < extent[1][0]) extent[1][0] = w.nodes[j].lon; - if (w.nodes[j].lat < extent[0][1]) extent[0][1] = w.nodes[j].lat; - if (w.nodes[j].lat > extent[1][1]) extent[1][1] = w.nodes[j].lat; - } - this.entities[id]._extent = extent; - } - } - if (iD.debug) { Object.freeze(this); Object.freeze(this.entities); @@ -72,6 +56,22 @@ iD.Graph.prototype = { entity._extent[1][1] > extent[1][1]; }, + indexWay: function(way) { + if (way.type === 'way' && !way._extent) { + // top left, bottom right + var extent = [[-Infinity, Infinity], [Infinity, -Infinity]]; + var w = way; + for (var j = 0, l = w.nodes.length; j < l; j++) { + if (w.nodes[j].lon > extent[0][0]) extent[0][0] = w.nodes[j].lon; + if (w.nodes[j].lon < extent[1][0]) extent[1][0] = w.nodes[j].lon; + if (w.nodes[j].lat < extent[0][1]) extent[0][1] = w.nodes[j].lat; + if (w.nodes[j].lat > extent[1][1]) extent[1][1] = w.nodes[j].lat; + } + way._extent = extent; + } + return true; + }, + // get all objects that intersect an extent. intersects: function(extent) { var items = []; @@ -81,7 +81,7 @@ iD.Graph.prototype = { items.push(entity); } else if (entity.type === 'way') { var w = this.fetch(entity.id); - if (this.wayIntersect(w, extent)) items.push(w); + if (this.indexWay(w) && this.wayIntersect(w, extent)) items.push(w); } } return items; diff --git a/js/id/id.js b/js/id/id.js index b94bd054c..793c8bd5f 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -73,7 +73,7 @@ var iD = function(container) { var l = iD.loading('committing changes to openstreetmap'); connection.putChangeset(map.history.changes(), e.comment, function() { l.remove(); - map.history = new iD.History(); + map.history = iD.History(); map.flush(); map.update(); map.redraw(); diff --git a/js/id/renderer/map.js b/js/id/renderer/map.js index 740f225ca..42bd100e0 100644 --- a/js/id/renderer/map.js +++ b/js/id/renderer/map.js @@ -424,7 +424,7 @@ iD.Map = function(elem, connection) { } function update() { - map.update(); + // map.update(); redraw(); }