Registering Required Resources

Lynicon includes its own utility subsystem for registering the need of script files for certain resources, whether these be inline or included CSS or Javascript, or HTML snippets which should only appear once.

To activate this subsystem, set up the FilterConfig.cs file like this:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
	...
	filters.Add(new ProcessIncludesAttribute()); 
	filters.Add(new ProcessHtmlAttribute());
}

If you prefer, you can use these filter attributes like any other in MVC and apply them locally instead of globally.

Currently the ProcessHtmlAttribute is used to post-process the entire HTML of the page (via HtmlAgilityPack) and the ProcessIncludesAttribute as a client of this function sets up registering resources.  This could potentially be more efficient, although at present it will often operate within a millisecond for small numbers of included items.

The functionality is accessed through some custom extensions to the HtmlHelper listed below:

@Html.RegisterScript(string script)
@Html.RegisterScript(string id, string script, List<string> dependencies)
@Html.RegisterCss(string url) @Html.RegisterLocalStyles(string id, string style)
@Html.RegisterHtmlBlock(string id, string html)
@Html.RegisterHtmlBlock(string id, MvcHtmlString html)
@Html.ScriptIsRegistered(string id)
@Html.CssIsRegistered(string id)
@Html.LocalStyleIsRegistered(string id)
@Html.HtmlBlockIsRegistered(string id)

The RegisterScript functions add a script into the page once.  If the 'script' argument starts with the string "javascript:" the script after this is taken to be Javascript to be inserted inline.  If not, it is assumed to be the url for a script file.

The second overload includes an id, which is set on the script element and used to identify where the same script is being registered from different places.  It also includes a list of dependencies, which are the ids of scripts which this one depends on and which must precede it in the output file.  Lynicon when sorting scripts in the file will extract any existing script references together with all the references whose need is registered using this method.  It will then sort them to attempt to satisfy all such dependency requirements.

RegisterCss simply adds a link to a CSS file whose url is given.  RegisterLocalStyles adds the style (or styles) given as the second parameter, using the first to avoid duplication.

RegisterHtmlBlock adds a piece of HTML into the file (which may be at any point in the body), ensuring it is not repeated.  This can be used for e.g. dialog boxes or overlays used in more than one place.  Because this can take an MvcHtmlString as an argument, this argument could be the output of another Html.XYZ type call.

The last 4 calls are tests for whether a requirement is registered already which return a bool result.