Add Events object as a helper for installing and removing events.

This commit is contained in:
Jürg Lehni 2011-03-04 23:31:51 +00:00
parent 7b237ceedc
commit b85235e1cb
2 changed files with 29 additions and 0 deletions

View file

@ -48,6 +48,7 @@ this.install = function(scope) {
//#include "util/CanvasProvider.js"
//#include "util/MathUtils.js"
//#include "util/Events.js"
//#include "util/PaperScript.js"
};

28
src/util/Events.js Normal file
View file

@ -0,0 +1,28 @@
var Events = {
add: function(obj, events) {
for (var type in events) {
var func = events[type];
if (obj.addEventListener) {
obj.addEventListener(type, func, false);
} else if (obj.attachEvent) {
// Make a bound closure that calls on the write object and
// passes the event object.
obj.attachEvent('on' + type, func.bound = function() {
func.call(obj, window.event);
});
}
}
},
remove: function(obj, events) {
for (var type in events) {
var func = events[type];
if (obj.removeEventListener) {
obj.removeEventListener(type, func, false);
} else if (obj.detachEvent) {
// Remove the bound function instead of func itself
obj.detachEvent('on' + type, func.bound);
}
}
}
};