mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-15 21:48:20 +02:00
Pass around context rather than map
This commit is contained in:
@@ -29,7 +29,7 @@ iD.behavior.Draw = function(context) {
|
||||
function click() {
|
||||
var d = datum();
|
||||
if (d.type === 'way') {
|
||||
var choice = iD.geo.chooseIndex(d, d3.mouse(context.surface().node()), context.map());
|
||||
var choice = iD.geo.chooseIndex(d, d3.mouse(context.surface().node()), context);
|
||||
event.clickWay(d, choice.loc, choice.index);
|
||||
|
||||
} else if (d.type === 'node') {
|
||||
|
||||
@@ -19,7 +19,7 @@ iD.behavior.DrawWay = function(context, wayId, index, mode, baseGraph) {
|
||||
if (datum.type === 'node' || datum.type === 'midpoint') {
|
||||
loc = datum.loc;
|
||||
} else if (datum.type === 'way') {
|
||||
loc = iD.geo.chooseIndex(datum, d3.mouse(context.surface().node()), context.map()).loc;
|
||||
loc = iD.geo.chooseIndex(datum, d3.mouse(context.surface().node()), context).loc;
|
||||
}
|
||||
|
||||
context.replace(iD.actions.MoveNode(nodeId, loc));
|
||||
|
||||
+3
-3
@@ -14,11 +14,11 @@ iD.geo.dist = function(a, b) {
|
||||
Math.pow(a[1] - b[1], 2));
|
||||
};
|
||||
|
||||
iD.geo.chooseIndex = function(way, point, map) {
|
||||
iD.geo.chooseIndex = function(way, point, context) {
|
||||
var dist = iD.geo.dist,
|
||||
graph = map.history().graph(),
|
||||
graph = context.graph(),
|
||||
nodes = graph.childNodes(way),
|
||||
projNodes = nodes.map(function(n) { return map.projection(n.loc); });
|
||||
projNodes = nodes.map(function(n) { return context.projection(n.loc); });
|
||||
|
||||
for (var i = 0, changes = []; i < projNodes.length - 1; i++) {
|
||||
changes[i] =
|
||||
|
||||
+2
-1
@@ -6,7 +6,7 @@ window.iD = function () {
|
||||
mode,
|
||||
container,
|
||||
ui = iD.ui(context),
|
||||
map = iD.Map().connection(connection).history(history);
|
||||
map = iD.Map(context);
|
||||
|
||||
/* Straight accessors. Avoid using these if you can. */
|
||||
context.ui = function () { return ui; };
|
||||
@@ -62,6 +62,7 @@ window.iD = function () {
|
||||
context.surface = function () { return map.surface; };
|
||||
context.projection = map.projection;
|
||||
context.tail = map.tail;
|
||||
context.redraw = map.redraw;
|
||||
|
||||
context.container = function (_) {
|
||||
if (!arguments.length) return container;
|
||||
|
||||
@@ -108,7 +108,7 @@ iD.modes.Select = function(context, selection, initial) {
|
||||
|
||||
if (datum instanceof iD.Way && !target.classed('fill')) {
|
||||
var choice = iD.geo.chooseIndex(datum,
|
||||
d3.mouse(context.surface().node()), context.map()),
|
||||
d3.mouse(context.surface().node()), context),
|
||||
node = iD.Node({ loc: choice.loc });
|
||||
|
||||
context.perform(
|
||||
|
||||
+13
-22
@@ -1,6 +1,5 @@
|
||||
iD.Map = function() {
|
||||
var connection, history,
|
||||
dimensions = [],
|
||||
iD.Map = function(context) {
|
||||
var dimensions = [],
|
||||
dispatch = d3.dispatch('move', 'drawn'),
|
||||
projection = d3.geo.mercator().scale(1024),
|
||||
roundedProjection = iD.svg.RoundProjection(projection),
|
||||
@@ -27,6 +26,12 @@ iD.Map = function() {
|
||||
surface, tilegroup;
|
||||
|
||||
function map(selection) {
|
||||
context.connection()
|
||||
.on('load.tile', connectionLoad);
|
||||
|
||||
context.history()
|
||||
.on('change.map', redraw);
|
||||
|
||||
selection.call(zoom);
|
||||
|
||||
tilegroup = selection.append('div')
|
||||
@@ -57,7 +62,7 @@ iD.Map = function() {
|
||||
function drawVector(difference) {
|
||||
var filter, all,
|
||||
extent = map.extent(),
|
||||
graph = history.graph();
|
||||
graph = context.graph();
|
||||
|
||||
function addParents(parents) {
|
||||
for (var i = 0; i < parents.length; i++) {
|
||||
@@ -121,7 +126,7 @@ iD.Map = function() {
|
||||
}
|
||||
|
||||
function connectionLoad(err, result) {
|
||||
history.merge(result);
|
||||
context.history().merge(result);
|
||||
redraw(Object.keys(result));
|
||||
}
|
||||
|
||||
@@ -182,7 +187,7 @@ iD.Map = function() {
|
||||
tilegroup.call(background);
|
||||
|
||||
if (map.editable()) {
|
||||
connection.loadTiles(projection, dimensions);
|
||||
context.connection().loadTiles(projection, dimensions);
|
||||
drawVector(difference);
|
||||
} else {
|
||||
editOff();
|
||||
@@ -347,15 +352,8 @@ iD.Map = function() {
|
||||
};
|
||||
|
||||
map.flush = function () {
|
||||
connection.flush();
|
||||
history.reset();
|
||||
return map;
|
||||
};
|
||||
|
||||
map.connection = function(_) {
|
||||
if (!arguments.length) return connection;
|
||||
connection = _;
|
||||
connection.on('load.tile', connectionLoad);
|
||||
context.connection().flush();
|
||||
context.history().reset();
|
||||
return map;
|
||||
};
|
||||
|
||||
@@ -378,13 +376,6 @@ iD.Map = function() {
|
||||
return map;
|
||||
};
|
||||
|
||||
map.history = function (_) {
|
||||
if (!arguments.length) return history;
|
||||
history = _;
|
||||
history.on('change.map', redraw);
|
||||
return map;
|
||||
};
|
||||
|
||||
map.background = background;
|
||||
map.projection = projection;
|
||||
map.redraw = redraw;
|
||||
|
||||
+2
-2
@@ -66,7 +66,7 @@ iD.ui = function (context) {
|
||||
|
||||
map.on('move.editor', _.debounce(function() {
|
||||
disableTooHigh();
|
||||
contributors.call(iD.ui.contributors(map));
|
||||
contributors.call(iD.ui.contributors(context));
|
||||
}, 500));
|
||||
|
||||
buttons.append('span')
|
||||
@@ -131,7 +131,7 @@ iD.ui = function (context) {
|
||||
.call(iD.ui.geocoder().map(map));
|
||||
|
||||
container.append('div').attr('class', 'map-control layerswitcher-control')
|
||||
.call(iD.ui.layerswitcher(map));
|
||||
.call(iD.ui.layerswitcher(context));
|
||||
|
||||
container.append('div')
|
||||
.style('display', 'none')
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
iD.ui.commit = function(map) {
|
||||
iD.ui.commit = function(context) {
|
||||
var event = d3.dispatch('cancel', 'save', 'fix');
|
||||
|
||||
function zipSame(d) {
|
||||
@@ -89,7 +89,7 @@ iD.ui.commit = function(map) {
|
||||
cancelbutton.append('span').attr('class','label').text('Cancel');
|
||||
|
||||
var warnings = body.selectAll('div.warning-section')
|
||||
.data(iD.validate(changes, map.history().graph()))
|
||||
.data(iD.validate(changes, context.graph()))
|
||||
.enter()
|
||||
.append('div').attr('class', 'modal-section warning-section fillL');
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
iD.ui.contributors = function(map) {
|
||||
iD.ui.contributors = function(context) {
|
||||
|
||||
function contributors(selection) {
|
||||
|
||||
var users = {},
|
||||
limit = 3,
|
||||
entities = map.history().graph().intersects(map.extent());
|
||||
entities = context.graph().intersects(context.map().extent());
|
||||
|
||||
for (var i in entities) {
|
||||
if (entities[i].user) users[entities[i].user] = true;
|
||||
@@ -21,7 +21,7 @@ iD.ui.contributors = function(map) {
|
||||
|
||||
l.enter().append('a')
|
||||
.attr('class', 'user-link')
|
||||
.attr('href', function(d) { return map.connection().userUrl(d); })
|
||||
.attr('href', function(d) { return context.connection().userUrl(d); })
|
||||
.attr('target', '_blank')
|
||||
.text(String);
|
||||
|
||||
@@ -37,7 +37,7 @@ iD.ui.contributors = function(map) {
|
||||
.append('a')
|
||||
.attr('target', '_blank')
|
||||
.attr('href', function() {
|
||||
var ext = map.extent();
|
||||
var ext = context.map().extent();
|
||||
return 'http://www.openstreetmap.org/browse/changesets?bbox=' + [
|
||||
ext[0][0], ext[0][1],
|
||||
ext[1][0], ext[1][1]];
|
||||
|
||||
+10
-10
@@ -1,4 +1,4 @@
|
||||
iD.ui.layerswitcher = function(map) {
|
||||
iD.ui.layerswitcher = function(context) {
|
||||
var event = d3.dispatch('cancel', 'save'),
|
||||
sources = [{
|
||||
name: 'Bing',
|
||||
@@ -94,7 +94,7 @@ iD.ui.layerswitcher = function(map) {
|
||||
function selectLayer(d) {
|
||||
content.selectAll('a.layer')
|
||||
.classed('selected', function(d) {
|
||||
return d.source === map.background.source();
|
||||
return d.source === context.background().source();
|
||||
});
|
||||
d3.select('#attribution a')
|
||||
.attr('href', d.link)
|
||||
@@ -126,9 +126,9 @@ iD.ui.layerswitcher = function(map) {
|
||||
d.source = configured;
|
||||
d.name = 'Custom (configured)';
|
||||
}
|
||||
map.background.source(d.source);
|
||||
map.history().imagery_used(d.name);
|
||||
map.redraw();
|
||||
context.background().source(d.source);
|
||||
context.history().imagery_used(d.name);
|
||||
context.redraw();
|
||||
selectLayer(d);
|
||||
})
|
||||
.insert('span')
|
||||
@@ -145,8 +145,8 @@ iD.ui.layerswitcher = function(map) {
|
||||
['bottom', [0, 1]]];
|
||||
|
||||
function nudge(d) {
|
||||
map.background.nudge(d[1]);
|
||||
map.redraw();
|
||||
context.background().nudge(d[1]);
|
||||
context.redraw();
|
||||
}
|
||||
|
||||
adjustments.append('a')
|
||||
@@ -181,12 +181,12 @@ iD.ui.layerswitcher = function(map) {
|
||||
.text('reset')
|
||||
.attr('class', 'reset')
|
||||
.on('click', function() {
|
||||
map.background.offset([0, 0]);
|
||||
map.redraw();
|
||||
context.background().offset([0, 0]);
|
||||
context.redraw();
|
||||
});
|
||||
|
||||
selection.call(clickoutside);
|
||||
selectLayer(map.background.source());
|
||||
selectLayer(context.background().source());
|
||||
}
|
||||
|
||||
return d3.rebind(layerswitcher, event, 'on');
|
||||
|
||||
+2
-2
@@ -50,12 +50,12 @@ iD.ui.save = function(context) {
|
||||
modal.select('.content')
|
||||
.classed('commit-modal', true)
|
||||
.datum(changes)
|
||||
.call(iD.ui.commit(map)
|
||||
.call(iD.ui.commit(context)
|
||||
.on('cancel', function() {
|
||||
modal.remove();
|
||||
})
|
||||
.on('fix', function(d) {
|
||||
map.extent(d.entity.extent(map.history().graph()));
|
||||
map.extent(d.entity.extent(context.graph()));
|
||||
if (map.zoom() > 19) map.zoom(19);
|
||||
context.enter(iD.modes.Select(context, [d.entity.id]));
|
||||
modal.remove();
|
||||
|
||||
@@ -1,22 +1,10 @@
|
||||
describe('iD.Map', function() {
|
||||
var container, map;
|
||||
var map;
|
||||
|
||||
beforeEach(function() {
|
||||
container = d3.select('body').append('div');
|
||||
map = iD.Map();
|
||||
container.call(map);
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
container.remove();
|
||||
});
|
||||
|
||||
describe('#connection', function() {
|
||||
it('gets and sets connection', function() {
|
||||
var connection = iD.Connection();
|
||||
expect(map.connection(connection)).to.equal(map);
|
||||
expect(map.connection()).to.equal(connection);
|
||||
});
|
||||
map = iD().map();
|
||||
d3.select(document.createElement('div'))
|
||||
.call(map);
|
||||
});
|
||||
|
||||
describe('#zoom', function() {
|
||||
|
||||
Reference in New Issue
Block a user