Ejemplo n.º 1
0
        /**
        * Injects preloads into the gadget output.
        *
        * If preloading fails for any reason, we just output an empty object.
        */
        private static void InjectPreloads(Gadget gadget, Node scriptTag) 
        {
            IPreloads preloads = gadget.getPreloads();

            Dictionary<String, Object> preload = new Dictionary<string, object>();

            foreach(PreloadedData preloaded in preloads.getData()) 
            {
                foreach(var entry in preloaded.toJson()) 
                {
                    preload.Add(entry.Key, entry.Value);
                }
            }
            Text text = scriptTag.getOwnerDocument().createTextNode("gadgets.io.preloaded_=");
            text.appendData(JsonConvert.ExportToString(preload));
            text.appendData(";");
            scriptTag.appendChild(text);
        }
Ejemplo n.º 2
0
        /**
        * Injects default values for user prefs into the gadget output.
        */
        private static void InjectDefaultPrefs(Gadget gadget, Node scriptTag)
        {
                List<UserPref> prefs = gadget.getSpec().getUserPrefs();
                Dictionary<String, String> defaultPrefs = new Dictionary<string, string>(prefs.Count);

                foreach(UserPref up in gadget.getSpec().getUserPrefs())
                {
                    defaultPrefs.Add(up.getName(), up.getDefaultValue());
                }
                Text text = scriptTag.getOwnerDocument().createTextNode("gadgets.Prefs.setDefaultPrefs_(");
                text.appendData(JsonConvert.ExportToString(defaultPrefs));
                text.appendData(");");
                scriptTag.appendChild(text);
        }
Ejemplo n.º 3
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()));
            }
        }
Ejemplo n.º 4
0
        /**
        * Injects message bundles into the gadget output.
        * @throws GadgetException If we are unable to retrieve the message bundle.
        */
        private void InjectMessageBundles(Gadget gadget, Node scriptTag) 
        {
            GadgetContext context = gadget.getContext();
            MessageBundle bundle = messageBundleFactory.getBundle(
                gadget.getSpec(), context.getLocale(), context.getIgnoreCache());

            String msgs = bundle.ToJSONString();

            Text text = scriptTag.getOwnerDocument().createTextNode("gadgets.Prefs.setMessages_(");
            text.appendData(msgs);
            text.appendData(");");
            scriptTag.appendChild(text);

        }
Ejemplo n.º 5
0
 private static void InjectOnLoadHandlers(Node bodyTag) 
 {
     Element onloadScript = bodyTag.getOwnerDocument().createElement("script");
     bodyTag.appendChild(onloadScript);
     onloadScript.appendChild(bodyTag.getOwnerDocument().createTextNode(
         "gadgets.util.runOnLoadHandlers();"));
 }