Add #toString() to Event#modifiers.

This commit is contained in:
Jürg Lehni 2011-05-08 15:50:17 +01:00
parent 585e3b6254
commit e79d890432
5 changed files with 23 additions and 7 deletions

View file

@ -124,8 +124,22 @@ Base.inject({
});
},
/**
* Utility function for rendering numbers to strings at a precision of up
* to 5 fractional digits.
*/
formatNumber: function(num) {
return (Math.round(num * 100000) / 100000).toString();
},
/**
* Utility function for rendering objects to strings, in object literal
* notation.
*/
formatObject: function(obj) {
return '{ ' + Base.each(obj, function(value, key) {
this.push(key + ': ' + value);
}, []).join(', ') + ' }';
}
});

View file

@ -196,7 +196,7 @@ var ToolEvent = this.ToolEvent = Base.extend({
return '{ type: ' + this.type
+ ', point: ' + this.point
+ ', count: ' + this.count
+ ', modifiers: ' + this.getModifiers(true)
+ ', modifiers: ' + this.getModifiers()
+ ' }';
}
});

View file

@ -35,9 +35,7 @@ var Event = this.Event = Base.extend({
this.preventDefault();
},
getModifiers: function(asString) {
return asString ? '{ ' + Base.each(Key.modifiers, function(value, key) {
this.push(key + ': ' + value);
}, []).join(', ') + ' }' : Key.modifiers;
getModifiers: function() {
return Key.modifiers;
}
});

View file

@ -44,7 +44,11 @@ var Key = this.Key = new function() {
control: false,
option: false,
command: false,
capsLock: false
capsLock: false,
toString: function() {
return Base.formatObject(this);
}
},
// Since only keypress gets proper keyCodes that are actually representing

View file

@ -27,7 +27,7 @@ var KeyEvent = this.KeyEvent = Event.extend(new function() {
return '{ type: ' + this.type
+ ', key: ' + this.key
+ ', character: ' + this.character
+ ', modifiers: ' + this.getModifiers(true)
+ ', modifiers: ' + this.getModifiers()
+ ' }';
}
};