From fa8fe270db15509e4fa272909dce915317065da4 Mon Sep 17 00:00:00 2001 From: Jesse Stuart Date: Thu, 23 Feb 2012 17:04:16 -0500 Subject: [PATCH] Page subscribes to Manager for key events --- script/application.coffee | 1 + script/application.js | 3 ++- script/manager.coffee | 19 ++++++++++--------- script/manager.js | 21 ++++++++------------- script/page.coffee | 14 ++++++++++++++ script/page.js | 19 +++++++++++++++++++ script/test/manager.coffee | 15 +++------------ script/test/manager.js | 19 +++---------------- script/test/page.coffee | 3 +++ script/test/page.js | 3 +++ 10 files changed, 66 insertions(+), 51 deletions(-) diff --git a/script/application.coffee b/script/application.coffee index 1746300..08277c9 100644 --- a/script/application.coffee +++ b/script/application.coffee @@ -16,6 +16,7 @@ class FluxType constructor: (@$container)-> @events = { page_init: new Event + manager_init: new Event } @drawUI() diff --git a/script/application.js b/script/application.js index 699a711..b18cfe6 100644 --- a/script/application.js +++ b/script/application.js @@ -24,7 +24,8 @@ FluxType = (function() { this.defaultText = __bind(this.defaultText, this); this.drawUI = __bind(this.drawUI, this); this.events = { - page_init: new Event + page_init: new Event, + manager_init: new Event }; this.drawUI(); } diff --git a/script/manager.coffee b/script/manager.coffee index 91161c4..12eda9a 100644 --- a/script/manager.coffee +++ b/script/manager.coffee @@ -3,6 +3,14 @@ class Manager # iPad support (tap header to enable keyboard) @$hidden_input = ($ "").prependTo @base.$container + @events = { + key_press: new Event + key_down: new Event + key_up: new Event + } + + @base.events.manager_init.trigger this + ($ document).bind 'keypress', @processKeyPress ($ document).bind 'keydown', @processKeyDown ($ document).bind 'keyup', @processKeyUp @@ -19,12 +27,7 @@ class Manager e.charCode ||= e.which # IE compatibility - space = @base.page.current_space - if space && space.match(e.charCode) - space.hit() - @base.page.nextSpace() - else if space - space.miss(e.charCode) + @events.key_press.trigger e.charCode processKeyDown: (e)=> @base.keyboard.selectKey e.charCode, e.keyCode @@ -32,9 +35,7 @@ class Manager if _.include TOUCHY_KEYS, e.keyCode e.preventDefault() - if _.include [KEYS.BACKSPACE], e.keyCode - space = @base.page.current_space - space.match e.keyCode + @events.key_down.trigger e.keyCode processKeyUp: (e)=> @base.keyboard.deselectKey e.charCode, e.keyCode diff --git a/script/manager.js b/script/manager.js index ac811a1..1a79d04 100644 --- a/script/manager.js +++ b/script/manager.js @@ -9,6 +9,12 @@ Manager = (function() { this.processKeyDown = __bind(this.processKeyDown, this); this.processKeyPress = __bind(this.processKeyPress, this); this.$hidden_input = ($("")).prependTo(this.base.$container); + this.events = { + key_press: new Event, + key_down: new Event, + key_up: new Event + }; + this.base.events.manager_init.trigger(this); ($(document)).bind('keypress', this.processKeyPress); ($(document)).bind('keydown', this.processKeyDown); ($(document)).bind('keyup', this.processKeyUp); @@ -25,27 +31,16 @@ Manager = (function() { } Manager.prototype.processKeyPress = function(e) { - var space; this.$hidden_input.val('Q'); if (e.keyCode === KEYS.BACKSPACE) return null; e.charCode || (e.charCode = e.which); - space = this.base.page.current_space; - if (space && space.match(e.charCode)) { - space.hit(); - return this.base.page.nextSpace(); - } else if (space) { - return space.miss(e.charCode); - } + return this.events.key_press.trigger(e.charCode); }; Manager.prototype.processKeyDown = function(e) { - var space; this.base.keyboard.selectKey(e.charCode, e.keyCode); if (_.include(TOUCHY_KEYS, e.keyCode)) e.preventDefault(); - if (_.include([KEYS.BACKSPACE], e.keyCode)) { - space = this.base.page.current_space; - return space.match(e.keyCode); - } + return this.events.key_down.trigger(e.keyCode); }; Manager.prototype.processKeyUp = function(e) { diff --git a/script/page.coffee b/script/page.coffee index 1a1773d..241a62a 100644 --- a/script/page.coffee +++ b/script/page.coffee @@ -11,6 +11,12 @@ class Page miss: new Event } + @base.events.manager_init.subscribe (manager)=> + manager.events.key_press.subscribe @_processKeyPress + manager.events.key_down.subscribe (keyCode)=> + if _.include [KEYS.BACKSPACE], keyCode + @current_space.match keyCode + default_config = { font_size: 18 padding: 4 @@ -77,6 +83,14 @@ class Page @events.next_page.trigger() + _processKeyPress: (charCode)=> + return unless @current_space + if @current_space.match(charCode) + @current_space.hit() + @nextSpace() + else + @current_space.miss(charCode) + _initText: (text)=> unless text return @base.defaultText (text)=> diff --git a/script/page.js b/script/page.js index d59bfe2..dba7395 100644 --- a/script/page.js +++ b/script/page.js @@ -9,6 +9,7 @@ Page = (function() { this.config = config; this._initTemplate = __bind(this._initTemplate, this); this._initText = __bind(this._initText, this); + this._processKeyPress = __bind(this._processKeyPress, this); this.drawText = __bind(this.drawText, this); this.resetRows = __bind(this.resetRows, this); this.nextSpace = __bind(this.nextSpace, this); @@ -20,6 +21,14 @@ Page = (function() { hit: new Event, miss: new Event }; + this.base.events.manager_init.subscribe(__bind(function(manager) { + manager.events.key_press.subscribe(this._processKeyPress); + return manager.events.key_down.subscribe(__bind(function(keyCode) { + if (_.include([KEYS.BACKSPACE], keyCode)) { + return this.current_space.match(keyCode); + } + }, this)); + }, this)); default_config = { font_size: 18, padding: 4, @@ -99,6 +108,16 @@ Page = (function() { return this.events.next_page.trigger(); }; + Page.prototype._processKeyPress = function(charCode) { + if (!this.current_space) return; + if (this.current_space.match(charCode)) { + this.current_space.hit(); + return this.nextSpace(); + } else { + return this.current_space.miss(charCode); + } + }; + Page.prototype._initText = function(text) { if (!text) { return this.base.defaultText(__bind(function(text) { diff --git a/script/test/manager.coffee b/script/test/manager.coffee index 3a55c0c..ab71c67 100644 --- a/script/test/manager.coffee +++ b/script/test/manager.coffee @@ -2,8 +2,10 @@ module "Manager" setup: -> @manager = new Manager { $container: ($ "#application") + events: { + manager_init: new Event + } page: { - nextSpace: => @space_index = (@space_index || 0) + 1 current_space: { match: -> true hit: -> null @@ -16,17 +18,6 @@ module "Manager" } } -test "keypress within document deligates to page and status", -> - e = $.Event "keypress" - e.charCode = 97 - - ($ document).trigger e - equal @space_index, 1, '@base.page.nextSpace called' - - @manager.base.page.current_space.match = -> false - ($ document).trigger e - equal @space_index, 1, '@base.page.nextSpace not called' - test "#processKeyPress does nothing for backspace", -> equal @manager.processKeyPress({ keyCode: 8 }), null diff --git a/script/test/manager.js b/script/test/manager.js index 8a61693..6ab0299 100644 --- a/script/test/manager.js +++ b/script/test/manager.js @@ -4,10 +4,10 @@ module("Manager", { setup: function() { return this.manager = new Manager({ $container: $("#application"), + events: { + manager_init: new Event + }, page: { - nextSpace: __bind(function() { - return this.space_index = (this.space_index || 0) + 1; - }, this), current_space: { match: function() { return true; @@ -32,19 +32,6 @@ module("Manager", { } }); -test("keypress within document deligates to page and status", function() { - var e; - e = $.Event("keypress"); - e.charCode = 97; - ($(document)).trigger(e); - equal(this.space_index, 1, '@base.page.nextSpace called'); - this.manager.base.page.current_space.match = function() { - return false; - }; - ($(document)).trigger(e); - return equal(this.space_index, 1, '@base.page.nextSpace not called'); -}); - test("#processKeyPress does nothing for backspace", function() { return equal(this.manager.processKeyPress({ keyCode: 8 diff --git a/script/test/page.coffee b/script/test/page.coffee index 74c04e8..e5453bb 100644 --- a/script/test/page.coffee +++ b/script/test/page.coffee @@ -9,6 +9,9 @@ module "Page" setup: -> @page = new Page { $container: ($ "#application") + events: { + manager_init: new Event + } defaultText: (callback)-> callback TEXT }, { diff --git a/script/test/page.js b/script/test/page.js index 83e4de4..473e37b 100644 --- a/script/test/page.js +++ b/script/test/page.js @@ -7,6 +7,9 @@ module("Page", { setup: function() { return this.page = new Page({ $container: $("#application"), + events: { + manager_init: new Event + }, defaultText: function(callback) { return callback(TEXT); }