Esempio n. 1
0
        /// <summary>
        /// Loads jQuery depending on configuration settings (CDN, WebResource or site url)
        /// and injects the full script link into the page.
        /// </summary>
        /// <param name="control"></param>
        /// <param name="jQueryUrl">Optional url to jQuery as a virtual or absolute server path</param>
        public static void LoadjQuery(Control control, string jQueryUrl)
        {
            ClientScriptProxy p = ClientScriptProxy.Current;

            if (!string.IsNullOrEmpty(jQueryUrl))
            {
                p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryUrl, ScriptRenderModes.HeaderTop);
            }
            else if (jQueryLoadMode == jQueryLoadModes.WebResource)
            {
                p.RegisterClientScriptResource(control, typeof(ControlResources), ControlResources.JQUERY_SCRIPT_RESOURCE, ScriptRenderModes.HeaderTop);
            }
            else if (jQueryLoadMode == jQueryLoadModes.ContentDeliveryNetwork)
            {
                // Load from CDN Url specified
                p.RegisterClientScriptInclude(control, typeof(ControlResources), jQueryCdnUrl, ScriptRenderModes.HeaderTop);

                // check if jquery loaded - if it didn't we're not online and use WebResource
                string scriptCheck =
                    @"if (typeof(jQuery) == 'undefined')  
        document.write(unescape(""%3Cscript src='{0}' type='text/javascript'%3E%3C/script%3E""));";

                jQueryUrl = p.GetClientScriptResourceUrl(control, typeof(ControlResources),
                                                         ControlResources.JQUERY_SCRIPT_RESOURCE);
                p.RegisterClientScriptBlock(control, typeof(ControlResources),
                                            "jquery_register", string.Format(scriptCheck, jQueryUrl), true,
                                            ScriptRenderModes.HeaderTop);
            }

            return;
        }
        /// <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 == ControlResources.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(ControlResources);

            if (script.ResourceControlType != null || !string.IsNullOrEmpty(script.ResourceControl))
            {
                if (script.ResourceControlType != null)
                {
                    ctlType = script.ResourceControlType;
                }
                else
                {
                    Control ctl = UrlUtils.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);
        }