1 
  2 
  3 /**
  4  * Helper that loads a CSS file (i.e. LINK tag).
  5  * 
  6  * @returns {Function} a function for loading LINKs into the current <code>document</code>:
  7  * 					   <code>loadLink(href : String, rel : String, callback : Function)</code>
  8  * 
  9  * where
 10  * <code>href</code>: (String) the URL for the LINK
 11  * <code>rel</code>: (String) OPTIONAL the value for the <code>rel</code> attribute of the LINK tag (DEFAULT: <code>stylesheet</code>)
 12  * <code>callback</code>: (Function) OPTIONAL a callback function for the <code>onload</code> event (NOTE: this is not supported or fully supported by all browsers -- may never be fired!)
 13  * 
 14  * or
 15  * <code>options</code>: (Object) an options object with properties.
 16  * where the properties should/can be:
 17  * <code>options.href</code>: (String) the URL for the LINK
 18  * <code>options.rel</code>: (String) OPTIONAL (see above)
 19  * <code>options.onload</code>: (Function) OPTIONAL callback for onload event (see above)
 20  * <code>options.<property></code>: (any) OPTIONAL any additional properties will be added to the created LINK object "as-is"
 21  * 
 22  * @requires document
 23  * @requires document.head
 24  * @requires document.createElement( link )
 25  */
 26 define(function loadJqmCss() {
 27 	
 28 	function loadLink(href, rel, callback){
 29 		
 30 		//normalize arguments:
 31 		var attrs = null;
 32 		if(typeof href === 'object'){
 33 			//handle case: args is options object
 34 			attrs = href;
 35 			
 36 			if(attrs.href){
 37 				href = attrs.href;
 38 				delete attrs.href;
 39 			}
 40 			
 41 			if(attrs.rel){
 42 				rel = attrs.rel;
 43 				delete attrs.rel;
 44 			}
 45 			
 46 			if(attrs.onload){
 47 				callback = attrs.onload;
 48 				delete attrs.onload;
 49 			}
 50 			
 51 		}
 52 		else if(typeof rel === 'function'){
 53 			//handle case: args' 2nd param is callback function
 54 			callback = rel;
 55 			rel = void(0);
 56 		}
 57 		
 58 		//create the link and its properties:
 59 		
 60 		var link = document.createElement('link');
 61 		link.rel = rel? rel : 'stylesheet';
 62 		link.href = href;
 63 		
 64 		if(attrs) for(var prop in attrs){
 65 			if(attrs.hasOwnProperty(prop)){
 66 //				if(/^on/.test(prop)){
 67 //					TODO russa: should we apply special treatment for event handlers?
 68 //				}
 69 				link[prop] = attrs[prop];
 70 			}
 71 		}
 72 	
 73 		//NOTE this may not work 
 74 		// (some browser do not support onload for CSS; some only fire onload for remotely loaded CSS files...)
 75 		if (typeof callback === 'function') {
 76 			/** @ignore */
 77 			link.onload = function() {
 78 				callback.apply(this, href, rel);
 79 			};
 80 		}
 81 		document.getElementsByTagName('head')[0].appendChild(link);
 82 	}
 83 	
 84 	return loadLink;
 85 	
 86 });
 87