Fixed a minor issue with ajax module, removed unused variables, added Camelcase to css function, and added String.trim polyfill

This commit is contained in:
Timothy Warren 2011-07-11 13:11:06 -04:00
parent 9014a081ab
commit f3963925a3

44
kis.js
View File

@ -20,6 +20,16 @@
var $_, $; var $_, $;
/**
* String trim function polyfill
*/
if(typeof String.prototype.trim === "undefined")
{
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g, "");
};
}
/** /**
* $ * $
* *
@ -39,6 +49,10 @@
{ {
x = document.getElementsByTagName(a); x = document.getElementsByTagName(a);
} }
else if(a.match(/^\.([\w\-]+)$/) && document.getElementsByClassName !== "undefined")
{
x = document.getElementsByClassName(a);
}
else else
{ {
x = document.querySelectorAll(a); x = document.querySelectorAll(a);
@ -86,7 +100,7 @@
$_.each = function (callback) $_.each = function (callback)
{ {
var sel = $_.el var sel = $_.el;
//Don't try to iterate over the window object //Don't try to iterate over the window object
if(sel === window || typeof sel === "undefined") if(sel === window || typeof sel === "undefined")
@ -142,8 +156,10 @@
var type = (isPost) ? "POST" : "GET"; var type = (isPost) ? "POST" : "GET";
url += (type === "GET") ? "?" + this._serialize(data, true) : ''; var d = (data.length > 0) ? "?"+this._serialize(data, true) : '';
url += (type === "GET") ? d : '';
request.open(type, url); request.open(type, url);
request.onreadystatechange = function () request.onreadystatechange = function ()
@ -426,10 +442,10 @@
add_remove = function (event, callback, add) add_remove = function (event, callback, add)
{ {
var i, len, selx; var i, len;
//Get the DOM object //Get the DOM object
var sel = $_.el var sel = $_.el;
if(arguments.length === 4) if(arguments.length === 4)
{ {
@ -496,7 +512,7 @@
//Private function for getting/setting attributes //Private function for getting/setting attributes
function _attr(sel, name, value) function _attr(sel, name, value)
{ {
var oldVal, doAttr, i; var oldVal, doAttr;
//Get the value of the attribute, if it exists //Get the value of the attribute, if it exists
if (typeof sel.hasAttribute !== "undefined") if (typeof sel.hasAttribute !== "undefined")
@ -638,12 +654,20 @@
} }
} }
function _toCamel(s)
{
return s.replace(/(\-[a-z])/g, function($1){
return $1.toUpperCase().replace('-','');
});
}
function _css(sel, prop, val) function _css(sel, prop, val)
{ {
var equi; var equi;
//Todo: camelCase things like border-left to borderLeft //Camel-case
prop = _toCamel(prop);
//Let's try the easy way first //Let's try the easy way first
if(typeof sel.style[prop] !== "undefined") if(typeof sel.style[prop] !== "undefined")
@ -679,16 +703,12 @@
d = { d = {
addClass: function (c) addClass: function (c)
{ {
var sel = $_.el
$_.each(function (e){ $_.each(function (e){
_class(e, c, true); _class(e, c, true);
}); });
}, },
removeClass: function (c) removeClass: function (c)
{ {
var sel = $_.el
$_.each(function (e){ $_.each(function (e){
_class(e, c, false); _class(e, c, false);
}); });
@ -708,7 +728,7 @@
}, },
attr: function (name, value) attr: function (name, value)
{ {
var sel = $_.el var sel = $_.el;
//Make sure you don't try to get a bunch of elements //Make sure you don't try to get a bunch of elements
if (sel.length > 1 && typeof value === "undefined") if (sel.length > 1 && typeof value === "undefined")
@ -732,7 +752,7 @@
{ {
var oldValue, set, type, sel; var oldValue, set, type, sel;
var sel = $_.el sel = $_.el;
set = (typeof value !== "undefined") ? true : false; set = (typeof value !== "undefined") ? true : false;