kis-js/src/modules/event.js

253 lines
5.3 KiB
JavaScript

/**
* Event
*
* Event api wrapper
*/
(function (){
"use strict";
// Property name for expandos on DOM objects
var kis_expando = "KIS_0_5_0";
var _attach, _remove, _add_remove, e, _attach_delegate;
// Define the proper _attach and _remove functions
// based on browser support
if(typeof document.addEventListener !== "undefined")
{
/**
* @private
*/
_attach = function (sel, event, callback)
{
if(typeof sel.addEventListener !== "undefined")
{
//Duplicated events are dropped, per the specification
sel.addEventListener(event, callback, false);
}
};
/**
* @private
*/
_remove = function (sel, event, callback)
{
if(typeof sel.removeEventListener !== "undefined")
{
sel.removeEventListener(event, callback, false);
}
};
}
//typeof function doesn't work in IE where attachEvent is available: brute force it
else if(typeof document.attachEvent !== "undefined")
{
/**
* @private
*/
_attach = function (sel, event, callback)
{
function listener () {
// Internet Explorer fails to correctly set the 'this' object
// for event listeners, so we need to set it ourselves.
callback.apply(arguments[0]);
}
if (typeof sel.attachEvent !== "undefined")
{
_remove(event, callback); // Make sure we don't have duplicate listeners
sel.attachEvent("on" + event, listener);
// Store our listener so we can remove it later
var expando = sel[kis_expando] = sel[kis_expando] || {};
expando.listeners = expando.listeners || {};
expando.listeners[event] = expando.listeners[event] || [];
expando.listeners[event].push({
callback: callback,
listener: listener
});
}
else
{
console.log("Failed to _attach event:"+event+" on "+sel);
}
};
/**
* @private
*/
_remove = function (sel, event, callback)
{
if(typeof sel.detachEvent !== "undefined")
{
var expando = sel[kis_expando];
if (expando && expando.listeners
&& expando.listeners[event])
{
var listeners = expando.listeners[event];
var len = listeners.length;
for (var i=0; i<len; i++)
{
if (listeners[i].callback === callback)
{
sel.detachEvent("on" + event, listeners[i].listener);
listeners.splice(i, 1);
if(listeners.length === 0)
{
delete expando.listeners[event];
}
return;
}
}
}
}
};
}
_add_remove = function (sel, event, callback, add)
{
var i, len;
if(typeof sel === "undefined")
{
console.log(arguments);
console.log(event);
return false;
}
//Multiple events? Run recursively!
if (!event.match(/^([\w\-]+)$/))
{
event = event.split(" ");
len = event.length;
for (i = 0; i < len; i++)
{
_add_remove(sel, event[i], callback, add);
}
return;
}
if(add === true)
{
_attach(sel, event, callback);
}
else
{
_remove(sel, event, callback);
}
};
_attach_delegate = function(sel, target, event, callback)
{
//_attach the listener to the parent object
_add_remove(sel, event, function(e){
var elem, t;
//Get the live version of the target selector
t = $_.$(target);
//Check each element to see if it matches the target
for(elem in t)
{
//Fire target callback when event bubbles from target
if(e.target == t[elem])
{
//Trigger the event callback
callback.call(t[elem], e);
//Stop event propegation
e.stopPropagation();
}
}
}, true);
};
// --------------------------------------------------------------------------
/**
* @namespace
* @name event
* @memberOf $_
*/
e = {
/**
* Adds an event that returns a callback when triggered on the selected
* event and selector
*
* @memberOf $_.event
* @name add
* @function
* @example Eg. $_("#selector").event.add("click", do_something());
* @param string event
* @param function callback
* @return void
*/
add: function (event, callback)
{
$_.each(function(e){
_add_remove(e, event, callback, true);
});
},
/**
* Removes an event bound the the specified selector, event type, and callback
*
* @memberOf $_.event
* @name remove
* @function
* @example Eg. $_("#selector").event.remove("click", do_something());
* @param string event
* @param string callback
* @return void
*/
remove: function (event, callback)
{
$_.each(function(e){
_add_remove(e, event, callback, false);
});
},
/**
* Binds a persistent, delegated event
*
* @memberOf $_.event
* @name live
* @function
* @example Eg. $_.event.live(".button", "click", do_something());
* @param string target
* @param string event
* @param function callback
* @return void
*/
live: function (target, event, callback)
{
_attach_delegate(document.documentElement, target, event, callback);
},
/**
* Binds an event to a parent object
*
* @memberOf $_.event
* @name delegate
* @function
* @example Eg. $_("#parent").delegate(".button", "click", do_something());
* @param string target
* @param string event_type
* @parma function callback
* @return void
*/
delegate: function(target, event, callback)
{
$_.each(function(e){
_attach_delegate(e, target, event, callback);
});
}
};
$_.ext('event', e);
}());