diff --git a/js/id/modes/select.js b/js/id/modes/select.js index 331707f39..0838f1d1c 100644 --- a/js/id/modes/select.js +++ b/js/id/modes/select.js @@ -17,14 +17,6 @@ iD.modes.Select = function(context, selection, initial) { var wrap = context.container() .select('.inspector-wrap'); - function changeTags(d, tags) { - if (!_.isEqual(singular().tags, tags)) { - context.perform( - iD.actions.ChangeTags(d.id, tags), - t('operations.change_tags.annotation')); - } - } - function singular() { if (selection.length === 1) { return context.entity(selection[0]); @@ -57,8 +49,6 @@ iD.modes.Select = function(context, selection, initial) { }; mode.enter = function() { - var entity = singular(); - behaviors.forEach(function(behavior) { context.install(behavior); }); @@ -87,41 +77,20 @@ iD.modes.Select = function(context, selection, initial) { id: selection.join(',') }), true)); - if (entity) { + if (singular()) { wrap.call(inspector); - - if (d3.event) { - // Pan the map if the clicked feature intersects with the position - // of the inspector - var inspectorSize = wrap.size(), - mapSize = context.map().size(), - offset = 50, - shiftLeft = d3.event.clientX - mapSize[0] + inspectorSize[0] + offset, - center = (mapSize[0] / 2) + shiftLeft + offset; - - if (shiftLeft > 0 && inspectorSize[1] > d3.event.clientY) { - context.map().centerEase(context.projection.invert([center, mapSize[1]/2])); - } - } - - inspector - .on('changeTags', changeTags) - .on('close', function() { context.enter(iD.modes.Browse(context)); }); } context.history() - .on('undone.select', updateInspector) - .on('redone.select', updateInspector); + .on('undone.select', update) + .on('redone.select', update); - function updateInspector() { + function update() { context.surface().call(radialMenu.close); if (_.any(selection, function(id) { return !context.entity(id); })) { // Exit mode if selected entity gets undone context.enter(iD.modes.Browse(context)); - - } else if (singular()) { - inspector.tags(context.entity(selection[0]).tags); } } diff --git a/js/id/ui/inspector.js b/js/id/ui/inspector.js index d45de74c3..4606beb3b 100644 --- a/js/id/ui/inspector.js +++ b/js/id/ui/inspector.js @@ -1,6 +1,24 @@ iD.ui.Inspector = function(context, entity) { - var event = d3.dispatch('changeTags', 'close', 'change'), - tagEditor; + var tagEditor; + + function changeTags(tags) { + if (!_.isEqual(entity.tags, tags)) { + context.perform( + iD.actions.ChangeTags(entity.id, tags), + t('operations.change_tags.annotation')); + } + } + + function browse() { + context.enter(iD.modes.Browse(context)); + } + + function update() { + entity = context.entity(entity.id); + if (entity) { + tagEditor.tags(entity.tags); + } + } function inspector(selection) { selection @@ -24,9 +42,7 @@ iD.ui.Inspector = function(context, entity) { .classed('pane', true); var presetGrid = iD.ui.PresetGrid(context, entity) - .on('close', function() { - event.close(); - }) + .on('close', browse) .on('choose', function(preset) { panewrap .transition() @@ -37,12 +53,8 @@ iD.ui.Inspector = function(context, entity) { tagEditor = iD.ui.TagEditor(context, entity) .tags(entity.tags) - .on('changeTags', function(tags) { - event.changeTags(entity, tags); - }) - .on('close', function() { - event.close(entity); - }) + .on('changeTags', changeTags) + .on('close', browse) .on('choose', function() { panewrap .transition() @@ -60,6 +72,23 @@ iD.ui.Inspector = function(context, entity) { panewrap.style('right', '-0%'); tagLayer.call(tagEditor); } + + if (d3.event) { + // Pan the map if the clicked feature intersects with the position + // of the inspector + var inspectorSize = selection.size(), + mapSize = context.map().size(), + offset = 50, + shiftLeft = d3.event.clientX - mapSize[0] + inspectorSize[0] + offset, + center = (mapSize[0] / 2) + shiftLeft + offset; + + if (shiftLeft > 0 && inspectorSize[1] > d3.event.clientY) { + context.map().centerEase(context.projection.invert([center, mapSize[1]/2])); + } + } + + context.history() + .on('change.inspector', update); } inspector.close = function(selection) { @@ -74,12 +103,10 @@ iD.ui.Inspector = function(context, entity) { // Firefox incorrectly implements blur, so typeahead elements // are not correctly removed. Remove any stragglers manually. d3.selectAll('div.typeahead').remove(); + + context.history() + .on('change.inspector', null); }; - inspector.tags = function() { - tagEditor.tags.apply(this, arguments); - return inspector; - }; - - return d3.rebind(inspector, event, 'on'); + return inspector; }; diff --git a/test/spec/ui/inspector.js b/test/spec/ui/inspector.js index 29f072652..a437209ba 100644 --- a/test/spec/ui/inspector.js +++ b/test/spec/ui/inspector.js @@ -1,37 +1,2 @@ describe("iD.ui.Inspector", function () { - var inspector, element, - tags = {highway: 'residential'}, - entity, context; - - beforeEach(function () { - entity = iD.Entity({type: 'node', id: "n12345", tags: tags}); - context = iD(); - inspector = iD.ui.Inspector(context, entity); - element = d3.select('body') - .append('div') - .attr('id', 'inspector-wrap') - .call(inspector); - }); - - afterEach(function () { - element.remove(); - }); - - it("emits a close event when the apply button is clicked", function () { - var spy = sinon.spy(); - inspector.on('close', spy); - - element.select('.apply').trigger('click'); - - expect(spy).to.have.been.calledWith(entity); - }); - - xit("emits a changeTags event when the apply button is clicked", function () { - var spy = sinon.spy(); - inspector.on('changeTags', spy); - - element.select('.apply').trigger('click'); - - expect(spy).to.have.been.calledWith(entity, tags); - }); });