Ejemplo n.º 1
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        ///     22-Mar-2016 - This method builds the html to include static resources like javascript and css files.
        ///
        ///     We want to move towards retreiving all the JS and Styles from a static resource domain e.g. static.datanirvana.org
        ///     To do this is fine, but we also want to be able to test using local resources, so lets setup a web config param specifying the static resource path
        ///     ~/ is the local path and e.g. static.datanirvana.org would be the static path
        ///     If an external resource we need to choose https or not depending on the current session ...
        ///     THe resource list will appear like this: "Scripts/jquery-1.11.3.min.js", "Scripts/Site.js", "Styles/Site.css"
        ///     The Scripts or Styles prefix is critical as this determines which type of resource is referenced.
        ///     Note that for external resources like maps.googleapi.com still DO NOT include the http / https prefix ...
        /// </summary>
        public static string BuildStaticResourcesHTML(Page currentPage, string[] resourceNames)
        {
            StringBuilder str = new StringBuilder();

            if (resourceNames != null)
            {
                //-----a1----- declare all the variables to building these static resources to improve the readability!
                string staticResourcePath = MGLApplicationInterface.Instance().StaticResourcePath;
                bool   minifyJS           = MGLApplicationInterface.Instance().StaticJavascriptIsMinified;
                string httpPrefix         = (MGLSessionInterface.Instance().UseHTTPS == true) ? "https://" : "http://";

                //-----a2----- 11-May-2016 - Ok so here, if the SSLError bool in the session is set, then we DONT want to use the external resources as they wont work, so reset to use the local variants
                if (MGLSessionInterface.Instance().SSLError == true)
                {
                    staticResourcePath = "~/";
                    int userID = (Authorisation.CurrentUser != null) ? Authorisation.CurrentUser.ID : 0;
                    // This may be a little verbose on some sites, but lets keep it in for now to see how often this occurs....
                    Logger.LogWarning("SSL configuration error detected for user " + userID + " with ipaddress " + IPAddressHelper.GetIPAddressFromHTTPRequest() + " on page '" + currentPage.Title + "'.");
                }

                //-----a3---- And double check if we need to add a trailing forward slash ....
                string dir = (staticResourcePath.EndsWith("/") == true) ? "" : "/";

                //-----a4----- 31-Mar-2016 - lets append the JSVersion to ALL static resources, so that we can try to influence clients browsers to automatically refresh after updates
                // without having to visit the new DefaultFullReload page - this needs testing!!
                // http://stackoverflow.com/questions/32414/how-can-i-force-clients-to-refresh-javascript-files#32427
                // lets also remove the . from the version just to make 100% sure we dont confuse any legacy code that splits the file suffix on the last dot (oooops!)
                string versionSuffix = "?v=" + MGLApplicationInterface.Instance().JSVersion.Replace(".", "");


                //-----b----- loop through the static resources
                foreach (string staticResource in resourceNames)
                {
                    //-----c----- Get the relative path to this resource
                    string tempPath = httpPrefix + staticResourcePath + dir + staticResource;
                    // 31-Mar-2016 - special case for script files that are built dynamically and accessed from the code directory
                    // We dont want to get these from the remote resource location, these should always be local....
                    // Note that we cannot use the default path as this gets reset to the full default path - we always just want to use the simple ~/
                    if (staticResource.StartsWith("Code", StringComparison.CurrentCultureIgnoreCase) == true)
                    {
                        tempPath = currentPage.ResolveClientUrl("~/" + staticResource);
                    }
                    // And lets resolve the right relative URL if this is a local implementation
                    if (staticResourcePath.StartsWith("~"))
                    {
                        tempPath = currentPage.ResolveClientUrl(staticResourcePath + staticResource);
                    }

                    //-----d----- Build the resource tag!
                    // 31-Mar-2016 - also include the code prefix here so that the semi-static files for the Information and Map pages are treated in the same way with the v suffix
                    if (staticResource.StartsWith("Scripts", StringComparison.CurrentCultureIgnoreCase) == true ||
                        staticResource.StartsWith("Code", StringComparison.CurrentCultureIgnoreCase) == true)
                    {
                        //-----e----- Determine whether or not the javascript needs to be minified - if it does then convert the JS at the end to be .min.js if it has not already been set
                        if (minifyJS == true && tempPath.EndsWith(".min.js", StringComparison.CurrentCultureIgnoreCase) == false)
                        {
                            tempPath = tempPath.ToLower().Replace(".js", ".min.js");
                        }
                        str.Append("<script src=\"" + tempPath + versionSuffix + "\" type=\"text/javascript\"></script>");
                    }
                    else if (staticResource.StartsWith("Styles", StringComparison.CurrentCultureIgnoreCase))
                    {
                        //-----f1----- 19-Apr-2016 - we are now also minifying the CSS too - if it does then convert the css at the end to be .min.css if it has not already been set
                        if (minifyJS == true && tempPath.EndsWith(".min.css", StringComparison.CurrentCultureIgnoreCase) == false)
                        {
                            tempPath = tempPath.ToLower().Replace(".css", ".min.css");
                        }
                        //-----f2----- Build the styles link!
                        str.Append("<link href=\"" + tempPath + versionSuffix + "\" rel=\"stylesheet\" type=\"text/css\" />");
                    }
                    else
                    {
                        // Unusual / unknown static resource ... In fact, most probably an external resource!  So lets not amend the staticResource string, we just prefix and dont add the ?v=
                        // To reiterate - DO prefix with http / https and DO NOT add the version as a parameter ...
                        // e.g. maps.googleapis.com/maps/api/js?key=MY_API_KEY
                        str.Append("<script src=\"" + httpPrefix + staticResource + "\" type=\"text/javascript\"></script>");
                    }
                }
            }

            return(str.ToString());
        }