public ListConfig ResolveByConvention(Type subject, Features features, ICollectionSetup?collection)
        {
            var listButtons = new List <ButtonConfig>();

            if (features.HasFlag(Features.CanEdit) || features.HasFlag(Features.CanGoToEdit))
            {
                listButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.New,
                    Label      = !(collection?.SubEntityVariants?.Any() ?? false) ? null : _languageResolver.ResolveText("New {0}")
                });
                listButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.Return
                });
                listButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.SaveExisting,
                    Label      = _languageResolver.ResolveText("Update all")
                });
            }
            ;
            var paneButtons = new List <ButtonConfig>();

            if (features.HasFlag(Features.CanGoToView))
            {
                paneButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.View
                });
            }
            if (features.HasFlag(Features.CanGoToEdit))
            {
                paneButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.Edit
                });
            }
            if (features.HasFlag(Features.CanEdit))
            {
                paneButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.SaveExisting
                });
                paneButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.SaveNew
                });
                paneButtons.Add(new DefaultButtonConfig
                {
                    ButtonType = DefaultButtonType.Delete
                });
            }

            var result = new ListConfig(subject)
            {
                PageSize       = 25,
                Buttons        = listButtons,
                ListEditorType = features.HasFlag(Features.IsBlockList) ? ListType.Block : ListType.Table,
                Panes          = new List <PaneConfig>
                {
                    new PaneConfig(subject)
                    {
                        Buttons     = paneButtons,
                        FieldIndex  = 1,
                        Fields      = _fieldConfigResolver.GetFields(subject, features).ToList(),
                        VariantType = subject
                    }
                },
                ReorderingAllowed = false,
                SearchBarVisible  = true
            };

            return(result);
        }
Example #2
0
        public IResolvedSetup <IButtonSetup> ResolveSetup(ButtonConfig config, ICollectionSetup?collection = default)
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            var @default = (config as DefaultButtonConfig)?.ButtonType.GetCustomAttribute <DefaultIconLabelAttribute>();

            var button = new ButtonSetup
            {
                Buttons = EmptySubButtons,

                Label     = _languageResolver.ResolveText(config.Label ?? @default?.Label ?? "Button"),
                Icon      = config.Icon ?? @default?.Icon ?? "",
                IsPrimary = config.IsPrimary,

                ButtonId = config.Id ?? throw new ArgumentNullException(nameof(config.Id)),
                                 EntityVariant = collection.EntityVariant
            };

            if (config is DefaultButtonConfig defaultButton)
            {
                if (defaultButton.ButtonType == DefaultButtonType.OpenPane)
                {
                    throw new InvalidOperationException($"An {DefaultButtonType.OpenPane} button is not allowed to be used by DefaultButton");
                }

                button.DefaultButtonType = defaultButton.ButtonType;

                button.ButtonHandlerType = typeof(DefaultButtonActionHandler);

                if (defaultButton.ButtonType == DefaultButtonType.New && collection.SubEntityVariants != null)
                {
                    button.Buttons = collection.SubEntityVariants.ToList(variant =>
                                                                         new ButtonSetup
                    {
                        Label             = _languageResolver.ResolveText(string.Format(button.Label ?? variant.Name, variant.Name)),
                        Icon              = variant.Icon ?? @default?.Icon ?? "",
                        IsPrimary         = config.IsPrimary,
                        ButtonId          = $"{config.Id}-{variant.Alias}",
                        EntityVariant     = variant,
                        DefaultButtonType = DefaultButtonType.New,
                        ButtonHandlerType = typeof(DefaultButtonActionHandler),
                        Buttons           = EmptySubButtons
                    });
                }
            }
            else if (config is CustomButtonConfig customButton)
            {
                button.CustomType        = customButton.CustomType;
                button.ButtonHandlerType = customButton.ActionHandler;
            }
            else if (config is PaneButtonConfig paneButton)
            {
                button.DefaultButtonType = DefaultButtonType.OpenPane;
                button.DefaultCrudType   = paneButton.CrudType;
                button.ButtonHandlerType = typeof(OpenPaneButtonActionHandler <>).MakeGenericType(paneButton.PaneType);
            }
            else
            {
                throw new InvalidOperationException();
            }

            return(new ResolvedSetup <IButtonSetup>(button, true));
        }
    }