Skip to content

Commit

Permalink
Merge pull request #201 from danheron/fix/refresh-on-load-styles
Browse files Browse the repository at this point in the history
Refresh calendar when stylesheet loads
  • Loading branch information
danheron authored Jan 9, 2025
2 parents 8d3d7dc + 4de7156 commit c3b1ed7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@code {
public static string __description__ = "Calendar Height Tests";

private bool _fixed;
private bool _fixed = true;

private List<CalendarItem> _events = new()
{
Expand Down
5 changes: 3 additions & 2 deletions Heron.MudCalendar/Components/MonthView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public partial class MonthView : CalendarViewBase, IDisposable
new StyleBuilder()
.AddStyle("grid-template-columns", $"repeat({Columns}, minmax(10px, 1fr))")
.AddStyle("grid-template-rows",
$"repeat({Cells.Count / Columns}, {(100.0 / (Cells.Count / (double)Columns)).ToInvariantString()}%)",
$"repeat({Rows}, {(100.0 / Rows).ToInvariantString()}%)",
Calendar.MonthCellMinHeight == 0)
.Build();

Expand All @@ -50,7 +50,7 @@ public partial class MonthView : CalendarViewBase, IDisposable
protected virtual string ContentGridStyle =>
new StyleBuilder()
.AddStyle("grid-template-rows",
$"repeat({Cells.Count / Columns}, {(100.0 / (Cells.Count / (double)Columns)).ToInvariantString()}%)",
$"repeat({Rows}, {(100.0 / Rows).ToInvariantString()}%)",
Calendar.MonthCellMinHeight == 0)
.Build();

Expand Down Expand Up @@ -96,6 +96,7 @@ protected virtual string DayClassname(CalendarCell calendarCell)
{
return new CssBuilder()
.AddClass("mud-cal-month-cell-title")
.AddClass("mud-cal-month-cell-header")
.AddClass("mud-cal-month-outside", calendarCell.Outside)
.Build();
}
Expand Down
8 changes: 5 additions & 3 deletions Heron.MudCalendar/Components/MudCalendar.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Runtime.Loader;
using Heron.MudCalendar.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
Expand Down Expand Up @@ -105,7 +106,7 @@ public partial class MudCalendar : MudComponentBase
/// </remarks>
[Parameter]
[Category(CategoryTypes.Calendar.Behavior)]
public DayOfWeek? FirstDayOfWeek { get; set; } = null;
public DayOfWeek? FirstDayOfWeek { get; set; }

/// <summary>
/// Gets or sets the first day of the week that the calendar is showing in Work Week View.
Expand All @@ -115,7 +116,7 @@ public partial class MudCalendar : MudComponentBase
/// </remarks>
[Parameter]
[Category(CategoryTypes.Calendar.Behavior)]
public DayOfWeek? FirstDayOfWorkWeek { get; set; } = null;
public DayOfWeek? FirstDayOfWorkWeek { get; set; }

/// <summary>
/// Gets or sets the view (day, week, month) being shown.
Expand Down Expand Up @@ -175,7 +176,7 @@ public partial class MudCalendar : MudComponentBase
/// </remarks>
[Parameter]
[Category(CategoryTypes.Calendar.Behavior)]
public bool ShowWorkWeek { get; set; } = false;
public bool ShowWorkWeek { get; set; }

/// <summary>
/// If false the month view is not shown.
Expand Down Expand Up @@ -616,6 +617,7 @@ private async Task SetLinks()
if (!string.IsNullOrEmpty(head) && head.Contains("Heron.MudCalendar.min.css")) return;

// Add link
_jsService.OnLinkLoaded += (_, _) => Refresh();
await _jsService.AddLink("_content/Heron.MudCalendar/Heron.MudCalendar.min.css", "stylesheet");
}

Expand Down
17 changes: 17 additions & 0 deletions Heron.MudCalendar/Scripts/HeadContent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function getHeadContent() {
return document.head.innerHTML;
}

export function addLink(href, rel, obj) {
const link = document.createElement("link");
link.href = href;
link.rel = rel;

if (obj) {
link.onload = () => {
obj.invokeMethodAsync("LinkLoaded")
}
}

document.head.appendChild(link);
}
14 changes: 12 additions & 2 deletions Heron.MudCalendar/Scripts/ItemPositioner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export function positionMonthItems(element, moreText, fixedHeight) {
container.querySelectorAll(".mud-cal-overflow-message").forEach(function (message) {
message.remove();
});

// Find the height of the header part
let headerHeight = 0;
container.querySelectorAll(".mud-cal-month-cell").forEach(function(cell) {
let height = 0;
cell.querySelectorAll(".mud-cal-month-cell-header").forEach(function(item) {
height += item.clientHeight;
});
if (height > headerHeight) headerHeight = height;
});

const positions = [];
const overlaps = [];
Expand All @@ -18,7 +28,7 @@ export function positionMonthItems(element, moreText, fixedHeight) {
// Create new position object
const position = new ItemPosition();
position.Item = item;
position.Top = 36;
position.Top = headerHeight;
position.Height = item.clientHeight;
position.Left = item.offsetLeft;
position.Width = item.clientWidth;
Expand Down Expand Up @@ -60,7 +70,7 @@ export function positionMonthItems(element, moreText, fixedHeight) {
else
{
// Calculate height of the row
let rowMaxBottom = 0;
let rowMaxBottom = headerHeight;
positions.forEach((position) => {
if (position.Bottom > rowMaxBottom) rowMaxBottom = position.Bottom;
})
Expand Down
12 changes: 0 additions & 12 deletions Heron.MudCalendar/Scripts/Scroll.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
export function scroll(element, top) {
element.scrollTo(0, top);
}

export function getHeadContent() {
return document.head.innerHTML;
}

export function addLink(href, rel) {
const link = document.createElement("link");
link.href = href;
link.rel = rel;

document.head.appendChild(link);
}
16 changes: 15 additions & 1 deletion Heron.MudCalendar/Services/JsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class JsService : IDisposable
{
private readonly Lazy<Task<IJSObjectReference>> _moduleTask;
private readonly CancellationTokenSource _cancellationTokenSource = new();
private DotNetObjectReference<JsService>? _this;

public event EventHandler? OnLinkLoaded;

public JsService(IJSRuntime jsRuntime)
{
Expand All @@ -28,8 +31,19 @@ public async Task<string> GetHeadContent()

public async Task AddLink(string href, string rel)
{
if (OnLinkLoaded != null)
{
_this ??= DotNetObjectReference.Create(this);
}

var module = await _moduleTask.Value;
await module.InvokeVoidAsync("addLink", href, rel);
await module.InvokeVoidAsync("addLink", href, rel, _this);
}

[JSInvokable]
public void LinkLoaded()
{
OnLinkLoaded?.Invoke(this, EventArgs.Empty);
}

public async Task AddDragHandler(string id, int width)
Expand Down

0 comments on commit c3b1ed7

Please sign in to comment.