📄 context.cs
字号:
{ this.baseElement = this.Parent.BaseElement; } return true; } protected string contextPassThroughId(Widget w) { return w.Parent.UID; } public void RemoveWidget( Widget c ) { if( c.rendered ) SendCommand( "{0}.Remove();", c.UID ); } public void HideWidget( Widget c ) { if( c.rendered && c.Visible ) SendCommand( "{0}.Hide();", c.UID ); } public void ShowWidget( Widget c ) { if( c.rendered ) { SendCommand( "{0}.Show();", c.UID ); } else { RenderWidget( c ); } } public void RenderWidget( Widget c ) { if( ClientEvents == null ) ClientEvents = new Queue(); ClientEvents.Enqueue( c ); } public void AddFrameToHistory(ContextHistoryFrame frame) { if (history == null) { history = new List<ContextHistoryFrame>(); } if( currentFrame < history.Count - 1 ) { history.RemoveRange( currentFrame + 1, history.Count - (currentFrame+1) ); } history.Add(frame); currentFrame = history.Count - 1; SendCommand("AddBack(" + Util.ToJavaScriptString(frame.Id) + ");"); } public void Transform() { //process order of events. Surface s = null; if( State == ContextState.Uninitialized ) { Init(); if (IsSecure && ! IsLoggedIn ) { RequiresLogin.Init(); RecurseInit(RequiresLogin); } else { RecurseInit(this); } this.State = ContextState.Initializing; s = new HtmlSurface(HttpContext); if( dataBindPostInit ) DataBindWidget(); } else { //process events. string widgetKey = HttpContext.Request["widget"]; if( widgetKey == null ) { //oops -lost state. Unregister(); Connect(name, this.GetType()); return; } string evt = HttpContext.Request["event"]; string args = null; if( HttpContext.Request["arg"] != null ) { args = HttpContext.Server.UrlDecode(HttpContext.Request["arg"]); } //string args = HttpContext.Request["arg"]; if( widgetKey == "root" ) { this.HandleEvents( evt, args ); } else { Widget c = Widgets.Find( widgetKey ); if( c != null ) c.HandleEvents( evt, args ); else if (widgetKey.IndexOf('_') > 0) { string[] argParts = widgetKey.Split('_'); int index = argParts.Length -1; while( c == null && index >= 0 ) c = Widgets.Find(Util.Join(argParts,"_",false,0,index--)); if (c != null) { c.ClientArguments["widgetKey"] = widgetKey; c.HandleEvents(evt, args); } } } s = new XmlHttpSurface(HttpContext); } if( State == ContextState.Initializing ) { HttpContext.Response.Write(this.DocumentHeader); } Render( s ); if( State == ContextState.Initializing ) { HttpContext.Response.Write(this.DocumentFooter); State = ContextState.Running; } } public override void HandleEvents( string evt, string args ) { switch(evt) { case "OnClick": string[] argsArray = args.Split(','); int X = int.Parse( argsArray[0] ); int Y = int.Parse( argsArray[1] ); this.OnClick( new MouseEventArgs( X, Y ) ); break; case "Back": if (history != null && currentFrame > 0) { ContextHistoryFrame frame = history[--currentFrame]; frame.CallBack(frame.State); } break; case "Forward": if (history != null && currentFrame < history.Count-1) { ContextHistoryFrame frame = history[++currentFrame]; frame.CallBack(frame.State); } break; case "Bookmark": if( OnBookmark != null ) { OnBookmark(args, null); } break; } } public Context CreateDynamicContext(string name, string xml) { return SetupContext<Context>(name, xml); } public T CreateContext<T>() where T : Context, new() { return CreateContext<T>(null); } public T CreateContext<T>(string name) where T : Context, new() { return SetupContext<T>(name, null); } private T SetupContext<T>(string name, string xml) where T : Context, new() { T context = CreateWidget<T>(); context.id = context.Name = name == null ? typeof(T).FullName : name; context.root = context; context.GetDynamicId = new DynamicIdDelegate(contextPassThroughId); (context as Context).BaseElement = BaseElement; context.Parse(xml); context.root = this; return context; } public T CreateWidget<T>() where T : Widget, new() { T newWidget = new T(); newWidget.RootContext = this; if (newWidget is RequiresLogin) RequiresLogin = newWidget as RequiresLogin; //default id int? count = 0; Type type = newWidget.GetType(); string name = type.Name; if (type.IsGenericType) { name = name.Substring(0, name.IndexOf('`')); foreach( Type t in type.GetGenericArguments() ) { name += t.Name; } } if (typeCounts.ContainsKey(name)) { count = typeCounts[name]; } newWidget.Id = name + count++; typeCounts[name] = count; newWidget.Parent = this; return newWidget; } public Widget CreateUnkownWidget(Type t, string name) { Type[] argumentList = Type.EmptyTypes; object[] args = null; string method = "CreateWidget"; if (t == typeof(Context) || t.IsSubclassOf(typeof(Context))) { method = "CreateContext"; if( name != null ) { argumentList = new Type[] { typeof(string) }; args = new object[] { name }; } } MethodInfo createwidgetMI = root.GetType().GetMethod(method, argumentList); createwidgetMI = createwidgetMI.MakeGenericMethod(t); return createwidgetMI.Invoke(this, args) as Widget; } public Widget CreateUnkownWidget(Type t) { return CreateUnkownWidget(t, null); } public static void Connect( string name, System.Type type ) { Context c = GetContext(HttpContext.Current.Session.SessionID, name); if( c == null ) { if (HttpContext.Current.Request["event"] != null) { //session timed out, refresh the page. HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString()); } if (type != null) { ConstructorInfo ci = type.GetConstructor(new Type[] { }); c = ci.Invoke(new object[] { }) as Context; HttpContext.Current.Items["context"] = c; } else { c = new Context(); } c.Register(name); } HttpContext.Current.Items["context"] = c; c.Transform(); } public string MapPath(string virtualPath) { return HttpContext.Server.MapPath(virtualPath); } public void Parse() { Parse(null); } public void Parse(string xml) { XmlDocument doc = new XmlDocument(); if (xml != null) { doc.LoadXml(xml); } else { if (!System.IO.File.Exists(HttpContext.Server.MapPath(this.name + ".xml"))) { return; } doc.Load(HttpContext.Server.MapPath(this.name + ".xml")); } WidgetCollection c = new WidgetCollection(); XmlNode contextNode = doc.SelectSingleNode("Context"); if( contextNode.Attributes["DefaultNamespace"] != null ) this.DefaultNamespace = contextNode.Attributes["DefaultNamespace"].Value; if (contextNode.Attributes["DataBind"] != null && Convert.ToBoolean(contextNode.Attributes["DataBind"].Value)) dataBindPostInit = true; XmlNodeList recordLists = doc.SelectNodes("//RecordList"); foreach (XmlNode n in recordLists) { n.ParentNode.RemoveChild(n); ParseRecordList(n); } ParseXml( contextNode ); } public IntPtr GetIntPtr( string Name ) { MethodInfo mi = this.GetType().GetMethod(Name); return mi.MethodHandle.GetFunctionPointer(); } protected void Alert( string Message ) { SendCommand( "alert('{0}');", Util.FormatForClient( Message ) ); } private Dictionary<string, IRecordList> recordLists; public Dictionary<string, IRecordList> RecordLists { get { if (recordLists == null) recordLists = new Dictionary<string, IRecordList>(); return recordLists; } } public static Context Current { get { return HttpContext.Current.Items["context"] as Context; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -