public void writeImports()

in tapestry-core/src/main/java/org/apache/tapestry5/internal/services/javascript/EsModuleManagerImpl.java [185:282]


    public void writeImports(Element root, List<EsModuleInitialization> inits) 
    {
        Element script;
        Element body = null;
        Element head = null;
        ImportPlacement placement;
        EsModuleInitializationImpl init;
        String functionName;
        Object[] arguments;
        
        for (EsModuleInitialization i : inits) 
        {
            
            init = (EsModuleInitializationImpl) i;
            final String moduleId = init.getModuleId();
            // Making sure the user doesn't shoot heir own foot
            final String url = cache.get(moduleId);
            if (url == null)
            {
                throw new UnknownValueException("ES module not found: " + moduleId, 
                        new AvailableValues("String", cache));
            }
            
            placement = init.getPlacement();
            if (placement.equals(ImportPlacement.HEAD))
            {
                if (head == null) 
                {
                    head = root.find("head");
                }
                script = head.element("script");
            }
            else {
                if (body == null)
                {
                    body = root.find("body");
                }
                if (placement.equals(ImportPlacement.BODY_BOTTOM)) {
                    script = body.element("script");
                }
                else if (placement.equals(ImportPlacement.BODY_TOP))
                {
                    script = body.elementAt(0, "script");
                }
                else
                {
                    throw new IllegalArgumentException("Unknown import placement: " + placement);
                }
            }
            
            writeAttributes(script, init);
            script.attribute("src", url);
            
            functionName = init.getFunctionName();
            arguments = init.getArguments();
            
            if (!productionMode)
            {
                script.attribute("data-module-id", moduleId);
                final Element log = script.element("script", "type", "text/javascript");
                log.text(String.format("console.debug('Imported ES module %s');", moduleId));
                log.moveBefore(script);
            }
            
            // If we have not only the import, but also an automatic function call
            if (arguments != null || functionName != null)
            {
                final Element moduleFunctionCall = script.element("script");
                
                moduleFunctionCall.moveAfter(script);
                
                final String moduleFunctionCallFormat = 
                        "import %s from '%s';\n"
                        + "%s(%s);";
                
                final String importName = functionName != null ? functionName : GENERIC_IMPORTED_VARIABLE;
                final String importDeclaration = functionName != null ? 
                        "{ " + functionName + " }": 
                            GENERIC_IMPORTED_VARIABLE;
                
                moduleFunctionCall.text(String.format(moduleFunctionCallFormat, 
                        importDeclaration, moduleId, importName,
                        convertToJsFunctionParameters(arguments, compactJSON)));
                
                writeAttributes(moduleFunctionCall, init);
                
                // Avoiding duplicated ids
                final String id = moduleFunctionCall.getAttribute("id");
                if (id != null)
                {
                    moduleFunctionCall.forceAttributes("id", id + "-function-call");
                }
                
            }
            
        }
        
    }