Skip to content

Commit

Permalink
Adding initial versions of utils files
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Apr 6, 2011
1 parent 7cc1037 commit 9b162ec
Show file tree
Hide file tree
Showing 2 changed files with 297 additions and 0 deletions.
164 changes: 164 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//Bind a function to an object
function bind(obj, func){
return function() { return func.apply(obj, arguments); };
}
/**
* Get an array of elements with the given className - using the given object as a context
* if no object is given document.body is used.
*/
function getElementsByClassName(className, obj){
var objs = new Array();
if(!obj) var obj = document.body;

if(obj.className.indexOf(className) > -1){
objs.push(obj);
}
if(obj.childNodes.length){
var children = obj.childNodes;
for(var i = 0; i < children.length; i++){
objs = objs.concat(getElementsByClassName(className, children[i]));
}
}
return objs;
}

/**
* Get the absolute top of an element
*/
function getTop(obj){
var top = 0;
while(obj){
top += parseInt(obj.offsetTop);
obj = obj.offsetParent;
}

return top;
}

/**
* Get the absolute top and left of an object
* if you only need the top it is more efficient to use the getTop function
*/
function getTopAndLeft(obj){
var top = 0;
var left = 0;
while(obj){
top += parseInt(obj.offsetTop);
left += parseInt(obj.offsetLeft);
obj = obj.offsetParent;
}

return { 'top' : top, 'left' : left };
}

/**
* Get the current css property for the given obj
*
*/
function getCSS(obj, prop){
var properties;
if(window.getComputedStyle){
properties = window.getComputedStyle(obj, null);

}
else if(obj.currentStyle){
properties = obj.currentStyle
}

if(prop == 'background'){
var color = properties['backgroundColor']?properties['backgroundColor']:"";
var img = properties['backgroundImage']?properties['backgroundImage']:"";
var repeat = properties['backgroundRepeat']?properties['backgroundRepeat']:"";
var attach = properties['backgroundAttachment']?properties['backgroundAttachment']:"";
var position = properties['backgroundPosition']?properties['backgroundPosition']:"";

return color + ' ' + img + ' ' + repeat + ' ' + attach + ' ' + position;
}
else {
return properties[prop];
}
}

/**
* Fade in after the timeout period.
* When the fade function is completed the callback function will be called
*/
function fadeInWait(obj, timeout, cb){
obj.style.opacity = '0';
obj.style.filter = 'alpha(opacity=0)';

var animate = new Animate(obj);
setTimeout(function(){
animate.fadeSlow(cb);
}, timeout);
}

/**
* Javascript class to register functions to run when the document is 'Ready'
*
* To register a function to run on DOM Ready simply call 'dR.add(<your function>;'
*/

function DocReady(func){
this.init();
if(func) this.add(func);
}

DocReady.prototype.readyFunctions = function() { };

DocReady.prototype.init = function(){
//flag
this.ran = false;

this.addListeners();
}

DocReady.prototype.addListeners = function()
{
var runFunc = bind(this, this.readyFunctions);
//register event Readyrs
if(document.addEventListener){ //Moz or Opera
document.addEventListener('DOMContentLoaded', runFunc, false);
window.addEventListener('load', runFunc, false); //just in case
} else if(document.all && !window.opera && document.readyState) { //IE
var src = (window.location.protocol == 'https:') ? '://0' : 'javascript:void(0)';
document.write("<script id='DOMReady' defer=true src='" + src + "'><\/script>");
document.getElementById("DOMReady").onreadystatechange=function(){
if (this.readyState=="complete"){ runFunc(); }
}
} else if(document.readyState && (navigator.userAgent.indexOf('AppleWebKit/') > -1)){ //safari
this.timer = setInterval(function() {
if (document.readyState == 'loaded' || document.readyState == 'complete') { runFunc(); }
}, 50);
} else { //older browsers
var fn = window.onload;
window.onload = function() {
runFunc();
if (fn) fn();
}
}
}

DocReady.prototype.add = function(func){
if(typeof(func) != 'function'){ return false; }
if(this.ran){ return func(); }
var fn;
if(typeof(this.readyFunctions) == 'function'){
fn = bind(this, this.readyFunctions);
}else{
fn = function(){};
}

this.readyFunctions = function () {
if(!this.ran){
if(this.timer){ clearInterval(this.timer); }
fn();
func();
}
this.ran = true;
}

this.addListeners();
}
//Create a DocReady object to register functions with
var dR = new DocReady();
133 changes: 133 additions & 0 deletions utilsClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* Group of utility functions for javascript
*
* @author jcaldwell
* @file utils.js
**/

var Utils = {
//add a class to an element
addClass : function(e, c){
if(!e.className.match(new RegExp("\b"+c+"\b"))){
e.className += e.className == '' ? c : ' '+c;
}
},
//remove a class from an element
rmClass : function(e, c){
e.className = e.className.replace(new RegExp("\b"+c+"\b", 'g'), '');
},
//A function for creating a DOM element and assigning attributes and content
createElement : function(tagName, content, attr){
var el = document.createElement(tagName);
if(el){
switch (typeof(content))
{
case 'object' :
if(content && content != null)
el.appendChild(content);
break;
case 'string' :
if(content.indexOf('<') > -1 && content.indexOf('>') > -1){
content.innerHTML = content;
} else {
if(tagName == 'input'){
el.value = content;
} else {
el.appendChild(document.createTextNode(content));
}
}
break;
default :
//We aren't sure what this is but we'll just set the innerHTML element and call it a day
typeof(el.innerHTML) != 'undefined' ? el.innerHTML = content : content;
}
//apply any provided attributes
for(var a in attr){
el[a] = attr[a];
}
}
return el;

},
//returns the 'real type' of an object instead of just 'Object'
realType : function(obj){
if(obj && obj.constructor){
return obj.constructor.name;
} else {
return typeof(obj);
}
},
//Inspect an object - returns a string
inspect : function(obj, index, tabs){
if(!index){
var index = '';
}else{
index += ' : ';
}
if(!tabs) var tabs = 0;

var rtype = u.realType(obj);

var mTabs = function(){
var str = '';
for(var i = 0; i < tabs; i++){
str += "\t";
}
return str;
};

if(typeof(obj) != 'object'){
return index + obj + ' <'+typeof(obj)+'>';
}
var ss = ' {', se = '}';
if (rtype == 'Array'){
ss = ' ['; se = ']';
}

var str = index + rtype + ss;
for(var sub in obj){
var ind = rtype =='Array' ? '' : sub;
str += "\n\t"+mTabs()+ u.inspect(obj[sub], ind, tabs+1)+',';
}
if(str.indexOf(',') > -1) str = str.substr(0, str.length-1);
str+= "\n"+mTabs()+se;
return str;

},
//inspect an object - returns an HTMLDivElement
inspectToElement : function(obj, index){
if(!index){
var index = '';
}else{
index += ' : ';
}
var rtype = u.realType(obj);

if(typeof(obj) != 'object'){
return u.createElement('div', index + obj + ' ('+typeof(obj)+')', {className : 'value'});
}
else if (rtype == 'Array'){
var el = u.createElement('div', index + rtype + ' [', {className : 'object-inspector'});
var children = u.createElement('ul', null, {className : 'child-list'});
el.appendChild(children);
for(var i in obj){
children.appendChild(u.createElement('li', u.inspectToElement(obj[i]), {className : 'child'}));
}
el.appendChild(document.createTextNode(']'));
return el;
}
else {
var el = u.createElement('div', index + rtype, {className : 'object-inspector'});
var children = u.createElement('ul', null, {className : 'child-list'});
el.appendChild(children);

for(var sub in obj){
children.appendChild(u.createElement('li', u.inspectToElement(obj[sub], sub), {className : 'child'}));
}

return el;
}
}
};
//alias window.u and Utils for shortness
window.u = Utils;

0 comments on commit 9b162ec

Please sign in to comment.