2011-10-24 12:26:00 -04:00
|
|
|
/**
|
2011-11-01 18:56:27 -04:00
|
|
|
* Event
|
2011-10-24 12:26:00 -04:00
|
|
|
*
|
|
|
|
* Event api wrapper
|
|
|
|
*/
|
|
|
|
(function (){
|
|
|
|
|
2011-11-01 08:29:21 -04:00
|
|
|
"use strict";
|
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
// Property name for expandos on DOM objects
|
2011-11-01 18:56:27 -04:00
|
|
|
var kis_expando = "KIS_0_5_0";
|
2011-10-24 12:26:00 -04:00
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
var _attach, _remove, _add_remove, e, _attach_delegate;
|
2011-10-24 12:26:00 -04:00
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
// Define the proper _attach and _remove functions
|
2011-10-24 12:26:00 -04:00
|
|
|
// based on browser support
|
|
|
|
if(typeof document.addEventListener !== "undefined")
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_attach = function (sel, event, callback)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
if(typeof sel.addEventListener !== "undefined")
|
|
|
|
{
|
2011-11-06 17:11:34 -05:00
|
|
|
// Duplicated events are dropped, per the specification
|
2011-11-01 18:56:27 -04:00
|
|
|
sel.addEventListener(event, callback, false);
|
|
|
|
}
|
2011-10-24 12:26:00 -04:00
|
|
|
};
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_remove = function (sel, event, callback)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
if(typeof sel.removeEventListener !== "undefined")
|
|
|
|
{
|
|
|
|
sel.removeEventListener(event, callback, false);
|
|
|
|
}
|
2011-10-24 12:26:00 -04:00
|
|
|
};
|
|
|
|
}
|
2011-11-06 17:11:34 -05:00
|
|
|
// typeof function doesn't work in IE where attachEvent is available: brute force it
|
2011-10-24 12:26:00 -04:00
|
|
|
else if(typeof document.attachEvent !== "undefined")
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_attach = function (sel, event, callback)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
2011-11-03 16:04:35 -04:00
|
|
|
function _listener () {
|
2011-10-24 12:26:00 -04:00
|
|
|
// Internet Explorer fails to correctly set the 'this' object
|
|
|
|
// for event listeners, so we need to set it ourselves.
|
2011-11-01 08:29:21 -04:00
|
|
|
callback.apply(arguments[0]);
|
2011-10-24 12:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof sel.attachEvent !== "undefined")
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
_remove(event, callback); // Make sure we don't have duplicate listeners
|
2011-10-24 12:26:00 -04:00
|
|
|
|
2011-11-03 16:04:35 -04:00
|
|
|
sel.attachEvent("on" + event, _listener);
|
|
|
|
// Store our _listener so we can remove it later
|
2011-10-24 12:26:00 -04:00
|
|
|
var expando = sel[kis_expando] = sel[kis_expando] || {};
|
|
|
|
expando.listeners = expando.listeners || {};
|
|
|
|
expando.listeners[event] = expando.listeners[event] || [];
|
|
|
|
expando.listeners[event].push({
|
|
|
|
callback: callback,
|
2011-11-03 16:04:35 -04:00
|
|
|
_listener: _listener
|
2011-10-24 12:26:00 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
console.log("Failed to _attach event:"+event+" on "+sel);
|
2011-10-24 12:26:00 -04:00
|
|
|
}
|
|
|
|
};
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_remove = function (sel, event, callback)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2011-11-03 16:04:35 -04:00
|
|
|
sel.detachEvent("on" + event, listeners[i]._listener);
|
2011-10-24 12:26:00 -04:00
|
|
|
listeners.splice(i, 1);
|
|
|
|
if(listeners.length === 0)
|
|
|
|
{
|
|
|
|
delete expando.listeners[event];
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-11-01 18:56:27 -04:00
|
|
|
|
|
|
|
_add_remove = function (sel, event, callback, add)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
|
|
|
var i, len;
|
|
|
|
|
|
|
|
if(typeof sel === "undefined")
|
|
|
|
{
|
|
|
|
console.log(arguments);
|
|
|
|
console.log(event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:11:34 -05:00
|
|
|
// Multiple events? Run recursively!
|
2011-10-24 12:26:00 -04:00
|
|
|
if (!event.match(/^([\w\-]+)$/))
|
|
|
|
{
|
|
|
|
event = event.split(" ");
|
|
|
|
|
|
|
|
len = event.length;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
_add_remove(sel, event[i], callback, add);
|
2011-10-24 12:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(add === true)
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
_attach(sel, event, callback);
|
2011-10-24 12:26:00 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
_remove(sel, event, callback);
|
2011-10-24 12:26:00 -04:00
|
|
|
}
|
|
|
|
};
|
2011-11-01 18:56:27 -04:00
|
|
|
|
|
|
|
_attach_delegate = function(sel, target, event, callback)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
2011-11-06 17:11:34 -05:00
|
|
|
// attach the listener to the parent object
|
2011-11-01 20:14:38 -04:00
|
|
|
_add_remove(sel, event, function(e){
|
2011-10-24 12:26:00 -04:00
|
|
|
|
2011-11-04 16:42:07 -04:00
|
|
|
var elem, t, tar;
|
|
|
|
|
2011-11-06 17:11:34 -05:00
|
|
|
// IE 8 doesn't have event bound to element
|
2011-11-04 16:42:07 -04:00
|
|
|
e = e || window.event;
|
2011-11-01 18:56:27 -04:00
|
|
|
|
2011-11-06 17:11:34 -05:00
|
|
|
// Get the live version of the target selector
|
2011-11-01 18:56:27 -04:00
|
|
|
t = $_.$(target);
|
|
|
|
|
2011-11-06 17:11:34 -05:00
|
|
|
// Check each element to see if it matches the target
|
2011-11-01 20:14:38 -04:00
|
|
|
for(elem in t)
|
2011-11-01 18:56:27 -04:00
|
|
|
{
|
2011-11-06 17:11:34 -05:00
|
|
|
// IE 8 doesn't have target in the event object
|
2011-11-04 16:42:07 -04:00
|
|
|
tar = e.target || e.srcElement;
|
|
|
|
|
2011-11-06 17:11:34 -05:00
|
|
|
// Fire target callback when event bubbles from target
|
2011-11-04 16:42:07 -04:00
|
|
|
if(tar == t[elem])
|
2011-11-01 18:56:27 -04:00
|
|
|
{
|
2011-11-06 17:11:34 -05:00
|
|
|
// Trigger the event callback
|
2011-11-01 20:14:38 -04:00
|
|
|
callback.call(t[elem], e);
|
|
|
|
|
2011-11-06 17:11:34 -05:00
|
|
|
// Stop event propegation
|
2011-11-01 20:14:38 -04:00
|
|
|
e.stopPropagation();
|
2011-11-01 18:56:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
}, true);
|
|
|
|
};
|
|
|
|
|
2011-11-01 20:14:38 -04:00
|
|
|
|
|
|
|
|
2011-10-24 12:26:00 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
2011-11-03 16:04:35 -04:00
|
|
|
* Event Listener module
|
|
|
|
*
|
2011-11-01 18:56:27 -04:00
|
|
|
* @namespace
|
|
|
|
* @name event
|
|
|
|
* @memberOf $_
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
e = {
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
add: function (event, callback)
|
|
|
|
{
|
|
|
|
$_.each(function(e){
|
2011-11-01 18:56:27 -04:00
|
|
|
_add_remove(e, event, callback, true);
|
2011-10-24 12:26:00 -04:00
|
|
|
});
|
|
|
|
},
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-10-24 12:26:00 -04:00
|
|
|
remove: function (event, callback)
|
|
|
|
{
|
|
|
|
$_.each(function(e){
|
2011-11-01 18:56:27 -04:00
|
|
|
_add_remove(e, event, callback, false);
|
2011-10-24 12:26:00 -04:00
|
|
|
});
|
2011-11-01 18:56:27 -04:00
|
|
|
},
|
|
|
|
/**
|
2011-11-03 16:04:35 -04:00
|
|
|
* Binds a persistent event to the document
|
|
|
|
*
|
2011-11-01 18:56:27 -04:00
|
|
|
* @memberOf $_.event
|
|
|
|
* @name live
|
|
|
|
* @function
|
|
|
|
* @example Eg. $_.event.live(".button", "click", do_something());
|
|
|
|
* @param string target
|
|
|
|
* @param string event
|
|
|
|
* @param function callback
|
|
|
|
*/
|
|
|
|
live: function (target, event, callback)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
2011-11-01 18:56:27 -04:00
|
|
|
_attach_delegate(document.documentElement, target, event, callback);
|
2011-10-24 12:26:00 -04:00
|
|
|
},
|
2011-11-01 18:56:27 -04:00
|
|
|
/**
|
|
|
|
* 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
|
2011-11-03 16:04:35 -04:00
|
|
|
* @param function callback
|
2011-11-01 18:56:27 -04:00
|
|
|
*/
|
2011-11-06 17:11:34 -05:00
|
|
|
delegate: function (target, event, callback)
|
2011-10-24 12:26:00 -04:00
|
|
|
{
|
|
|
|
$_.each(function(e){
|
2011-11-01 18:56:27 -04:00
|
|
|
_attach_delegate(e, target, event, callback);
|
2011-10-24 12:26:00 -04:00
|
|
|
});
|
2011-11-01 18:56:27 -04:00
|
|
|
}
|
2011-10-24 12:26:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
$_.ext('event', e);
|
|
|
|
|
|
|
|
}());
|