/******** * @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; using System.ComponentModel; using System.Drawing; using System.Text; using System.Web.UI; using Ext.Net.Utilities; namespace Ext.Net { /// /// Handles mapping key events to handling functions for an element or a Component. One KeyMap can be used for multiple actions. /// /// A KeyMap must be configured with a target as an event source which may be an Element or a Component. /// /// If the target is an element, then the keydown event will trigger the invocation of bindings. /// /// It is possible to configure the KeyMap with a custom eventName to listen for. This may be useful when the target is a Component. /// /// The KeyMap's event handling requires that the first parameter passed is a key event. So if the Component's event signature is different, specify a processEvent configuration which accepts the event's parameters and returns a key event. /// /// Functions specified in bindings are called with this signature : (String key, Ext.EventObject e) (if the match is a multi-key combination the callback will still be called only once). A KeyMap can also handle a string representation of keys. By default KeyMap starts enabled. /// [Meta] [ToolboxData("<{0}:KeyMap runat=\"server\">")] [ToolboxBitmap(typeof(KeyMap), "Build.ToolboxIcons.KeyMap.bmp")] [Designer(typeof(EmptyDesigner))] [Description("Handles mapping keys to actions for an element.")] public partial class KeyMap : LazyObservable, ICustomConfigSerialization { /// /// /// [Description("")] public KeyMap() { } /// /// /// [Category("0. About")] [Description("")] public override string InstanceOf { get { return "Ext.util.KeyMap"; } } protected internal bool BelongsToCmp { get; set; } private KeyBindingCollection binding; /// /// Either a single object describing a handling function for s specified key (or set of keys), or an array of such objects. /// [Meta] [Category("3. KeyMap")] [NotifyParentProperty(true)] [PersistenceMode(PersistenceMode.InnerProperty)] [Description("Either a single object describing a handling function for s specified key (or set of keys), or an array of such objects.")] public virtual KeyBindingCollection Binding { get { if (this.binding == null) { this.binding = new KeyBindingCollection { Owner = this }; } return this.binding; } } [ConfigOption("binding", JsonMode.Raw)] [DefaultValue("")] protected virtual string BindingProxy { get { if (this.Binding.Count == 0) { return ""; } if (this.Binding.Count == 1) { return new ClientConfig().SerializeInternal(this.Binding[0], this); } else { StringBuilder sb = new StringBuilder(); bool comma = false; sb.Append("["); foreach (KeyBinding keyBinding in this.Binding) { if (comma) { sb.Append(","); } sb.Append(new ClientConfig().SerializeInternal(keyBinding, this)); comma = true; } sb.Append("]"); return sb.ToString(); } } } /// /// The event to listen for to pick up key events. Defaults to: "keydown" /// [Meta] [ConfigOption] [Category("3. KeyMap")] [DefaultValue("")] [Description("The event to listen for to pick up key events. Defaults to: \"keydown\"")] public virtual string EventName { get { return this.State.Get("EventName", ""); } set { this.State.Set("EventName", value); } } /// /// True to listen component event instead underlying element /// [Meta] [ConfigOption] [Category("3. KeyMap")] [DefaultValue(false)] [Description("True to listen component event instead underlying element")] public virtual bool ComponentEvent { get { return this.State.Get("ComponentEvent", false); } set { this.State.Set("ComponentEvent", value); } } /// /// Element name of target component (can be used if KeyMap belongs to a component) /// [Meta] [ConfigOption("cmpEl")] [Category("3. KeyMap")] [DefaultValue("")] [Description("Element name of target component (can be used if KeyMap belongs to a component)")] public virtual string ComponentElement { get { return this.State.Get("ComponentElement", ""); } set { this.State.Set("ComponentElement", value); } } /// /// Configure this as true if there are any input fields within the target, and this KeyNav should not process events from input fields, (<input>, <textarea> and elements withcontentEditable="true"`). Defaults to: false /// [Meta] [ConfigOption] [Category("3. KeyMap")] [DefaultValue(false)] [Description("Configure this as true if there are any input fields within the target, and this KeyNav should not process events from input fields, (<input>, <textarea> and elements withcontentEditable=\"true\"). Defaults to: false")] public virtual bool IgnoreInputFields { get { return this.State.Get("IgnoreInputFields", false); } set { this.State.Set("IgnoreInputFields", value); } } private JFunction processEvent; /// /// An optional event processor function which accepts the argument list provided by the configured event of the target, and returns a keyEvent for processing by the KeyMap. /// /// This may be useful when the target is a Component with s complex event signature, where the event is not the first parameter. Extra information from the event arguments may be injected into the event for use by the handler functions before returning it. /// [Meta] [ConfigOption(JsonMode.Raw)] [PersistenceMode(PersistenceMode.InnerProperty)] [TypeConverter(typeof(ExpandableObjectConverter))] [Description("")] public virtual JFunction ProcessEvent { get { if (this.processEvent == null) { this.processEvent = new JFunction(); } return this.processEvent; } } /// /// The scope (this context) in which the processEvent method is executed. /// [Meta] [ConfigOption(JsonMode.Raw)] [DefaultValue("")] [NotifyParentProperty(true)] [Description("The scope (this context) in which the processEvent method is executed.")] public virtual string ProcessEventScope { get { return this.State.Get("ProcessEventScope", ""); } set { this.State.Set("ProcessEventScope", value); } } /// /// The object on which to listen for the event specified by the eventName config option. /// [Meta] [Category("3. KeyMap")] [DefaultValue("")] [Description("The object on which to listen for the event specified by the eventName config option.")] public virtual string Target { get { return this.State.Get("Target", ""); } set { this.State.Set("Target", value); } } [ConfigOption("target", JsonMode.Raw)] [DefaultValue("")] protected virtual string TargetProxy { get { if(this.Target.IsEmpty()) { return ""; } string parsedTarget = TokenUtils.ParseTokens(this.Target, this); if (TokenUtils.IsRawToken(parsedTarget)) { return TokenUtils.ReplaceRawToken(parsedTarget); } return "\"".ConcatWith(parsedTarget, "\""); } } /* Public Methods -----------------------------------------------------------------------------------------------*/ /// /// Enables this KeyMap /// public virtual void Enable() { this.Call("enable"); } /// /// Disable this KeyMap /// public virtual void Disable() { this.Call("disable"); } /// /// Convenience function for setting disabled/enabled by boolean. /// /// public virtual void SetDisabled(bool disabled) { this.Call("setDisabled", disabled); } /// /// /// /// public virtual void AddKeyBinding(KeyBinding keyBinding) { RequestManager.EnsureDirectEvent(); this.Call("addBinding", JRawValue.From(new ClientConfig().Serialize(keyBinding, true))); } /// /// Destroys the KeyMap instance and removes all handlers. /// /// True to also remove the target public virtual void Destroy(bool removeTarget) { this.Call("destroy", removeTarget); } /// /// Destroys the KeyMap instance and removes all handlers. /// public override void Destroy() { this.Call("destroy"); } public string ToScript(Control owner) { string config = new ClientConfig().Serialize(this, true); if(this.BelongsToCmp) { return config; } string script; if (this.IsIdRequired) { script = string.Format("window.{0}=Ext.create(\"{1}\",{2})", this.ClientID, this.InstanceOf, config); } else { script = string.Format("Ext.create(\"{0}\",{1})", this.InstanceOf, config); } return this.IsLazy ? script : (script + ";"); } } }