Exemple #1
0
 public static TElement[] Scripts <TElement, TAttribute, TObject>(IElementProvider <TElement, TAttribute, TObject> elementProvider, bool isDebug = false, TimeSpan?refreshPeriod = null, bool isInlineSyncScript = true)
 {
     return(new[]
     {
         elementProvider.Element("meta",
                                 elementProvider.Attribute("http-equiv", "X-UA-Compatible"),
                                 elementProvider.Attribute("content", "IE=11")
                                 ),
         //<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
         elementProvider.Element("link",
                                 elementProvider.Attribute("rel", "stylesheet"),
                                 elementProvider.Attribute("href", "http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css")
                                 ),
         elementProvider.Element("script", elementProvider.Attribute("src", "http://cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.min.js"), ""),
         isDebug
     ? elementProvider.Element("script", elementProvider.Attribute("src", "http://code.jquery.com/jquery-1.10.2.js"), "")
     : elementProvider.Element("script", elementProvider.Attribute("src", "http://code.jquery.com/jquery-1.10.2.min.js"), ""),
         elementProvider.Element("script", elementProvider.Attribute("src", "http://code.jquery.com/ui/1.10.3/jquery-ui.js"), ""),
         isInlineSyncScript
   ? elementProvider.Element("script",
                             elementProvider.Attribute("type", "text/javascript"),
                             elementProvider.Raw(SyncScript)
                             )
   : default(TElement),
         isInlineSyncScript ? default(TElement) : elementProvider.Element("script", elementProvider.Attribute("src", "/sync.js"), ""),
         isInlineSyncScript ? default(TElement) : elementProvider.Element("script", "$(function(){new ContainerSynchronizer(__ARGS__);});".Replace("__ARGS__", refreshPeriod != null ? "null, null, " + refreshPeriod.Value.TotalMilliseconds.ToString("f0") : ""))
     });
 }
Exemple #2
0
 public static TElement[] Scripts <TElement, TAttribute, TObject>(IElementProvider <TElement, TAttribute, TObject> elementProvider, bool isDebug = false, TimeSpan?refreshPeriod = null, bool isInlineSyncScript = true, string syncJsName = null, string frame = null)
 {
     return(new[]
     {
         elementProvider.Element("meta",
                                 elementProvider.Attribute("http-equiv", "X-UA-Compatible"),
                                 elementProvider.Attribute("content", "IE=11")
                                 ),
         isDebug
     ? elementProvider.Element("script", elementProvider.Attribute("src", "https://code.jquery.com/jquery-1.10.2.js"), "")
     : elementProvider.Element("script", elementProvider.Attribute("src", "https://code.jquery.com/jquery-1.10.2.min.js"), ""),
         isInlineSyncScript
   ? elementProvider.Element("script",
                             elementProvider.Attribute("type", "text/javascript"),
                             elementProvider.Raw(SyncScript)
                             )
   : default(TElement),
         isInlineSyncScript ? default(TElement) : elementProvider.Element("script", elementProvider.Attribute("src", (!syncJsName.IsNullOrEmpty() && !syncJsName.StartsWith("/") ? "/" + syncJsName : syncJsName) ?? "/sync.js"), ""),
         isInlineSyncScript ? default(TElement) : elementProvider.Element("script", "$(function(){new ContainerSynchronizer(__ARGS__);});".Replace("__ARGS__", refreshPeriod != null ? "null, null, " + refreshPeriod.Value.TotalMilliseconds.ToString("f0") : ""))
     });
 }
Exemple #3
0
        public static IEnumerable <object> JsSync <TElement, TAttribute, TObject>(IElementProvider <TElement, TAttribute, TObject> elementProvider, TElement oldBody, TElement body, params object[] path)
        {
            if (oldBody == null)
            {
                oldBody = elementProvider.Element("body");

                yield return(new { path, cmd = "clear-all" });
            }

            var oldElements = elementProvider.Elements(oldBody).ToArray();
            var newElements = elementProvider.Elements(body).ToArray();

            var lastText = elementProvider.Texts(oldBody).JoinToString(null);
            var newText  = elementProvider.Texts(body).JoinToString(null);

            var lastHtml = elementProvider.Raws(oldBody).JoinToString(null);
            var newHtml  = elementProvider.Raws(body).JoinToString(null);

            if (oldElements.Length > 0 != newElements.Length > 0 && (lastText != "") != (newText != "") || lastHtml != "" && newHtml == "")
            {
                yield return(new { path, cmd = "clear" });

                oldElements = Array <TElement> .Empty;
                lastText    = "";
                lastHtml    = "";
            }


            var left_i = 0;

            for (var i = 0; i < Math.Max(oldElements.Length, newElements.Length); ++i)
            {
                var oldChild = oldElements.ElementAtOrDefault(i);
                var newChild = newElements.ElementAtOrDefault(i);
                if (newChild == null)
                {
                    yield return new { path = path.Concat(new[] { new { kind = "element", index = left_i } }).ToArray(), cmd = "remove" }
                }
                ;
                else if (oldChild == null || elementProvider.LocalName(oldChild) != elementProvider.LocalName(newChild) || elementProvider.Attribute_Get(oldChild, "data-id") != elementProvider.Attribute_Get(newChild, "data-id"))
                {
                    if (oldChild != null)
                    {
                        yield return new { path = path.Concat(new[] { new { kind = "element", index = left_i } }).ToArray(), cmd = "remove" }
                    }
                    ;

                    if (i == 0)
                    {
                        yield return new { path, cmd = "insert", value = CreateJsFromXElement(elementProvider, newChild) }
                    }
                    ;
                    else
                    {
                        yield return
                            new
                            {
                                path  = path.Concat(new[] { new { kind = "element", index = i - 1 } }).ToArray(),
                                cmd   = "after",
                                value = CreateJsFromXElement(elementProvider, newChild)
                            }
                    };

                    left_i++;
                }
                else
                {
                    foreach (var res in JsSync(elementProvider, oldChild, newChild, path.Concat(new[] { new { kind = "element", index = i } }).ToArray()))
                    {
                        yield return(res);
                    }

                    left_i++;
                }
            }

            var attributes = elementProvider.Attributes(body).ToArray();


            var attrs = elementProvider.Attributes(oldBody)
                        .OuterJoin(attributes, lastAtr => elementProvider.LocalName(lastAtr), newAtr => elementProvider.LocalName(newAtr), (lastAttr, newAttr) => new { lastAttr, newAttr })
                        .Where(pair => elementProvider.Value(pair.lastAttr) != elementProvider.Value(pair.newAttr))
                        .Select(pair =>
            {
                var attrName = pair.lastAttr._f(_ => elementProvider.LocalName(_)) ?? pair.newAttr._f(_ => elementProvider.LocalName(_));
                return(new { name = attrName, value = elementProvider.Value(pair.newAttr) });
            }
                                )
                        .ToArray();

            var t = newText != lastText ? new { value = newText } : null;
            var h = newHtml != lastHtml ? newHtml : null;

            if (attrs.Any() || t != null || h != null)
            {
                yield return(new { path, cmd = "set", value = new { a = attrs, t, h } });
            }

            if (true)
            {
                var jsUpdate = attributes.FirstOrDefault(_attr => elementProvider.LocalName(_attr) == "js-update");
                if (jsUpdate != null)
                {
                    yield return new { path, cmd = "js-update", value = elementProvider.Value(jsUpdate) }
                }
                ;
            }
        }