|
vxCore
The vX Core Package
IntroductionThe vX core package provides a basic toolkit for Web scripting with an emphasis on size and speed. The whole of vX, including subordinate packages, resides within the _ (underscore) namespace. A file containing the full package is available at http://vxjs.googlecode.com/svn/trunk/lib/vx.all.js The online build system at http://vxjs.googlecode.com/svn/trunk/build.htm can be used to generate subset packages. OverviewThe vX Javascript library is designed to be small. There are several ways to do most things. All functions have a long, human readable name and many have shortcuts that are a single uppercase letter. For example, .ajax maps to .X, while some are simpler to memorize than others. Everything is prefixed with the symbol, similar to how many other libraries prefix things with $. Everything can be alternatively accessed through the vx object as well. Function List
Add Class: Add ClassAdd a class to an element (does not add if already present) Usage_.AC(element, classname);
Example_.AC(document.body, 'purple') Ajax: AJAX QueryAJAX is a staple of the "Web 2.0" world. vX provides an extremely lightweight module to perform simple asynchronous GET/POST requests. Usage_.X(url,callback[,query]);
Example// perform a GET request
_.X("ajax.php?you=suck&howmuch=alot",function(response){alert(response);})
// perform a POST request
_.X("ajax.php",function(response){alert(response)},"you=suck&howmuch=alot")Animation: Basic AnimationHere is a low level complicated yet flexible linear animation function. It is not recomended for normal use Usage_.A(frames, interval, callback, finishcallback);
Example
_.A(100, 100, function(p){
_.G("progress").innerHTML = (p*100)+"%"
})Array: Convert iterables to real arraysAccepts an array-like collection and returns an array equivalent Usage_.Y(collection);
Example// a bunch of dom nodes var original = document.body.childNodes // create a clone and override a property var array = _.Y(original) // array functions like splice, etc. array.splice(1,1); //get rid of first object Bind: Bind a functiont to a scopeBind a function to a scope, such as for event handlers Usage_.B(function, scope);
Examplefunction VectorEditor(elem, width, height){
this.draw.canvas.onmousedown = _.bind(function(event){
this.onMouseDown(event.clientX - offset()[0], event.clientY - offset()[1], event.target)
}, this);
}Get By Class: Get By Class NameUsage_.L(classname[,node=document]);
ExampleClone: Object Copying and CloningA copy of an object is an independent replica of the original. A clone of an object is a "subclass" that overrides the original. The difference between a copy and a clone is that when an object is modified, its copies are unaffected while its clones inherit the changes. Usage_.C(obj[,copy=false]);
Example// set up the original object
var original = {a: 1, b: 2, c: 3}
// create a clone and override a property
var clone = _.C(original)
clone.b = 4
// create a copy and modify a property
var copy = _.C(original,true)
copy.b = 4
// modify the original object
original.b = 5
original.c = 6
original.d = 7
// copy is {a: 1, b: 4, c: 3}
// clone is {a: 1, b: 4, c: 6, d: 7}Events: Event HandlingEvent handling is quite an important thing. It just is. If you want something to happen in response to something, then you need it. Usage_.E(element, event type, handler function, [,remove=false]);
Example
_.E(document.body, "click", function(){
alert('YOU CLICKED ME!')
});Extend Object: Object MergingSometimes you need to extend an object with new attributes. Usage_.T(original, new);
Example _.extend({
"hello": "world",
"internet": "LOLCAT"
},
{
"name": "toni avonni",
"job": "Cheese salesman"
});
//{"hello": "world", "internet": "LOLCAT", "name":"toni avonni","job": "cheese salesman"}Fade: Simple Fade AnimationFading is probably the most common and most useful effect. It makes sense most people don't like the complexity of the Core Animation function, and want a simpler function. Usage_.F(DIRECTION IN/OUT, HTML ELEMENT[, END CALLBACK, FRAMES, INTERVAL]);
Example_.F("out",document.body)Get Element: Get Element By IDMost frameworks include shortcuts for common functions such as document.getElementById, famous ones include $ in Prototype and MooTools Usage_.G(element id);
Example_.G("hello").innerHTML = "happy"Has Class: Has Classdetecting whether a CSS class is present in an element Usage_.HC(element, classname);
Exampleif(_.HC(document.body, 'purple')){
alert('YAY PURPLE')
}HTML Entity: HTML Entity EncodingWhen dealing with HTML and XML, it is often necessary to convert troublesome characters into entities. The HTML entity encoder escapes and unescapes strings. Usage_.H(str[,unesc=false]);
Example// escape a string
_.H('ham & eggs')
//"ham & eggs"
// unescape a string
_.H('ham & eggs',true)
//"ham & eggs"Script Include: Dynamically load a scriptDynamic loading scripts are an Ajax design pattern. Usage_.N('vx.index.js');
Example_.N('vx.index.js');Index: Array IndexOfUsage_.I(obj, array);
Example_.I("ham", ["bacon", "trees", "cows", "nuclear-pests"]);
//-1
var breakfast = ["bacon", "ham", "eggs", "javascript"]
_.I("ham", breakfast);
//1Namespacing: Namespace GenerationGenerate long namespaces (objects) chained together somehow. Usage_.N(namespace string);
Example_.N('com.vxjs.beta.application.longname.library.core') = 42;
com.vx.beta.applciation.longname.library.core
//42Ninja: Ninja ModeSimilar to jQuery's noConflict() Usage_.ninja(extreme);
Example(function(_){
//vX stuffs
})(vx.ninja())Position: Element PositionSometimes it's important to get the location and size of an element. Usage_.P(element);
Examplevar pos = _.pos(_.G("el"));
alert("Width: "+pos.w);
alert("Height:"+pos.h);
alert("Top:"+pos.t);
alert("Left:"+pos.l);Query Encode: Query EncodingThe query encoder builds an HTTP query string out of a Javascript object; this simple serialization scheme prepares data for sending to the server. Usage_.Q(obj);
Example_.Q({you: "suck", howmuch: "alot"})
//"you=suck&howmuch=alot"Queue: QueueUsage_.U(eval function array);
Example_.U(["_.F('in',document.body,0,0,f)","_.F('out',document.body,0,0,f)"])Ready: Document onReadyMany people use Window.onLoad to execute their scripts after the page has loaded. But, there is only 1 window.onload function you can set, and it happens after the images and etc are loaded. Document onReady allows you to execute javascript immediately when the document is ready for manipulation, before images are loaded and you can use it with multiple functions. Usage_.R(function);
Example_.R(function(){
alert("this will execute before the page is fully loaded, but after DOM can be manipulated");
//manipulate DOM
})Array Remove: Remove items from arraysRemoves an the first occurance of an element from an array. NOTE: MODIFIES ORIGINAL Usage_.V(element, array);
Example// a bunch of dom nodes var original = document.body.childNodes // create a clone and override a property var array = _.Y(original) // array functions like splice, etc. array.splice(1,1); //get rid of first object vX Shortcuts: Used mostly internallylets vX be smaller Usage_.d
Exampledocument = _.d Slide Animation: Slide AnimationIt's one of those awesome animations. Usage_.slide(direction,element[, finishcallback, frames, interval]);
Example_.slide(1,menu,function() //slide in
{
menu.style.height = ''; //reset height
menu.style.visibility = 'visible'; //yay for light
},5,20)
_.slide(0,menu,function() //slide out
{
menu.style.height = ''; //hi
menu.style.visibility = "hidden" //scary!
})Stringify: JSON EncodingThe JSON encoder serializes complex objects into JSON strings suitable for AJAX queries and unserializes JSON strings into objects. Usage_.S(data[,decode=false]);
Example// serialize an object
_.S({hello: "world!", recurse: {awesome: "true", undef: undefined, notfalse: true}})
//"{hello:'world!',recurse:{awesome:'true',undef:undefined,notfalse:true}}"
// unserialize a string
_.S("{hello:'world!',recurse:{awesome:'true',undef:undefined,notfalse:true}}",true)
//{hello:'world!',recurse:{awesome:'true',undef:undefined,notfalse:true}}Templates: TemplatesSimple string templating Usage_.M(string, vars);
Examplevar template = "hello {name}! I {verb} you! No, not really, I actually {verb2} you.";
var variables = {
name: "Bob",
verb: "love",
verb2: "want to kill"
}
_.M(template, variables);
//hello Bob! I love you! No, not really, I actually want to kill you.Trim String: Remove leading and ending whitespaceRemove leading and ending whitespace Usage_.TM(string);
Example_.trim(" i am a blob of text ")
//i am a blob of textArray Unique: Create a new unique arrayCreate a new array with only unique items from an array with repeating items Usage_.unique(array);
Example_.unique([1,3,3,7]) //[1,3,7] |
I'm impressed! Keep up the good work!
Any notes on browsers this library may not work with? As long as it is good with all "A" grade browsers I'm ready to dive in!
haven't tested extensively yet but this library now has a place in my heart. thanks for sharing.
I try to use the Get By Class function without success. Can you please provide an example? By the way i like a lot your class.