DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Java-Like Javascript Library Container //Javascript Object

07.02.2005
| 5288 views |
  • submit to reddit
        
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

libraryControl = {
	baseURI: '',
	packs: {},

	register: function( pack ){
		for( var _, m, p = this.packs, pack = ( m = pack.split( '.' ) ).pop(); _ = m.shift(); p = p[_] || ( p[_]={} ) );
		( p['%files%'] || ( p['%files%'] = {} ) )[pack] = {included:false,loaded:false};
	},

	findPackage: function( pack ){
		for( var m, p = this.packs, pack = ( m = pack.split( '.' ) ).pop(); m.length && ( p = p[ m.shift() ] ); );
		return !m.length && (p=p['%files%']) ? p[pack] : null;
	},

	isIncluded: function( pack ){
		with( {x:this.findPackage( pack )} ) return x && x.included;
	},

	isLoaded: function( pack ){
		with( {x:this.findPackage( pack )} ) return x && x.loaded;
	},

	include: function( pack ) {
		var p = this.findPackage( pack ), pack = this.baseURI + pack.split( '.' ).join( '/' ) + '.js';
		if( p ){
			if( !document.body )
				document.write( '<script type="text/javascript" src="'+pack+'"><\/script>' );
			else with( {s: document.createElement( 'script' ) } ){
				s.type = 'text/javascript';
				s.src = pack;
				document.body.appendChild( s );
			}
			return p.included = true;
		}
		return false;
	},

	require: function( pack ) {
		var p = this.findPackage( pack );
  		if( p && !p.included )
			return this.include( pack );
		return false;
	}
}

//libraryControl.register( 'library.test' );
//libraryControl.require( 'library.test' );
//libraryControl.include( 'library.test' );

    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

Please remove the tag "java" as this does not relate to java ;-)