Skip to content

Commit

Permalink
Merge pull request #74 from wanadev/bugfix-accelmanager-removal
Browse files Browse the repository at this point in the history
Fixed AccelManager keystroke removal
  • Loading branch information
flozz authored Sep 14, 2016
2 parents 3507009 + 6709a3e commit 84257b2
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions src/nonvisual/accelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var AccelManager = Base.$extend({

// Constructor
__init__: function () {
this.__keys = {};
this.__kbd = {};
this.$super();
},
Expand All @@ -61,6 +62,20 @@ var AccelManager = Base.$extend({

// ====== Private properties ======

/**
* Registered keys.
*
* {
* "key": [ "id", "id", ... ]
* ...
* }
*
* @property __keys
* @private
* @type Object
*/
__keys: null,

/**
* Keyboard bindings.
*
Expand All @@ -74,6 +89,7 @@ var AccelManager = Base.$extend({
* }
*
* @property __kbd
* @private
* @type Object
*/
__kbd: null,
Expand Down Expand Up @@ -101,7 +117,12 @@ var AccelManager = Base.$extend({
safe: ((safe === undefined) ? true : safe),
callback: callback
};
KeyboardJS.bind(keys, this.__onAccell.bind(this, id));

if (!this.__keys[keys]) {
KeyboardJS.bind(keys, this.__onAccel.bind(this, keys));
this.__keys[keys] = [];
}
this.__keys[keys].push(id);
},

/**
Expand All @@ -111,10 +132,17 @@ var AccelManager = Base.$extend({
* @param {String} id the accelerator id.
*/
removeAccel: function (id) {
if (!this.__kbd[id]) {
var bd = this.__kbd[id];
if (!bd) {
return;
}
KeyboardJS.unbind(this.__kbd[id].keys);

var keys = this.__keys[bd.keys];
keys.splice(keys.indexOf(id), 1);
if (keys.length === 0) {
KeyboardJS.unbind(this.__kbd[id].keys);
delete this.__keys[bd.keys];
}
delete this.__kbd[id];
},

Expand All @@ -130,28 +158,31 @@ var AccelManager = Base.$extend({
//////////////////////////////////////////

/**
* @method __onAccell
* @method __onAccel
* @private
* @param event
* @param keys
* @param combo
* @param {String} keys
* @param {Event} event
*/
__onAccell: function (id, event) {
if (!this.__kbd[id]) {
__onAccel: function (keys, event) {
if (!this.__keys[keys]) {
return;
}

if (this.__kbd[id].safe) {
if (document.activeElement instanceof HTMLInputElement ||
document.activeElement instanceof HTMLSelectElement ||
document.activeElement instanceof HTMLTextAreaElement) {
return;
for (var i = 0; i < this.__keys[keys].length; ++i) {
var id = this.__keys[keys][i];

if (this.__kbd[id].safe) {
if (document.activeElement instanceof HTMLInputElement ||
document.activeElement instanceof HTMLSelectElement ||
document.activeElement instanceof HTMLTextAreaElement) {
continue;
}
}
}

this.__kbd[id].callback();
event.stopPropagation();
event.preventDefault();
this.__kbd[id].callback();
event.stopPropagation();
event.preventDefault();
}
}

});
Expand Down

0 comments on commit 84257b2

Please sign in to comment.