key refactoring

This commit is contained in:
Peter Cottle 2012-12-25 17:12:54 -08:00
parent f71aa87b7d
commit 9ddfaf2f18
3 changed files with 80 additions and 83 deletions

View file

@ -11451,6 +11451,7 @@ var Errors = require('../util/errors');
var Warning = Errors.Warning; var Warning = Errors.Warning;
var util = require('../util'); var util = require('../util');
var keyboard = require('../util/keyboard');
var CommandPromptView = Backbone.View.extend({ var CommandPromptView = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
@ -11549,25 +11550,22 @@ var CommandPromptView = Backbone.View.extend({
this.onKey(e); this.onKey(e);
// we need to capture some of these events. // we need to capture some of these events.
// WARNING: this key map is not internationalized :( var keyToFuncMap = {
var keyMap = { enter: _.bind(function() {
// enter
13: _.bind(function() {
this.submit(); this.submit();
}, this), }, this),
// up up: _.bind(function() {
38: _.bind(function() {
this.commandSelectChange(1); this.commandSelectChange(1);
}, this), }, this),
// down down: _.bind(function() {
40: _.bind(function() {
this.commandSelectChange(-1); this.commandSelectChange(-1);
}, this) }, this)
}; };
if (keyMap[e.which] !== undefined) { var key = keyboard.mapKeycodeToKey(e.which);
if (keyToFuncMap[key] !== undefined) {
e.preventDefault(); e.preventDefault();
keyMap[e.which](); keyToFuncMap[key]();
this.onKey(e); this.onKey(e);
} }
}, },
@ -11834,6 +11832,60 @@ exports.CommandPromptView = CommandPromptView;
exports.CommandLineHistoryView = CommandLineHistoryView; exports.CommandLineHistoryView = CommandLineHistoryView;
});
require.define("/src/js/util/keyboard.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
var Backbone = require('backbone');
var mapKeycodeToKey = function(keycode) {
// TODO -- internationalize? Dvorak? I have no idea
var keyMap = {
37: 'left',
38: 'up',
39: 'right',
40: 'down',
27: 'esc',
13: 'enter'
};
return keyMap[keycode];
};
function KeyboardListener(options) {
this.events = options.events || _.clone(Backbone.Events);
this.aliasMap = options.aliasMap || {};
this.keydownListener = _.bind(this.keydown, this);
this.listen();
}
KeyboardListener.prototype.listen = function() {
$(document).bind('keydown', this.keydownListener);
};
KeyboardListener.prototype.mute = function() {
$(document).unbind('keydown', this.keydownListener);
};
KeyboardListener.prototype.keydown = function(e) {
var which = e.which;
console.log('key which', which);
var key = mapKeycodeToKey(which);
if (key === undefined) {
return;
}
this.fireEvent(key);
};
KeyboardListener.prototype.fireEvent = function(eventName) {
eventName = this.aliasMap[eventName] || eventName;
this.events.trigger(eventName);
};
exports.KeyboardListener = KeyboardListener;
exports.mapKeycodeToKey = mapKeycodeToKey;
}); });
require.define("/src/js/visuals/index.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore'); require.define("/src/js/visuals/index.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
@ -13908,59 +13960,6 @@ var MultiView = Backbone.View.extend({
exports.MultiView = MultiView; exports.MultiView = MultiView;
});
require.define("/src/js/util/keyboard.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
var Backbone = require('backbone');
var mapKeycodeToKey = function(keycode) {
var keyMap = {
37: 'left',
38: 'up',
39: 'right',
40: 'down',
27: 'esc',
13: 'enter'
};
return keyMap[keycode];
};
function KeyboardListener(options) {
this.events = options.events || _.clone(Backbone.Events);
this.aliasMap = options.aliasMap || {};
this.keydownListener = _.bind(this.keydown, this);
this.listen();
}
KeyboardListener.prototype.listen = function() {
$(document).bind('keydown', this.keydownListener);
};
KeyboardListener.prototype.mute = function() {
$(document).unbind('keydown', this.keydownListener);
};
KeyboardListener.prototype.keydown = function(e) {
var which = e.which;
console.log('key which', which);
var key = mapKeycodeToKey(which);
if (key === undefined) {
return;
}
this.fireEvent(key);
};
KeyboardListener.prototype.fireEvent = function(eventName) {
eventName = this.aliasMap[eventName] || eventName;
this.events.trigger(eventName);
};
exports.KeyboardListener = KeyboardListener;
exports.mapKeycodeToKey = mapKeycodeToKey;
}); });
require.define("/src/js/app/index.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore'); require.define("/src/js/app/index.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
@ -16567,6 +16566,7 @@ require.define("/src/js/util/keyboard.js",function(require,module,exports,__dirn
var Backbone = require('backbone'); var Backbone = require('backbone');
var mapKeycodeToKey = function(keycode) { var mapKeycodeToKey = function(keycode) {
// TODO -- internationalize? Dvorak? I have no idea
var keyMap = { var keyMap = {
37: 'left', 37: 'left',
38: 'up', 38: 'up',
@ -16644,6 +16644,7 @@ var Errors = require('../util/errors');
var Warning = Errors.Warning; var Warning = Errors.Warning;
var util = require('../util'); var util = require('../util');
var keyboard = require('../util/keyboard');
var CommandPromptView = Backbone.View.extend({ var CommandPromptView = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
@ -16742,25 +16743,22 @@ var CommandPromptView = Backbone.View.extend({
this.onKey(e); this.onKey(e);
// we need to capture some of these events. // we need to capture some of these events.
// WARNING: this key map is not internationalized :( var keyToFuncMap = {
var keyMap = { enter: _.bind(function() {
// enter
13: _.bind(function() {
this.submit(); this.submit();
}, this), }, this),
// up up: _.bind(function() {
38: _.bind(function() {
this.commandSelectChange(1); this.commandSelectChange(1);
}, this), }, this),
// down down: _.bind(function() {
40: _.bind(function() {
this.commandSelectChange(-1); this.commandSelectChange(-1);
}, this) }, this)
}; };
if (keyMap[e.which] !== undefined) { var key = keyboard.mapKeycodeToKey(e.which);
if (keyToFuncMap[key] !== undefined) {
e.preventDefault(); e.preventDefault();
keyMap[e.which](); keyToFuncMap[key]();
this.onKey(e); this.onKey(e);
} }
}, },

View file

@ -2,6 +2,7 @@ var _ = require('underscore');
var Backbone = require('backbone'); var Backbone = require('backbone');
var mapKeycodeToKey = function(keycode) { var mapKeycodeToKey = function(keycode) {
// TODO -- internationalize? Dvorak? I have no idea
var keyMap = { var keyMap = {
37: 'left', 37: 'left',
38: 'up', 38: 'up',

View file

@ -11,6 +11,7 @@ var Errors = require('../util/errors');
var Warning = Errors.Warning; var Warning = Errors.Warning;
var util = require('../util'); var util = require('../util');
var keyboard = require('../util/keyboard');
var CommandPromptView = Backbone.View.extend({ var CommandPromptView = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
@ -109,25 +110,22 @@ var CommandPromptView = Backbone.View.extend({
this.onKey(e); this.onKey(e);
// we need to capture some of these events. // we need to capture some of these events.
// WARNING: this key map is not internationalized :( var keyToFuncMap = {
var keyMap = { enter: _.bind(function() {
// enter
13: _.bind(function() {
this.submit(); this.submit();
}, this), }, this),
// up up: _.bind(function() {
38: _.bind(function() {
this.commandSelectChange(1); this.commandSelectChange(1);
}, this), }, this),
// down down: _.bind(function() {
40: _.bind(function() {
this.commandSelectChange(-1); this.commandSelectChange(-1);
}, this) }, this)
}; };
if (keyMap[e.which] !== undefined) { var key = keyboard.mapKeycodeToKey(e.which);
if (keyToFuncMap[key] !== undefined) {
e.preventDefault(); e.preventDefault();
keyMap[e.which](); keyToFuncMap[key]();
this.onKey(e); this.onKey(e);
} }
}, },