您的位置:首页 > Web前端 > JavaScript

测试2----Javascript代码片段之Event封装

2011-12-02 13:21 411 查看
研究一下事件

var addEventHandler=(function(){
//closure's shared date
var _obj={};
// config {type:,handler:,count:,preDefault:,stop:,toggle:,handler2}
return (function(e,handler){
var _arg={type:null,// for string
handler:null, //for function
stop:null, //for bool
preDefault:null, // for bool
count:null ,//for number
toggle:null,// control toggle for handler and handler2
handler2:null //use must set toggle is true
},
_h=function(){
// simulate happen event (handler) execute
var _evt=(arguments[0]&&arguments[0].f)?arguments[0]:Event.apply(this,[arguments[0],_arg.type]);
// for trigger
this["_evt"+_arg.type]=_evt;
// for count
// how remind the number is a problem ! this is not good
if(this[_arg.type+"count"]){
// when <
if(this[_arg.type+"c"]<this[_arg.type+"count"]){
// for toggle
if(typeof this[_arg.type+"tf"]==="boolean"){
if(this[_arg.type+"tf"]){
this[_arg.type+"t1"].apply(this,[_evt]);
this[_arg.type+"tf"]=false;
}
else{
this[_arg.type+"t2"].apply(this,[_evt]);
this[_arg.type+"tf"]=true;
}
}
// normal
else{
for(var i=0;i<this[_arg.type].length;i++){
this[_arg.type][i].apply(this,[_evt]);
}
}
this[_arg.type+"c"]++;
// stop bubble
if(this[_arg.type+"stop"]){
_evt.stop();
}
// prevent default
if(this[_arg.type+"preDefault"]){
_evt.preDeafult();
}
return this;
}
// when >
else{
return this;
}
}
//for no count
else{
// for toggle
if(typeof this[_arg.type+"tf"]==="boolean"){
if(this[_arg.type+"tf"]){
this[_arg.type+"t1"].apply(this,[_evt]);
this[_arg.type+"tf"]=false;
}
else{
this[_arg.type+"t2"].apply(this,[_evt]);
this[_arg.type+"tf"]=true;
}
}
// normal
else{
for(var i=0;i<this[_arg.type].length;i++){
this[_arg.type][i].apply(this,[_evt]);
}
}
// stop bubble
if(this[_arg.type+"stop"]){
_evt.stop();
}
// prevent default
if(this[_arg.type+"preDefault"]){
_evt.preDeafult();
}
return this;
}
};
// for IE in Strict active event must hava backgroundColor
// god ! this hava 'this' problem for IE 2011.11.29 10:50
/***/
if(Sys.ie6||Sys.ie7||Sys.ie8){
if(this!==window&&this!==document&&this!==document.documentElement&&color.apply(this,[])==="transparent"){
this.parentNode.insertBefore(this.cloneNode(true),this);
setStyle.apply(this,[{backgroundColor:"red",
opacity:0
}]);
//this.bgc=true;
}
}
/***/
// deal with arguments done it in 2011.10.31 13:00-14:30
if(arguments.length===2&&typeof arguments[0]==="string"&&typeof arguments[1]==="function"){
_arg.type=arguments[0];
_arg.handler=arguments[1];
}
else if(arguments.length===3&&typeof arguments[0]==="string"&&typeof arguments[1]==="function"&&typeof arguments[2]==="function"){
_arg.type=arguments[0];
_arg.handler=arguments[1];
_arg.handler2=arguments[2];
_arg.toggle=true;
if(_arg.toggle){
this[_arg.type+"tf"]=true;
}
}
else if(arguments.length===1&&arguments[0].constructor===Object){
for(var p in _arg){
_arg[p]=arguments[0][p]||null;
if(_arg.toggle){
this[_arg.type+"tf"]=true;
}
}
}
else{
return null;
}
// store config _arg for count , toggle , preDefault
this[_arg.type+"count"]=_arg.count||this[_arg.type+"count"];
this[_arg.type+"toggle"]=_arg.toggle||this[_arg.type+"toggle"];
this[_arg.type+"stop"]=_arg.stop||this[_arg.type+"stop"];
this[_arg.type+"preDefault"]=_arg.preDefault||this[_arg.type+"preDefault"];
//this is reason for shared's data _obj["on"+e] override
//you must not use dom obj.onxxx
//have event handler?
// prevent toggle is update or after toggle handler is null
if(this["on"+_arg.type]){
_obj["on"+_arg.type].push(_arg.handler);
if(_arg.toggle&&_arg.handler2){
_obj["on"+_arg.type].push(_arg.handler2);
this[_arg.type+"t1"]=_arg.handler;
this[_arg.type+"t2"]=_arg.handler2;
}
}
else{
/*****/
// _h is event handler
this["on"+_arg.type]=_h;
/********/
_obj["on"+_arg.type]=[];
if(_arg.toggle&&_arg.handler2){
_obj["on"+_arg.type].push(_arg.handler);
_obj["on"+_arg.type].push(_arg.handler2);
this[_arg.type+"t1"]=_arg.handler;
this[_arg.type+"t2"]=_arg.handler2;
}
else{
_obj["on"+_arg.type].push(_arg.handler);
}
}
//shared data is override
this[_arg.type]=_obj["on"+_arg.type];
this[_arg.type+"c"]=0;
return this;
});
})();
呵呵 没有优化!不过功能还挺强大的,程序的健壮性足可以承受的起工业应用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: