Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable context menu #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/BlazorDatasheet.Server/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
Custom Editor
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="ContextMenuDisable">
Context menu disable
</NavLink>
</div>
</nav>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@
<AdditionalFiles Include="Icons\Icons.razor" />
</ItemGroup>

<ItemGroup>
<Content Update="Pages\ContextMenuDisabledExample.razor">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@page "/ContextMenuDisable"
@using BlazorDatasheet.Core.Data

<PageTitle>Context menu disable example</PageTitle>

<h1>Context menu disable example</h1>

<p>Below is a sheet where context menu is disabled.</p>

<Datasheet
Sheet="sheet"

CanUserClearCells="false"
CanUserHideCols="false"
CanUserHideRows="false"
CanUserInsertCols="false"
CanUserInsertRows="false"
CanUserMergeRows="false"
CanUserRemoveCols="false"
CanUserRemoveRows="false"
CanUserSort="false"/>

@code{

private Sheet sheet;

protected override void OnInitialized()
{
sheet = new Sheet(1, 1);
}

}
5 changes: 5 additions & 0 deletions src/BlazorDatasheet.Wasm/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
Custom Editor
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="ContextMenuDisable">
Context menu disable
</NavLink>
</div>
</nav>
</div>

Expand Down
38 changes: 21 additions & 17 deletions src/BlazorDatasheet/ColumnHeadingsRenderer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,37 @@
@for (int i = ColStart; i < (ColStart + NCols); i++)
{
var col = i;
if(!Sheet.Columns.IsVisible(i))
if (!Sheet.Columns.IsVisible(i))
continue;

var colHeading = Sheet.Columns.GetHeading(i) ?? RangeText.ColNumberToLetters(col);

<th class="unselectable col-head">
<div style="width: 100%; display: flex; justify-content: start; position: relative; overflow-x: visible;">
<div class="sheet-cell" data-col="@col" data-row="-1" style="display: flex; justify-content: space-between; width: 100%;">
<div>
@HeadingRenderer(new HeadingContext(col, colHeading))
</div>
<div style="display: flex; flex-direction: row;">
<SheetMenuTarget MenuId="@ContextMenus.Selection" MenuData="Sheet">
<CaretButton/>
</SheetMenuTarget>
</div>
@if (HasAnyUserContextMenuActions)
{
<div style="display: flex; flex-direction: row;">
<SheetMenuTarget MenuId="@ContextMenus.Selection" MenuData="Sheet">
<CaretButton />
</SheetMenuTarget>
</div>
}
</div>

<div class="unselectable"
@onmousedown="_ => HandleMouseDownOnResizer(col)"
style="position: absolute;
right: 0;
z-index: 4;
margin-right:-5px;
width: 10px;
height: 100%;
float: left;
display: inline-block;
style="position: absolute;
right: 0;
z-index: 4;
margin-right:-5px;
width: 10px;
height: 100%;
float: left;
display: inline-block;
cursor: col-resize;">
</div>
</div>
Expand All @@ -82,6 +85,7 @@
[Parameter, EditorRequired] public RenderFragment<HeadingContext> HeadingRenderer { get; set; } = default!;
[Parameter] public int NCols { get; set; }
[Parameter] public int ColStart { get; set; }
[Parameter] public bool HasAnyUserContextMenuActions { get; set; } = true;

[CascadingParameter(Name = "DatasheetId")]
public string DatasheetId { get; set; } = default!;
Expand Down Expand Up @@ -191,7 +195,7 @@

private void UpdateIntervals()
{

StateHasChanged();
}

Expand Down
5 changes: 3 additions & 2 deletions src/BlazorDatasheet/Datasheet.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<!-- Register Menus -->
<SelectionMenu
CanUserSort="@CanUserSort"
CanUserClearCells="@CanUserClearCells"
CanUserMergeRange="@CanUserMergeRows"
CanUserInsertCols="@CanUserInsertCols"
CanUserInsertRows="@CanUserInsertRows"
Expand All @@ -32,7 +33,6 @@
CanUserHideCols="@CanUserHideCols"
CanUserHideRows="@CanUserHideRows"/>


<SheetMenuTarget
MenuId="SelectionMenu"
MenuData="Sheet"
Expand Down Expand Up @@ -102,7 +102,8 @@
NCols="@_visualSheet.Viewport.VisibleRegion.Width"
ColStart="@_visualSheet.Viewport.VisibleRegion.Left"
CellLayoutProvider="_cellLayoutProvider"
Sheet="_sheetLocal">
Sheet="_sheetLocal"
HasAnyUserContextMenuActions="HasAnyUserContextMenuActions">
<HeadingRenderer>
@if (ColumnHeaderTemplate != null)
{
Expand Down
10 changes: 10 additions & 0 deletions src/BlazorDatasheet/Datasheet.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public partial class Datasheet : SheetComponentBase
[Parameter]
public bool CanUserSort { get; set; } = true;

/// <summary>
/// If set to true, the user can clear regions using the context menu.
/// </summary>
[Parameter]
public bool CanUserClearCells { get; set; } = true;

/// <summary>
/// If set to true, the user can merge regions using the context menu.
/// </summary>
Expand All @@ -120,6 +126,10 @@ public partial class Datasheet : SheetComponentBase
[Parameter]
public bool CanUserHideCols { get; set; } = true;

private bool HasAnyUserContextMenuActions =>
(CanUserRemoveRows || CanUserRemoveCols || CanUserInsertRows ||
CanUserInsertCols || CanUserSort || CanUserMergeRows ||
CanUserHideRows || CanUserHideCols || CanUserClearCells);

[Parameter] public bool ShowFormulaDependents { get; set; }

Expand Down
131 changes: 73 additions & 58 deletions src/BlazorDatasheet/SelectionMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,92 @@
@using BlazorDatasheet.Core.Data
@using BlazorDatasheet.DataStructures.Geometry
@using BlazorDatasheet.Menu
<SheetMenu MenuId="@ContextMenus.Selection">

@{
var sheet = context as Sheet;
}
@if (HasAnyUserContextMenuActions)
{
<SheetMenu MenuId="@ContextMenus.Selection">

@if (sheet is not null && sheet.Selection.ActiveRegion != null)
{
<MenuSection SectionId="0">
<SheetMenuItem OnClick="() => sheet.Cells.ClearCells(sheet.Selection.Regions)">Clear</SheetMenuItem>
@if (CanUserMergeRange)
{
<SheetMenuItem OnClick="() => sheet.Cells.Merge(sheet.Selection.Regions)">Merge</SheetMenuItem>
}
</MenuSection>

<SheetMenuDivider/>
@{
var sheet = context as Sheet;
}

@if (sheet.Selection.Regions.Count == 1 && sheet.Selection.ActiveRegion is ColumnRegion c)
@if (sheet is not null && sheet.Selection.ActiveRegion != null)
{
@if (CanUserInsertCols)
{
<SheetMenuItem OnClick="() => sheet.Columns.InsertAt(c.Left, c.Width)">Insert column(s) left</SheetMenuItem>
}
<MenuSection SectionId="0">
@if (CanUserClearCells)
{
<SheetMenuItem OnClick="() => sheet.Cells.ClearCells(sheet.Selection.Regions)">Clear</SheetMenuItem>
}
@if (CanUserMergeRange)
{
<SheetMenuItem OnClick="() => sheet.Cells.Merge(sheet.Selection.Regions)">Merge</SheetMenuItem>
}
</MenuSection>

@if (CanUserRemoveCols)
{
<SheetMenuItem OnClick="() => sheet.Columns.RemoveAt(c.Left, c.Width)">Delete column(s)</SheetMenuItem>
}

@if (CanUserHideCols)
{
<SheetMenuItem OnClick="() => sheet.Columns.Hide(c.Left, c.Width)">Hide column(s)</SheetMenuItem>
<SheetMenuItem OnClick="() => sheet.Columns.Unhide(c.Left, c.Width)">Un-hide column(s)</SheetMenuItem>
}
<SheetMenuDivider/>

@if (CanUserInsertCols || CanUserRemoveCols || CanUserHideCols)
@if (sheet.Selection.Regions.Count == 1 && sheet.Selection.ActiveRegion is ColumnRegion c)
{
<SheetMenuDivider/>
}
}
@if (CanUserInsertCols)
{
<SheetMenuItem OnClick="() => sheet.Columns.InsertAt(c.Left, c.Width)">Insert column(s) left</SheetMenuItem>
}

@if (sheet.Selection.Regions.Count == 1 && sheet.Selection.ActiveRegion is RowRegion r)
{
@if (CanUserInsertRows)
{
<SheetMenuItem OnClick="() => sheet.Rows.InsertAt(r.Top, r.Height)">Insert row(s) above</SheetMenuItem>
}
@if (CanUserRemoveCols)
{
<SheetMenuItem OnClick="() => sheet.Columns.RemoveAt(c.Left, c.Width)">Delete column(s)</SheetMenuItem>
}

@if (CanUserHideCols)
{
<SheetMenuItem OnClick="() => sheet.Columns.Hide(c.Left, c.Width)">Hide column(s)</SheetMenuItem>
<SheetMenuItem OnClick="() => sheet.Columns.Unhide(c.Left, c.Width)">Un-hide column(s)</SheetMenuItem>
}

@if (CanUserInsertCols || CanUserRemoveCols || CanUserHideCols)
{
<SheetMenuDivider/>
}

@if (CanUserRemoveRows)
{
<SheetMenuItem OnClick="() => sheet.Rows.RemoveAt(r.Top, r.Height)">Delete row(s)</SheetMenuItem>
}
@if (CanUserHideRows)

@if (sheet.Selection.Regions.Count == 1 && sheet.Selection.ActiveRegion is RowRegion r)
{
<SheetMenuItem OnClick="() => sheet.Rows.Hide(r.Top, r.Height)">Hide row(s)</SheetMenuItem>
<SheetMenuItem OnClick="() => sheet.Rows.Unhide(r.Top, r.Height)">Un-hide row(s)</SheetMenuItem>
@if (CanUserInsertRows)
{
<SheetMenuItem OnClick="() => sheet.Rows.InsertAt(r.Top, r.Height)">Insert row(s) above</SheetMenuItem>
}

@if (CanUserRemoveRows)
{
<SheetMenuItem OnClick="() => sheet.Rows.RemoveAt(r.Top, r.Height)">Delete row(s)</SheetMenuItem>
}

@if (CanUserHideRows)
{
<SheetMenuItem OnClick="() => sheet.Rows.Hide(r.Top, r.Height)">Hide row(s)</SheetMenuItem>
<SheetMenuItem OnClick="() => sheet.Rows.Unhide(r.Top, r.Height)">Un-hide row(s)</SheetMenuItem>
}

@if (CanUserInsertRows || CanUserRemoveRows || CanUserHideRows)
{
<SheetMenuDivider/>
}

}

@if (CanUserInsertRows || CanUserRemoveRows || CanUserHideRows)
@if (CanUserSort)
{
<SheetMenuDivider/>
<SheetSubMenu Label="Sort">
<SheetMenuItem OnClick="() => sheet.SortRange(sheet.Selection.ActiveRegion)">Sort Ascending</SheetMenuItem>
<SheetMenuItem OnClick="() => sheet.SortRange(sheet.Selection.ActiveRegion, new List<ColumnSortOptions>() { new ColumnSortOptions(0, false) })">Sort Descending</SheetMenuItem>
</SheetSubMenu>
}
}

@if (CanUserSort)
{
<SheetSubMenu Label="Sort">
<SheetMenuItem OnClick="() => sheet.SortRange(sheet.Selection.ActiveRegion)">Sort Ascending</SheetMenuItem>
<SheetMenuItem OnClick="() => sheet.SortRange(sheet.Selection.ActiveRegion, new List<ColumnSortOptions>() { new ColumnSortOptions(0, false) })">Sort Descending</SheetMenuItem>
</SheetSubMenu>
}
}

</SheetMenu>
</SheetMenu>
}

@code {

Expand All @@ -88,8 +98,13 @@
[Parameter] public bool CanUserInsertRows { get; set; } = true;
[Parameter] public bool CanUserInsertCols { get; set; } = true;
[Parameter] public bool CanUserSort { get; set; } = true;
[Parameter] public bool CanUserClearCells { get; set; } = true;
[Parameter] public bool CanUserMergeRange { get; set; } = true;
[Parameter] public bool CanUserHideRows { get; set; } = true;
[Parameter] public bool CanUserHideCols { get; set; } = true;

private bool HasAnyUserContextMenuActions =>
(CanUserRemoveRows || CanUserRemoveCols || CanUserInsertRows ||
CanUserInsertCols || CanUserSort || CanUserMergeRange ||
CanUserHideRows || CanUserHideCols || CanUserClearCells);
}