From b9acb41321c9666154a9eddfafaca59141497523 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Fri, 2 Nov 2012 16:32:36 -0400 Subject: [PATCH] Escape actions with the escape key --- index.html | 2 -- js/iD/actions/actions.js | 46 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index f285645b7..52efa243a 100755 --- a/index.html +++ b/index.html @@ -83,8 +83,6 @@ }); } - for (var a in iD.actions) iD.actions[a].bind(); - d3.select('#geocode-form').on('submit', function() { d3.event.preventDefault(); var val = d3.select('#geocode-location').node().value; diff --git a/js/iD/actions/actions.js b/js/iD/actions/actions.js index 876f280cd..4bc28f191 100644 --- a/js/iD/actions/actions.js +++ b/js/iD/actions/actions.js @@ -1,7 +1,8 @@ iD.actions = {}; iD.actions.AddPlace = { - bind: function() { + bind: function(controller) { + this.controller = controller; d3.selectAll('button#place').on('click', function() { iD.actions.AddPlace.enter(); }); @@ -19,7 +20,8 @@ iD.actions.AddPlace = { }; iD.actions.AddRoad = { - bind: function() { + bind: function(controller) { + this.controller = controller; d3.selectAll('button#road').on('click', function() { iD.actions.AddRoad.enter(); }); @@ -37,7 +39,8 @@ iD.actions.AddRoad = { }; iD.actions.AddArea = { - bind: function() { + bind: function(controller) { + this.controller = controller; d3.selectAll('button#area').on('click', function() { iD.actions.AddArea.enter(); }); @@ -53,3 +56,40 @@ iD.actions.AddArea = { d3.selectAll('button#area').classed('active', false); } }; + +iD.actions.Move = { + bind: function(controller) { + this.controller = controller; + }, + enter: function() { + d3.selectAll('button').classed('active', false); + }, + exit: function() { } +}; + +iD.controller = (function() { + var controller = {}, + action; + + for (var a in iD.actions) iD.actions[a].bind(controller); + + controller.go = function(x) { + if (action) { + action.exit(); + } + x.enter(); + action = x; + }; + + controller.go(iD.actions.Move); + + // Pressing 'escape' should exit any action. + d3.select(document).on('keydown', function() { + if (d3.event.keyCode === 27) { + iD.controller.go(iD.actions.Move); + } + }); + + return controller; +})(); +