/// <summary> /// Creates page using Render method. /// </summary> /// <param name="path">Page path.</param> /// <param name="windowType">Page type.</param> public static void Write(string path, string prototypeJsPath, Type windowType) { var window = (Window)windowType.GetConstructor(Type.EmptyTypes).Invoke(null); var render = windowType.GetMethod("Render", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null); Element[] elements; if (render.ReturnType == typeof(Element)) { elements = new Element[] { (Element)render.Invoke(window, null) }; } else { elements = ((IEnumerable<Element>)render.Invoke(window, null)).ToArray(); } #region css Dictionary<Style, string> css = new Dictionary<Style, string>(); gather(css, elements); #endregion #region binding int bindingsCount = 0; Dictionary<char, string> bindings = new Dictionary<char, string>(); var serializer = (ClassSerializer)Serializer.GetSerializer(windowType); foreach (var fieldInfo in windowType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Where( f => { var type = f.FieldType.IsArray ? f.FieldType.GetElementType() : f.FieldType; return type == typeof(Element) || type.IsSubclassOf(typeof(Element)); })) { if (fieldInfo.FieldType.IsArray) { var array = (Element[])fieldInfo.GetValue(window); if (array != null) { var attributes = fieldInfo.GetCustomAttributes(typeof(DataMemberAttribute), false); if (attributes.Length != 1) { throw new Exception("Field " + windowType.FullName + "::" + fieldInfo.Name + " cannot be set in Render method because it is not marked as DataMember."); } bindings[serializer.GetToken(fieldInfo)] = "[" + string.Join(",", array.Select(element => { if (element == null) return "null"; if (element.Id == null) { element.Id = idPrefix + (bindingsCount++).ToString("x"); } return "\"" + element.Id + "\""; }).ToArray()) + "]"; } } else { var element = (Element)fieldInfo.GetValue(window); if (element != null) { var attributes = fieldInfo.GetCustomAttributes(typeof(DataMemberAttribute), false); if (attributes.Length != 1) { throw new Exception("Field " + windowType.FullName + "::" + fieldInfo.Name + " cannot be set in Render method because it is not marked as DataMember."); } if (element.Id == null) { element.Id = idPrefix + (bindingsCount++).ToString("x"); } bindings[serializer.GetToken(fieldInfo)] = "\"" + element.Id + "\""; } } } #endregion #region render var sb = new StringBuilder(); sb.Append( @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><!--page generated by il2js--><head><title>").Append(window.Document.Title).Append(@"</title><script type=""text/javascript"" src=""").Append(prototypeJsPath).Append(@"""></script>"); if (css.Count != 0) { sb.Append(@"<link type=""text/css"" rel=""stylesheet"" href=""").Append(Path.GetFileName(path)).Append(".css\" />"); var sbCss = new StringBuilder(); foreach (var item in css) { sbCss.Append('.').Append(item.Value).Append('{'); CssWriter.append(item.Key, sbCss); sbCss.Append('}'); } File.WriteAllText(path + ".css", sbCss.ToString()); } if (bindings.Count != 0) { sb.Append(@"<script type=""text/javascript"">function __(){document.observe(""dom:loaded"",function(x,y){x={") .Append(string.Join(",", bindings.Select(b => "\"" + b.Key + "\":" + b.Value).ToArray())) .Append("};for(y in x)window[y]=$.apply(0,Object.isArray(x[y])?x[y]:[x[y]])})};if(!Prototype.Browser.IE)__()</script>"); } sb.Append("<!--powered by il2js framework (http://smp.if.uj.edu.pl/~mkol/il2js)--><!--/il2js-->"); if (bindings.Count != 0) { sb.Append(@"<script type=""text/javascript"">;if(Prototype.Browser.IE)__()</script>"); } sb.Append("</head><body>"); foreach (var element in elements) { element.append(sb); } sb.Append(@"</body></html>"); File.WriteAllText(path, sb.ToString()); #endregion }
public void InsertBefore(Element what, Element where) { this.elements.Insert(this.elements.IndexOf(where), what); }
public Element RemoveChild(Element child) { this.elements.Remove(child); return child; }
public static string Serialize(this HTMLElement element) { throw new InvalidOperationException(JS.CannotRunAtServer); }
public void Add(Element element) { this.elements.Add(element); }
public static HTMLElement Select(this HTMLElement element) { throw new InvalidOperationException(JS.CannotRunAtServer); }
public static HTMLElement Insert(this HTMLElement element, Dictionary <string, HTMLElement> content) { throw new InvalidOperationException(JS.CannotRunAtServer); }
public static bool HasClassName(this HTMLElement element, string className) { throw new InvalidOperationException(JS.CannotRunAtServer); }
public static HTMLElement SetStyle(this HTMLElement element, string styles) { element["style"] = styles; return(element); }
public static HTMLElement[] Select(this HTMLElement element, params string[] selector) { throw new InvalidOperationException(JS.CannotRunAtServer); }