Skip to content

Commit

Permalink
call on size changed when window resizes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimeatstariongroup committed Sep 20, 2023
1 parent f96a6b8 commit 19b8b84
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
15 changes: 12 additions & 3 deletions COMETwebapp/Components/BookEditor/BookEditorColumn.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public partial class BookEditorColumn<TItem>
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
await this.DomDataService.SubscribeToResizeEvent(nameof(OnSizeChanged));
var dotnetReference = DotNetObjectReference.Create(this);
await this.DomDataService.LoadDotNetHelper(dotnetReference);
await this.DomDataService.SubscribeToResizeEvent("OnSizeChanged");
}

/// <summary>
Expand All @@ -169,6 +171,7 @@ protected override async Task OnInitializedAsync()
/// <returns>an asynchronous operation</returns>
private async Task OnSelectedValueChanged(TItem item, int itemIndex)
{
this.SelectedValue = item;
this.firstItemSizeAndPosition = await this.DomDataService.GetElementSizeAndPosition(0, this.CssClass, false);
this.lastItemSizeAndPosition = await this.DomDataService.GetElementSizeAndPosition(this.Items.Count - 1, this.CssClass, false);
this.selectedItemSizeAndPosition = await this.DomDataService.GetElementSizeAndPosition(itemIndex, this.CssClass, true);
Expand Down Expand Up @@ -256,10 +259,16 @@ private string GenerateHorizontalPathPoints()
return $"{x1},{y},{x2},{y}";
}

/// <summary>
/// Method called when the size of the window changes
/// </summary>
/// <returns>an asynchronous operation</returns>
[JSInvokable]
public static async Task OnSizeChanged()
public async Task OnSizeChanged()
{

this.firstItemSizeAndPosition = await this.DomDataService.GetElementSizeAndPosition(0, this.CssClass, false);
this.lastItemSizeAndPosition = await this.DomDataService.GetElementSizeAndPosition(this.Items.Count - 1, this.CssClass, false);
this.selectedItemSizeAndPosition = await this.DomDataService.GetElementSizeAndPosition(0, this.CssClass, true);
}
}
}
4 changes: 2 additions & 2 deletions COMETwebapp/Pages/BookEditor/BookEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ Copyright (c) 2023 RHEA System S.A.

<SingleIterationApplicationTemplate IterationId="@this.RequestedIteration" ShowActiveModel="true">
<Body>
<BookEditorBody/>
<BookEditorBody/>
</Body>
</SingleIterationApplicationTemplate>
</SingleIterationApplicationTemplate>
12 changes: 12 additions & 0 deletions COMETwebapp/Services/Interoperability/DomDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace COMETwebapp.Services.Interoperability
{
using COMETwebapp.Components.BookEditor;
using COMETwebapp.Components.ModelEditor;
using Microsoft.JSInterop;

/// <summary>
Expand All @@ -39,6 +41,16 @@ public DomDataService(IJSRuntime jsRuntime) : base(jsRuntime)
{
}

/// <summary>
/// Set the dotnet helper
/// </summary>
/// <param name="dotNetHelper">the dotnet helper</param>
/// <returns>A <see cref="Task" /></returns>
public async Task LoadDotNetHelper<TItem>(DotNetObjectReference<BookEditorColumn<TItem>> dotNetHelper)
{
await this.JsRuntime.InvokeVoidAsync("setDotNetHelper", dotNetHelper);
}

/// <summary>
/// Gets an element size and position
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions COMETwebapp/Services/Interoperability/IDomDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

namespace COMETwebapp.Services.Interoperability
{
using COMETwebapp.Components.BookEditor;

using Microsoft.JSInterop;

/// <summary>
/// The service used to retrieve several data from the DOM
/// </summary>
Expand All @@ -44,5 +48,12 @@ public interface IDomDataService
/// <param name="callbackMethodName">the callback method name</param>
/// <returns>an asynchronous operation</returns>
Task SubscribeToResizeEvent(string callbackMethodName);

/// <summary>
/// Set the dotnet helper
/// </summary>
/// <param name="dotNetHelper">the dotnet helper</param>
/// <returns>A <see cref="Task" /></returns>
Task LoadDotNetHelper<TItem>(DotNetObjectReference<BookEditorColumn<TItem>> dotNetHelper);
}
}
21 changes: 18 additions & 3 deletions COMETwebapp/wwwroot/Scripts/DomData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function GetElementSizeAndPosition(index, cssSelector, useScroll)
/**
* Sets the dotnet helper
* @param {any} helper
*/
function setDotNetHelper(helper) {
dotNetHelper = helper;
}

function GetElementSizeAndPosition(index, cssSelector, useScroll)
{
var elements = document.getElementsByClassName(cssSelector);
var element = elements[index];
Expand All @@ -24,9 +32,16 @@
return [0, 0, 0, 0];
}

function SubscribeToResizeEvent(callbackMethodName) {
function SubscribeToResizeEvent(callbackMethodName)
{
window.addEventListener("resize", () =>
{
DotNet.invokeMethodAsync('COMETwebapp', callbackMethodName);
try
{
dotNetHelper.invokeMethodAsync(callbackMethodName);
}
catch (error) {
console.log(error);
}
});
}

0 comments on commit 19b8b84

Please sign in to comment.