protected void registerScripts(AppScripts scripts) { scripts.ScriptsFolder = "/ui/"; scripts.Add("component1", "component1.js", "component1.js"); scripts.Add("component2", "component2.js", "component2.js", "component1"); scripts.Add("pagescript1", "pagescript1.js", "pagescript1.js", "component2"); }
protected void writeScriptPath(FileScriptResource script, AppScripts appScripts, WriteDelegate write) { string path = Debug ? script.DebugPath : script.MinPath; // Don't append scripts folder if path looks like: // http://... https://... //www.blah... /folder/blah... if (!(path.StartsWith("http") || path.StartsWith("/"))) write(appScripts.ScriptsFolder); write(path); }
protected void renderAsync(TextWriter writer, HashSet<ScriptLoadGroup> scriptMap, AppScripts appScripts) { string labJsPath = Debug ? appScripts.LabJsSrc : appScripts.LabJsMin; writer.Write("<script src="); if (labJsPath.Contains(" ")) writer.Write("\""); if (!(labJsPath.StartsWith("http") || labJsPath.StartsWith("/"))) writer.Write(appScripts.ScriptsFolder); writer.Write(labJsPath); if (labJsPath.Contains(" ")) writer.Write("\""); writer.WriteLine("></script>"); writer.WriteLine("<script>"); writer.Write("$LAB.setGlobalDefaults({AppendTo:'body'});"); foreach (ScriptLoadGroup group in scriptMap) { System.Diagnostics.Debug.Assert(group.Parent == null, "Root ScriptLoadGroup has a parent, meaning it's not actually at the root!"); StringBuilder inlineScriptSB = new StringBuilder(); StringBuilder fileScriptSB = new StringBuilder(); // Batch up first group of inline scripts and file scripts foreach (ScriptLoadItem item in group.Scripts) { if (item.Script is InlineScriptResource) { // Get inline scripts out of the way - these are in the root // so we don't even bother with LabJs, we just call them. // Important to include them for concat/minify functionality. InlineScriptResource script = (InlineScriptResource)item.Script; inlineScriptSB.AppendLine(); // Function wrapper usually pointless if var keyword not used // TODO: Handle cases where no vars declared in code that needs // to be wrapped //bool wrap = script.Body.Contains("var"); //if (wrap) inlineScriptSB.AppendLine("(function(){"); inlineScriptSB.AppendLine(script.Body); //if (wrap) inlineScriptSB.Append("})();"); } else { // Start the dependency tree - fire off root includes FileScriptResource script = (FileScriptResource)item.Script; fileScriptSB.AppendLine(); fileScriptSB.Append(".script('"); writeScriptPath(script, appScripts, delegate(string str) { fileScriptSB.Append(str); }); fileScriptSB.Append("')"); } } // Write out first group of inline scripts outside LabJs writer.Write(inlineScriptSB.ToString()); inlineScriptSB = null; // Start up LabJs writer.WriteLine(); writer.Write("$LAB"); // Write out first group of file scripts writer.Write(fileScriptSB.ToString()); // Start digging through child groups ScriptLoadGroup childGroup = group.ChildGroup; while (childGroup != null) { fileScriptSB = new StringBuilder(); List<InlineScriptResource> inlineScripts = new List<InlineScriptResource>(); // Batch up inline scripts, and begin building file scripts string foreach (ScriptLoadItem item in childGroup.Scripts) { if (item.Script is InlineScriptResource) { InlineScriptResource script = (InlineScriptResource)item.Script; inlineScripts.Add(script); } else { FileScriptResource script = (FileScriptResource)item.Script; fileScriptSB.AppendLine(); fileScriptSB.Append(".script('"); writeScriptPath(script, appScripts, delegate(string str) { fileScriptSB.Append(str); }); fileScriptSB.Append("')"); } } writer.WriteLine(); // Write out inline scripts, if any, in a wait() statement so they // load after their dependencies as requested if (inlineScripts.Count == 0) { // No inline scripts - just a plain wait statement to separate // the previous round of includes from the scripts waiting in // fileScriptsSB writer.Write(".wait()"); } else { // TODO: Get fancier and try to notice a wrapper function and toss it as well, // probably requires something like Rhino or the DLR to really do right - some // parsing ourselves of the whole resulting tree after the fact and elimination of // useless code. // Actually, we should probably create this piece of script then put it through // ajaxmin -hc ! // If it's just a function call, ditch the empty params and just pass the function // Looking for string like "fn();" if (inlineScripts.Count == 1) { if (new Regex(@"^[\w\d_-]+\(\);?$").IsMatch(inlineScripts[0].Body)) { string body = inlineScripts[0].Body; // Writes out like .wait(fn) writer.Write(".wait("); writer.Write(body.Substring(0, body.IndexOf('('))); writer.Write(")"); } else { writer.WriteLine(".wait(function(){"); writer.WriteLine(inlineScripts[0].Body); writer.Write("})"); } } else { writer.WriteLine(".wait(function(){"); foreach (InlineScriptResource script in inlineScripts) { // Dump the body out like // (function(){ // alert('hi'); // })(); writer.WriteLine("(function(){"); writer.WriteLine(script.Body); writer.WriteLine("})();"); } writer.Write("})"); } } // Dump out the file scripts string we built earlier in the loop writer.Write(fileScriptSB.ToString()); // On to the next child childGroup = childGroup.ChildGroup; } writer.WriteLine(";"); } writer.WriteLine("</script>"); }
protected void renderSimpleScript(ScriptLoadItem item, TextWriter writer, AppScripts appScripts) { if (item.Script is InlineScriptResource) { var script = (InlineScriptResource)item.Script; writer.WriteLine("<script>"); writer.WriteLine("(function() {"); writer.WriteLine(script.Body); writer.WriteLine("})();"); writer.WriteLine("</script>"); } else { var script = (FileScriptResource)item.Script; writer.Write("<script src=\""); writeScriptPath(script, appScripts, delegate(string str) { writer.Write(str); }); writer.WriteLine("\"></script>"); } }
protected void renderSimple(TextWriter writer, HashSet<ScriptLoadGroup> scriptMap, AppScripts appScripts) { foreach (ScriptLoadGroup group in scriptMap) { foreach (ScriptLoadItem item in group.Scripts) { renderSimpleScript(item, writer, appScripts); } ScriptLoadGroup childGroup = group.ChildGroup; while (childGroup != null) { foreach (ScriptLoadItem item in childGroup.Scripts) { renderSimpleScript(item, writer, appScripts); } childGroup = childGroup.ChildGroup; } } }