Created
May 11, 2012 06:50
-
-
Save bmutinda/e712269c11d60ef156e4 to your computer and use it in GitHub Desktop.
SIMPLE lightweight javascript library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* kScript javascript library | |
* http://www.kachusoftwares.is-great.org/kscript | |
* | |
* Copyright 2012, Mutinda Boniface - boniface.info@gmail.com | |
* | |
* Date: Sun March / 08 / 2012 08:11 A.M | |
*/ | |
(function(){ | |
var document = window.document; | |
trim = String.prototype.trim; | |
var kScript = { | |
version: "1.1.1", | |
build: "111", | |
date:"Sun March / 08 / 2012 08:11 A.M", | |
developer: "Mutinda Boniface", | |
_helper: null, | |
_browser: null, | |
//kscript global elements for chaining purposes | |
_elements: [], | |
_current_element: null, | |
error : null, | |
//required elem() selector array... (#, . ,!) | |
/* # for element by id | |
. for element by classname | |
! for element by tagname | |
*/ | |
_required_selectors : ("#,.,!").split(','), | |
_required_selector_length : 0, | |
_found_selector_index : 0, | |
_id_selector_index : 0, | |
_class_selector_index : 1, | |
_tagname_selector_index : 2, | |
//kscript initialize method | |
initialize : function(){ | |
//override global variables to hold other objects in the kScript namespace.... | |
this._helper = kScript.helper; | |
this._browser = kScript.browser; | |
//initialize browser settings | |
this._browser.init(); | |
//initialize helper properties | |
this._helper.init(); | |
//required elems selectors length | |
this._required_selector_length = this._required_selectors.length; | |
}, | |
ready: function( callback ){ | |
//make sure that DOM is successfully loaded.. | |
kScript.fn.ready( callback ); | |
//call kscript.initializer method | |
kScript.initialize(); | |
}, | |
//this is a selector method...... _ks.elem("#byid"), _ks.(".byclass"), ....... | |
elem: function( ){ | |
//if is a selector...determine by id,class, tagname | |
for(var i=0; i<arguments.length; i++){ | |
if(this._selector( arguments[i] )){ | |
this._determineSelector( arguments[i] ); | |
//set the element selector itself | |
this._setSelector( this._found_selector_index ); | |
//returns the object for chaining purposes.... | |
} | |
} | |
return this; | |
}, | |
// get elements by Id | |
byid: function( ){ | |
var temporary_id_elements = []; | |
for(var i=0; i<arguments.length; i++){ | |
//am i actually working with strings? | |
if(this._helper._isString(this._current_element)){ | |
temporary_id_elements.push(document.getElementById(this._current_element)); | |
} | |
} | |
this._elements = temporary_id_elements; | |
}, | |
// get elements by classname | |
byclass:function( selector, type, parent ){ | |
//gets all the elements inside a parent element or looks in the complete page | |
var temporary_class_elements = []; | |
//regExp t check whether the element has a class | |
var pattern = new RegExp("(^| )" + selector + "( |$)"); //Regular expression to check if the elements have the class | |
//Look for the elements by tagName with the condition of we can chose the parent,select one specific tag or all of them | |
var e = (parent || document).getElementsByTagName(type || '*') | |
for(var i=0;i < e.length;i++){ | |
if(pattern.test(e[i].className)){//if the elements has the className then... | |
temporary_class_elements.push(e[i]); //Add the element to temporary_class_elements | |
} | |
} | |
this._elements = temporary_class_elements; | |
}, | |
// get elements by tagName | |
byTagName:function(){ | |
var temporary_tag_names = []; | |
for(var i=0; i<arguments.length; i++){ | |
//am i working with strings? | |
if(this._helper._isString(arguments[i])){ | |
var tags_length = document.getElementsByTagName(arguments[i]).length; | |
for(var j=0; j<tags_length; j++){ | |
temporary_tag_names.push(document.getElementsByTagName(arguments[i][j])); | |
} | |
} | |
} | |
this._elements = temporary_tag_names; | |
}, | |
//function that receives the elem( parameters ) and determines the kind of element selector | |
// byid, byclass, bytag,,,,,,, | |
_selector: function( options ){ | |
return kScript.helper._isString(options)? | |
this._helper.trim( options ).length !==0? true: //if string, length==0? | |
false: | |
false; | |
}, | |
//extract the first xter to find out whether its by id, class, tag | |
_determineSelector: function( options ){ | |
//update the _current_element holder | |
this._current_element = this._helper.trim(options); | |
var selector_determinant = this._current_element.charAt(0); | |
var result = false; | |
for(var i=0; i<this._required_selector_length; i++){ | |
result = selector_determinant===this._required_selectors[i]?true:false; | |
if(result) {this._found_selector_index = i; break;} | |
else {continue;} | |
} | |
}, | |
_setSelector : function( selector_index ){ | |
//first remove the leading selector xters .i.e. '.' or '#', or '!' | |
this._updateCurrentElement(); | |
//for element by id ..... selector_index = this._id_selector_index | |
switch( selector_index ){ | |
case this._id_selector_index: | |
this.byid(this._current_element); break; | |
case this._class_selector_index: | |
this.byclass(this._current_element); break; | |
case this._tagname_selector_index: | |
this.byTagName( this._current_element ); break; | |
default: | |
this.error = "Incorrect selectors specified... use #id_name, .class_name, !tagname"; break; | |
} | |
}, | |
//this method removes the leading '.', '#' or '!' | |
_updateCurrentElement: function(){ | |
var element_length = this._current_element.length; | |
this._current_element = this._current_element.substring(1,element_length); | |
}, | |
addClass : function( class_name ){ | |
for(var i=0; i<this._elements.length; i++){ | |
this._elements[i].className+=''+class_name; | |
} | |
//for chaining purposes..... | |
return this; | |
}, | |
/* ---------------------- events --------------------------------- */ | |
bind:function(event,callback){ kScript.events.bind(event,callback) }, | |
submit:function(){}, | |
click:function(){}, | |
over:function(){}, | |
/*----------------------- @end events --------------------------- */ | |
html : function( text ){ | |
for(var i=0; i< this._elements.length; i++){ | |
this._elements[i].innerHTML = text; | |
} | |
}, | |
/* ====== ajax =================== */ | |
kajax: function( parameters ){ | |
kScript.ajax.init( parameters ); | |
}, | |
css : function( objectStyles){ | |
for( var i=0; i<this._elements.length; i++ ){ | |
for( css_style in objectStyles ){ | |
this._elements[i].style[css_style] = objectStyles[css_style]; | |
} | |
} | |
return this; | |
} | |
} | |
/* ----------------------- events implementations --------------------------------- */ | |
kScript.events = kScript.prototype= { | |
parent: kScript, | |
bind: function(event, callback){ | |
var trimmed_event = this.parent._helper.trim(event); | |
//am i working with strings? | |
this.parent._helper._isString(trimmed_event)==true? | |
//am i blank: | |
trimmed_event=='' || trimmed_event==null?this.error = "on "+trimmed_event+" Could not be binded ": | |
//add my events man | |
this._registerEvent(trimmed_event, callback): | |
this.error = "on "+trimmed_event+" Could not be binded"; | |
}, | |
//form submit | |
submit:function(){ | |
}, | |
_registerEvent: function(event, callback){ | |
//if the first object i.e. window used addEventListener | |
if(this.parent._elements[0].addEventListener){ | |
for(var i=0; i<this.parent._elements.length; i++){ | |
this.parent._elements[i].addEventListener(event, callback, false); | |
} | |
} | |
if(this.parent._elements[0].attachEvent){ | |
for(var i=0; i<this.parent._elements.length; i++){ | |
this.parent._elements[i].attachEvent("on"+event, callback); | |
} | |
} | |
} | |
} | |
/* ---------------------- @end events implementation --------------------------------- */ | |
//kscript helper object..... | |
kScript.helper = kScript.prototype = { | |
//helper initialization called after the DOM is loaded | |
self: kScript.helper, | |
parent: kScript, | |
init : function(){ | |
leftTrim = /^\s+/; | |
rightTrim = /\s+$/; | |
name = "Mutinda "; | |
}, | |
trim : function( string_options ){ | |
return string_options.replace(self.leftTrim,"").replace(self.rightTrim,""); | |
return this; | |
}, | |
/* | |
var name = "Mutinda boniface"; -- returns string | |
var name = new String("Mutinda Boniface"); -- returns object [ Be careful ] | |
*/ | |
_parType : function( par_element ){ | |
return Object.prototype.toString.call(par_element); | |
}, | |
_isArray : function( par_element ){ | |
/*try duck typing --- not recommended | |
if(par_element.push && par_element.slice && par_element.join){ return true } | |
*/ | |
//recommended..... | |
return Object.prototype.toString.call(par_element) === '[object Array]'? true: false; | |
}, | |
_isNumber : function( par_element ){ | |
return Object.prototype.toString.call(par_element) === '[object Number]'? true: false; | |
}, | |
_isString : function( par_element ){ | |
return Object.prototype.toString.call(par_element) === '[object String]'? true: false; | |
}, | |
_isFloat : function( par_element ){ | |
//Object.prototype.toString.call(par_element) === '[object Number]'? true: false; | |
}, | |
_isHTMLobject : function( par_element){ | |
//html objects are in this format .... ['object HTMLDivElement']... | |
// starts with object then followed by HTML and the element name... | |
//get a substring from 0 - 12: [object HTML | |
//force it to act as a string--typecast | |
var object_in_string = new String(Object.prototype.toString.call(par_element)); | |
//return object_in_string.substring(0,12) === '[object HTML'? true:false; | |
return object_in_string.substring(0,12); | |
} | |
} | |
// string handlers | |
kScript.string = kScript.prototype = { | |
charAt : function( index ){ | |
return 11; | |
} | |
} | |
// kScript.browser implementation | |
kScript.browser = kScript.prototype = { | |
init : function(){ | |
//reqExpressions for browsers... | |
// Useragent RegExp | |
rwebkit = /(webkit)[ \/]([\w.]+)/; | |
ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/; | |
rmsie = /(msie) ([\w.]+)/; | |
rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/; | |
userAgent = navigator.userAgent; | |
}, | |
agentMatch : function(){ | |
} | |
} | |
//ajax object handler | |
kScript.ajax = kScript.prototype = { | |
constructor: kScript, | |
init: function( options ){ | |
alert(options.Type); | |
} | |
} | |
//kscript initializer function, dom successfully loaded | |
kScript.fn = kScript.prototype = { | |
constructor: kScript, | |
ready: function( callback ){ | |
window.addEventListener? | |
//event handlers for mozilla, webkit, opera | |
document.addEventListener("DOMContentLoaded", callback, false): | |
//event handle for IE | |
window.attachEvent("onload", callback) | |
}, | |
bindReady:function(){ | |
}, | |
//cleanup functions for the document ready function | |
cleanup: function(){ | |
if(document.addEventListener){ | |
//before detaching.... make the callback function execute..... | |
DOMContentLoaded = function(){ | |
document.removeEventListener("DOMContentLoaded", DOMContentLoaded, false) | |
} | |
}else if(document.attachEvent){ | |
DOMContentLoaded = function(){ | |
document.readyState ==="complete"? | |
document.detachEvent("onreadystatechange", DOMContentLoaded, false):false; | |
window.detachEvent("onload",DOMContentLoaded); | |
} | |
} | |
} | |
} | |
//PUBLIC FUNCTIONS .... not in the kscript library | |
trim = trim? function(text) { | |
return text== null? "": trim.call(text); | |
}:function(text){ | |
constructor = kScript.helper; | |
return text==null? "": text.toString.replace(kScript.helper.init.leftTrim,"").replace(kScript.helper.init.rightTrim,""); | |
} | |
//finishing kscript,,,, allow usage by _ks.method_name() | |
if(!window._ks){ | |
window._ks = kScript;} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment