/* * @Author : l.w.kampfer@gmail.com */ (function(global) { var COMPILED = false; var kampfer = {}; kampfer.global = global; kampfer.basePath = ''; kampfer.implicitNamespaces = {}; kampfer.isProvided = function(name) { return !kampfer.implicitNamespaces[name] && !!kampfer.getPropertyByName(name); }; kampfer.getPropertyByName = function( name, obj ) { var namespace = name.split('.'), cur = obj || kampfer; for( var part; (part = namespace.shift()); ) { if( kampfer.isDefAndNotNull( cur[part] ) ) { cur = cur[part]; } else { return null; } } return cur; }; kampfer.exportPath = function(name, value, objectToExportTo) { var cur = objectToExportTo || kampfer.global, namespace = name.split('.'); for( var part; (part = namespace.shift()); ) { if( !namespace.length && kampfer.isDefAndNotNull(value) ) { cur[part] = value; } else if( cur[part] ) { cur = cur[part]; } else { cur = cur[part] = {}; } } }; kampfer.provide = function(name) { //if(!COMPILED) { if( kampfer.isProvided(name) ) { throw Error('Namespace "' + name + '" already declared.'); } delete kampfer.implicitNamespaces[name]; var namespace = name; while( (namespace = namespace.substring( 0, namespace.lastIndexOf('.') )) ) { if( kampfer.getPropertyByName(namespace) ) { break; } else { kampfer.implicitNamespaces[namespace] = true; } } //} kampfer.exportPath(name, null, kampfer); }; kampfer.require = function(name) { if( !COMPILED ) { if ( !name || kampfer.isProvided(name) ) { return; } var path = kampfer._getPathFromDeps(name); if (path) { kampfer._included[path] = true; kampfer._writeScripts(); } } }; kampfer.addDependency = function( path, provides, requires ) { if( !COMPILED ) { var provide, require, deps; path = path.replace(/\\/g, '/'); deps = kampfer._dependencies; for( var i = 0; (provide = provides[i]); i++) { deps.nameToPath[provide] = path; if (!(path in deps.pathToNames)) { deps.pathToNames[path] = {}; } deps.pathToNames[path][provide] = true; } for( var j = 0; (require = requires[j]); j++) { if (!(path in deps.requires)) { deps.requires[path] = {}; } deps.requires[path][require] = true; } } }; if(!COMPILED) { kampfer._inHtmlDocument = function() { var doc = kampfer.global.document; return typeof doc != 'undefined' && 'write' in doc; // XULDocument misses write. }; kampfer._getPathFromDeps = function(name) { if( name in kampfer._dependencies.nameToPath ) { return kampfer._dependencies.nameToPath[name]; } else { return null; } }; kampfer._importScript = function(src) { var _importScript = kampfer._writeScriptTag; if(!kampfer._dependencies.written[src] && _importScript(src)) { kampfer._dependencies.written[src] = true; } }; kampfer._writeScripts = function() { var scripts = [], seenScript = {}, deps = kampfer._dependencies; function visitNode(path) { if( path in deps.written ) { return; } if( path in deps.visited ) { if( !(path in seenScript) ) { seenScript[path] = true; scripts.push(path); } return; } deps.visited[path] = true; if (path in deps.requires) { for (var requireName in deps.requires[path]) { if (!kampfer.isProvided(requireName)) { if (requireName in deps.nameToPath) { visitNode(deps.nameToPath[requireName]); } else { throw Error('Undefined nameToPath for ' + requireName + ' in ' + path); } } } } if (!(path in seenScript)) { seenScript[path] = true; scripts.push(path); } } for( var path in kampfer._included ) { if( !deps.written[path] ) { visitNode(path); } } for( var i = 0; i < scripts.length; i++ ) { if ( scripts[i] ) { kampfer._importScript( kampfer.basePath + scripts[i] ); } else { throw Error('Undefined script input'); } } }; kampfer._writeScriptTag = function(src) { if( kampfer._inHtmlDocument() ) { var doc = kampfer.global.document; doc.write('