Ejemplo n.º 1
0
 public sealed override string ToJavaScript(IControlResolver resolver)
 {
     if (this.Control.Visible)
     {
         return(this.ToJavaScriptWhenVisible(resolver));
     }
     return("null");
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ControlTreeResolverBase"/> class.
        /// </summary>
        public ControlTreeResolverBase(IControlResolver controlResolver, IAbstractTreeBuilder treeBuilder)
        {
            this.controlResolver = controlResolver;
            this.treeBuilder     = treeBuilder;

            rawLiteralMetadata  = new Lazy <IControlResolverMetadata>(() => controlResolver.ResolveControl(new ResolvedTypeDescriptor(typeof(RawLiteral))));
            literalMetadata     = new Lazy <IControlResolverMetadata>(() => controlResolver.ResolveControl(new ResolvedTypeDescriptor(typeof(Literal))));
            placeholderMetadata = new Lazy <IControlResolverMetadata>(() => controlResolver.ResolveControl(new ResolvedTypeDescriptor(typeof(PlaceHolder))));
        }
Ejemplo n.º 3
0
        public string ToJavaScript(IControlResolver resolver)
        {
            StringBuilder stringBuilder = new StringBuilder("[");

            stringBuilder.Append(string.Join(",", (from o in this
                                                   select o.ToJavaScript(resolver)).ToArray <string>()));
            stringBuilder.Append("]");
            return(stringBuilder.ToString());
        }
Ejemplo n.º 4
0
        public override string ToJavaScript(IControlResolver resolver)
        {
            object obj = this.Value ?? this.DefaultValue;

            if (this.TargetType != null)
            {
                obj = ValueConvertor.ConvertValue(obj, this.TargetType, null);
            }
            return(string.Format("new StaticBinding({0})", obj.ToJsonString(null)));
        }
Ejemplo n.º 5
0
        public string ToJavaScript(IControlResolver resolver)
        {
            StringBuilder stringBuilder = new StringBuilder("{");

            foreach (KeyValuePair <string, Binding> keyValuePair in this)
            {
                stringBuilder.Append('"');
                stringBuilder.Append(keyValuePair.Key);
                stringBuilder.Append("\":");
                stringBuilder.Append(keyValuePair.Value.ToJavaScript(resolver));
                stringBuilder.Append(",");
            }
            if (stringBuilder.Length > 1)
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }
            stringBuilder.Append("}");
            return(stringBuilder.ToString());
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultControlTreeResolver"/> class.
 /// </summary>
 public DefaultControlTreeResolver(IControlResolver controlResolver, IAbstractTreeBuilder treeBuilder, IBindingExpressionBuilder expressionBuilder)
     : base(controlResolver, treeBuilder)
 {
     this.bindingExpressionBuilder = expressionBuilder;
 }
        private static Dictionary <string, object> BuildGraph(object obj, IUrlResolutionService uriResolver,
                                                              IControlResolver controlResolver)
        {
            Dictionary <string, object>  dict = new Dictionary <string, object>();
            PropertyDescriptorCollection pdc  = TypeDescriptor.GetProperties(obj);

            foreach (PropertyDescriptor pd in pdc)
            {
                ExtenderControlPropertyAttribute ecpa = GetAttribute <ExtenderControlPropertyAttribute>(pd);

                ClientPropertyNameAttribute    cpna = GetAttribute <ClientPropertyNameAttribute>(pd);
                IDReferencePropertyAttribute   idr  = GetAttribute <IDReferencePropertyAttribute>(pd);
                UrlPropertyAttribute           ura  = GetAttribute <UrlPropertyAttribute>(pd);
                ExtenderControlMethodAttribute ecma = GetAttribute <ExtenderControlMethodAttribute>(pd);
                ExtenderControlEventAttribute  ecea = GetAttribute <ExtenderControlEventAttribute>(pd);
                ElementReferenceAttribute      era  = GetAttribute <ElementReferenceAttribute>(pd);
                ComponentReferenceAttribute    cra  = GetAttribute <ComponentReferenceAttribute>(pd);
                if (ecpa == null && cra == null && cpna == null && era == null)
                {
                    continue;
                }


                string propName =
                    cpna != null && !string.IsNullOrEmpty(cpna.PropertyName)
                        ? cpna.PropertyName
                        : pd.Name;


                if (propName == "ClientClassName")
                {
                    propName = "typeToBuild";
                }

                object o     = null;
                object value = pd.GetValue(obj);
                if (value == null)
                {
                    continue;
                }

                if (value as IClientClass != null && ((IClientClass)value).NotSet)
                {
                    continue;
                }

                if (value as ClientEvalScript != null && string.IsNullOrEmpty((value as ClientEvalScript).ClientScript))
                {
                    continue;
                }

                if (pd.PropertyType == typeof(string))
                {
                    o = value;
                    if (ura != null)
                    {
                        o = uriResolver.ResolveClientUrl((string)o);
                    }
                    else if (idr != null || cra != null)
                    {
                        o = BuildGraph(new Reference
                        {
                            ReferenceType = ReferenceType.Component,
                            ClientId      = controlResolver.ResolveControl((string)o).ClientID
                        }, uriResolver, controlResolver);
                    }
                    else if (era != null)
                    {
                        Control c = controlResolver.ResolveControl((string)o);
                        o = BuildGraph(
                            new Reference
                        {
                            ReferenceType = ReferenceType.Element, ClientId = c == null ? (string)o : c.ClientID
                        },
                            uriResolver, controlResolver);
                    }
                }
                else if (value is IEnumerable)
                {
                    List <object> lst = new List <object>();
                    foreach (object a in (IEnumerable)value)
                    {
                        object b;
                        if (a as IUICollectionItem != null)
                        {
                            b = a is UriValue
                                    ? uriResolver.ResolveClientUrl(((UriValue)a).Value.ToString())
                                    : ((IUICollectionItem)a).Value;
                        }
                        else
                        {
                            b = BuildGraph(a, uriResolver, controlResolver);
                        }
                        if (b != null)
                        {
                            lst.Add(b);
                        }
                    }
                    if (lst.Count > 0)
                    {
                        o = lst;
                    }
                }
                else if (pd.PropertyType.IsEnum)
                {
                    o = Enum.GetName(pd.PropertyType, value);
                }
                else if (pd.PropertyType.IsPrimitive || pd.PropertyType.IsValueType)
                {
                    o = value;
                }
                else
                {
                    o = BuildGraph(value, uriResolver, controlResolver);
                }

                if (o != null)
                {
                    dict.Add(propName, o);
                }
            }
            return(dict.Count > 0 ? dict : null);
        }
Ejemplo n.º 8
0
 public abstract string ToJavaScript(IControlResolver resolver);
Ejemplo n.º 9
0
 public override string ToJavaScript(IControlResolver resolver)
 {
     return(string.Format("new {0}({1})", base.GetType().Name, this.binding.ToJavaScript(resolver)));
 }
Ejemplo n.º 10
0
 protected override string ToJavaScriptWhenVisible(IControlResolver resolver)
 {
     return(string.Format("new ComboBoxBinding('{0}','{1}')", this.ClientID, base.ClientPropertyName));
 }
Ejemplo n.º 11
0
		public static void DescribeComponent(object instance, ScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver)
		{
			// validate preconditions
			if (instance == null) throw new ArgumentNullException("instance");
			if (descriptor == null) throw new ArgumentNullException("descriptor");
			if (urlResolver == null) urlResolver = instance as IUrlResolutionService;
			if (controlResolver == null) controlResolver = instance as IControlResolver;

			// describe properties
			// PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

			PropertyInfo[] properties = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

			foreach (PropertyInfo prop in properties)
			{
				ScriptControlPropertyAttribute propAttr = null;
				ScriptControlEventAttribute eventAttr = null;
				string propertyName = prop.Name;

				System.ComponentModel.AttributeCollection attribs = new System.ComponentModel.AttributeCollection(Attribute.GetCustomAttributes(prop, false));

				// Try getting a property attribute
				propAttr = (ScriptControlPropertyAttribute)attribs[typeof(ScriptControlPropertyAttribute)];
				if (propAttr == null || !propAttr.IsScriptProperty)
				{
					// Try getting an event attribute
					eventAttr = (ScriptControlEventAttribute)attribs[typeof(ScriptControlEventAttribute)];
					if (eventAttr == null || !eventAttr.IsScriptEvent)
					{
						continue;
					}
				}

				// attempt to rename the property/event
				ClientPropertyNameAttribute nameAttr = (ClientPropertyNameAttribute)attribs[typeof(ClientPropertyNameAttribute)];
				if (nameAttr != null && !string.IsNullOrEmpty(nameAttr.PropertyName))
				{
					propertyName = nameAttr.PropertyName;
				}

				// determine whether to serialize the value of a property.  readOnly properties should always be serialized
				//bool serialize = true;// prop.ShouldSerializeValue(instance) || prop.IsReadOnly;
				//if (serialize)
				//{
				// get the value of the property, skip if it is null
				Control c = null;
				object value = prop.GetValue(instance, new object[0] { });
				if (value == null)
				{
					continue;
				}

				// convert and resolve the value
				if (eventAttr != null && prop.PropertyType != typeof(String))
				{
					throw new InvalidOperationException("ScriptControlEventAttribute can only be applied to a property with a PropertyType of System.String.");
				}
				else
				{
					if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum)
					{
						if (prop.PropertyType == typeof(Color))
						{
							value = ColorTranslator.ToHtml((Color)value);
						}
						else
						{
							// TODO: Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization
							//TypeConverter conv = prop.Converter;
							//value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value);

							//if (prop.PropertyType == typeof(CssStyleCollection))
							//    value = (new CssStyleCollectionJSCoverter()).Serialize(value, new JavaScriptSerializer());
							//if (prop.PropertyType == typeof(Style))
							//    value = (new CssStyleCollectionJSCoverter()).Serialize(((Style)value).GetStyleAttributes(null), new JavaScriptSerializer());                                

							Type valueType = value.GetType();

							JavaScriptConverterAttribute attr = (JavaScriptConverterAttribute)attribs[typeof(JavaScriptConverterAttribute)];
							JavaScriptConverter converter = attr != null ?
								(JavaScriptConverter)TypeCreator.CreateInstance(attr.ConverterType) :
								JSONSerializerFactory.GetJavaScriptConverter(valueType);

							if (converter != null)
								value = converter.Serialize(value, JSONSerializerFactory.GetJavaScriptSerializer());
							else
								value = JSONSerializerExecute.PreSerializeObject(value);

							//Dictionary<string, object> dict = value as Dictionary<string, object>;
							//if (dict != null && !dict.ContainsKey("__type"))
							//    dict["__type"] = valueType.AssemblyQualifiedName;
						}
					}
					if (attribs[typeof(IDReferencePropertyAttribute)] != null && controlResolver != null)
					{
						c = controlResolver.ResolveControl((string)value);
					}
					if (attribs[typeof(UrlPropertyAttribute)] != null && urlResolver != null)
					{
						value = urlResolver.ResolveClientUrl((string)value);
					}
				}

				// add the value as an appropriate description
				if (eventAttr != null)
				{
					if (!string.IsNullOrEmpty((string)value))
						descriptor.AddEvent(propertyName, (string)value);
				}
				else if (attribs[typeof(ElementReferenceAttribute)] != null)
				{
					if (c == null && controlResolver != null) c = controlResolver.ResolveControl((string)value);
					if (c != null) value = c.ClientID;
					descriptor.AddElementProperty(propertyName, (string)value);
				}
				else if (attribs[typeof(ComponentReferenceAttribute)] != null)
				{
					if (c == null && controlResolver != null) c = controlResolver.ResolveControl((string)value);
					if (c != null)
					{
						//ExtenderControlBase ex = c as ExtenderControlBase;
						//if (ex != null && ex.BehaviorID.Length > 0)
						//    value = ex.BehaviorID;
						//else
						value = c.ClientID;
					}
					descriptor.AddComponentProperty(propertyName, (string)value);
				}
				else
				{
					if (c != null) value = c.ClientID;
					descriptor.AddProperty(propertyName, value);
				}
			}
			//}

			// determine if we should describe methods
			foreach (MethodInfo method in instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
			{
				ScriptControlMethodAttribute methAttr = (ScriptControlMethodAttribute)Attribute.GetCustomAttribute(method, typeof(ScriptControlMethodAttribute));
				if (methAttr == null || !methAttr.IsScriptMethod)
				{
					continue;
				}

				// We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method
				Control control = instance as Control;
				if (control != null)
				{
					// Force WebForms.js
					control.Page.ClientScript.GetCallbackEventReference(control, null, null, null);

					// Add the callback target
					descriptor.AddProperty("_callbackTarget", control.UniqueID);
				}
				break;
			}
		}
Ejemplo n.º 12
0
 protected override string ToJavaScriptWhenVisible(IControlResolver resolver)
 {
     return(string.Format("new AjaxUploaderBinding('{0}','{1}')", base.Control.ClientID, base.ClientPropertyName));
 }
Ejemplo n.º 13
0
 protected override string ToJavaScriptWhenVisible(IControlResolver resolver)
 {
     return(string.Format("new ImageUrlBinding('{0}', {1})", this.ClientID, this.neverDirty ? "true" : "false"));
 }
 // Token: 0x060000B7 RID: 183 RVA: 0x00003468 File Offset: 0x00001668
 public static void AddElementProperty(this ScriptComponentDescriptor descriptor, string name, string value, IControlResolver controlResolver)
 {
     if (!string.IsNullOrEmpty(value))
     {
         Control control = null;
         if (controlResolver != null)
         {
             control = controlResolver.ResolveControl(value);
         }
         if (control != null)
         {
             value = control.ClientID;
         }
         descriptor.AddElementProperty(name, value);
     }
 }
 // Token: 0x060000B4 RID: 180 RVA: 0x000033E0 File Offset: 0x000015E0
 public static void AddComponentProperty(this ScriptComponentDescriptor descriptor, string name, string value, IControlResolver controlResolver)
 {
     if (!string.IsNullOrEmpty(value))
     {
         Control control = null;
         if (controlResolver != null)
         {
             control = controlResolver.ResolveControl(value);
         }
         if (control != null)
         {
             ExtenderControlBase extenderControlBase = control as ExtenderControlBase;
             if (extenderControlBase != null && extenderControlBase.BehaviorID.Length > 0)
             {
                 value = extenderControlBase.BehaviorID;
             }
             else
             {
                 value = control.ClientID;
             }
         }
         descriptor.AddComponentProperty(name, value);
     }
 }
Ejemplo n.º 16
0
 public override string ToJavaScript(IControlResolver resolver)
 {
     return(string.Format("new ParameterBinding({0})", this.Value.ToJsonString(null)));
 }
Ejemplo n.º 17
0
        public static void DescribeComponent(object instance, IScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver)
        {
            // validate preconditions
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            if (urlResolver == null)
            {
                urlResolver = instance as IUrlResolutionService;
            }
            if (controlResolver == null)
            {
                controlResolver = instance as IControlResolver;
            }

            // describe properties
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

            foreach (PropertyDescriptor prop in properties)
            {
                ExtenderControlPropertyAttribute propAttr  = null;
                ExtenderControlEventAttribute    eventAttr = null;

                ClientPropertyNameAttribute  nameAttr    = null;
                IDReferencePropertyAttribute idRefAttr   = null;
                UrlPropertyAttribute         urlAttr     = null;
                ElementReferenceAttribute    elementAttr = null;
                ComponentReferenceAttribute  compAttr    = null;

                foreach (Attribute attr in prop.Attributes)
                {
                    Type attrType = attr.GetType();
                    if (attrType == typeof(ExtenderControlPropertyAttribute))
                    {
                        propAttr = attr as ExtenderControlPropertyAttribute;
                    }
                    else if (attrType == typeof(ExtenderControlEventAttribute))
                    {
                        eventAttr = attr as ExtenderControlEventAttribute;
                    }
                    else if (attrType == typeof(ClientPropertyNameAttribute))
                    {
                        nameAttr = attr as ClientPropertyNameAttribute;
                    }
                    else if (attrType == typeof(IDReferencePropertyAttribute))
                    {
                        idRefAttr = attr as IDReferencePropertyAttribute;
                    }
                    else if (attrType == typeof(UrlPropertyAttribute))
                    {
                        urlAttr = attr as UrlPropertyAttribute;
                    }
                    else if (attrType == typeof(ElementReferenceAttribute))
                    {
                        elementAttr = attr as ElementReferenceAttribute;
                    }
                    else if (attrType == typeof(ComponentReferenceAttribute))
                    {
                        compAttr = attr as ComponentReferenceAttribute;
                    }
                }

                string propertyName = prop.Name;

                // Try getting a property attribute
                if (propAttr == null || !propAttr.IsScriptProperty)
                {
                    // Try getting an event attribute
                    if (eventAttr == null || !eventAttr.IsScriptEvent)
                    {
                        continue;
                    }
                }

                // attempt to rename the property/event
                if (nameAttr != null && !string.IsNullOrEmpty(nameAttr.PropertyName))
                {
                    propertyName = nameAttr.PropertyName;
                }

                // determine whether to serialize the value of a property.  readOnly properties should always be serialized
                bool serialize = prop.ShouldSerializeValue(instance) || prop.IsReadOnly;
                if (serialize)
                {
                    // get the value of the property, skip if it is null
                    Control c     = null;
                    object  value = prop.GetValue(instance);
                    if (value == null)
                    {
                        continue;
                    }

                    // convert and resolve the value
                    if (eventAttr != null && prop.PropertyType != typeof(String))
                    {
                        throw new InvalidOperationException("ExtenderControlEventAttribute can only be applied to a property with a PropertyType of System.String.");
                    }
                    else
                    {
                        if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum)
                        {
                            // Check if we can use any of our custom converters
                            // (first do a direct lookup on the property type,
                            // but also check all of its base types if nothing
                            // was found)
                            Converter <object, string> customConverter = null;
                            if (!_customConverters.TryGetValue(prop.PropertyType, out customConverter))
                            {
                                foreach (KeyValuePair <Type, Converter <object, string> > pair in _customConverters)
                                {
                                    if (prop.PropertyType.IsSubclassOf(pair.Key))
                                    {
                                        customConverter = pair.Value;
                                        break;
                                    }
                                }
                            }

                            // Use the custom converter if found, otherwise use
                            // its current type converter
                            if (customConverter != null)
                            {
                                value = customConverter(value);
                            }
                            else
                            {
                                // Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization
                                if (propAttr != null && propAttr.UseJsonSerialization)
                                {
                                    // Use ASP.NET JSON serialization
                                }
                                else
                                {
                                    // Use the property's own converter
                                    TypeConverter conv = prop.Converter;

                                    if (value.GetType() == typeof(DateTime))
                                    {
                                        value = ((DateTime)value).ToString("s", CultureInfo.InvariantCulture);
                                    }
                                    else
                                    {
                                        value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value);
                                    }
                                }
                            }
                        }
                        if (idRefAttr != null && controlResolver != null)
                        {
                            c = controlResolver.ResolveControl((string)value);
                        }
                        if (urlAttr != null && urlResolver != null)
                        {
                            value = urlResolver.ResolveClientUrl((string)value);
                        }
                    }

                    // add the value as an appropriate description
                    if (eventAttr != null)
                    {
                        descriptor.AddEvent(propertyName, (string)value);
                    }
                    else if (elementAttr != null)
                    {
                        if (c == null && controlResolver != null)
                        {
                            c = controlResolver.ResolveControl((string)value);
                        }
                        if (c != null)
                        {
                            value = c.ClientID;
                        }
                        descriptor.AddElementProperty(propertyName, (string)value);
                    }
                    else if (compAttr != null)
                    {
                        if (c == null && controlResolver != null)
                        {
                            c = controlResolver.ResolveControl((string)value);
                        }
                        if (c != null)
                        {
                            ExtenderControlBase ex = c as ExtenderControlBase;
                            if (ex != null && ex.BehaviorID.Length > 0)
                            {
                                value = ex.BehaviorID;
                            }
                            else
                            {
                                value = c.ClientID;
                            }
                        }
                        descriptor.AddComponentProperty(propertyName, (string)value);
                    }
                    else
                    {
                        if (c != null)
                        {
                            value = c.ClientID;
                        }
                        descriptor.AddProperty(propertyName, value);
                    }
                }
            }

            // determine if we should describe methods
            foreach (MethodInfo method in instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
            {
                ExtenderControlMethodAttribute methAttr = (ExtenderControlMethodAttribute)Attribute.GetCustomAttribute(method, typeof(ExtenderControlMethodAttribute));
                if (methAttr == null || !methAttr.IsScriptMethod)
                {
                    continue;
                }

                // We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method
                Control control = instance as Control;
                if (control != null)
                {
                    // Force WebForms.js
                    control.Page.ClientScript.GetCallbackEventReference(control, null, null, null);

                    // Add the callback target
                    descriptor.AddProperty("_callbackTarget", control.UniqueID);
                }
                break;
            }
        }
Ejemplo n.º 18
0
 public ControlTreeResolver(DotvvmConfiguration configuration)
 {
     controlResolver = configuration.ServiceLocator.GetService <IControlResolver>();
     bindingParser   = configuration.ServiceLocator.GetService <IBindingParser>();
 }
Ejemplo n.º 19
0
 protected virtual string ToJavaScriptWhenVisible(IControlResolver resolver)
 {
     return(string.Format("new {0}('{1}')", base.GetType().Name, this.ClientID));
 }
Ejemplo n.º 20
0
 protected override string ToJavaScriptWhenVisible(IControlResolver resolver)
 {
     return(string.Format("new SortedComboBoxBinding('{0}','{1}',{2})", this.ClientID, base.ClientPropertyName, (this.SortedDirection == SortDirection.Ascending).ToString().ToLowerInvariant()));
 }
        public static void DescribeComponent(object instance, IScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver) {
            // validate preconditions
            if(instance == null)
                throw new ArgumentNullException("instance");
            if(descriptor == null)
                throw new ArgumentNullException("descriptor");
            if(urlResolver == null)
                urlResolver = instance as IUrlResolutionService;
            if(controlResolver == null)
                controlResolver = instance as IControlResolver;

            // describe properties
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);
            foreach(PropertyDescriptor prop in properties) {
                ExtenderControlPropertyAttribute propAttr = null;
                ExtenderControlEventAttribute eventAttr = null;

                ClientPropertyNameAttribute nameAttr = null;
                IDReferencePropertyAttribute idRefAttr = null;
                UrlPropertyAttribute urlAttr = null;
                ElementReferenceAttribute elementAttr = null;
                ComponentReferenceAttribute compAttr = null;

                foreach(Attribute attr in prop.Attributes) {
                    Type attrType = attr.GetType();
                    if(attrType == typeof(ExtenderControlPropertyAttribute))
                        propAttr = attr as ExtenderControlPropertyAttribute;
                    else if(attrType == typeof(ExtenderControlEventAttribute))
                        eventAttr = attr as ExtenderControlEventAttribute;
                    else if(attrType == typeof(ClientPropertyNameAttribute))
                        nameAttr = attr as ClientPropertyNameAttribute;
                    else if(attrType == typeof(IDReferencePropertyAttribute))
                        idRefAttr = attr as IDReferencePropertyAttribute;
                    else if(attrType == typeof(UrlPropertyAttribute))
                        urlAttr = attr as UrlPropertyAttribute;
                    else if(attrType == typeof(ElementReferenceAttribute))
                        elementAttr = attr as ElementReferenceAttribute;
                    else if(attrType == typeof(ComponentReferenceAttribute))
                        compAttr = attr as ComponentReferenceAttribute;
                }

                string propertyName = prop.Name;

                // Try getting a property attribute
                if(propAttr == null || !propAttr.IsScriptProperty) {
                    // Try getting an event attribute
                    if(eventAttr == null || !eventAttr.IsScriptEvent) {
                        continue;
                    }
                }

                // attempt to rename the property/event
                if(nameAttr != null && !string.IsNullOrEmpty(nameAttr.PropertyName)) {
                    propertyName = nameAttr.PropertyName;
                }

                // determine whether to serialize the value of a property.  readOnly properties should always be serialized
                bool serialize = prop.ShouldSerializeValue(instance) || prop.IsReadOnly;
                if(serialize) {
                    // get the value of the property, skip if it is null
                    Control c = null;
                    object value = prop.GetValue(instance);
                    if(value == null) {
                        continue;
                    }

                    // convert and resolve the value
                    if(eventAttr != null && prop.PropertyType != typeof(String)) {
                        throw new InvalidOperationException("ExtenderControlEventAttribute can only be applied to a property with a PropertyType of System.String.");
                    } else {
                        if(!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum) {
                            // Check if we can use any of our custom converters
                            // (first do a direct lookup on the property type,
                            // but also check all of its base types if nothing
                            // was found)
                            Converter<object, string> customConverter = null;
                            if(!_customConverters.TryGetValue(prop.PropertyType, out customConverter)) {
                                foreach(KeyValuePair<Type, Converter<object, string>> pair in _customConverters) {
                                    if(prop.PropertyType.IsSubclassOf(pair.Key)) {
                                        customConverter = pair.Value;
                                        break;
                                    }
                                }
                            }

                            // Use the custom converter if found, otherwise use
                            // its current type converter
                            if(customConverter != null) {
                                value = customConverter(value);
                            } else {
                                // Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization
                                if(propAttr != null && propAttr.UseJsonSerialization) {
                                    // Use ASP.NET JSON serialization
                                } else {
                                    // Use the property's own converter
                                    TypeConverter conv = prop.Converter;

                                    if(value.GetType() == typeof(DateTime))
                                        value = ((DateTime)value).ToString("s", CultureInfo.InvariantCulture);
                                    else
                                        value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value);
                                }
                            }
                        }
                        if(idRefAttr != null && controlResolver != null) {
                            c = controlResolver.ResolveControl((string)value);
                        }
                        if(urlAttr != null && urlResolver != null) {
                            value = urlResolver.ResolveClientUrl((string)value);
                        }
                    }

                    // add the value as an appropriate description
                    if(eventAttr != null) {
                        descriptor.AddEvent(propertyName, (string)value);
                    } else if(elementAttr != null) {
                        if(c == null && controlResolver != null)
                            c = controlResolver.ResolveControl((string)value);
                        if(c != null)
                            value = c.ClientID;
                        descriptor.AddElementProperty(propertyName, (string)value);
                    } else if(compAttr != null) {
                        if(c == null && controlResolver != null)
                            c = controlResolver.ResolveControl((string)value);
                        if(c != null) {
                            ExtenderControlBase ex = c as ExtenderControlBase;
                            if(ex != null && ex.BehaviorID.Length > 0)
                                value = ex.BehaviorID;
                            else
                                value = c.ClientID;
                        }
                        descriptor.AddComponentProperty(propertyName, (string)value);
                    } else {
                        if(c != null)
                            value = c.ClientID;
                        descriptor.AddProperty(propertyName, value);
                    }
                }
            }

            // determine if we should describe methods
            foreach(MethodInfo method in instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)) {
                ExtenderControlMethodAttribute methAttr = (ExtenderControlMethodAttribute)Attribute.GetCustomAttribute(method, typeof(ExtenderControlMethodAttribute));
                if(methAttr == null || !methAttr.IsScriptMethod) {
                    continue;
                }

                // We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method
                Control control = instance as Control;
                if(control != null) {
                    // Force WebForms.js
                    control.Page.ClientScript.GetCallbackEventReference(control, null, null, null);

                    // Add the callback target
                    descriptor.AddProperty("_callbackTarget", control.UniqueID);
                }
                break;
            }
        }
Ejemplo n.º 22
0
        public override string ToJavaScript(IControlResolver resolver)
        {
            string arg = string.IsNullOrEmpty(this.ContractType) ? "Object" : this.ContractType;

            return(string.Format("new {0}({1},{2})", base.GetType().Name, arg, this.Bindings.ToJavaScript(resolver)));
        }
Ejemplo n.º 23
0
        public static void DescribeComponent(object instance, ScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver)
        {
            // validate preconditions
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }
            if (urlResolver == null)
            {
                urlResolver = instance as IUrlResolutionService;
            }
            if (controlResolver == null)
            {
                controlResolver = instance as IControlResolver;
            }

            // describe properties
            // PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);

            PropertyInfo[] properties = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (PropertyInfo prop in properties)
            {
                ScriptControlPropertyAttribute propAttr  = null;
                ScriptControlEventAttribute    eventAttr = null;
                string propertyName = prop.Name;

                System.ComponentModel.AttributeCollection attribs = new System.ComponentModel.AttributeCollection(Attribute.GetCustomAttributes(prop, false));

                // Try getting a property attribute
                propAttr = (ScriptControlPropertyAttribute)attribs[typeof(ScriptControlPropertyAttribute)];
                if (propAttr == null || !propAttr.IsScriptProperty)
                {
                    // Try getting an event attribute
                    eventAttr = (ScriptControlEventAttribute)attribs[typeof(ScriptControlEventAttribute)];
                    if (eventAttr == null || !eventAttr.IsScriptEvent)
                    {
                        continue;
                    }
                }

                // attempt to rename the property/event
                ClientPropertyNameAttribute nameAttr = (ClientPropertyNameAttribute)attribs[typeof(ClientPropertyNameAttribute)];
                if (nameAttr != null && !string.IsNullOrEmpty(nameAttr.PropertyName))
                {
                    propertyName = nameAttr.PropertyName;
                }

                // determine whether to serialize the value of a property.  readOnly properties should always be serialized
                //bool serialize = true;// prop.ShouldSerializeValue(instance) || prop.IsReadOnly;
                //if (serialize)
                //{
                // get the value of the property, skip if it is null
                Control c     = null;
                object  value = prop.GetValue(instance, new object[0] {
                });
                if (value == null)
                {
                    continue;
                }

                // convert and resolve the value
                if (eventAttr != null && prop.PropertyType != typeof(String))
                {
                    throw new InvalidOperationException("ScriptControlEventAttribute can only be applied to a property with a PropertyType of System.String.");
                }
                else
                {
                    if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum)
                    {
                        if (prop.PropertyType == typeof(Color))
                        {
                            value = ColorTranslator.ToHtml((Color)value);
                        }
                        else
                        {
                            // TODO: Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization
                            //TypeConverter conv = prop.Converter;
                            //value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value);

                            //if (prop.PropertyType == typeof(CssStyleCollection))
                            //    value = (new CssStyleCollectionJSCoverter()).Serialize(value, new JavaScriptSerializer());
                            //if (prop.PropertyType == typeof(Style))
                            //    value = (new CssStyleCollectionJSCoverter()).Serialize(((Style)value).GetStyleAttributes(null), new JavaScriptSerializer());

                            Type valueType = value.GetType();

                            JavaScriptConverterAttribute attr      = (JavaScriptConverterAttribute)attribs[typeof(JavaScriptConverterAttribute)];
                            JavaScriptConverter          converter = attr != null ?
                                                                     (JavaScriptConverter)TypeCreator.CreateInstance(attr.ConverterType) :
                                                                     JsonSerializerFactory.GetJavaScriptConverter(valueType);

                            if (converter != null)
                            {
                                value = converter.Serialize(value, JsonSerializerFactory.GetJavaScriptSerializer());
                            }
                            else
                            {
                                value = JsonHelper.PreSerializeObject(value);
                            }

                            //Dictionary<string, object> dict = value as Dictionary<string, object>;
                            //if (dict != null && !dict.ContainsKey("__type"))
                            //    dict["__type"] = valueType.AssemblyQualifiedName;
                        }
                    }
                    if (attribs[typeof(IDReferencePropertyAttribute)] != null && controlResolver != null)
                    {
                        c = controlResolver.ResolveControl((string)value);
                    }
                    if (attribs[typeof(UrlPropertyAttribute)] != null && urlResolver != null)
                    {
                        value = urlResolver.ResolveClientUrl((string)value);
                    }
                }

                // add the value as an appropriate description
                if (eventAttr != null)
                {
                    if (!string.IsNullOrEmpty((string)value))
                    {
                        descriptor.AddEvent(propertyName, (string)value);
                    }
                }
                else if (attribs[typeof(ElementReferenceAttribute)] != null)
                {
                    if (c == null && controlResolver != null)
                    {
                        c = controlResolver.ResolveControl((string)value);
                    }
                    if (c != null)
                    {
                        value = c.ClientID;
                    }
                    descriptor.AddElementProperty(propertyName, (string)value);
                }
                else if (attribs[typeof(ComponentReferenceAttribute)] != null)
                {
                    if (c == null && controlResolver != null)
                    {
                        c = controlResolver.ResolveControl((string)value);
                    }
                    if (c != null)
                    {
                        //ExtenderControlBase ex = c as ExtenderControlBase;
                        //if (ex != null && ex.BehaviorID.Length > 0)
                        //    value = ex.BehaviorID;
                        //else
                        value = c.ClientID;
                    }
                    descriptor.AddComponentProperty(propertyName, (string)value);
                }
                else
                {
                    if (c != null)
                    {
                        value = c.ClientID;
                    }
                    descriptor.AddProperty(propertyName, value);
                }
            }
            //}

            // determine if we should describe methods
            foreach (MethodInfo method in instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
            {
                ScriptControlMethodAttribute methAttr = (ScriptControlMethodAttribute)Attribute.GetCustomAttribute(method, typeof(ScriptControlMethodAttribute));
                if (methAttr == null || !methAttr.IsScriptMethod)
                {
                    continue;
                }

                // We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method
                Control control = instance as Control;
                if (control != null)
                {
                    // Force WebForms.js
                    control.Page.ClientScript.GetCallbackEventReference(control, null, null, null);

                    // Add the callback target
                    descriptor.AddProperty("_callbackTarget", control.UniqueID);
                }
                break;
            }
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DefaultControlTreeResolver"/> class.
 /// </summary>
 public DefaultControlTreeResolver(IControlResolver controlResolver, IAbstractTreeBuilder treeBuilder)
     : base(controlResolver, treeBuilder)
 {
 }