Пример #1
0
        /// <summary>
        /// Adds a path to the bundle with the specified order.
        /// </summary>
        /// <param name="path">The path to add to the bundle.</param>
        /// <param name="order">The specified order.</param>
        /// <param name="recursive">When adding a folder, tells whether to include contents of subfolders</param>
        public void AddPath(string path, int order = 0, bool recursive = true)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (_pathsWithOrders.Any(x => x.Item1 == path))
            {
                return;
            }

            if (IsClosed)
            {
                throw new Exception("You can't add more files to the bundle when it's closed.");
            }

            // Take care of folders
            if (RepositoryTools.RecurseFilesInVirtualPath(path, recursive, p => AddPath(p, order, recursive)))
            {
                return;
            }

            // Take care of files
            var  item             = Tuple.Create(path, order);
            bool foundHigherOrder = false;
            int  index            = -1;

            foreach (var pathWithOrder in _pathsWithOrders)
            {
                index++;
                if (pathWithOrder.Item2 > order)
                {
                    foundHigherOrder = true;
                    break;
                }
            }

            if (foundHigherOrder)
            {
                _pathsWithOrders.Insert(index, item);
            }
            else
            {
                _pathsWithOrders.Add(item);
            }
        }
Пример #2
0
        /// <summary>
        /// Adds a script reference to ScriptManager that will be rendered to the html.
        /// </summary>
        /// <param name="scriptPath">Path of the script. Can be a skin-relative path.</param>
        /// <param name="control">Source control, optional. It will be used to find the current CacheablePortlet and the script reference to it too.</param>
        public static void AddScript(string scriptPath, Control control = null)
        {
            // Take care of folders
            if (RepositoryTools.RecurseFilesInVirtualPath(SkinManager.Resolve(scriptPath), true, p => AddScript(p, control)))
            {
                return;
            }

            var currScriptManager = GetScriptManager();

            if (currScriptManager == null)
            {
                throw new Exception("The current page does not contain a script manager.");
            }

            if (currScriptManager is SNScriptManager)
            {
                // use SNScriptManager's SmartLoader if present
                var smartLoader = ((SNScriptManager)currScriptManager).SmartLoader;
                smartLoader.AddScript(scriptPath);
            }
            else
            {
                // fallback to ASP.NET ScriptManager
                var scriptReference = new ScriptReference {
                    Path = scriptPath
                };
                currScriptManager.Scripts.Add(scriptReference);
            }

            // Add script reference to cacheable portlet if possible, to
            // be able to add references even if the portlet html is cached.
            var cb = GetCacheablePortlet(control);

            if (cb != null)
            {
                cb.AddScript(scriptPath);
            }
        }
Пример #3
0
        /// <summary>
        /// Adds a CSS link to the given header using the given order and parameters. If a link with the given order already exists new link is added right after.
        /// </summary>
        /// <param name="header">Page header</param>
        /// <param name="cssPath">Path of CSS file</param>
        /// <param name="order">Desired order of CSS link</param>
        /// <param name="allowBundlingIfEnabled"></param>
        /// <param name="control">Source control, optional. It will be used to find the current CacheablePortlet and the script reference to it too.</param>
        public static void AddStyleSheetToHeader(Control header, string cssPath, int order, string rel, string type, string media, string title, bool allowBundlingIfEnabled = true, Control control = null)
        {
            if (header == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(cssPath))
            {
                return;
            }

            // Take care of folders
            if (RepositoryTools.RecurseFilesInVirtualPath(SkinManager.Resolve(cssPath), true, p => AddStyleSheetToHeader(header, p, order, rel, type, media, title, allowBundlingIfEnabled, control)))
            {
                return;
            }

            var resolvedPath = SkinManager.Resolve(cssPath);

            if (allowBundlingIfEnabled && rel == "stylesheet" && type == "text/css" && PortalBundleOptions.Current.AllowCssBundling)
            {
                if (!string.IsNullOrEmpty(title))
                {
                    throw new Exception("The title attribute on link tags is not supported when CSS bundling is enabled.");
                }

                PortalBundleOptions.Current.EnableCssBundling(header);

                // If this is CSS stylesheet and bundling is enabled, add it to the bundle

                // Find the bundle object for the current media
                var bundle = PortalBundleOptions.Current.CssBundles.SingleOrDefault(x => x.Media == media);

                if (bundle == null)
                {
                    bundle = new CssBundle()
                    {
                        Media = media,
                    };
                    PortalBundleOptions.Current.CssBundles.Add(bundle);
                }

                // Add the current resolved path to the bundle
                if (PortalBundleOptions.CssIsBlacklisted(resolvedPath))
                {
                    bundle.AddPostponedPath(resolvedPath);
                }
                else
                {
                    bundle.AddPath(resolvedPath, order);
                }
            }
            else
            {
                // If bundling is disabled, fallback to the old behaviour

                var cssLink = new HtmlLink();
                cssLink.ID = "cssLink_" + resolvedPath.GetHashCode().ToString();

                // link already added to header
                if (header.FindControl(cssLink.ID) != null)
                {
                    return;
                }

                cssLink.Href = resolvedPath;
                cssLink.Attributes["rel"]      = rel;
                cssLink.Attributes["type"]     = type;
                cssLink.Attributes["media"]    = media;
                cssLink.Attributes["title"]    = title;
                cssLink.Attributes["cssorder"] = order.ToString();

                // find next control with higher order
                var  index = -1;
                bool found = false;
                foreach (Control headerControl in header.Controls)
                {
                    index++;

                    var link = headerControl as HtmlLink;
                    if (link == null)
                    {
                        continue;
                    }

                    var orderStr = link.Attributes["cssorder"];
                    if (string.IsNullOrEmpty(orderStr))
                    {
                        continue;
                    }

                    int linkOrder = Int32.MinValue;
                    if (Int32.TryParse(orderStr, out linkOrder) && linkOrder > order)
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    // add link right before higher order link
                    header.Controls.AddAt(index, cssLink);
                }
                else
                {
                    // add link at end of header's controlcollection
                    header.Controls.Add(cssLink);
                }
            }

            // Add stylesheet reference to cacheable portlet if possible, to
            // be able to add references even if the portlet html is cached.
            var cb = GetCacheablePortlet(control);

            if (cb != null)
            {
                cb.AddStyleSheet(new StyleSheetReference
                {
                    CssPath = cssPath,
                    Media   = media,
                    Order   = order,
                    Rel     = rel,
                    Title   = title,
                    Type    = type,
                    AllowBundlingIfEnabled = allowBundlingIfEnabled
                });
            }
        }