首页 > 代码库 > jLim - 紧凑的 JavaScript 框架
jLim - 紧凑的 JavaScript 框架
1. [代码][JavaScript]代码
/*!
* jLim Core
*
* jLim is a small JavaScript base code. It can be used to built your own
* JavaScript library or framework.
*
* @version 0.1.1
* @author Victor Villaverde Laan
* @link http://www.freelancephp.net/jlim-small-javascript-framework/
* @license MIT license
*/
(function ( window ) {
/**
* jLim function
* @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
* @param {string|DOM node|array of DOM nodes} context
* @return {jLim} New instance
*/
var jLim = window.jLim = function ( selector, context ) {
return new jLim.core.init( selector, context );
};
// set $ global ( when not already exists )
if ( ! window.$ )
window.$ = jLim;
/**
* jLim Core
*/
jLim.core = jLim.prototype = {
els: [],
selector: [],
context: [],
/**
* Init function, set selected elements
* @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
* @param {string|DOM node|array of DOM nodes} context Optional, default is document
*/
init: function ( selector, context ) {
// set selector and context property
this.selector = selector || document;
this.context = context || document;
// get elements by selector
if ( typeof selector == ‘string‘ ) {
// trim spaces at the begin and end
selector = jLim.trim( selector );
if ( selector == ‘body‘ ) {
// set body
this.els = document.body;
} else if ( selector.substr( 0, 1 ) == ‘<‘ ) {
// create element
this.els = jLim.create( selector );
} else {
// find elements
this.els = jLim.selector( selector, context );
}
} else if ( jLim.isFunction( selector ) ) {
// set DOM ready function
jLim.ready( selector );
} else if ( selector instanceof jLim ) {
this.els = selector.get();
} else {
this.els = selector;
}
// make sure elements property is an array
if ( ! jLim.isArray( this.els ) )
this.els = this.els ? [ this.els ] : [];
// support for using jLim object as an array
// set indices
for ( var i in this.els )
this[ i ] = this.els[ i ];
// set length property
this.length = this.els.length;
},
/**
* Get element (return all current elements when no index given)
* @param {integer} index
* @return {DOM node|array of DOM nodes}
*/
get: function ( index ) {
if ( typeof index == ‘undefined‘ )
return this.els;
var el = ( index === -1 )
? this.els.slice( index )
: this.els.slice( index, +index + 1 );
return el.length ? el[0] : null;
},
/**
* Get count of current elements
* @return {integer}
*/
size: function () {
return this.els.length;
},
/**
* Call function for each element
* @param {function} fn
* @param {array} args
* @return {this} For chaining
*/
each: function ( fn, args ) {
jLim.each( this.els, fn, args );
return this;
},
/**
* Find elements within the current elements (context)
* @param {string} selector
* @return {jLim} Instance of new selection
*/http://www.huiyi8.com/css3/?
find: function ( selector ) {css3教程
return this.chain( selector, this.els );
},
/**
* Add to the current elements in a new created jLim object
* @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
* @param {string|DOM node|array of DOM nodes} context Optional, when none is given it will use the context of current jLim object
* @return {jLim} Instance of new selection
*/
add: function ( selector, context ) {
var $new = this.chain( selector, context || this.context ),
els = this.els.concat( $new.get() );
$new.els = els;
return $new;
},
/**
* Set one of current elements as new jLim object
* @param {integer} index Negative integer also possible, -1 means last item
* @return {jLim} Instance of new selection
*/
eq: function ( index ) {
return this.chain( this.get( index ) );
},
/**
* Set slice of current elements as new jLim object
* @param {integer} start Like the first param of the standard Array.slice() function
* @param {integer} end Like the second param of the standard Array.slice() function
* @return {jLim} Instance of new selection
*/
slice: function ( start, end ) {
var els = this.els.slice( start, end || this.els.length );
return this.chain( els );
},
/**
* Chain content as new jLim object
* @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
* @param {string|DOM node|array of DOM nodes} context Optional
* @return {jLim} Instance of new selection
*/
chain: function ( selector, context ) {
var $new = jLim( selector, context );
$new.prevjLim = this;
return $new;
},
/**
* Set pointer to previous jLim object
* @return {jLim} Previous jLim object in the chain
*/
end: function () {
return this.prevjLim || jLim( null );
}
};
// init function gets core prototype
jLim.core.init.prototype = jLim.core;
/**
* Extend method
* @return {jLim|jLim.core|object|array|boolean}
*/
jLim.extend = jLim.core.extend = function () {
// target is current object if only one argument
var i = 0,
target = this,
deep = false,
obj, empty, item, x;
// check extending recursive (deep)
if ( typeof arguments[ 0 ] === ‘boolean‘ ) {
deep = true;
i = 1;
if ( arguments.length > 2 ) {
i = 2;
target = arguments[ 1 ];
}
} else if ( arguments.length > 1 ) {
i = 1;
target = arguments[ 0 ];
}
// loop through all source objects
for ( x = i; x < arguments.length; x++ ) {
obj = arguments[ x ];
// copy object items (properties and methods)
for ( item in obj ) {
if ( obj[ item ] === target )
continue;
if ( deep && typeof obj[ item ] == ‘object‘ && obj[ item ] !== null ) {
// item is also object, make copy
empty = jLim.isArray( obj[ item ] ) ? [] : {};
target[ item ] = jLim.extend( deep, target[ item ] || empty, obj[ item ] );
} else {
// copy property or method
target[ item ] = obj[ item ];
}
}
}
// return modified target
return target;
};
/**
* Extent basic functions
*/
jLim.extend({
/**
* Selector method
* @param {string} selector
* @param {string|DOM node|array of DOM nodes} context Default document
* @return {DOM node|array of DOM nodes|empty array}
*/
selector: function ( selector, context ) {
return jLim.$$( selector, context );
},
/**
* Add DOMReady callbacks
* @param {function|string} fn When string will be run like code with eval()
* @return {this}
*/
ready: function ( fn ) {
// default use the DOMReady add() method
jLim.DOMReady.add( fn );
// return this for chaining
return this;
},
/**
* Create DOM element
* @param {string} html
* @return {DOM element|array of DOM elements}
*/
create: function ( html ) {
var ph = document.createElement( ‘div‘ ),
els = [];
ph.innerHTML = html;
// get created elements
els = ph.childNodes;
// return element or array of elements
return els.length == 1 ? els[0] : els;
},
/**
* Each function for arrays and objects
* @param {object|array} obj
* @param {function} fn