faster oz events

--HG--
extra : convert_revision : svn%3Ab267cdba-c1da-11dd-874b-8bacd04a0a74/trunk%4076
This commit is contained in:
ondrej.zara
2010-01-04 09:59:37 +00:00
parent 29839b3e81
commit b238426f9a
+27 -15
View File
@@ -1,4 +1,4 @@
/* (c) 2007 - now() Ondrej Zara, 1.3 */ /* (c) 2007 - now() Ondrej Zara, 1.5 */
var OZ = { var OZ = {
$:function(x) { return typeof(x) == "string" ? document.getElementById(x) : x; }, $:function(x) { return typeof(x) == "string" ? document.getElementById(x) : x; },
opera:!!window.opera, opera:!!window.opera,
@@ -8,7 +8,8 @@ var OZ = {
khtml:!!navigator.userAgent.match(/khtml/i) || !!navigator.userAgent.match(/konqueror/i), khtml:!!navigator.userAgent.match(/khtml/i) || !!navigator.userAgent.match(/konqueror/i),
Event:{ Event:{
_id:0, _id:0,
_cache:{}, _byName:{},
_byID:{},
add:function(elm,event,cb) { add:function(elm,event,cb) {
var id = OZ.Event._id++; var id = OZ.Event._id++;
var element = OZ.$(elm); var element = OZ.$(elm);
@@ -21,12 +22,16 @@ var OZ = {
element.attachEvent("on"+event,fnc); element.attachEvent("on"+event,fnc);
} }
} }
OZ.Event._cache[id] = [element,event,fnc]; if (!(event in OZ.Event._byName)) { OZ.Event._byName[event] = {}; }
var obj = OZ.Event._byName[event];
obj[id] = [element,event,fnc];
OZ.Event._byID[id] = obj;
return id; return id;
}, },
remove:function(id) { remove:function(id) {
var e = OZ.Event._cache[id]; var obj = OZ.Event._byID[id];
if (!e) { return; } if (!obj) { return; }
var e = obj[id];
var elm = e[0]; var elm = e[0];
if (elm) { if (elm) {
if (elm.removeEventListener) { if (elm.removeEventListener) {
@@ -35,7 +40,8 @@ var OZ = {
elm.detachEvent("on"+e[1],e[2]); elm.detachEvent("on"+e[1],e[2]);
} }
} }
delete OZ.Event._cache[id]; delete OZ.Event._byID[id];
delete obj[id];
}, },
stop:function(e) { e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true; }, stop:function(e) { e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true; },
prevent:function(e) { e.preventDefault ? e.preventDefault() : e.returnValue = false; }, prevent:function(e) { e.preventDefault ? e.preventDefault() : e.returnValue = false; },
@@ -56,12 +62,7 @@ var OZ = {
this.prototype = new tmp(); this.prototype = new tmp();
return this; return this;
}; };
c.prototype.bind = function(fnc) { c.prototype.bind = function(fnc) { return fnc.bind(this); };
var obj = this;
return function() {
return fnc.apply(obj,arguments);
}
};
c.prototype.dispatch = function(type, data) { c.prototype.dispatch = function(type, data) {
var obj = { var obj = {
type:type, type:type,
@@ -70,9 +71,10 @@ var OZ = {
data:data data:data
} }
var tocall = []; var tocall = [];
for (var p in OZ.Event._cache) { var list = OZ.Event._byName[type];
var item = OZ.Event._cache[p]; for (var id in list) {
if (item[1] == type && (!item[0] || item[0] == this)) { tocall.push(item[2]); } var item = list[id];
if (!item[0] || item[0] == this) { tocall.push(item[2]); }
} }
var len = tocall.length; var len = tocall.length;
for (var i=0;i<len;i++) { tocall[i](obj); } for (var i=0;i<len;i++) { tocall[i](obj); }
@@ -218,6 +220,16 @@ var OZ = {
} }
} }
if (!Function.prototype.bind) {
Function.prototype.bind = function(thisObj) {
var fn = this;
var args = Array.prototype.slice.call(arguments, 1);
return function() {
return fn.apply(thisObj, args.concat(Array.prototype.slice.call(arguments)));
}
}
};
if (!Array.prototype.indexOf) { if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(item, from) { Array.prototype.indexOf = function(item, from) {
var len = this.length; var len = this.length;