@File: /surebert/sb.js
@File: /surebert/sb.js
@Author: Paul Visco of http://paul.estrip.org
@Version: 4.601 04/24/04 - 11/21/08
@File: /surebert/sb.js
@Description: One of the most important parts of the surebert library. Can reference DOM elements in many way using CSS selectors. The simplest use of it is to reference DOM elements by their id property.
e.g.'#myForm' An element id. When passed an element ID it returns a reference to the element with that id'
e.g.'body' An tag name. When passed a tag name it returns an array of all the tags that match that tag name. If the tag is found in sb.singleTags e.g. body, head, title then only one element is returned instead of an array
e.g. '#myDiv' returns node with the id 'myDiv'
e.g. '.myClass' returns all nodes with the class 'myClass', see also [class="myClass"] below
e.g. '*' returns all nodes
e.g. '#myDiv p' returns all the p tags that are decendents of #myDiv
e.g. '#myDiv > p' returns all the p tags that are direct decendents of #myDiv
e.g. 'p + b' returns all the b tags that are direct adjacent siblings of p tags
e.g. 'div ~ p' a p element preceded by an div element
e.g 'p:first-child' returns all the p tags that are the first child of their parent, be careful its not the first child of each p tag
e.g. 'p:last-child' returns all the p tags that are the last child of their parent
e.g. 'p:empty' returns all the p tags that are empty
e.g '#myDiv *:not(p)' returns all nodes that are not p tags within #myDiv
e.g 'input[name"choosen"]' returns all the input nodes with the name 'choosen'
e.g. 'a[href="http://www.surebert.com"] return all the a tags that have the href http://www.surebert.com
e.g. 'a[href$="google.com"] return all the a tags that end in google.com
e.g. 'a[href^="http"] return all the a tags that start with http
e.g. 'a[href*="surebert"] return all the a tags that have the substring "surebert" in them
e.g. a[hreflang|="en"] returns all a tags whose "hreflang" attribute has a hyphen-separated list of values beginning (from the left) with "en"
e.g. 'p[class~="bob"] returns an array of all p tags whose "class" attribute value is a list of space-separated values, one of which is exactly equal to "bob"
e.g. 'p, b, #wrapper' Commas allow you to make multiple selections at once.This example returns all b nodes, all p nodes and node with the id 'wrapper'
e.g '*:not(p)' LIMITED SUPPORT - returns all nodes that are not p tags
@File: /surebert/sb.js
@Description: Checks to see if a value is contained in the array
var myArray = [1,2,3];
var answer = myArray.inArray(2);
//answer is true
@File: /surebert/sb.js
@Author: Paul Visco
@Version: 1.1 11/19/07
@Description: Removes a value or a set of values from an array.
var myArray = [5, 10, 15];
var answer = myArray.remove([10,5]);
//answer =[15];
var answer = myArray.remove(5);
//answer =[10, 15];
@File: /surebert/sb.js
@Description: Adds a className to the sb.element, using this methods sb.element instances can have multiple classNames
myElement.addClassName('redStripe');
@File: /surebert/sb.js
@Description: Appends another DOM element to the element as a child
myElement.append(myOtherElement);
@File: /surebert/sb.js
@Description: Appends the element after another DOM element as a sibling
//appends myElement to the parent of "#myDiv" as a sibling of "#myDiv" directly after "#myDiv"
myElement.appendAfter('#myDiv');
@File: /surebert/sb.js
@Description: Appends the element before another DOM element as a sibling
//appends myElement to the parent of "#myDiv" as a sibling of "#myDiv" directly before "#myDiv"
myElement.appendBefore('#myDiv');
@File: /surebert/sb.js
@Description: Appends the element to another DOM element as a child
//appends myElement to the page body
myElement.appendTo('body');
//appends myElement to a div with the ID "myDiv"
myElement.appendTo('#myDiv');
@File: /surebert/sb.js
@Description: Appends the element to the top DOM element as a child
//appends myElement to the page body
myElement.appendToTop('body');
//appends myElement to a div with the ID "myDiv"
myElement.appendToTop('#myDiv');
@File: /surebert/sb.js
@Description: Used to set event cross-browser event handlers. For more information see sb.events.
//sets the backgroundColor peroperty to red
myElement.event('click', function(e){
//alerts the x value of the click
alert(e.clientX);
//alerts the innerHTML of myElement
alert(this.innerHTML);
});
@File: /surebert/sb.js
@Description: Removes an event created with Element.prototype.event
//sets the backgroundColor property to red
var myEvt = myElement.event('click', function(e){
alert(this.innerHTML);
});
myElement.eventRemove(myEvt);
@File: /surebert/sb.js
@Description: Used to assign multiple events at once
var myDiv = $('#myDiv');
myDiv.events({
click : function(){
do something
},
mouseover : function(){
//do somthing
}
});
@File: /surebert/sb.js
@Description: Used keep track of events added to a sb.element. All events added with this.event are pushed into this array where they are stored for removal
@File: /surebert/sb.js
@Description: Removes all event observers for the sb.element that were added using this.event() or this.events()
myElement.eventsRemoveAll();
@File: /surebert/sb.js
@Description: calculates the style of an sb.element based on the current style read from css
myElement.getStyle('background-color');
//or
myElement.getStyle('backgroundColor').
@File: /surebert/sb.js
@Description: Calculates the absolute x position of an element
myElement.getX();
@File: /surebert/sb.js
@Description: Calculates the absolute x position of an element
myElement.getY();
@File: /surebert/sb.js
@Description: Checks to see if the element has the className specified. Elements can have more than one className.
myElement.hasClassName('redStripe');
@File: /surebert/sb.js
@Description: Used keep track of the last event added to a sb.element.
@File: /surebert/sb.js
@Description: Sets the style of an sb.element
myElement.setStyle('backgroundColor', blue);
//or
myElement.setStyle('opacity', 0.5);
@File: /surebert/sb.js
@Description: Removes an element from the DOM
myElement.remove();
@File: /surebert/sb.js
@Description: Removes a className from the elements className array. Elements can have more than one className
myElement.removeClassName('redStripe');
@File: /surebert/sb.js
@Description: Replaces an element with another element in the DOM
myElement.replace('#myOtherElement');
@File: /surebert/sb.js
@Description: Sets multiple style properties for an sb.element
myElement.styles({
backgroundColor : '#000000',
fontSize : '18px',
border : '1px solid #FF0000'
});
@File: /surebert/sb.js
@Description: Converts all dashes to camelStyle
var str = 'background-color';
var newString = str.toCamel();
//newString = 'backgroundColor'
@File: /surebert/sb.js
@Description: sb.ajax is a constructor that can be used to instanitate objects which communicate in real time from the client to the server without refreshing the page, all properties can be passed as the only object argument
var myAjax = new sb.ajax({
//optional 'get' is the default
method : 'post',
//optional 'text' is the default
format : 'text',
//optional no data needs to be sent to the server side script
data : 'name=paul&friend=tim&day=monday',
// the server side script to call
url : 'process.php',
//the handler function receives all data returned from the server side script, depending on the format specified, result has different properties, by default it is a text string
onResponse : function(result){
//alerts the text returned from the server side script
alert(result);
}
});
@File: /surebert/sb.js
@Description: The default way the ajax instances handles the data retreived from the scripts. This sets the default format for all sb.ajax instances that do not already specify a format. It is text by default but you can override this in your script. The options are;
sb.ajax.defaultFormat = 'json';
@File: /surebert/sb.js
@Description: The default transport method used for communicating with server side scripts. If this is changed, all insatnces with non specified transport methods will use this one. It is 'get' by default. Another option is 'post'.
@File: /surebert/sb.js
@Description: The default url the ajax instances semd data to. This sets the url for all sb.ajax instances that do not already specify a url.
sb.ajax.defaultURL = 'process.php';
@File: /surebert/sb.js
@Description: You can use this to abort an ajax function that is fetching. In addition, if you have defined an onabort() method for your sb.ajax instance it will fire whenever the fetch is canceled.
var myAjax = new sb.ajax({
url : 'process.php'
});
myAjax.fetch();
//aborts a fetch already in progress, you could attach this event to a cancel button
myAjax.abort();
@File: /surebert/sb.js
@Description: You can use this to abort an ajax function that is fetching. In addition, if you have defined an onabort() method for your sb.ajax instance it will fire whenever the fetch is canceled.
var myAjax = new sb.ajax({
url : 'process.php',
onAbort : function(){
alert('ajax call aborted');
}
});
//aborts a fetch already in progress, you could attach this event to a cancel button, also used by timeout
myAjax.abort();
@File: /surebert/sb.js
@Type: Boolean false performs synchronous and pauses, true performs asynchronously which is the default allowing other processes to continue
@Description: Determines if the script is paused while the data is loaded.
var myAjax = new sb.ajax({
url : 'process.php',
async : 1
});
//or added afterwards with
myAjax.async = 1;
@File: /surebert/sb.js
@Type: Boolean
@Description: Is set to 0 when if the ajax call is not complete or set to 1 if it is compelete. Used to check for complete state when using synchronous calls in safari. Each instances completed status is reset on each fetch() call so that asynchronous calls can still be fetched more than once.
@File: /surebert/sb.js
@Type: String
@Description: The data sent to the server side script specified in the url property. The values are passed as key value pairs e.g. x=1&y=2&name=joe. You should always escape or URIencode data that includes anything other than alphanumeric data.
var myAjax = new sb.ajax({
url : 'process.php',
data : 'name=paul&day=monday&value='+escape($('myInput').value)
});
//or added afterwards with
myAjax.data = 'name=paul&day=monday&value='+escape($('myInput').value);
@File: /surebert/sb.js
@Type: Boolean
@Description: Determines if the data sent and received is debugged to the to surebert debug consol which. This only works if you include sb.developer.js This makes debuggin much easier.
var myAjax = new sb.ajax({
url : 'process.php',
debug : 1
});
//or added afterwards with
myAjax.debug =1;
@File: /surebert/sb.js
@Description: Sends any data specified to the external server side file specified in your instances .url property and returns the data recieved to the instance's onResponse method
var myAjax = new sb.ajax({
url : 'process.php'
});
//fetches the data from the url specified
myAjax.fetch();
@File: /surebert/sb.js
@Type: Boolean
@Description: The format the data is retreived in. Can be json, text, xml, head, js, send - s. This value overides any sb.ajax.defaultFormat value set or if the content type of the server page matches a specific format.
var myAjax = new sb.ajax({
url : 'process.php',
format : 'json'
});
//or added afterwards with
myAjax.format = 'json';
@File: /surebert/sb.js
@Type: Boolean
@Description: Allows ajax object instances to fetch data from a local file instead of from a server. Normally, the instance checks for the HTTP server response - e.g. 200, 404, 500, etc and if you grab a klocal file this does not exist. If you are serving your pages from a web server you should never need to use this.
var myAjax = new sb.ajax({
url : 'process.php',
local : 1
});
@File: /surebert/sb.js
@Description: Fires when the ajax request gets it headers back
var myAjax = new sb.ajax({
url : 'process.php',
onHeaders : function(status, statusText){
//alert 400 if file not found
alert(status);
//you also have access to other headers
alert(this.getResponseHeader('Content-type');
}
});
@File: /surebert/sb.js
@Description: Fires when the ajax request gets it headers back
var myAjax = new sb.ajax({
url : 'process.php',
timeout : 500,
onTimeout : function(status, statusText){
//alert 400 if file not found
alert(status);
//you also have access to other headers
alert(this.getResponseHeader('Content-type');
}
});
@File: /surebert/sb.js
@Description: Fires when the ajax request gets its response back from the server.
var myAjax = new sb.ajax({
url : 'process.php',
onResponse : function(response){
alert(response);
}
});
@File: /surebert/sb.js
@Description: The amount of time in milliseconds the ajax request will wait before it aborts. This is optional
var myAjax.timeout = 1000;
//fetches the data from the url specified
myAjax.fetch();
@File: /surebert/sb.js
@Description: Find out what browser we are using and gets the query string and screen data
@File: /surebert/sb.js
@Type: boolean
@Description: Is teh page being displayed with ie 6. Normally you would access this information through sb.browser.agent and sb.browser.version but I added this for convenience with ie6
@File: /surebert/sb.js
@Description: Measures the inside view area of the window
var pos = sb.browser.measure();
//pos = [800, 642]
@File: /surebert/sb.js
@Description: Methods used to calculate and manipulate color values, see also /colors direcory
@File: /surebert/sb.js
@Description: Sets up global alias for a variable if one does not already exist.
//checks to see if global jump function exists and creates it if it does not
sb.createIfNotExists('jump', function(){alert('jump');});
@File: /surebert/sb.js
@Description: Used to run a function when a DOM element becomes available
//In this example the onloaded function fires when the node with the id #navigation is available the onloaded function, receives a this which is essentialy the element passed through sb.$
sb.dom.onReady({
id : '#navigation',
onReady : function(){
alert(this.innerHTML);
},
interval : 100,
tries : 10,
ontimeout function(el){
alert(el+' not found');
},
args : ['one', 'two']
});
@File: /surebert/sb.js
@Description: Used to create DOM nodes. If a string is passed to the fuction it simply return document.createElement(str);
var myDiv = new sb.element({
tag : 'div',
className : 'redStripe',
innerHTML : 'I am a redstriped div',
events : {
click : function(){
alert(this.innerHTML);
},
mouseover : function(){
this.style.backgroundColor='red';
}
},
styles : {
backgroundColor : 'blue',
fontSize : '18px'
},
addAttributes : function{
friend : 'xxx'
}
});
myDiv.appendTo('body');
//OR just pass the nodeType
var myDiv = new sb.element('div');
myDiv.appendChild(myOtherDiv);
@File: /surebert/sb.js
@Description: Methods of sb.element instances. Assume that myElement is an sb.element instance in all examples of Element.prototype
@File: /surebert/sb.js
@Description: Cross browser event handling that references the proper "this" and passes the event to the handler method. Using sb.events, multiple events can be added to a single DOM node for the same event. e.g. multiple onclick handlers
@File: /surebert/sb.js
@Description: Add an event listener to a DOM element, re-write of surebert events based on tips from http://www.digital-web.com/articles/seven_javascript_techniques/
var myEvent = sb.events.add('#myList', 'click', function(e){
alert(this.innerHTML);
});
@File: /surebert/sb.js
@Description: Used to prevent default actions from occurring on an event e.g. links that are clicked would do whatever is in the event handler but not the ordinary default event of goign to the page specified by the href attribute.
var myEvent = sb.events.add('#myList', 'click', function(e){
sb.events.preventDefault(e);
});
@File: /surebert/sb.js
@Description: Related targets are specified for events that have related targets. e.g. mouseover and mouseout. when the event is mouseout the relatedTarget refers to the element the mouse is moving to. When the event is mouseover, the relatedTarget refers to the element that the mouse is moving from.
var myEvent = sb.events.add('#myList', 'click', function(e){
var target = sb.events.relatedTarget(e);
alert(target.nodeName);
});
@File: /surebert/sb.js
@Description: Removes an event listener
var myEvent = sb.events.add('#myList', 'click', function(e){
alert(this.innerHTML);
});
sb.events.remove(myEvent);
@File: /surebert/sb.js
@Description: Removes all event listeners added with sb.events.add or sb.elements or $'s event method
sb.events.removeAll();
@File: /surebert/sb.js
@Description: Used to stop event bubbling e.g. when a ordered list is clicked the event bubbles to its children.
var myEvent = sb.events.add('#myList', 'click', function(e){
sb.events.stopPropagation(e);
});
@File: /surebert/sb.js
@Description: used to stop event bubbling and prevent default actions for an event
var myEvent = sb.events.add('#myList', 'click', function(e){
sb.events.stopAndPrevent(e);
});
@File: /surebert/sb.js
@Description: Determines the target of the event, becaus eof event bubbling, this is not necessarily the this of the event. e.g when an orderlist is clicked, the target might have been one of the child list items, however, it's click event fires because teh chidlren are within it. By referencing the target you can see which child was clicked.
var myEvent = sb.events.add('#myList', 'click', function(e){
var target = sb.events.target(e);
alert(target.innerHTML);
});
@File: /surebert/sb.js
@Description: Includes another surebert module. Make sure you surebert files are in /surebert or that you have set sb.base before using this.
sb.include('String.prototype.nl2br');
@File: /surebert/sb.js
@Description: An array of all modules that are included, updated live and can be used for debugging and making compressed libraries before putting into production
alert(sb.included);
@File: /surebert/sb.js
@Description: Used to load external javascript from the same server synchronously and on demand.
sb.load('/surebert/surebert.effects.js');
if(sb.load('../js/myJavascript.js')){
//run function from myJavascript.js
}
@File: /surebert/sb.js
@Description: Used to create sb.nodeLists which are groups of sb.elements that have many of the same methods as sb.element but which act on all sb.elements in the sb.nodeList. It also has all the properties of an sb.array. These are returned by sb.s$
@File: /surebert/sb.js
@Description: Adds more nodes to the nodeList nodes array and assigns sb_ids or adds super element properties if required
var nodes = $('ol li');
//adds element with id 'wrapper' to the node list
nodes.add($('#wrapper'));
@File: /surebert/sb.js
@Description: Determines if superElements should be created
@File: /surebert/sb.js
@Description: drop dom nodes, either array o single node from a sb.nodeList
var nodes = $('ol li');
//adds element with id 'wrapper' to the node list
nodes.drop('#wrapper');
//add all the links to the sb.nodeList
nodes.drop('a');
@File: /surebert/sb.js
@Description: Return the length of the this.nodes array which represents how many nodes the nodeList instance is holding
@File: /surebert/sb.js
@Description: The CSS selector used to find the nodes
@File: /surebert/sb.js
@Description: Makes a copy of an object and it's properties
var o = {name : 'paul, language : 'javascript'};
var f = sb.objects.copy(o);
@File: /surebert/sb.js
@Description: Returns the properties of the object and their values for an object
var o = {name : 'paul, language : 'javascript'};
sb.objects.dump({o});
@File: /surebert/sb.js
@Description: Used to add properties from one object to another. If you have globals enabled you can just call infuse on any object or constructor and pass teh object to copy the properties from.
var boy = {
name : 'paul'
};
var otherBoy : {
eats : function(){
alert('yum');
}
}
//copies eat function to boy object
sb.objects.infuse(otherBoy, boy);
//or with globals enabled
boy.infuse(otherBoy);
@File: /surebert/sb.js
@Description: Serializes all the properties of an object into a post data style key value string
@File: /surebert/sb.js
@Description: an array of functions that run once the DOM loads, they are fired in order, funcitons can be function references or inline anonymous functions
sb.onbodyload.push({myFunction});
@File: /surebert/sb.js
@Description: an array of functions that run once when leaving the page, they are fired in order
sb.onleavepage.push({myFunction});
@File: /surebert/sb.js
@Description: converts other types of iterable objects into an array e.g. an arguments list or an element sb.nodeList returned from getElementsByTagName.
var images = document.getElementsByTagName('img');
images = sb.toArray(images);
images.forEach(function(image,key,arr){
alert(image.src);
});
@File: /surebert/sb.js
@Description: returns the type of the object it is passed
var obj = {name : 'joe'}
sb.typeOf(obj); //return 'object'
@File: /surebert/sb.js
@Description: produces a unique id, ideal for DOM element which are created on the fly but require unique ids
var myUniqueId = sb.uniqueID();
//myUniqueId = 'uid_5' //<--just an example return would be unique each time it is called on a page
@File: /surebert/sb.js
@Description: calculates the current time as a unix timestamp
var unixtime = sb.unixTime();
//unixtime = 1170091311//<- just a possible example - would return current time