      1 //////////////////////////////////////////////////////////////////////////////////
      2 //
      3 // minilib.js
      4 //
      5 // Copyright © 2008 Nicolas BONARDELLE <http://nicobo.net/contact>
      6 //
      7
      8
      9
     10 /**
     11 	@fileoverview
     12
     13 	This library provides a very minimalistic set of functions to fill some gaps in the core Javascript language.<br>
     14 	It should be replaced with one of the (better) frameworks on the market (jquery, mootools, ...) if size is not a problem.<br>
     15
     16 	@author Nicolas BONARDELLE <<a href="http://nicobo.net/contact">http://nicobo.net/contact</a>>
     17 */
     18
     19
     20
     21 //
     22 // ELEMENT SELECTION
     23 //
     24
     25
     26
     27 /**
     28 	@return true if the argument is not undefined nor null
     29 	@type boolean
     30 */
     31 function $defined( name )
     32 {
     33 	return name != null && name != undefined;
     34 }
     35
     36
     37
     38 /**
     39 	Light implementation of the $ function of modern libraries : tries to find a DOM node from a versatile key.<br>
     40
     41 	<p>NOTE : Not quite compatible with jQuery (because it requires something like '#myid').</p>
     42
     43 	@param fieldName the name of the field to find, or the field itself
     44 	@return The DOM node corresponding to the parameters or undefined if not found
     45 	@type Element
     46 */
     47 function $( fieldName )
     48 {
     49 	return typeof fieldName == "object" ? fieldName : document.getElementById(fieldName);
     50 }
     51
     52
     53
     54 //
     55 // OBJECTS AND COLLECTIONS MANIPULATION
     56 //
     57
     58
     59
     60 /**
     61 	Copies all fields from the given arguments into this object.
     62
     63 	@return this
     64 	@type Object
     65 	@addon
     66 */
     67 Object.prototype.extend = function()
     68 {
     69 	for ( var a=0 ; a<arguments.length ; a++ )
     70 	{
     71 		if ( typeof arguments[a] == "object" )
     72 		{
     73 			for ( var f in arguments[a] )
     74 			{
     75 				this[f] = arguments[a][f];
     76 			}
     77 		}
     78 	}
     79 	return this;
     80 }
     81
     82
     83
     84 /**
     85 	Creates a new object with the properties of several objects merged together.<br>
     86 	The properties of the latest argument overrides the ones of the previous arguments.
     87
     88 	@return a new object with the properties of all given arguments
     89 	@type Object
     90 */
     91 function merge()
     92 {
     93 	var merged = new Object();
     94 	Object.extend.apply(merged,arguments);
     95 	return merged;
     96 }
     97
     98
     99
    100 /**
    101 	Compares two objects of any type.
    102
    103 	@param noCase	If both objects are Strings, do a case insensitive comparison
    104 	@return true if the objects are equals, else false
    105 	@type boolean
    106 */
    107 function equals( o1, o2, noCase )
    108 {
    109 	return (noCase && typeof o1 == "string" && typeof o2 == "string") ? (o1.toUpperCase() == o2.toUpperCase()) : (o1 == o2);
    110 }
    111
    112
    113
    114 /**
    115     @param value The value to look for
    116 	@param noCase If the value is a String, use a case insensitive test to search this object
    117 	@return true if this array contains the given value (not key)
    118 	@addon
    119 	@type boolean
    120 */
    121 Array.prototype.containsValue = function( value, noCase )
    122 {
    123 	var notCaseSensitive = noCase ? true : false;
    124
    125 	for ( e in this ) {
    126 		if ( equals(this[e],value,notCaseSensitive) ) {
    127 			return true;
    128 		}
    129 	}
    130
    131 	return false;
    132 }
    133
    134
    135
    136 //
    137 // DHTML UTILITIES
    138 //
    139
    140
    141
    142 /**
    143 	Tests the Element to see if it has the passed in className.
    144 	@see "MooTools" {@link http://docs.mootools.net/Element/Element#Element:hasClass}
    145 */
    146 HTMLElement.prototype.hasClass = function( className )
    147 {
    148 	return (" "+this.className+" ").indexOf(" "+className+" ") != -1;
    149 }
    150
    151
    152
    153 /**
    154 	Adds the passed in class to the Element, if the Element doesnt already have it.
    155 	@see "MooTools" {@link http://docs.mootools.net/Element/Element#Element:addClass}
    156 */
    157 HTMLElement.prototype.addClass = function( className )
    158 {
    159 	if ( ! this.hasClass(className) ) {
    160 		this.className = this.className + " " + className;
    161 	}
    162 }
    163
    164
    165
    166 /**
    167 	Works like Element:addClass, but removes the class from the Element.
    168 	@see "MooTools" {@link http://docs.mootools.net/Element/Element#Element:removeClass}
    169 */
    170 HTMLElement.prototype.removeClass = function( className )
    171 {
    172 	if ( this.hasClass(className) ) {
    173 		this.className = (" "+this.className+" ").replace(" "+className+" ","");
    174 	}
    175 }
    176
    177
    178
    179 //
    180 // PATCHES, TRICKS
    181 //
    182
    183
    184
    185 /**
    186 	Utility object dedicated to multi-threading, asynchrone calls, ...
    187 	It should not be used directly.
    188 	@see Object#delay
    189 */
    190 Thread = {
    191 	/**
    192 		An array used to store function calls to be made in an asynchronous way.
    193 		Use {@link Thread#queue Thread.queue}.push(myfunction)}
    194 	*/
    195 	queue: [],
    196
    197 	/**
    198 		Executes a function stored in the queue.
    199 		@param i the index of the function in the queue
    200 		@return the value returned by the function once executed
    201 	*/
    202 	execute: function( i ) {
    203 		console.log(this);
    204 		return this.queue[i]();
    205 	}
    206 }
    207
    208
    209
    210 /**
    211 	Delays the execution of a function in time.
    212
    213 	@param {int}	delay	delay in milliseconds
    214 	@param {Function} func	The function to execute
    215 	@param arg1		(optional) ... and next args : the arguments to pass to the function when executed
    216 */
    217 function delay( delay, obj, func, arg1 )
    218 {
    219 	var args = arguments;
    220 	var i = Thread.queue.push( function(){ func.apply(obj,args) } ) - 1;
    221 	setTimeout( "Thread.execute("+i+")", delay );
    222 }
    223
    224
    225
    226 /**
    227 	@see GLOBALS#delay
    228 */
    229 Object.prototype.delay = function( delay, func, arg1 )
    230 {
    231 	delay(delay,this,func,arg1);
    232 }
    233
    234
    235
    236 /**
    237 	Attempts forcing refreshing an object on the screen.<br>
    238
    239 	<p>This can be usefull for instance when you want an element to be redrawn on the screen
    240 	to show a slight change in a CSS property, but there is an intensive Javascript operation
    241 	going on that takes precedence over graphical redrawing.</p>
    242
    243 	<p><em>WARNING</em> : this operation involves doing a deep copy of the object, which can consume a lot of resources.</p>
    244
    245 	Seen at {@link http://bytes.com/forum/thread612023.html Bytes.com}
    246
    247 	@param {Node} o The object to refresh
    248 */
    249 function refresh( o )
    250 {
    251 	// performs a deep copy of the object so that no change will be noticeable on the screen
    252 	var b = o.cloneNode(true);
    253 	// the first 'replaceChild' is the operation that should force the web navigator to refresh the object
    254 	o.parentNode.replaceChild(b,o);
    255 	// the second 'replaceChild' ensures that the references to the object will be left unchanged
    256 	b.parentNode.replaceChild(o,b);
    257 }
    258
    259
    260
    261 /**
    262 	@see GLOBALS#refresh
    263 */
    264 Object.prototype.refresh = function()
    265 {
    266 	refresh(this);
    267 }
    268
    269