Exemplo n.º 1
0
        public DynamicHandlerAttribute(DisplayContexts context, params Type[] toHandle)
        {
            if (toHandle is null || toHandle.Length == 0)
            {
                throw new ArgumentException("Must specify at least one type to handle", nameof(toHandle));
            }

            this.DisplayContexts = context;

            this.ToHandle = toHandle;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs a new instance of this attribute
        /// </summary>
        /// <param name="context">The display context this attribute should apply to</param>
        /// <param name="controller">The name of the controller to route to</param>
        /// <param name="action">The controller action to route to</param>
        /// <param name="area">The area the controller resides in</param>
        /// <param name="namespace">The namespace the controller resides in</param>
        public CustomRouteAttribute(DisplayContexts context, string controller, string action, string area = "", string @namespace = "")
        {
            this.RouteValues = new Dictionary <string, object>
            {
                { "controller", controller },
                { "action", action },
                { "area", area },
                { "namespace", @namespace }
            };

            this.Context = context;
        }
        /// <summary>
        /// Gets the view result containing editor view information for the current metaObject
        /// </summary>
        /// <param name="metaObject">The MetaObject to get view information for</param>
        /// <param name="requestContext">The current RequestContext</param>
        /// <param name="displayType">The type to be used when finding the view, if not the MetaObject type</param>
        /// <returns>A result containing editor view information</returns>
        protected DynamicViewResult?GetView(IMetaObject metaObject, DisplayContexts requestContext, IMetaType?displayType = null)
        {
            if (metaObject is null)
            {
                throw new ArgumentNullException(nameof(metaObject));
            }

            string BasePath = $"/Areas/Admin/Views/{requestContext}/";

            if (metaObject.IsRoot())
            {
                return(null);
            }

            IMetaProperty property = metaObject.Property;

            displayType ??= GetDisplayType(metaObject);

            string Key = $"{displayType.AssemblyQualifiedName}+{property?.Name}+{requestContext}";

            if (!Views.TryGetValue(Key, out string?path))
            {
                DynamicRenderer renderer = new DynamicRenderer(new DynamicRendererSettings(displayType, property, this.FileProvider)
                {
                    BasePath = BasePath
                });

                if (renderer.IsDynamic)
                {
                    Views.TryAdd(Key, null);
                }
                else
                {
                    path = renderer.MatchedPath;
                    Views.TryAdd(Key, path);
                }
            }

            if (string.IsNullOrWhiteSpace(path))
            {
                return(null);
            }
            else
            {
                return(new DynamicViewResult(path));
            }
        }
        protected static DynamicActionResult?GetAction(IMetaObject metaObject, DisplayContexts requestContext, IMetaType?displayType = null)
        {
            if (metaObject is null)
            {
                throw new ArgumentNullException(nameof(metaObject));
            }

            displayType ??= metaObject.Type;

            foreach (MethodInfo methodInfo in TypeFactory.GetDerivedTypes(typeof(Controller)).SelectMany(m => m.GetMethods()))
            {
                if (!metaObject.IsRoot())
                {
                    if (methodInfo.GetCustomAttribute <DynamicPropertyHandlerAttribute>() is DynamicPropertyHandlerAttribute propertyAttribute && propertyAttribute.DisplayContexts.HasFlag(requestContext))
                    {
                        if (metaObject.Property.Name == propertyAttribute.PropertyName && metaObject.Parent.Type.Is(propertyAttribute.Type))
                        {
                            return(GetActionResult(methodInfo));
                        }
                    }
                }

                if (methodInfo.GetCustomAttribute <DynamicHandlerAttribute>() is DynamicHandlerAttribute attribute && attribute.DisplayContexts.HasFlag(requestContext))
                {
                    if (attribute.ToHandle.FirstOrDefault(t => displayType.Is(t)) is Type handledType)
                    {
                        return(GetActionResult(methodInfo));
                    }
                }
            }

            IMetaObject?customRouteAttribute = metaObject?.Property
                                               ?.Attributes
                                               ?.Where(a => a.Type.FullName == typeof(CustomRouteAttribute).FullName)
                                               ?.Where(a => a.Instance[nameof(CustomRouteAttribute.Context)].GetValue <DisplayContexts>().HasFlag(DisplayContexts.Edit))
                                               ?.Select(a => a.Instance)
                                               ?.SingleOrDefault();

            if (customRouteAttribute != null)
            {
                return(new DynamicActionResult(customRouteAttribute[nameof(CustomRouteAttribute.RouteValues)].ToDictionary <string, object>()));
            }

            return(null);
        }
        /// <summary>
        /// Returns the handler that should be used to render a given MetaObject
        /// </summary>
        /// <param name="metaObject">The MetaObject to find a handler for</param>
        /// <param name="requestContext">The current request context</param>
        /// <param name="displayType">The type used when finding the handler, if not the MetaObject type</param>
        /// <returns>A result object that may point to either an action or a view to be used when rendering the object</returns>
        public EditorHandlerResult FindHandler(IMetaObject metaObject, DisplayContexts requestContext, IMetaType?displayType = null)
        {
            if (metaObject is null)
            {
                throw new ArgumentNullException(nameof(metaObject));
            }

            if (metaObject.Property?.HasAttribute <ForceDynamicAttribute>() ?? false)
            {
                return(new DynamicEditorResult());
            }

            return(GetAction(metaObject, requestContext, displayType) ??
                   this.GetView(metaObject, requestContext, displayType) ??
                   (metaObject.GetCoreType() == CoreType.Value ?
                    new StaticValueResult() :
                    new DynamicEditorResult() as EditorHandlerResult));
        }
 /// <summary>
 /// Creates a new instance of this attribute
 /// </summary>
 /// <param name="context">The display context the action is used in</param>
 /// <param name="type">The name of the property of the meta object that this action is an editor for</param>
 /// <param name="propertyName">The target object type handled by this action</param>
 public DynamicPropertyHandlerAttribute(DisplayContexts context, Type type, string propertyName)
 {
     this.DisplayContexts = context;
     this.Type            = type;
     this.PropertyName    = propertyName;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Creates a new instance of this attribute
 /// </summary>
 /// <param name="context">The display context(s) that this property should be hidden from</param>
 public DontAllowAttribute(DisplayContexts context)
 {
     this.Context = context;
 }