Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Page subscribes to Manager for key events
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Stuart committed Feb 23, 2012
1 parent f51c1cf commit fa8fe27
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 51 deletions.
1 change: 1 addition & 0 deletions script/application.coffee
Expand Up @@ -16,6 +16,7 @@ class FluxType
constructor: (@$container)->
@events = {
page_init: new Event
manager_init: new Event
}

@drawUI()
Expand Down
3 changes: 2 additions & 1 deletion script/application.js
Expand Up @@ -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();
}
Expand Down
19 changes: 10 additions & 9 deletions script/manager.coffee
Expand Up @@ -3,6 +3,14 @@ class Manager
# iPad support (tap header to enable keyboard)
@$hidden_input = ($ "<textarea type='text' class='hidden-input' value='Q'></textarea>").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
Expand All @@ -19,22 +27,15 @@ 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

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
21 changes: 8 additions & 13 deletions script/manager.js
Expand Up @@ -9,6 +9,12 @@ Manager = (function() {
this.processKeyDown = __bind(this.processKeyDown, this);
this.processKeyPress = __bind(this.processKeyPress, this);
this.$hidden_input = ($("<textarea type='text' class='hidden-input' value='Q'></textarea>")).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);
Expand All @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions script/page.coffee
Expand Up @@ -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
Expand Down Expand Up @@ -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)=>
Expand Down
19 changes: 19 additions & 0 deletions script/page.js
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 3 additions & 12 deletions script/test/manager.coffee
Expand Up @@ -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
Expand All @@ -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

Expand Down
19 changes: 3 additions & 16 deletions script/test/manager.js
Expand Up @@ -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;
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions script/test/page.coffee
Expand Up @@ -9,6 +9,9 @@ module "Page"
setup: ->
@page = new Page {
$container: ($ "#application")
events: {
manager_init: new Event
}
defaultText: (callback)->
callback TEXT
}, {
Expand Down
3 changes: 3 additions & 0 deletions script/test/page.js
Expand Up @@ -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);
}
Expand Down

0 comments on commit fa8fe27

Please sign in to comment.