Scripts
Fastjs (mini) framework
Dit is een super klein framework(je) gemaakt met Javascript. Ik heb dit een tijdje geleden geschreven om te oefenen met Javascript, ik weet niet of iemand er nog wat aan heeft. En om sommige vragen te voorkomen: Nee, dit framework is bij langer na niet beter dan jQuery, mootools e.t.c. wel een stuk kleiner. ** aangepast op verzoek van de auteur **
fastjs.js
[code]
/*
Fastjs Mini Framework (Based on jQuery)
Author: Dillen Meijboom
License: None
*/
var fastjs = { //Defining the Fastjs object
getElement : function(obj) { //The dom function
var type = "default";
var nr = false;
var attr = false;
var objStr = obj;
if (obj.nodeName) { //Check if it is an object.
for (property in fastjs.obj) {
obj[property] = fastjs.obj[property];
}
return obj;
}
if (typeof(obj) != "string") {
return false;
}
//Check options and create variables
if (obj.indexOf('|') > 0 && !arguments[1]) {
return true;
}
if (obj.indexOf('>') > 0) {
nr = obj.split(">")['1'].replace(/ /gi, "");
obj = obj.split(">")['0'].replace(/ /gi, "");
}
if (obj.indexOf('(') > 0) {
attr = obj.split("(")['1'].replace(/ /gi, "").replace(")", "").split("|");
obj = obj.split("(")['0'].replace(/ /gi, "");
}
var array = obj.split("");
switch(array['0']) {
case ".":
var cName = obj.substr(1);
var elements = document.getElementsByTagName("*");
var rnr = 0;
var result = [];
for (i in elements) {
var el = elements[i];
var hasAttr = true;
if (el.className == cName) {
if (attr) {
for (j in attr) {
var attrName = attr[j].split("=")['0'].replace("class", "className");
var attrValue = attr[j].split("=")['1'];
if (el[attrName] && el[attrName] != attrValue) {
hasAttr = false;
}
if (!el[attrName]) hasAttr = false;
}
}
if (hasAttr || !attr) {
result[rnr] = el;
rnr++;
}
}
}
if (result.length < 1) return false;
if (result.length > 1 && !nr) {
var elements = result;
for (property in fastjs.obj) {
elements[property] = fastjs.obj[property];
}
return elements;
} else if (nr) {
var element = result[nr];
for (property in fastjs.obj) {
element[property] = fastjs.obj[property];
}
return element;
} else {
var element = result['0'];
for (property in fastjs.obj) {
element[property] = fastjs.obj[property];
}
return element;
}
break;
case "&":
var tagName = obj.substr(1);
var elements = document.getElementsByTagName(tagName);
var rnr = 0;
var result = [];
for (i in elements) {
var el = elements[i];
var hasAttr = true;
if (el.nodeName) {
if (attr) {
for (j in attr) {
var attrName = attr[j].split("=")['0'].replace("class", "className");
var attrValue = attr[j].split("=")['1'];
if (el[attrName] && el[attrName] != attrValue) {
hasAttr = false;
}
if (!el[attrName]) hasAttr = false;
}
}
if (hasAttr || !attr) {
result[rnr] = el;
rnr++;
}
}
}
if (result.length < 1) return false;
if (result.length > 1 && !nr) {
var elements = result;
for (property in fastjs.obj) {
elements[property] = fastjs.obj[property];
}
return elements;
} else if (nr) {
var element = result[nr];
for (property in fastjs.obj) {
element[property] = fastjs.obj[property];
}
return element;
} else {
var element = result['0'];
for (property in fastjs.obj) {
element[property] = fastjs.obj[property];
}
return element;
}
break;
case "#":
var idName = obj.substr(1);
var elements = document.getElementsByTagName("*");
var rnr = 0;
var result = [];
for (i in elements) {
var el = elements[i];
var hasAttr = true;
if (el.id && el.id == idName) {
if (attr) {
for (j in attr) {
var attrName = attr[j].split("=")['0'].replace("class", "className");
var attrValue = attr[j].split("=")['1'];
if (el[attrName] && el[attrName] != attrValue) {
hasAttr = false;
}
if (!el[attrName]) hasAttr = false;
}
}
if (hasAttr || !attr) {
result[rnr] = el;
rnr++;
}
}
}
if (result.length < 1) return false;
if (result.length > 1 && !nr) {
var elements = result;
for (property in fastjs.obj) {
elements[property] = fastjs.obj[property];
}
return elements;
} else if (nr) {
var element = result[nr];
for (property in fastjs.obj) {
element[property] = fastjs.obj[property];
}
return element;
} else {
var element = result['0'];
for (property in fastjs.obj) {
element[property] = fastjs.obj[property];
}
return element;
}
break;
default:
if (document.getElementById(obj)) {
return $("#"+objStr);
} else {
if (document.getElementsByTagName(obj).length > 0) {
return $("&"+objStr);
} else {
return false;
}
}
break;
}
},
empty : true,
each : function(array, func) {
for (x in array) {
func(array[x], x); //Call the users function.
}
},
hasWord : function(variable, word) {
return variable.indexOf(word) > -1 ? true : false;
},
ready : function(func) {
var windowOnload = window.onload;
if (windowOnload) {
if (document.readyState == "complete") {
func();
return true;
} else {
setTimeout("fastjs.ready("+func.toString()+")", 1);
return false;
}
return true;
} else {
setTimeout("fastjs.ready("+func.toString()+")", 1);
return false;
}
},
doWithObject : function(array, func) {
if (array['1']) {
for (var i=0; i<array.length; i++) {
func(array[i]);
}
} else {
func(array);
}
},
ajax : function(page, options) {
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
return false; //Browser doesnt support AJAX.
}
}
}
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState < 4) {
if (options.preload) {
options.preload(ajaxRequest.readyState); //Give the preloader its current status.
}
}
if(ajaxRequest.readyState == 4) {
if (options.ready) {
options.ready(ajaxRequest.responseText); //Call ready function.
}
}
}
ajaxRequest.open("GET", page, true);
ajaxRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1000 00:00:00 GMT");
ajaxRequest.send(null);
},
browser : {
ff : navigator.userAgent.toLowerCase().indexOf("firefox")> 0 ? true : false,
chrome : navigator.userAgent.toLowerCase().indexOf("chrome")> 0 ? true : false,
ie : navigator.appName.toLowerCase()==="microsoft internet explorer" ? true : false,
opera : window.opera ? true : false
},
obj : {
tag : function(tagName) {
if (arguments['1']) {
var nr = arguments['1'];
return $(this.getElementsByTagName(tagName)[nr]);
} else {
return this.getElementsByTagName(tagName);
}
},
selectAll : function() {
var func = arguments['0'];
fastjs.each(this.getElementsByTagName("input"), function(obj) {
if (obj.type == "checkbox") {
obj.checked = true;
if (func) {
func(obj);
}
}
});
},
selectNone : function() {
var func = arguments['0'];
fastjs.each(this.getElementsByTagName("input"), function(obj) {
if (obj.type == "checkbox") {
obj.checked = false;
if (func) {
func(obj);
}
}
});
},
on : function(event, func) {
fastjs.doWithObject(this, function(obj) {
if (event.split("|")['1']) {
var events = event.split("|");
for (x in events) {
if (fastjs.browser.ie) {
var thisEvent = "on"+events[x];
obj.attachEvent(thisEvent, func);
} else {
var thisEvent = events[x];
obj.addEventListener(thisEvent, func, false);
}
}
} else {
if (fastjs.browser.ie) {
event = "on"+event;
obj.attachEvent(event, func);
} else {
obj.addEventListener(event, func, false);
}
}
});
return this;
},
addClass : function(name) {
fastjs.doWithObject(this, function(obj) {
var array = obj.className.split(" ");
if (array.length == 1) {
obj.setAttribute("class", name);
} else {
obj.setAttribute("class", obj.className+" "+name);
}
});
},
remClass : function(name) {
fastjs.doWithObject(this, function(obj) {
var array = obj.className.split(" ");
var string = "";
for (x in array) {
if (array[x] != name) {
string=string+array[x]+" ";
}
}
if (array.length == 1) {
obj.setAttribute("class", string.substr(1, (string.length-1)));
} else {
obj.setAttribute("class", string.substr(0, (string.length-1)));
}
});
},
opacity : function(value) {
fastjs.doWithObject(this, function(obj) {
if (value == 100) {
obj.style.filter = "";
obj.fadeState = value;
} else {
$(obj).css("-ms-filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=50);filter:alpha(opacity="+value+");opacity:"+(value/100)+";-moz-opacity:"+(value/100)+";-khtml-opacity:"+(value/100)+";filter:progid:DXImageTransform.Microsoft.Alpha(Opacity="+value+");");
obj.fadeState = value;
}
});
return this;
},
getAttr : function(attribute) {
var results = [];
var nr = 0;
fastjs.doWithObject(this, function(obj) {
results[nr] = obj.getAttribute(attribute) ? obj.getAttribute(attribute) : false;
nr++;
});
if (results.length > 1) {
return results;
} else {
return results[0];
}
},
setAttr : function(attribute, value) {
fastjs.doWithObject(this, function(obj) {
obj.setAttribute(attribute, value);
});
return this;
},
html : function(html) {
fastjs.doWithObject(this, function(obj) {
obj.innerHTML = html;
});
return this;
},
css : function(css) {
fastjs.doWithObject(this, function(obj) {
if (obj.cssStyle) {
obj.cssStyle = obj.cssStyle+css;
} else {
obj.setAttribute("style", obj.getAttribute("style")+";"+css);
}
});
return this;
},
getCss : function(property) { //Function that requires 1 element!
if (fastjs.hasWord(property, "-") && fastjs.browser.ie) {
var array = property.split("-");
var second = array['1'].split("");
var secondStr = array['1'].substr(1, (second.length-1));
property = array['0']+second['0'].toUpperCase()+secondStr;
}
return this.currentStyle ? this.currentStyle[property]||this.style[property] : document.defaultView.getComputedStyle(this,null).getPropertyValue(property)||this.style[property];
},
attr : function(attributes) {
fastjs.doWithObject(this, function(obj) {
for (x in attributes) {
obj[x] = attributes[x];
}
});
return this;
},
show : function() {
fastjs.doWithObject(this, function(obj) {
obj.style.display = "block"; return $(this);
});
return this;
},
hide : function() {
fastjs.doWithObject(this, function(obj) {
obj.style.display = "none"; return $(this);
});
return this;
},
remove : function() {
fastjs.doWithObject(this, function(obj) {
obj.parentNode.removeChild(this);
});
return this;
}
}
};
var $ = fastjs.getElement; //Give a little shortcut to the fastjs.getElement function.
[/code]
Reacties
0