From a78aeeb62591770ed2856389d07bceeb09b9f8b5 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 1 Feb 2013 11:46:46 -0500 Subject: [PATCH] Merge controller into iD --- Makefile | 1 - index.html | 1 - js/id/behavior/hash.js | 2 +- js/id/controller.js | 19 ------------------- js/id/id.js | 22 +++++++++++++++++----- js/id/ui.js | 15 +++++++-------- test/index.html | 1 - 7 files changed, 25 insertions(+), 36 deletions(-) delete mode 100644 js/id/controller.js diff --git a/Makefile b/Makefile index 1db4f2efb..3b0f2d1b7 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,6 @@ all: \ js/id/modes/*.js \ js/id/operations.js \ js/id/operations/*.js \ - js/id/controller.js \ js/id/graph/*.js \ js/id/renderer/*.js \ js/id/svg.js \ diff --git a/index.html b/index.html index ab6070fbc..52ff209f2 100644 --- a/index.html +++ b/index.html @@ -123,7 +123,6 @@ - diff --git a/js/id/behavior/hash.js b/js/id/behavior/hash.js index 871b73a35..9056db393 100644 --- a/js/id/behavior/hash.js +++ b/js/id/behavior/hash.js @@ -48,7 +48,7 @@ iD.behavior.Hash = function(context) { context.enter(iD.modes.Select([id])); }); - context.controller().on('enter.hash', function() { + context.on('enter.hash', function() { if (context.mode().id !== 'browse') selectoff(); }); } diff --git a/js/id/controller.js b/js/id/controller.js deleted file mode 100644 index af1e7c388..000000000 --- a/js/id/controller.js +++ /dev/null @@ -1,19 +0,0 @@ -// A controller holds a single action at a time and calls `.enter` and `.exit` -// to bind and unbind actions. -iD.Controller = function() { - var event = d3.dispatch('enter', 'exit'); - var controller = { mode: null }; - - controller.enter = function(mode) { - if (controller.mode) { - controller.mode.exit(); - event.exit(controller.mode); - } - - mode.enter(); - controller.mode = mode; - event.enter(mode); - }; - - return d3.rebind(controller, event, 'on'); -}; diff --git a/js/id/id.js b/js/id/id.js index 42a8cfb11..b2d28fb3f 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -2,7 +2,8 @@ window.iD = function () { var context = {}, history = iD.History(), connection = iD.Connection().version(iD.version), - controller = iD.Controller(), + dispatch = d3.dispatch('enter', 'exit'), + mode, container, ui = iD.ui(context), map = iD.Map().connection(connection).history(history); @@ -11,7 +12,6 @@ window.iD = function () { context.ui = function () { return ui; }; context.connection = function () { return connection; }; context.history = function () { return history; }; - context.controller = function () { return controller; }; context.map = function () { return map; }; /* History delegation. */ @@ -26,8 +26,20 @@ window.iD = function () { context.entity = function (id) { return history.graph().entity(id); }; context.geometry = function (id) { return context.entity(id).geometry(history.graph()); }; - context.enter = controller.enter; - context.mode = function () { return controller.mode; }; + context.enter = function(newMode) { + if (mode) { + mode.exit(); + dispatch.exit(mode); + } + + mode = newMode; + mode.enter(); + dispatch.enter(mode); + }; + + context.mode = function() { + return mode; + }; context.install = function (behavior) { context.surface().call(behavior); }; context.uninstall = function (behavior) { context.surface().call(behavior.off); }; @@ -46,7 +58,7 @@ window.iD = function () { context.background() .source(iD.BackgroundSource.Bing); - return context; + return d3.rebind(context, dispatch, 'on'); }; iD.version = '0.0.0-alpha1'; diff --git a/js/id/ui.js b/js/id/ui.js index 978d9175e..6b9b3cc08 100644 --- a/js/id/ui.js +++ b/js/id/ui.js @@ -4,8 +4,7 @@ iD.ui = function (context) { var connection = context.connection(), history = context.history(), - map = context.map(), - controller = context.controller(); + map = context.map(); if (!iD.supported()) { container.html('This editor is supported in Firefox, Chrome, Safari, Opera, ' + @@ -48,7 +47,7 @@ iD.ui = function (context) { .attr('data-original-title', function (mode) { return hintprefix(mode.key, mode.description); }) - .on('click.editor', function (mode) { controller.enter(mode); }); + .on('click.editor', function (mode) { context.enter(mode); }); function disableTooHigh() { if (map.editable()) { @@ -57,7 +56,7 @@ iD.ui = function (context) { } else { buttons.attr('disabled', 'disabled'); notice.message(true); - controller.enter(iD.modes.Browse(context)); + context.enter(iD.modes.Browse(context)); } } @@ -77,12 +76,12 @@ iD.ui = function (context) { buttons.append('span').attr('class', 'label').text(function (mode) { return mode.title; }); - controller.on('enter.editor', function (entered) { + context.on('enter.editor', function (entered) { buttons.classed('active', function (mode) { return entered.button === mode.button; }); container.classed("mode-" + entered.id, true); }); - controller.on('exit.editor', function (exited) { + context.on('exit.editor', function (exited) { container.classed("mode-" + exited.id, false); }); @@ -228,7 +227,7 @@ iD.ui = function (context) { .on('⌫', function() { d3.event.preventDefault(); }); modes.forEach(function(m) { - keybinding.on(m.key, function() { if (map.editable()) controller.enter(m); }); + keybinding.on(m.key, function() { if (map.editable()) context.enter(m); }); }); d3.select(document) @@ -246,7 +245,7 @@ iD.ui = function (context) { .on('logout.editor', connection.logout) .on('login.editor', connection.authenticate)); - controller.enter(iD.modes.Browse(context)); + context.enter(iD.modes.Browse(context)); if (!localStorage.sawSplash) { iD.ui.splash(); diff --git a/test/index.html b/test/index.html index 879cc6171..d32331ba9 100644 --- a/test/index.html +++ b/test/index.html @@ -119,7 +119,6 @@ -