Merge pull request #3 from Na7coldwater/master

Fix issue #2 and added browser support module
This commit is contained in:
Timothy Warren 2011-07-01 03:36:24 -07:00
commit 424d4993ae
2 changed files with 136 additions and 37 deletions

View File

@ -14,6 +14,17 @@ Browser support: IE9+, Latest versions of Firefox, Chrome, Safari, Opera
## Modules: ## ## Modules: ##
**Support**: Provides browser feature detection
properties:
* attachEvent:
True if `attachEvent` is supported
* addEventListener:
True if `addEventListener` is supported
* querySelector:
True if `querySelectorAll` is supported
**Ajax**: simple, jQuery-like ajax functions **Ajax**: simple, jQuery-like ajax functions
functions: functions:
@ -60,6 +71,7 @@ Browser support: IE9+, Latest versions of Firefox, Chrome, Safari, Opera
**Event**: wrapper for applying events to DOM objects **Event**: wrapper for applying events to DOM objects
*Depends on the `Support` module*
functions: functions:

109
kis.js
View File

@ -9,8 +9,11 @@
"use strict"; "use strict";
// Property name for expandos on DOM objects
var kis_expando = "KIS_0_1_0";
//Browser requirements check //Browser requirements check
if (typeof document.querySelectorAll !== "function" || typeof window.addEventListener !== "function") if (!document.querySelectorAll)
{ {
return; return;
} }
@ -19,7 +22,7 @@
$_ = {}; $_ = {};
window.$_ = window.$_ || $_; $_ = window.$_ = window.$_ || $_;
/** /**
* $ * $
@ -35,6 +38,23 @@
window.$ = $; window.$ = $;
/**
* Support
*
* Module for browser feature detection
*/
(function (){
var support = {
attachEvent: typeof window.attachEvent === "function",
addEventListener: typeof window.addEventListener === "function",
querySelector: typeof document.querySelectorAll === "function"
};
$_.support = support;
}());
/** /**
* Ajax * Ajax
* *
@ -108,12 +128,12 @@
} }
}; };
window.$_.get = function (url, data, callback) $_.get = function (url, data, callback)
{ {
ajax._do(url, data, callback, false); ajax._do(url, data, callback, false);
}; };
window.$_.post = function (url, data, callback) $_.post = function (url, data, callback)
{ {
ajax._do(url, data, callback, true); ajax._do(url, data, callback, true);
}; };
@ -126,7 +146,7 @@
*/ */
(function (){ (function (){
window.$_.hb = (history.pushState) ? false : true; $_.hb = (history.pushState) ? false : true;
var qs = { var qs = {
parse: function (hb) parse: function (hb)
@ -207,7 +227,7 @@
} }
}; };
window.$_.qs = qs; $_.qs = qs;
}()); }());
@ -249,18 +269,25 @@
} }
}; };
window.$_.store = store; $_.store = store;
}()); }());
/** /**
* Event object * Event object
* *
* Event api wrapper * Event api wrapper
* Requires Support module
*/ */
(function (){ (function (){
var attach, remove, add_remove, e; var attach, remove, add_remove, e, support;
support = $_.support;
// Define the proper attach and remove functions
// based on browser support
if(support.addEventListener)
{
attach = function (sel, event, callback) attach = function (sel, event, callback)
{ {
if (typeof sel.addEventListener === "function") if (typeof sel.addEventListener === "function")
@ -268,7 +295,6 @@
sel.addEventListener(event, callback, false); sel.addEventListener(event, callback, false);
} }
}; };
remove = function (sel, event, callback) remove = function (sel, event, callback)
{ {
if (typeof sel.removeEventListener === "function") if (typeof sel.removeEventListener === "function")
@ -276,6 +302,59 @@
sel.removeEventListener(event, callback, false); sel.removeEventListener(event, callback, false);
} }
}; };
}
else if(support.attachEvent)
{
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(sel, arguments);
}
if (typeof sel.attachEvent === "function")
{
remove(sel, event, callback); // Make sure we don't have duplicate listeners
sel.attachEvent("on" + event, listener);
// Store our listener so we can remove it later
// TODO: Fix memory leak in IE6/7 with event listeners
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
});
}
};
remove = function (sel, event, callback)
{
if(typeof typeof sel.detachEvent === "function")
{
var expando = sel[kis_expando];
if (expando && expando.listeners
&& expando.listeners[event])
{
var listeners = expando.listeners[event];
for (var i=0; i<listeners.length; 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) add_remove = function (sel, event, callback, add)
{ {
@ -335,7 +414,7 @@
} }
}; };
window.$_.event = e; $_.event = e;
}()); }());
@ -494,6 +573,13 @@
sel = $(sel); sel = $(sel);
} }
if(!sel.length)
{
// sel is a DOM Element
callback(sel);
}
else
{
var len = sel.length; var len = sel.length;
if (len === 0) if (len === 0)
@ -510,6 +596,7 @@
{ {
callback(sel[x]); callback(sel[x]);
} }
}
}, },
addClass: function (sel, c) addClass: function (sel, c)
{ {
@ -616,7 +703,7 @@
} }
}; };
window.$_.dom = d; $_.dom = d;
}()); }());
}()); }());