/******** * @version : 2.1.1 - Ext.NET Pro License * @author : Ext.NET, Inc. http://www.ext.net/ * @date : 2012-12-10 * @copyright : Copyright (c) 2007-2012, Ext.NET, Inc. (http://www.ext.net/). All rights reserved. * @license : See license.txt and http://www.ext.net/license/. ********/ using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using Ext.Net.Utilities; namespace Ext.Net { /// /// /// [Description("")] public abstract partial class ScriptClass : IScriptable, IResourceManager { /* IScriptable -----------------------------------------------------------------------------------------------*/ /// /// /// [Description("")] public virtual string InstanceOf { get; private set; } /// /// /// [Description("")] public abstract string ToScript(); /// /// /// [Description("")] public virtual void Render() { this.AddScript(this.ToScript()); } /// /// /// [Description("")] public virtual void Call(string name) { this.Call(name, null); } /// /// /// [Description("")] public virtual void Call(string name, params object[] args) { string tpl = this.InstanceOf.IsEmpty() ? "{1}({2});" : "{0}.{1}({2});"; this.CallTemplate(tpl, name, args); } /// /// /// [Description("")] public virtual void Set(string name, object value) { string tpl = this.InstanceOf.IsEmpty() ? "{1}={2};" : "{0}.{1}={2};"; this.CallTemplate(tpl, name, value); } /// /// /// [Description("Adds the script to be be called on the client. The script is formatted using the template and args.")] public virtual void AddScript(string template, params object[] args) { this.AddScript(string.Format(template, args)); } /// /// /// [Description("")] public virtual void AddScript(string script) { if (this.Page != null && this.ResourceManager != null) { this.ResourceManager.AddScript(script); return; } ResourceManager.AddInstanceScript(script); } /// /// /// [Description("")] protected virtual void CallTemplate(string template, string name, params object[] args) { string s = this.FormatArgs(args); this.AddScript(template, this.InstanceOf, name, s); } /// /// /// [Description("")] protected virtual string FormatCall(string name, params object[] args) { return "{0}.{1}({2});".FormatWith(this.InstanceOf, name, this.FormatArgs(args)); } /// /// /// [Description("")] protected virtual string FormatCallTemplate(string template, string name, params object[] args) { return template.FormatWith(this.InstanceOf, name, this.FormatArgs(args)); } /// /// /// [Description("")] protected virtual string FormatArgs(object[] args) { StringBuilder sb = new StringBuilder(); if (args != null && args.Length > 0) { foreach (object arg in args) { if (arg is string) { sb.AppendFormat("{0},", TokenUtils.ParseAndNormalize(arg.ToString(), this.ResourceManager)); } else { sb.AppendFormat("{0},", JSON.Serialize(arg)); } } } return sb.ToString().LeftOfRightmostOf(','); } /* IResourceManager -----------------------------------------------------------------------------------------------*/ private Page page; /// /// /// [Description("")] public virtual Page Page { get { if (this.page == null && this.ResourceManager != null) { this.page = this.ResourceManager.Page; } return this.page; } } private ResourceManager resourceManager; /// /// /// [Description("")] public virtual ResourceManager ResourceManager { get { if (this.resourceManager == null) { this.resourceManager = ResourceManager.GetInstance(); } return this.resourceManager; } } } }