/*
* @create time 2015-03-24
* @author Tong_yx
* @desc 全局事件处理中心
*/
/**
* 事件对象
* @param {string} token 事件的token值,用于删除事件
* @param {object} obj 事件的对象
* @param {string} funcname 函数名称
* @param {object} target 目标对象
* @constructor
*/
function CallBackFunc(token, obj, funcname, target) {
this._obj = obj;
this._funcname = funcname;
this._token = token;
this._target = target;
};
CallBackFunc.prototype.Execute = function() {
if (this._obj) {
var func = this._obj[this._funcname];
if (func) {
func.apply(this._obj, arguments);
}
} else {
this._funcname.apply(null, arguments);
}
};
CallBackFunc.prototype.CheckTarget = function(target) {
return this._target == target ? true : false;
};
CallBackFunc.prototype.HasTarget = function() {
return this._target;
};
CallBackFunc.prototype.IsValid = function() {
return typeof this._funcname == "function" || (this._obj && typeof this._funcname == "string");
};
var EventCenter = function() {
/**
* 事件对象存储
* @type {{}}
* @private
*/
this._EventHandlerMap = {};
/**
* 待处理异步事件列表
* @type {array}
* @private
*/
this._EventList = [];
this._TimerID = null;
this._subUid = 0;
/**
* 是否需要先进行事件注册才能进行事件的绑定
* @type {boolean}
* @private
*/
//this._m_bMustReg = false;
this._m_bMustReg = true;
}
/**
* 设置是否需要先行注册才能绑定事件
* @param {boolean} bEnable 是否启用该功能
*/
EventCenter.prototype.setMustRegMode = function (bEnable){
this._m_bMustReg = (bEnable)?true:false;
}
/**
* 返回是否能注册事件
* @param {string} id 事件id
* @param {object} target 对象绑定
* @returns {boolean | object}
* @constructor
*/
EventCenter.prototype.CanRegEvent = function (id,target)
{
var handler = this.FindEventHandler(id,target);
if(this._m_bMustReg) {
return (handler)?handler:false;
}
return (handler)?handler:true;
};
/**
* 取得事件的Handler
* @param {string} id
* @param {object} target
* @returns {boolean} 返回找到的Handler,若Handler不存在,则返回是否注册过该事件
* @constructor
*/
EventCenter.prototype.FindEventHandler = function(id, target) {
var handler = this._EventHandlerMap[id];
return (handler)?handler:this._EventHandlerMap.hasOwnProperty(id);
};
/**
* 注册事件
* @param {string} id 事件id
* @constructor
*/
EventCenter.prototype.RegisterEvent = function (id)
{
if(id)
{
if(id.constructor == Array)
{
for(var i=0;i<id.length; ++i)
{
var sID = id[i];
this.RegisterEvent(sID);
}
}
else if(!this.FindEventHandler(id))
{
this._EventHandlerMap[id] = undefined;
}
}
}
/**
* 绑定事件
* @param {string} id
* @param {object} object
* @param {string} strFuncName
* @param {object} target
* @returns {*}
*/
EventCenter.prototype.on = function(id, object, strFuncName, target) {
switch (arguments.length) {
case 2:
{
strFuncName = object;
object = null;
}
break;
}
if (!id || !strFuncName) {
return;
}
var handler = this.CanRegEvent(id, target);
if( handler)
{
var token = (++this._subUid).toString();
if(handler === true)
{
handler = [];
handler.push(new CallBackFunc(token, object, strFuncName, target));
this._EventHandlerMap[id] = handler;
}
else{
handler.push(new CallBackFunc(token, object, strFuncName, target));
}
return token;
}
else{
throw new Error('Can\'t Find Register Event ['+id+']');
}
return false;
};
/**
* 清除事件的Handler
* @param id
* @constructor
*/
EventCenter.prototype.ClearEventHandler = function(id) {
var handler = this.FindEventHandler(id);
if (handler instanceof Array) {
var len = handler.length;
handler.splice(0, len);
}
this._EventHandlerMap[id] = undefined;
}
/**
* 使用Token清除 相应注册的事件
* @param id
* @param token
* @constructor
*/
EventCenter.prototype.removeEvent = function(id, token) {
var retVal = false;
switch (arguments.length) {
case 1:
{
//若仅传入一个参数,则该参数默认为token值
token = id;
id = null;
}
break;
}
//若没有传入token,则直接返回失败
if (!token) {
return retVal;
}
var bolBrk = false;
//删除相应的事件
for (var eventID in this._EventHandlerMap) {
if (id) {
//若有指定id时,则使用指定的id进行查找
bolBrk = true;
eventID = id;
}
var handler = this.FindEventHandler(eventID);
if (handler) {
for (var index in handler) {
var cbFunc = handler[index];
//和token进行比对
if (cbFunc._token == token) {
handler.splice(index, 1);
retVal = true;
bolBrk = true;
}
}
}
//若bolBrk被设定为True,则不再进行查找,直接break
if (bolBrk) {
break;
}
}
return retVal;
};
/**
* 清理所有的事件
* @constructor
*/
EventCenter.prototype.ClearAllHandler = function() {
for (var id in this._EventHandlerMap) {
//this.removeEvent(id);
this.ClearEventHandler(id);
}
this._EventHandlerMap = {};
var len = this._EventList.length;
while (len--) {
delete this._EventList[len];
}
this._EventList.splice[0, this._EventList.length];
};
// 同步处理事件
EventCenter.prototype.RaiseEvent = function() {
var oHandlers = this.FindEventHandler(arguments[0]);
if (oHandlers) {
var nLen = oHandlers.length;
while (nLen--) {
var oHandler = oHandlers[nLen];
if (oHandler.IsValid()) {
switch (arguments.length) {
case 1:
oHandler.Execute.call(oHandler);
break;
case 2:
oHandler.Execute.call(oHandler, arguments[1]);
break;
case 3:
oHandler.Execute.call(oHandler, arguments[1], arguments[2]);
break;
default:
var l = arguments.length;
var args = [];
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
var stopNext = oHandler.Execute.apply(oHandler, args);
//是否停止事件向下传递
if (stopNext === true) {
nLen = 0;
}
break;
}
}
}
}
};
// 同步处理事件
EventCenter.prototype.RaiseEventByTarget = function() {
var aHandlers = this.FindEventHandler(arguments[0]);
if (aHandlers) {
var nLen = aHandlers.length;
var oTarget = arguments[arguments.length-1];
while (nLen--) {
var oHandler = aHandlers[nLen];
if (oHandler.IsValid()) {
if(oHandler.CheckTarget(oTarget)) {
switch (arguments.length) {
case 2:
oHandler.Execute.call(oHandler);
break;
case 3:
oHandler.Execute.call(oHandler, arguments[1]);
break;
case 4:
oHandler.Execute.call(oHandler, arguments[1], arguments[2]);
break;
default:
var l = arguments.length;
var args = [];
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
var stopNext = oHandler.Execute.apply(oHandler, args);
//是否停止事件向下传递
if (stopNext === true) {
nLen = 0;
}
break;
}
}
}
}
}
};
//异步处理事件
EventCenter.prototype.PostEvent = function() {
if (arguments.length > 0) {
this._EventList.push(arguments);
if (!this._TimerID) {
this._TimerID = setTimeout(this.OnUpdate, 500, this);
}
}
};
EventCenter.prototype.raise = EventCenter.prototype.RaiseEvent;
EventCenter.prototype.raiseByTarget = EventCenter.prototype.RaiseEventByTarget;
EventCenter.prototype.emit = EventCenter.prototype.RaiseEvent;
EventCenter.prototype.post = EventCenter.prototype.PostEvent;
//心跳函数
EventCenter.prototype.OnUpdate = function(THIS) {
var len = THIS._EventList.length;
for (var i = 0; i < len; ++i) {
var args = THIS._EventList[i];
THIS.RaiseEvent.apply(THIS, args);
delete THIS._EventList[i];
}
THIS._EventList.splice(0, len);
if (THIS._EventList.length) {
THIS._TimerID = setTimeout(THIS.OnUpdate, 500, THIS);
} else {
clearTimeout(THIS._TimerID);
THIS._TimerID = null;
}
};
var eventCenter = null;
EventCenter.GetInstance = function() {
if (!eventCenter) {
eventCenter = new EventCenter();
}
return eventCenter;
};
EventCenter.GetInstance();
引用JS写的一个事件的分发中心
最新推荐文章于 2024-04-10 14:39:20 发布