⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 context.cs

📁 一种.net实现ajax方法的类库
💻 CS
📖 第 1 页 / 共 2 页
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.*  All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* *     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.*     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.*     * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**//** * Project: emergetk: stateful web framework for the masses * File name: .cs * Description: *    * Author: Ben Joldersma *    */using System;using System.Collections;using System.Collections.Generic;using System.Web;using System.Xml;using System.Reflection;using System.IO;using EmergeTk.Model;using System.Threading;using System.Web.Caching;using System.Web.SessionState;using EmergeTk.Widgets.Html;namespace EmergeTk{	public struct MouseEventArgs	{		public int X, Y;		public MouseEventArgs( int x, int y ) { X = x; Y = y; }	}	public delegate void MouseEventHandler( MouseEventArgs e );    public delegate void CometSocketConnectedHandler();	public enum ContextState	{		Uninitialized,		Initializing,		Running,		Finishing	}	/// <summary>	/// Summary decription for Kontext.	/// </summary>	public class Context : Widget	{        static Dictionary<string,HttpSessionState> sessions = new Dictionary<string,HttpSessionState>();        static CometServer cometServer = CometServer.Singleton;        public static void Release()        {            cometServer.Shutdown();        }        public static Context GetContext(string SessionID, string ContextName)        {        	try        	{	            if (sessions.ContainsKey(SessionID))	            {	                Context c = sessions[SessionID][ContextName] as Context;	                if (c != null && c.registered)	                    return c;	            }	            return null;	        }	        catch( Exception e )	        {	        	System.Console.WriteLine("Context.GetContext:" + e.Message);	        	return null;	        }        }        bool documentFooterOverridden = false;        private string documentFooter;        public string DocumentFooter        {            get            {                if (documentFooterOverridden)                    return documentFooter;                else                    return "\r\n        checkForBookmark(); if( emerge_post_load ) emerge_post_load(); } dojo.addOnLoad( emerge_load ); </script>\r\n" + FooterHtml + "\r\n    </body>\r\n</html>";            }            set            {                documentFooter = value;                documentFooterOverridden = true;            }        }        private string name;        protected string DocumentHeader;        public string DefaultNamespace = "EmergeTk.Widgets.Html";        private string baseElement = "document.body";        private bool dataBindPostInit = false;        public override string BaseElement { get { return baseElement; } set { baseElement = value; } }        protected new event MouseEventHandler OnClick;        public RequiresLogin RequiresLogin = null;        protected CometClient cometClient;        private bool registered = false;        private string sessionId;        private bool isBot = false;        private List<ContextHistoryFrame> history;        private int currentFrame = 0;        private Dictionary<string, int?> typeCounts = new Dictionary<string, int?>();		protected ContextState State = ContextState.Uninitialized;		public HttpContext HttpContext		{			get { return System.Web.HttpContext.Current; }		}        public bool DataBindPostInit { get { return dataBindPostInit; } }				public string Name		{			get { return name; }			set { name = value; }		}        string host;        public string Host        {            get { return host; }            set { host = value; }        }        public CometClient CometClient        {            get { return cometClient; }        }		public virtual bool IsSecure        {            get { return RequiresLogin != null; }        }        public bool IsBot        {            get { return isBot; }        }        public bool IsLoggedIn        {            get {                if (RootContext.HttpContext.Session["Username"] != null)                    return true;                return false;             }        }        public string FooterHtml        {            get            {                if (File.Exists(Util.RootPath + "/" + this.GetType().Name + "_footer.html"))                {                    StreamReader sr = File.OpenText(this.GetType().Name + "_footer.html");                    string result = sr.ReadToEnd();                    sr.Close();                    return result;                }                else                    return string.Empty;            }        }			public Context()		{            string debugText = string.Empty;#if DEBUG            debugText = ", isDebug: true";#endif			root = this;            sessionId = HttpContext.Session.SessionID;            isBot = HttpContext.Request.ServerVariables["HTTP_USER_AGENT"].Contains("Googlebot");            DocumentHeader = string.Format(				@"<html>    <head>        <style type=""text/css"" title=""currentStyle"">            @import ""styles.css"";            @import """ + this.GetType().Name + @".css"";        </style>        <script src=scripts/tiny_mce/tiny_mce.js></script>        <script>            var djConfig = {{                baseScriptUri: 'scripts/'{0},                preventBackButtonFix:false            }};        </script>        <script src=scripts/dojo.js></script>        <script src=client.js></script>        <script src=" + this.GetType().Name + @".js></script>    </head>    <body>        <script> function emerge_load() {{", debugText) ;		}        ~Context()        {            if (this.cometClient != null)            {                this.cometClient.Shutdown();            }        }        public string CacheKey { get { return buildCacheKey(this.name); } }        private string buildCacheKey(string name) { return sessionId + "_" + name; }		public void Register( string name )		{			this.name = name;            this.host = HttpContext.Request.Url.Host;            if (!sessions.ContainsKey(sessionId))            {                sessions[sessionId] = HttpContext.Session;            }            sessions[sessionId][name] = this;            this.registered = true;			Parse();			if( HttpContext.Request.QueryString["Log"] == "1" && Find( "Log" ) == null )			{				Label log = new Label("Log","Log:");				Add( log );			}		}        public event EventHandler OnUnload;        public event CometSocketConnectedHandler CometConnected;        public event EventHandler OnBookmark;        public void ConnectComet(CometClient cc)        {            this.cometClient = cc;            if (CometConnected != null)                CometConnected();        }        public void DisconnectComet()        {            cometClient = null;        }		public virtual void Unregister()		{                        if( OnUnload != null )            {                OnUnload(this, new EventArgs());            }            if (sessions[sessionId]!= null)            {                sessions[sessionId].Remove(this.name);            }            if (this.cometClient != null)            {                this.cometClient.Shutdown();            }            this.registered = false;            this.State = ContextState.Uninitialized;		}        public virtual void SocketDisconnected()        {        }		public void RecurseInit( Widget c )		{            if (c.Widgets != null)                for(int i = 0; i < c.Widgets.Count;i++)                {                    c.Widgets[i].Init();                    RecurseInit(c.Widgets[i]);                }		}		public override bool Render( Surface surface )		{            if (this == this.root)            {                HttpContext.Response.Cache.SetExpires(DateTime.MinValue);                HttpContext.Response.ContentType = "text/html";                if (surface is HtmlSurface && this.OnClick != null)                {                    surface.Write("document.addEventListener('click', DocumentOnClick, true );");                }                if (IsSecure && !IsLoggedIn && RequiresLogin != null)                {                    if (!RequiresLogin.rendered)                    {                        RequiresLogin.Render(surface);                        RequiresLogin.rendered = true;                    }                    RecurseRender(RequiresLogin, surface);                }                else                {                    RecurseRender(this, surface);                }                if (surface is XmlHttpSurface && surface.BytesSent == 0)                {                    //quick fix to deal with no element error.                    surface.Write("/*NOOP*/");                }            }            else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -