add support for bindkey.position

This commit is contained in:
nightwing 2015-01-25 16:13:24 +04:00
commit b0ce5630c9

View file

@ -87,9 +87,12 @@ MultiHashHandler.prototype = HashHandler.prototype;
}
};
this.bindKey = function(key, command, asDefault) {
if (typeof key == "object")
this.bindKey = function(key, command, position) {
if (typeof key == "object") {
if (position == undefined)
position = key.position;
key = key[this.platform];
}
if (!key)
return;
if (typeof command == "function")
@ -110,11 +113,15 @@ MultiHashHandler.prototype = HashHandler.prototype;
}
var binding = this.parseKeys(keyPart);
var id = KEY_MODS[binding.hashId] + binding.key;
this._addCommandToBinding(chain + id, command, asDefault);
this._addCommandToBinding(chain + id, command, position);
}, this);
};
this._addCommandToBinding = function(keyId, command, asDefault) {
function getPosition(command) {
return typeof command == "object" && command.bindKey
&& command.bindKey.position || 0;
}
this._addCommandToBinding = function(keyId, command, position) {
var ckb = this.commandKeyBinding, i;
if (!command) {
delete ckb[keyId];
@ -126,11 +133,21 @@ MultiHashHandler.prototype = HashHandler.prototype;
} else if ((i = ckb[keyId].indexOf(command)) != -1) {
ckb[keyId].splice(i, 1);
}
if (asDefault || command.isDefault)
ckb[keyId].unshift(command);
else
ckb[keyId].push(command);
if (typeof position != "number") {
if (position || command.isDefault)
position = -100;
else
position = getPosition(command);
}
var commands = ckb[keyId];
for (i = 0; i < commands.length; i++) {
var other = commands[i];
var otherPos = getPosition(other);
if (otherPos > position)
break;
}
commands.splice(i, 0, command);
}
};