Exemplo n.º 1
0
        /// <summary>
        /// Injects javascript libraries needed to satisfy feature dependencies.
        /// </summary>
        /// <param name="gadget"></param>
        /// <param name="headTag"></param>
        private void InjectFeatureLibraries(Gadget gadget, Node headTag)
        {
            // TODO: If there isn't any js in the document, we can skip this. Unfortunately, that means
            // both script tags (easy to detect) and event handlers (much more complex).
            GadgetContext    context    = gadget.getContext();
            GadgetSpec       spec       = gadget.getSpec();
            String           forcedLibs = context.getParameter("libs");
            HashKey <String> forced;

            if (string.IsNullOrEmpty(forcedLibs))
            {
                forced = new HashKey <string>();
            }
            else
            {
                forced = new HashKey <string>();
                foreach (var item in forcedLibs.Split(':'))
                {
                    forced.Add(item);
                }
            }


            // Forced libs are always done first.
            if (forced.Count != 0)
            {
                String  jsUrl   = urlGenerator.getBundledJsUrl(forced, context);
                Element libsTag = headTag.getOwnerDocument().createElement("script");
                libsTag.setAttribute("src", jsUrl);
                headTag.appendChild(libsTag);

                // Forced transitive deps need to be added as well so that they don't get pulled in twice.
                // TODO: Figure out a clean way to avoid having to call getFeatures twice.
                foreach (GadgetFeature dep in featureRegistry.GetFeatures(forced))
                {
                    forced.Add(dep.getName());
                }
            }

            // Inline any libs that weren't forced. The ugly context switch between inline and external
            // Js is needed to allow both inline and external scripts declared in feature.xml.
            String container = context.getContainer();
            ICollection <GadgetFeature> features = GetFeatures(spec, forced);

            // Precalculate the maximum length in order to avoid excessive garbage generation.
            int size = 0;

            foreach (GadgetFeature feature in features)
            {
                foreach (JsLibrary library in feature.getJsLibraries(RenderingContext.GADGET, container))
                {
                    if (library._Type == JsLibrary.Type.URL)
                    {
                        size += library.Content.Length;
                    }
                }
            }

            // Really inexact.
            StringBuilder inlineJs = new StringBuilder(size);

            foreach (GadgetFeature feature in features)
            {
                foreach (JsLibrary library in feature.getJsLibraries(RenderingContext.GADGET, container))
                {
                    if (library._Type == JsLibrary.Type.URL)
                    {
                        if (inlineJs.Length > 0)
                        {
                            Element inlineTag = headTag.getOwnerDocument().createElement("script");
                            headTag.appendChild(inlineTag);
                            inlineTag.appendChild(headTag.getOwnerDocument().createTextNode(inlineJs.ToString()));
                            inlineJs.Length = 0;
                        }
                        Element referenceTag = headTag.getOwnerDocument().createElement("script");
                        referenceTag.setAttribute("src", library.Content);
                        headTag.appendChild(referenceTag);
                    }
                    else
                    {
                        if (!forced.Contains(feature.getName()))
                        {
                            // already pulled this file in from the shared contents.
                            if (context.getDebug())
                            {
                                inlineJs.Append(library.DebugContent);
                            }
                            else
                            {
                                inlineJs.Append(library.Content);
                            }
                            inlineJs.Append(";\n");
                        }
                    }
                }
            }

            inlineJs.Append(GetLibraryConfig(gadget, features));

            if (inlineJs.Length > 0)
            {
                Element inlineTag = headTag.getOwnerDocument().createElement("script");
                headTag.appendChild(inlineTag);
                inlineTag.appendChild(headTag.getOwnerDocument().createTextNode(inlineJs.ToString()));
            }
        }