Your Ad Here

Posted By

freelancephp on 07/24/11


Tagged

javascript js mini jquery core Framework lightweight compact jLim


Versions (?)

jLim - compact JavaScript framework


 / Published in: JavaScript
 

URL: http://www.freelancephp.net/jlim-small-javascript-framework/

jLim is a small JavaScript base code. It can be used to built your own JavaScript library or framework.

  1. /*!
  2.  * jLim Core
  3.  *
  4.  * jLim is a small JavaScript base code. It can be used to built your own
  5.  * JavaScript library or framework.
  6.  *
  7.  * @version 0.1.1
  8.  * @author Victor Villaverde Laan
  9.  * @link http://www.freelancephp.net/jlim-small-javascript-framework/
  10.  * @license MIT license
  11.  */
  12. (function ( window ) {
  13.  
  14. /**
  15.  * jLim function
  16.  * @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
  17.  * @param {string|DOM node|array of DOM nodes} context
  18.  * @return {jLim} New instance
  19.  */
  20. var jLim = window.jLim = function ( selector, context ) {
  21. return new jLim.core.init( selector, context );
  22. };
  23.  
  24. // set $ global ( when not already exists )
  25. if ( ! window.$ )
  26. window.$ = jLim;
  27.  
  28. /**
  29.  * jLim Core
  30.  */
  31. jLim.core = jLim.prototype = {
  32.  
  33. els: [],
  34. selector: [],
  35. context: [],
  36.  
  37. /**
  38. * Init function, set selected elements
  39. * @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
  40. * @param {string|DOM node|array of DOM nodes} context Optional, default is document
  41. */
  42. init: function ( selector, context ) {
  43. // set selector and context property
  44. this.selector = selector || document;
  45. this.context = context || document;
  46.  
  47. // get elements by selector
  48. if ( typeof selector == 'string' ) {
  49. // trim spaces at the begin and end
  50. selector = jLim.trim( selector );
  51.  
  52. if ( selector == 'body' ) {
  53. // set body
  54. this.els = document.body;
  55. } else if ( selector.substr( 0, 1 ) == '<' ) {
  56. // create element
  57. this.els = jLim.create( selector );
  58. } else {
  59. // find elements
  60. this.els = jLim.selector( selector, context );
  61. }
  62. } else if ( jLim.isFunction( selector ) ) {
  63. // set DOM ready function
  64. jLim.ready( selector );
  65. } else if ( selector instanceof jLim ) {
  66. this.els = selector.get();
  67. } else {
  68. this.els = selector;
  69. }
  70.  
  71. // make sure elements property is an array
  72. if ( ! jLim.isArray( this.els ) )
  73. this.els = this.els ? [ this.els ] : [];
  74.  
  75. // support for using jLim object as an array
  76. // set indices
  77. for ( var i in this.els )
  78. this[ i ] = this.els[ i ];
  79.  
  80. // set length property
  81. this.length = this.els.length;
  82. },
  83.  
  84. /**
  85. * Get element (return all current elements when no index given)
  86. * @param {integer} index
  87. * @return {DOM node|array of DOM nodes}
  88. */
  89. get: function ( index ) {
  90. if ( typeof index == 'undefined' )
  91. return this.els;
  92.  
  93. var el = ( index === -1 )
  94. ? this.els.slice( index )
  95. : this.els.slice( index, +index + 1 );
  96.  
  97. return el.length ? el[0] : null;
  98. },
  99.  
  100. /**
  101. * Get count of current elements
  102. * @return {integer}
  103. */
  104. size: function () {
  105. return this.els.length;
  106. },
  107.  
  108. /**
  109. * Call function for each element
  110. * @param {function} fn
  111. * @param {array} args
  112. * @return {this} For chaining
  113. */
  114. each: function ( fn, args ) {
  115. jLim.each( this.els, fn, args );
  116. return this;
  117. },
  118.  
  119. /**
  120. * Find elements within the current elements (context)
  121. * @param {string} selector
  122. * @return {jLim} Instance of new selection
  123. */
  124. find: function ( selector ) {
  125. return this.chain( selector, this.els );
  126. },
  127.  
  128. /**
  129. * Add to the current elements in a new created jLim object
  130. * @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
  131. * @param {string|DOM node|array of DOM nodes} context Optional, when none is given it will use the context of current jLim object
  132. * @return {jLim} Instance of new selection
  133. */
  134. add: function ( selector, context ) {
  135. var $new = this.chain( selector, context || this.context ),
  136. els = this.els.concat( $new.get() );
  137.  
  138. $new.els = els;
  139. return $new;
  140. },
  141.  
  142. /**
  143. * Set one of current elements as new jLim object
  144. * @param {integer} index Negative integer also possible, -1 means last item
  145. * @return {jLim} Instance of new selection
  146. */
  147. eq: function ( index ) {
  148. return this.chain( this.get( index ) );
  149. },
  150.  
  151. /**
  152. * Set slice of current elements as new jLim object
  153. * @param {integer} start Like the first param of the standard Array.slice() function
  154. * @param {integer} end Like the second param of the standard Array.slice() function
  155. * @return {jLim} Instance of new selection
  156. */
  157. slice: function ( start, end ) {
  158. var els = this.els.slice( start, end || this.els.length );
  159. return this.chain( els );
  160. },
  161.  
  162. /**
  163. * Chain content as new jLim object
  164. * @param {string|DOM node|array of DOM nodes} selector When selector is not a string the context will be ignored
  165. * @param {string|DOM node|array of DOM nodes} context Optional
  166. * @return {jLim} Instance of new selection
  167. */
  168. chain: function ( selector, context ) {
  169. var $new = jLim( selector, context );
  170. $new.prevjLim = this;
  171. return $new;
  172. },
  173.  
  174. /**
  175. * Set pointer to previous jLim object
  176. * @return {jLim} Previous jLim object in the chain
  177. */
  178. end: function () {
  179. return this.prevjLim || jLim( null );
  180. }
  181.  
  182. };
  183.  
  184. // init function gets core prototype
  185. jLim.core.init.prototype = jLim.core;
  186.  
  187. /**
  188.  * Extend method
  189.  * @return {jLim|jLim.core|object|array|boolean}
  190.  */
  191. jLim.extend = jLim.core.extend = function () {
  192. // target is current object if only one argument
  193. var i = 0,
  194. target = this,
  195. deep = false,
  196. obj, empty, item, x;
  197.  
  198. // check extending recursive (deep)
  199. if ( typeof arguments[ 0 ] === 'boolean' ) {
  200. deep = true;
  201. i = 1;
  202.  
  203. if ( arguments.length > 2 ) {
  204. i = 2;
  205. target = arguments[ 1 ];
  206. }
  207. } else if ( arguments.length > 1 ) {
  208. i = 1;
  209. target = arguments[ 0 ];
  210. }
  211.  
  212. // loop through all source objects
  213. for ( x = i; x < arguments.length; x++ ) {
  214. obj = arguments[ x ];
  215.  
  216. // copy object items (properties and methods)
  217. for ( item in obj ) {
  218. if ( obj[ item ] === target )
  219. continue;
  220.  
  221. if ( deep && typeof obj[ item ] == 'object' && obj[ item ] !== null ) {
  222. // item is also object, make copy
  223. empty = jLim.isArray( obj[ item ] ) ? [] : {};
  224. target[ item ] = jLim.extend( deep, target[ item ] || empty, obj[ item ] );
  225. } else {
  226. // copy property or method
  227. target[ item ] = obj[ item ];
  228. }
  229. }
  230. }
  231.  
  232. // return modified target
  233. return target;
  234. };
  235.  
  236. /**
  237.  * Extent basic functions
  238.  */
  239. jLim.extend({
  240.  
  241. /**
  242. * Selector method
  243. * @param {string} selector
  244. * @param {string|DOM node|array of DOM nodes} context Default document
  245. * @return {DOM node|array of DOM nodes|empty array}
  246. */
  247. selector: function ( selector, context ) {
  248. return jLim.$$( selector, context );
  249. },
  250.  
  251. /**
  252. * Add DOMReady callbacks
  253. * @param {function|string} fn When string will be run like code with eval()
  254. * @return {this}
  255. */
  256. ready: function ( fn ) {
  257. // default use the DOMReady add() method
  258. jLim.DOMReady.add( fn );
  259.  
  260. // return this for chaining
  261. return this;
  262. },
  263.  
  264. /**
  265. * Create DOM element
  266. * @param {string} html
  267. * @return {DOM element|array of DOM elements}
  268. */
  269. create: function ( html ) {
  270. var ph = document.createElement( 'div' ),
  271. els = [];
  272.  
  273. ph.innerHTML = html;
  274.  
  275. // get created elements
  276. els = ph.childNodes;
  277.  
  278. // return element or array of elements
  279. return els.length == 1 ? els[0] : els;
  280. },
  281.  
  282. /**
  283. * Each function for arrays and objects
  284. * @param {object|array} obj
  285. * @param {function} fn
  286. * @return {this}
  287. */
  288. each: function ( obj, fn ) {
  289. var item, retVal;
  290.  
  291. // call given function for each item
  292. for ( item in obj ) {
  293. retVal = fn.call( obj[ item ], item, obj[ item ] );
  294.  
  295. // do not continue further when return value is false
  296. if ( retVal === false )
  297. break;
  298. }
  299.  
  300. // return this for chaining
  301. return this;
  302. },
  303.  
  304. /**
  305. * Trim spaces
  306. * @param {string} str
  307. * @return {string}
  308. */
  309. trim: function ( str ) {
  310. return str.replace( /^\s+/, '' ).replace( /\s+$/, '' );
  311. },
  312.  
  313. /**
  314. * Check if argument is array
  315. * @param {mixed} obj
  316. * @return {boolean}
  317. */
  318. isArray: function ( obj ) {
  319. return ( Object.prototype.toString.call( obj ) === "[object Array]" );
  320. },
  321.  
  322. /**
  323. * Check if argument is function
  324. * @param {mixed} obj
  325. * @return {boolean}
  326. */
  327. isFunction: function ( obj ) {
  328. return ( Object.prototype.toString.call( obj ) === "[object Function]" );
  329. }
  330.  
  331. });
  332.  
  333. /**
  334.  * External components
  335.  */
  336.  
  337. /**
  338.  * DOMReady
  339.  *
  340.  * @version 1.0
  341.  * @link http://www.freelancephp.net/domready-javascript-object-cross-browser/
  342.  * @license MIT license
  343.  */
  344. jLim.DOMReady=(function(){var fns=[],isReady=false,errorHandler=null,getFunc=function(fn){if(typeof fn=='string')return function(){eval(fn);};return fn;},ready=function(){isReady=true;for(var x=0;x<fns.length;x++){try{fns[x]();}catch(err){if(errorHandler)errorHandler(err);}}};this.setOnError=function(fn){errorHandler=getFunc(fn);return this;};this.add=function(fn){fn=getFunc(fn);if(isReady){fn();}else{fns[fns.length]=fn;}return this;};if(window.addEventListener){document.addEventListener('DOMContentLoaded',function(){ready();},false);}else{(function(){if(!document.uniqueID&&document.expando)return;var tempNode=document.createElement('document:ready');try{tempNode.doScroll('left');ready();}catch(err){setTimeout(arguments.callee,0);}})();}return this;})();
  345.  
  346. /**
  347.  * SS
  348.  *
  349.  * @version 0.1
  350.  * @link http://www.freelancephp.net/simple-css-selector-function/
  351.  * @license MIT license
  352.  */
  353. var $$=function(selector,context){var isObjType=function(o,type){return Object.prototype.toString.call(o)==='[object '+type+']';},isDesc=function(d,a){return d.parentNode==a||d.tagName.toUpperCase()!='HTML'&&isDesc(d.parentNode,a);},s=selector,c=context,els=[];s=s.split(',');c=isObjType(c,'String')?$$(c):c&&c.length?c:document;if(!isObjType(c,'Array'))c=[c];for(var i in c){for(var j in s){var strim=s[j].replace(/\s+/g,''),sp=s[j].split(' '),op=strim.substr(0,1),name=strim.substr(1),tels=[],nextContext;if(sp.length>1){nextContext=$$(sp[0],c[i]);tels=$$(sp.slice(1).join(' '),nextContext);els=els.concat(tels);}else if(op=='#'){tels[0]=c[i].getElementById?c[i].getElementById(name):document.getElementById(name);if(tels[0]&&isDesc(tels[0],c[i]))els.push(tels[0]);}else if(op=='.'){var expr=new RegExp('\\b'+name+'\\b'),all=c[i].getElementsByTagName('*');for(var v=0,w=all.length;v<w;v++){if(expr.test(all[v].className))els.push(all[v]);}}else{tels=c[i].getElementsByTagName(strim);for(var y=0,z=tels.length;y<z;y++)els.push(tels[y]);}}}return els.length==1?els[0]:els;};
  354. jLim.$$=$$;
  355.  
  356. })( window );

Report this snippet  

You need to login to post a comment.