diff --git a/src/admin/Bootstrap.Admin/Pages/Components/AssignModalBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/AssignModalBase.cs new file mode 100644 index 00000000..ec6d46b7 --- /dev/null +++ b/src/admin/Bootstrap.Admin/Pages/Components/AssignModalBase.cs @@ -0,0 +1,114 @@ +using Bootstrap.Admin.Pages.Shared; +using Microsoft.AspNetCore.Components; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Bootstrap.Admin.Pages.Components +{ + /// + /// 模态框组件类 + /// + public class AssignModalBase : ComponentBase + { +#nullable disable + /// + /// 获得/设置 数据绑定项 + /// + [Parameter] + public TItem Item { get; set; } +#nullable restore + + /// + /// 获得/设置 Modal 实例 + /// + [Parameter] + public ModalBase? Modal { get; set; } + + /// + /// 获得/设置 Id + /// + [Parameter] + public string Id { get; set; } = ""; + + /// + /// 获得/设置 弹窗标题 + /// + [Parameter] + public string Title { get; set; } = "未设置"; + + /// + /// 获得/设置 表格 Toolbar 按钮模板 + /// + [Parameter] + public RenderFragment? ItemTemplate { get; set; } + + /// + /// 获得/设置 Items + /// + public IEnumerable Items { get; set; } = new TItem[0]; + + /// + /// Toast 组件实例 + /// + protected Toast? Toast { get; set; } + + /// + /// 显示提示信息 + /// + /// + /// + /// + protected void ShowMessage(string title, string text, ToastCategory cate = ToastCategory.Success) => Toast?.ShowMessage(title, text, cate); + + /// + /// 获得/设置 保存回调事件 + /// + [Parameter] + public Func, bool>? OnSave { get; set; } + + /// + /// OnAfterRender 方法 + /// + /// + protected override void OnAfterRender(bool firstRender) + { + if (show) + { + show = false; + Modal?.Toggle(); + } + } + + /// + /// SetParametersAsync 方法 + /// + public override Task SetParametersAsync(ParameterView parameters) + { + parameters.SetParameterProperties(this); + if (string.IsNullOrEmpty(Id)) throw new InvalidOperationException("Modal Component Id property must be set"); + return base.SetParametersAsync(ParameterView.Empty); + } + + /// + /// Save 方法 + /// + protected void Save() + { + bool ret = OnSave?.Invoke(Items) ?? false; + Modal?.Toggle(); + ShowMessage(Title, ret ? "保存成功" : "保存失败", ret ? ToastCategory.Success : ToastCategory.Error); + } + + private bool show; + /// + /// Update 方法 + /// + public void Update(IEnumerable items) + { + Items = items; + show = true; + StateHasChanged(); + } + } +} diff --git a/src/admin/Bootstrap.Admin/Pages/Components/CheckboxBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/CheckboxBase.cs index 5cc91f9c..7a9a70a1 100644 --- a/src/admin/Bootstrap.Admin/Pages/Components/CheckboxBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Components/CheckboxBase.cs @@ -32,31 +32,31 @@ namespace Bootstrap.Admin.Pages.Components /// 勾选回调方法 /// [Parameter] - public Action ToggleCallback { get; set; } = new Action((v, c) => { }); + public Action? OnClick { get; set; } /// - /// + /// 组件状态改变回调方法 /// [Parameter] - public Func SetCheckCallback { get; set; } = new Func(item => CheckBoxState.UnChecked); + public Func? SetCheckCallback { get; set; } /// - /// + /// OnParametersSet 方法 /// protected override void OnParametersSet() { - State = SetCheckCallback(Item); + State = SetCheckCallback?.Invoke(Item) ?? CheckBoxState.UnChecked; Checked = State == CheckBoxState.Checked; } /// - /// + /// 获得/设置 选择框状态 /// [Parameter] public CheckBoxState State { get; set; } /// - /// + /// RenderStateCss 方法 /// /// protected string RenderStateCss() @@ -75,5 +75,15 @@ namespace Bootstrap.Admin.Pages.Components } return ret; } + + /// + /// 点击选择框方法 + /// + protected void ToggleClick() + { + Checked = !Checked; + State = Checked ? CheckBoxState.Checked : CheckBoxState.UnChecked; + OnClick?.Invoke(Item, Checked); + } } } diff --git a/src/admin/Bootstrap.Admin/Pages/Components/EditPageBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/EditPageBase.cs index c8205e88..f7ae0333 100644 --- a/src/admin/Bootstrap.Admin/Pages/Components/EditPageBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Components/EditPageBase.cs @@ -160,6 +160,11 @@ namespace Bootstrap.Admin.Pages.Components } } + /// + /// 获得 Table 组件选择项目集合 + /// + public IEnumerable SelectedItems { get { return Table?.SelectedItems ?? new List(); } } + /// /// 分页查询方法 /// diff --git a/src/admin/Bootstrap.Admin/Pages/Components/ModalBase.cs b/src/admin/Bootstrap.Admin/Pages/Components/ModalBase.cs index f96e3721..b7f199a9 100644 --- a/src/admin/Bootstrap.Admin/Pages/Components/ModalBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Components/ModalBase.cs @@ -1,7 +1,7 @@ -using Bootstrap.Admin.Pages.Extensions; -using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using System; +using System.Threading.Tasks; namespace Bootstrap.Admin.Pages.Components { @@ -11,13 +11,13 @@ namespace Bootstrap.Admin.Pages.Components public class ModalBase : ComponentBase { /// - /// + /// 获得/设置 IJSRuntime 实例 /// [Inject] protected IJSRuntime? JSRuntime { get; set; } /// - /// + /// 获得/设置 ModalBody 代码块 /// [Parameter] public RenderFragment? ModalBody { get; set; } @@ -29,13 +29,13 @@ namespace Bootstrap.Admin.Pages.Components public RenderFragment? ModalFooter { get; set; } /// - /// + /// 获得/设置 Id /// [Parameter] public string Id { get; set; } = ""; /// - /// + /// 获得/设置 弹窗标题 /// [Parameter] public string Title { get; set; } = "未设置"; @@ -65,7 +65,7 @@ namespace Bootstrap.Admin.Pages.Components public bool ShowFooter { get; set; } = true; /// - /// + /// OnAfterRender 方法 /// /// protected override void OnAfterRender(bool firstRender) @@ -111,7 +111,7 @@ namespace Bootstrap.Admin.Pages.Components } /// - /// + /// Toggle 弹窗方法 /// public void Toggle() { @@ -125,19 +125,19 @@ namespace Bootstrap.Admin.Pages.Components public enum ModalSize { /// - /// + /// 默认大小 /// Default, /// - /// + /// 小窗口 /// Small, /// - /// + /// 大窗口 /// Large, /// - /// + /// 超大窗口 /// ExtraLarge, } diff --git a/src/admin/Bootstrap.Admin/Pages/Shared/AssignModal.razor b/src/admin/Bootstrap.Admin/Pages/Shared/AssignModal.razor new file mode 100644 index 00000000..7453ae7d --- /dev/null +++ b/src/admin/Bootstrap.Admin/Pages/Shared/AssignModal.razor @@ -0,0 +1,29 @@ +@typeparam TItem +@inherits AssignModalBase + + + + + + @foreach(var item in Items) + { + + @ItemTemplate?.Invoke(item) + + } + + + + + + + 关闭 + + + + 保存 + + + + + diff --git a/src/admin/Bootstrap.Admin/Pages/Shared/Checkbox.razor b/src/admin/Bootstrap.Admin/Pages/Shared/Checkbox.razor index 1bb45d3c..c2c3c5f5 100644 --- a/src/admin/Bootstrap.Admin/Pages/Shared/Checkbox.razor +++ b/src/admin/Bootstrap.Admin/Pages/Shared/Checkbox.razor @@ -1,7 +1,7 @@ @typeparam TItem @inherits CheckboxBase - ToggleCallback(Item, !Checked)"> + diff --git a/src/admin/Bootstrap.Admin/Pages/Shared/Modal.razor b/src/admin/Bootstrap.Admin/Pages/Shared/Modal.razor index 5c4e446e..761fa6b0 100644 --- a/src/admin/Bootstrap.Admin/Pages/Shared/Modal.razor +++ b/src/admin/Bootstrap.Admin/Pages/Shared/Modal.razor @@ -21,7 +21,3 @@ - -@code { - -} diff --git a/src/admin/Bootstrap.Admin/Pages/Shared/Table.razor b/src/admin/Bootstrap.Admin/Pages/Shared/Table.razor index 824482ff..79117ebb 100644 --- a/src/admin/Bootstrap.Admin/Pages/Shared/Table.razor +++ b/src/admin/Bootstrap.Admin/Pages/Shared/Table.razor @@ -54,7 +54,7 @@ @if (ShowCheckbox) { - + } @if (ShowLineNo) { @@ -77,7 +77,7 @@ @if (ShowCheckbox) { - + } @if (ShowLineNo) { @@ -98,7 +98,7 @@ @if (ShowCheckbox) { - + } @if (ShowLineNo) { @@ -125,7 +125,7 @@ @if (ShowCheckbox) { - + } @if (ShowLineNo) { @@ -146,7 +146,7 @@ @if (ShowCheckbox) { - + } @if (ShowLineNo) { diff --git a/src/admin/Bootstrap.Admin/Pages/Views/Admin/Menus.razor b/src/admin/Bootstrap.Admin/Pages/Views/Admin/Menus.razor index 0094ad66..3ca8eebb 100644 --- a/src/admin/Bootstrap.Admin/Pages/Views/Admin/Menus.razor +++ b/src/admin/Bootstrap.Admin/Pages/Views/Admin/Menus.razor @@ -1,6 +1,6 @@ @inherits MenusBase - + @@ -54,3 +54,9 @@ + + + + + + \ No newline at end of file diff --git a/src/admin/Bootstrap.Admin/Pages/Views/Components/MenusBase.cs b/src/admin/Bootstrap.Admin/Pages/Views/Components/MenusBase.cs index 8343e1b0..0f36db24 100644 --- a/src/admin/Bootstrap.Admin/Pages/Views/Components/MenusBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Views/Components/MenusBase.cs @@ -20,6 +20,11 @@ namespace Bootstrap.Admin.Pages.Views.Admin.Components [Inject] protected AuthenticationStateProvider? AuthenticationStateProvider { get; set; } + /// + /// 获得/设置 Modal 实例 + /// + protected AssignModalBase? AssignRoleModal { get; set; } + /// /// 获得/设置 菜单类别 /// @@ -168,11 +173,50 @@ namespace Bootstrap.Admin.Pages.Views.Admin.Components } /// - /// 分配角色方法 + /// 弹窗分配角色方法 /// protected void AssignRoles() { - + // 菜单对角色授权操作 + if (EditPage != null && EditPage.SelectedItems.Any()) + { + var menuId = EditPage.SelectedItems.First().Id; + if (!string.IsNullOrEmpty(menuId)) + { + var roles = RoleHelper.RetrievesByMenuId(menuId); + AssignRoleModal?.Update(roles); + } + } } + + /// + /// 保存授权角色方法 + /// + protected bool SaveRoles(IEnumerable roles) + { + bool ret = false; + if (EditPage != null && EditPage.SelectedItems.Any()) + { + var menuId = EditPage.SelectedItems.First().Id; + var roleIds = roles.Where(r => r.Checked == "checked").Select(r => r.Id ?? ""); + if (!string.IsNullOrEmpty(menuId)) ret = RoleHelper.SavaByMenuId(menuId, roleIds); + } + return ret; + } + + /// + /// 选择框点击时调用此方法 + /// + /// + /// + protected void OnClick(Role item, bool check) + { + item.Checked = check ? "checked" : ""; + } + + /// + /// 设置初始化值 + /// + protected CheckBoxState SetCheck(Role item) => item.Checked == "checked" ? CheckBoxState.Checked : CheckBoxState.UnChecked; } } diff --git a/src/admin/Bootstrap.Admin/Pages/Views/Components/QueryPageBase.cs b/src/admin/Bootstrap.Admin/Pages/Views/Components/QueryPageBase.cs index 381fdcd4..004c44db 100644 --- a/src/admin/Bootstrap.Admin/Pages/Views/Components/QueryPageBase.cs +++ b/src/admin/Bootstrap.Admin/Pages/Views/Components/QueryPageBase.cs @@ -16,6 +16,11 @@ namespace Bootstrap.Admin.Pages.Views.Admin.Components [Parameter] public bool FixedHeader { get; set; } + /// + /// 获得/设置 EditPage 实例 + /// + protected EditPageBase? EditPage { get; set; } + /// /// 获得/设置 TItem 实例 ///