public static List <CategoryInfoModel> GetPropertyGroups(PropertyGridConfig config, Type modelType, bool useCamelCase)
        {
            var flags = BindingFlags.Public | BindingFlags.Instance;

            if (config.HideInherited)
            {
                flags = flags | BindingFlags.DeclaredOnly;
            }

            var groups = modelType.GetProperties(flags)
                         .Where(p =>
                                p.CanRead && p.CanWrite &&
                                !config.HiddenProperties.Contains(p.Name) &&
                                (p.GetAttribute <BrowsableAttribute>()?.Browsable ?? true) == true &&
                                (!config.ShowOnlyCategorized || !string.IsNullOrWhiteSpace(GetCategoryName(p, config.ModelType))) &&
                                (config.PropertiesVisibility?.Invoke(p) ?? true)
                                )
                         .GroupBy(p => GetCategoryName(p, config.ModelType) ?? config.DefaultCategoryName, p => p, (c, props) =>
                                  new CategoryInfoModel
            {
                Category   = c,
                OrderIndex = config.CategoriesOrderFunc?.Invoke(c) ?? 0,
                Properties = props.Select(p => p.GetHardCodedConfig(config.ModelType, useCamelCase)).ToList()
            })
                         .Where(g => !config.HiddenCategories.Contains(g.Category))
                         .ToList();

            if (config.CategoriesVisibility != null)
            {
                groups = groups.Where(g => config.CategoriesVisibility.Invoke(g.Category)).ToList();
            }

            return(groups);
        }
        public List <CategoryInfoModel> GetGroups(PropertyGridConfig config)
        {
            var modelType = Model.GetType().StripCastleProxyType();

            var propertyGroups = GetPropertyGroups(config, modelType, false);

            var customCategoriesAttributes = GetCustomCategoriesAttributes(modelType, config.HideInherited);

            foreach (var customCategoriesAttribute in customCategoriesAttributes)
            {
                if (!string.IsNullOrWhiteSpace(customCategoriesAttribute.ScanCategoriesPath))
                {
                    /*
                     * var categoriesPath = Path.Combine(customCategoriesAttribute.ScanCategoriesPath,
                     *  config.Readonly ? "DisplayCategories" : "EditCategories").Replace('\\', '/');
                     *
                     * var virtualDir = HostingEnvironment.VirtualPathProvider.GetDirectory(categoriesPath);
                     *
                     * var files = virtualDir.Files.Cast<VirtualFileBase>();
                     * foreach (var file in files)
                     * {
                     *  var categoryName = Path.GetFileNameWithoutExtension(file.Name).ToFriendlyName();
                     *  if (config.HiddenCategories.Contains(categoryName))
                     *      continue;
                     *
                     *  var category = propertyGroups.FirstOrDefault(g => g.Category.Equals(categoryName, StringComparison.InvariantCultureIgnoreCase));
                     *  if (category == null)
                     *  {
                     *      category = new CategoryInfoModel
                     *      {
                     *          Category = categoryName
                     *      };
                     *      propertyGroups.Add(category);
                     *  }
                     *  if (!category.CustomViews.Contains(file.VirtualPath, StringComparer.InvariantCultureIgnoreCase))
                     *      category.CustomViews.Add(file.VirtualPath);
                     * }
                     */
                }
            }

            foreach (var editor in config.CustomEditors)
            {
                var category = propertyGroups.FirstOrDefault(g => g.Category.Equals(editor.Category, StringComparison.InvariantCultureIgnoreCase));
                if (category == null)
                {
                    category = new CategoryInfoModel
                    {
                        Category = editor.Category
                    };
                    propertyGroups.Add(category);
                }

                if (!category.CustomViews.Contains(editor.PartialViewName, StringComparer.InvariantCultureIgnoreCase))
                {
                    category.CustomViews.Add(editor.PartialViewName);
                }
            }

            if (config.CategoriesVisibility != null)
            {
                propertyGroups = propertyGroups.Where(g => config.CategoriesVisibility.Invoke(g.Category)).ToList();
            }

            propertyGroups = propertyGroups
                             .OrderBy(g => g.Category.Equals("Uncategorized") ? int.MaxValue : g.OrderIndex)
                             .ThenBy(g => g.Category)
                             .ToList();

            return(propertyGroups);
        }
Ejemplo n.º 3
0
        public static void AutoGenerateForm(Form form, bool displayMode)
        {
            var modelType = !string.IsNullOrWhiteSpace(form.ModelType)
                ? Type.GetType(form.ModelType)
                : null;

            if (modelType == null)
            {
                return;
            }

            // inactivate all existing components
            var toDelete = form.GetComponents().ToList();

            foreach (var component in toDelete)
            {
                ComponentService.Delete(component);
            }

            var propertyGridConfig = new PropertyGridConfig(null)
            {
                ModelType            = modelType,
                DefaultCategoryName  = "Misc",
                CategoriesVisibility = c => !c.Equals("Audit", StringComparison.InvariantCultureIgnoreCase)
            };

            // skip lists
            propertyGridConfig.PropertiesVisibility = prop => !prop.PropertyType.IsSubtypeOfGeneric(typeof(IList <>));

            var mapper = GetDefaultMapper();

            var groups     = PropertyGridDataProvider.GetPropertyGroups(propertyGridConfig, modelType, false);
            var panelIndex = 0;

            foreach (var group in groups)
            {
                var panelModel = new PanelModel()
                {
                    Type      = "panel",
                    ApiKey    = $"panel{panelIndex}",
                    Label     = group.Category,
                    SortIndex = panelIndex
                };

                var componentModels = group.Properties.Select(p => GetComponentByPropertyConfig(p, displayMode)).Where(c => c != null).ToList();
                if (componentModels.Any())
                {
                    var panel = new FormComponent()
                    {
                        Form = form
                    };
                    mapper.Map(panelModel, panel);
                    ComponentService.InsertOrUpdate(panel);
                    panelIndex++;

                    var componentIndex = 0;
                    foreach (var componentModel in componentModels)
                    {
                        componentModel.SortIndex = componentIndex++;
                        var component = new FormComponent()
                        {
                            Form   = form,
                            Parent = panel
                        };
                        mapper.Map(componentModel, component);

                        component.CustomSettings = SerializeCustomProperties(componentModel);

                        ComponentService.InsertOrUpdate(component);
                    }
                }
            }
        }