Moved functions for compatibility into Skeleton namespace.

This commit is contained in:
Eric Kever 2011-08-10 13:20:52 -04:00
parent 70ab570ba4
commit 553f2fa375

View File

@ -8,17 +8,14 @@
* un-jQuerified by Eric Kever * un-jQuerified by Eric Kever
*/ */
(function(){
var Skeleton = {
getElementsByClassName : function(context, cls){
if(context.getElementsByClassName){
return context.getElementsByClassName(cls);
}
if(window.addEventListener){ var ele = context.getElementsByTagName("*"),
var BIND_HANDLER = "addEventListener";
var BIND_HANDLER_PREFIX = "";
}else if(window.attachEvent){
var BIND_HANDLER = "attachEvent";
var BIND_HANDLER_PREFIX = "on";
}
function getElementsByClassName(context, tag, cls){
var ele = context.getElementsByTagName(tag),
ret = [], ret = [],
current, current,
classes; classes;
@ -35,9 +32,27 @@ function getElementsByClassName(context, tag, cls){
} }
return ret; return ret;
} },
function addClass(element, name){ filterTags : function(list, tags){
var exists = false;
for(var i = 0, j = list.length; i < j; i++){
for(var k = 0, l = tags.length; k < l; k++){
if(list[i].tagName === tags[k]){
exists = true;
break;
}
}
if(exists === true){
list.splice(i, 1);
}
}
return list;
},
addClass : function(element, name){
var classes = (element.getAttribute("className") || element.className).split(" "), var classes = (element.getAttribute("className") || element.className).split(" "),
exists = false; exists = false;
@ -51,9 +66,9 @@ function addClass(element, name){
classes.push(name); classes.push(name);
element.className = classes.join(" "); element.className = classes.join(" ");
} }
} },
function removeClass(element, name){ removeClass : function(element, name){
var classes = (element.getAttribute("className") || element.className).split(" "), var classes = (element.getAttribute("className") || element.className).split(" "),
exists = false; exists = false;
@ -67,17 +82,36 @@ function removeClass(element, name){
classes.splice(i, 1); classes.splice(i, 1);
element.className = classes.join(" "); element.className = classes.join(" ");
} }
} },
addListener : function(element, on, fn, last){
last = (last || false);
var BH;
if(window.addEventListener){ //AddEventListener takes precedence here
BH = "addEventListener";
}else if(window.attachEvent){
BH = "attachEvent";
on = "on" + on;
}
element[BH](on, function(e){
var event = e || window.event;
return fn.call(element, event); //Force it to call the handler in the proper context (IE 7 & 8 do not)
}, last);
}
};
window.Skeleton = Skeleton;
})();
function doFancyExpensiveTabThings(){ function doFancyExpensiveTabThings(){
var tabs = (document.getElementsByClassName ? document.getElementsByClassName("tabs") : getElementsByClassName(document, "ul", "tabs")); var tabs = Skeleton.filterTags(Skeleton.getElementsByClassName(document, "tabs"), ["ul"]);
for(var i = 0, j = tabs.length; i < j; i++){ for(var i = 0, j = tabs.length; i < j; i++){
var tabList = tabs[i].getElementsByTagName('li'); var tabList = tabs[i].getElementsByTagName("li");
for(var k = 0, l = tabList.length; k < l; k++){ for(var k = 0, l = tabList.length; k < l; k++){
tabList[k].getElementsByTagName('a')[0][BIND_HANDLER](BIND_HANDLER_PREFIX + "click", function(e){ Skeleton.addListener(tabList[k].getElementsByTagName('a')[0], "click", function(e){
var event = e || window.event;
return (function(e){
var contentLocation = this.href.substr(this.href.indexOf("#")) + "Tab", var contentLocation = this.href.substr(this.href.indexOf("#")) + "Tab",
contentElement, contentElement,
siblings; siblings;
@ -91,25 +125,25 @@ function doFancyExpensiveTabThings(){
} }
for(var m = 0; m < k; m++){ for(var m = 0; m < k; m++){
removeClass(tabList[m].getElementsByTagName('a')[0], "active"); Skeleton.removeClass(tabList[m].getElementsByTagName('a')[0], "active");
} }
addClass(this, "active");
Skeleton.addClass(this, "active");
contentElement = document.getElementById(contentLocation.substr(1)); contentElement = document.getElementById(contentLocation.substr(1));
addClass(contentElement, "active"); Skeleton.addClass(contentElement, "active");
siblings = contentElement.parentNode.getElementsByTagName('li'); siblings = contentElement.parentNode.getElementsByTagName('li');
for(m = 0, n = siblings.length; m < n; m++){ for(m = 0, n = siblings.length; m < n; m++){
if(siblings[m] !== contentElement){ if(siblings[m] !== contentElement){
removeClass(siblings[m], "active"); Skeleton.removeClass(siblings[m], "active");
} }
} }
} }
return false; return false;
}).call(event.target || event.srcElement, event); });
}, false);
} }
} }
} }
window[BIND_HANDLER](BIND_HANDLER_PREFIX + "load", doFancyExpensiveTabThings, false); Skeleton.addListener(window, "load", doFancyExpensiveTabThings);