/// <summary> /// Parses HtmlgenericControl attributes into a script object /// </summary> /// <param name="ctl"></param> /// <returns></returns> private ScriptItem ParseScriptProperties(HtmlGenericControl ctl) { ScriptItem script = new ScriptItem(); script.Src = ctl.Attributes["Src"]; string val = ctl.Attributes["RenderMode"] ?? ""; switch (val.ToLower()) { case "header": script.RenderMode = ScriptRenderModes.Header; break; case "headertop": script.RenderMode = ScriptRenderModes.HeaderTop; break; case "script": script.RenderMode = ScriptRenderModes.Script; break; case "inline": script.RenderMode = ScriptRenderModes.Inline; break; default: script.RenderMode = ScriptRenderModes.Inherit; break; } val = ctl.Attributes["AllowMinScript"] ?? ""; if (val.ToLower() == "true") script.AllowMinScript = true; val = ctl.Attributes["Resource"] ?? ""; if (!string.IsNullOrEmpty(val)) { script.Resource = val; foreach (ScriptResourceAlias alias in ClientScriptProxy.ScriptResourceAliases) { if (val == alias.Alias) { script.Resource = alias.Resource; if (alias.ControlType != null) script.ResourceControlType = alias.ControlType; } } } if (string.IsNullOrEmpty(script.Src) && string.IsNullOrEmpty(script.Resource)) throw new ArgumentException("ScriptContainer <script> tag requires either the 'src' or 'Resource' property set."); val = ctl.Attributes["ResourceControl"] ?? ""; if (!string.IsNullOrEmpty(val)) script.ResourceControl = val; val = ctl.Attributes["Assembly"] ?? ""; if (!string.IsNullOrEmpty(val)) script.ResourceAssembly = val; val = ctl.Attributes["Version"] ?? ""; if (!string.IsNullOrEmpty(val)) script.Version = val; return script; }
/// <summary> /// Helper function that is used to load script resources for various AJAX controls /// Loads a script resource based on the following scriptLocation values: /// /// * WebResource /// Loads the Web Resource specified out of WebResources. Specify the resource /// that applied in the resourceName parameter /// /// * Url/RelativeUrl /// loads the url with ResolveUrl applied /// /// * empty (no value) /// No action is taken /// </summary> /// <param name="control">The control instance for which the resource is to be loaded</param> /// <param name="scriptLocation">WebResource, a Url or empty (no value)</param> /// <param name="resourceName">The name of the resource when WebResource is used for scriptLocation</param> /// <param name="topOfHeader">Determines if scripts are loaded into the header whether they load at the top or bottom</param> public void LoadControlScript(Control control, string scriptLocation, string resourceName, ScriptRenderModes renderMode) { ClientScriptProxy proxy = ClientScriptProxy.Current; // Specified nothing to do if (string.IsNullOrEmpty(scriptLocation)) return; ScriptItem scriptItem = new ScriptItem(); scriptItem.Src = scriptLocation; scriptItem.RenderMode = renderMode; if (scriptLocation == "WebResource") { scriptItem.Resource = resourceName; scriptItem.ResourceAssembly = null; scriptItem.Src = resourceName; } //// It's a relative url //if (ClientScriptProxy.LoadScriptsInHeader) // proxy.RegisterClientScriptIncludeInHeader(control, control.GetType(), // control.ResolveUrl(scriptLocation), topOfHeader); //else // proxy.RegisterClientScriptInclude(control, control.GetType(), // Path.GetFileName(scriptLocation), control.ResolveUrl(scriptLocation)); AddScript(scriptItem); }
/// <summary> /// Adds a script item to the page with full options /// </summary> /// <param name="scriptUrl">The Url to load script from. Can include ~/ syntax</param> /// <param name="renderMode">Determines where the script is rendered</param> /// <param name="allowMinScript">Determines if in non-debug mode .min.js files are used</param> public void AddScript(ScriptItem script) { ScriptItem match = null; if (!string.IsNullOrEmpty(script.Src)) { // Grab just the path script.FileId = Path.GetFileName(script.Src).ToLower(); // Check if the script was already added to the page match = InternalScripts.Find( item => item.FileId == script.FileId ); } if (match == null) InternalScripts.Add(script); else match = script; }
/// <summary> /// Adds a script to the collection of embedded scripts /// </summary> /// <param name="scriptUrl"></param> /// <param name="renderMode"></param> /// <param name="allowMinScript"></param> public void AddScript(string scriptUrl, ScriptRenderModes renderMode, bool allowMinScript) { ScriptItem script = new ScriptItem(); script.Src = scriptUrl; script.RenderMode = renderMode; script.AllowMinScript = allowMinScript; AddScript(script); }
/// <summary> /// Registers an individual script item in the page /// </summary> /// <param name="script"></param> private void RegisterScriptItem(ScriptItem script) { // Allow inheriting from control which is the default behavior if (script.RenderMode == ScriptRenderModes.Inherit) script.RenderMode = RenderMode; // Skip over inline scripts if (script.RenderMode == ScriptRenderModes.Inline) return; // special case jquery load from Content network if (ScriptLoader.jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork && script.Resource == WebResources.JQUERY_SCRIPT_RESOURCE || script.Resource == "jquery" || script.Resource == "jquery.js" || script.Src.ToLower().Contains("/jquery.js") ) { ScriptLoader.LoadjQuery(Page); return; } // special case jquery-uiload from Content network if (script.Src.ToLower().Contains("/jquery-ui.") || script.Resource == "jqueryui" || script.Resource == "jquery-ui") { ScriptLoader.LoadjQueryUi(Page, null); return; } string src = string.Empty; Type ctlType = typeof(WebResources); if (script.ResourceControlType != null || !string.IsNullOrEmpty(script.ResourceControl)) { if (script.ResourceControlType != null) ctlType = script.ResourceControlType; else { Control ctl = WebUtils.FindControlRecursive(Page, script.ResourceControl, false); if (ctl != null) { ctlType = ctl.GetType(); } else throw new ArgumentException("Invalid Web Control passed for resource retrieval. Please pass a control from the assembly where the resource are located."); } src = scriptProxy.GetClientScriptResourceUrl(Page, ctlType, script.Resource); } else if (!string.IsNullOrEmpty(script.Resource)) { src = scriptProxy.GetClientScriptResourceUrl(this, ctlType, script.Resource); } else { src = ResolveUrl(script.Src); // Fix up the URL so we can allow ~/script syntax if (script.AllowMinScript && !HttpContext.Current.IsDebuggingEnabled) src = src.ToLower().Replace(".js", MinScriptExtension); } // if there's a version number implied add a par if ( !string.IsNullOrEmpty(script.Version) ) { if (src.Contains("?")) src += "&ver=" + script.Version; else src += "?ver=" + script.Version; } scriptProxy.RegisterClientScriptInclude(Page, ctlType, src, script.RenderMode); }