From 7ea7326f9227198df3e846115019af222d08cb18 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 31 Jan 2013 14:11:53 -0500 Subject: [PATCH] Extract Select behavior --- index.html | 1 + js/id/behavior/select.js | 22 ++++++++++++++++++++++ js/id/modes/browse.js | 10 +--------- js/id/modes/select.js | 22 +++++----------------- test/index.html | 1 + 5 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 js/id/behavior/select.js diff --git a/index.html b/index.html index f286e8a52..4fe1bd621 100644 --- a/index.html +++ b/index.html @@ -94,6 +94,7 @@ + diff --git a/js/id/behavior/select.js b/js/id/behavior/select.js new file mode 100644 index 000000000..cee2cc940 --- /dev/null +++ b/js/id/behavior/select.js @@ -0,0 +1,22 @@ +iD.behavior.Select = function(mode) { + var controller = mode.controller; + + function click() { + var datum = d3.select(d3.event.target).datum(); + if (datum instanceof iD.Entity) { + controller.enter(iD.modes.Select([datum.id])); + } else { + controller.enter(iD.modes.Browse()); + } + } + + var behavior = function(selection) { + selection.on('click.select', click); + }; + + behavior.off = function(selection) { + selection.on('click.select', null); + }; + + return behavior; +}; diff --git a/js/id/modes/browse.js b/js/id/modes/browse.js index b1fb1c8ae..7652576ea 100644 --- a/js/id/modes/browse.js +++ b/js/id/modes/browse.js @@ -14,19 +14,13 @@ iD.modes.Browse = function() { behaviors = [ iD.behavior.Hover(), + iD.behavior.Select(mode), iD.behavior.DragNode(mode), iD.behavior.DragMidpoint(mode)]; behaviors.forEach(function(behavior) { behavior(surface); }); - - surface.on('click.browse', function () { - var datum = d3.select(d3.event.target).datum(); - if (datum instanceof iD.Entity) { - mode.controller.enter(iD.modes.Select([datum.id])); - } - }); }; mode.exit = function() { @@ -35,8 +29,6 @@ iD.modes.Browse = function() { behaviors.forEach(function(behavior) { behavior.off(surface); }); - - surface.on('click.browse', null); }; return mode; diff --git a/js/id/modes/select.js b/js/id/modes/select.js index 37b2dee1d..b369bfca7 100644 --- a/js/id/modes/select.js +++ b/js/id/modes/select.js @@ -36,6 +36,7 @@ iD.modes.Select = function(selection, initial) { behaviors = [ iD.behavior.Hover(), + iD.behavior.Select(mode), iD.behavior.DragNode(mode), iD.behavior.DragMidpoint(mode)]; @@ -106,15 +107,6 @@ iD.modes.Select = function(selection, initial) { surface.call(radialMenu.close); }); - function click() { - var datum = d3.select(d3.event.target).datum(); - if (datum instanceof iD.Entity) { - mode.controller.enter(iD.modes.Select([datum.id])); - } else { - mode.controller.enter(iD.modes.Browse()); - } - } - function dblclick() { var target = d3.select(d3.event.target), datum = target.datum(); @@ -134,13 +126,11 @@ iD.modes.Select = function(selection, initial) { } } - surface.on('click.select', click) - .on('dblclick.select', dblclick); - d3.select(document) .call(keybinding); - surface.selectAll("*") + surface.on('dblclick.select', dblclick) + .selectAll("*") .filter(function (d) { return d && selection.indexOf(d.id) >= 0; }) .classed('selected', true); @@ -182,12 +172,10 @@ iD.modes.Select = function(selection, initial) { keybinding.off(); - surface.on('click.select', null) - .on('dblclick.select', null); - history.on('change.select', null); - surface.selectAll(".selected") + surface.on('dblclick.select', null) + .selectAll(".selected") .classed('selected', false); surface.call(radialMenu.close); diff --git a/test/index.html b/test/index.html index 52070c414..370b238fe 100644 --- a/test/index.html +++ b/test/index.html @@ -90,6 +90,7 @@ +