-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yakali, Yagizhan Necat (ADV D EU TR AP&I PIA1)
committed
Apr 30, 2023
1 parent
f71a9f8
commit c00931e
Showing
7 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
SiemensIXBlazor/Components/CategoryFilter/CategoryFilter.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
@using Microsoft.JSInterop; | ||
@inherits IXBaseComponent | ||
@inject IJSRuntime JSRuntime | ||
|
||
<ix-category-filter | ||
id="@Id" | ||
placeholder="@Placeholder" | ||
hide-icon="@HideIcon" | ||
i-1-8n-plain-text="@I18nPlainText" | ||
icon="@Icon" | ||
label-categories="@LabelCategories" | ||
repeat-categories="@RepeatCategories"></ix-category-filter> | ||
|
112 changes: 112 additions & 0 deletions
112
SiemensIXBlazor/Components/CategoryFilter/CategoryFilter.razor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.JSInterop; | ||
using Newtonsoft.Json; | ||
using SiemensIXBlazor.Interops; | ||
using SiemensIXBlazor.Objects; | ||
using System.Text.Json; | ||
|
||
namespace SiemensIXBlazor.Components.CategoryFilter | ||
{ | ||
public partial class CategoryFilter | ||
{ | ||
private Dictionary<string, Category>? _categories; | ||
private FilterState? _filterState; | ||
private Dictionary<string, string>? _nonSelectableCategories; | ||
private Lazy<Task<IJSObjectReference>> moduleTask; | ||
private BaseInterop _interop; | ||
|
||
[Parameter, EditorRequired] | ||
public string Id { get; set; } = string.Empty; | ||
public Dictionary<string, Category>? Categories | ||
{ | ||
get => _categories; | ||
set | ||
{ | ||
_categories = value; | ||
InitialParameter("setCategories", _categories); | ||
} | ||
} | ||
public FilterState? FilterState | ||
{ | ||
get => _filterState; | ||
set | ||
{ | ||
_filterState = value; | ||
InitialParameter("setFilterState", _filterState); | ||
} | ||
} | ||
[Parameter] | ||
public bool? HideIcon { get; set; } | ||
[Parameter] | ||
public string I18nPlainText { get; set; } = "Filter by text"; | ||
[Parameter] | ||
public string Icon { get; set; } = "search"; | ||
[Parameter] | ||
public string LabelCategories { get; set; } = "Categories"; | ||
public Dictionary<string, string>? NonSelectableCategories | ||
{ | ||
get => _nonSelectableCategories; | ||
set | ||
{ | ||
_nonSelectableCategories = value; | ||
InitialParameter("setNonSelectableCategories", _nonSelectableCategories); | ||
} | ||
} | ||
[Parameter] | ||
public string? Placeholder { get; set; } | ||
[Parameter] | ||
public bool RepeatCategories { get; set; } = true; | ||
[Parameter] | ||
public string[]? Suggestions { get; set; } | ||
[Parameter] | ||
public EventCallback<FilterState> FilterChangedEvent { get; set; } | ||
[Parameter] | ||
public EventCallback<dynamic> InputChangedEvent { get; set; } | ||
|
||
protected override void OnAfterRender(bool firstRender) | ||
{ | ||
if(firstRender) | ||
{ | ||
_interop = new(JSRuntime); | ||
|
||
Task.Run(async () => | ||
{ | ||
await _interop.AddEventListener(this, Id, "filterChanged", "FilterChanged"); | ||
await _interop.AddEventListener(this, Id, "inputChanged", "InputChanged"); | ||
}); | ||
} | ||
} | ||
|
||
[JSInvokable] | ||
public async void FilterChanged(JsonElement filterState) | ||
{ | ||
string jsonDataText = filterState.GetRawText(); | ||
FilterState? state = JsonConvert.DeserializeObject<FilterState>(jsonDataText); | ||
await FilterChangedEvent.InvokeAsync(state); | ||
} | ||
|
||
[JSInvokable] | ||
public async void InputChanged(JsonElement inputState) | ||
{ | ||
string jsonDataText = inputState.GetRawText(); | ||
dynamic? state = JsonConvert.DeserializeObject<dynamic>(jsonDataText); | ||
await InputChangedEvent.InvokeAsync(state); | ||
} | ||
|
||
private void InitialParameter(string functionName, object param) | ||
{ | ||
|
||
moduleTask = new(() => JSRuntime.InvokeAsync<IJSObjectReference>( | ||
"import", $"./_content/SiemensIXBlazor/js/interops/categoryFilterInterop.js").AsTask()); | ||
|
||
Task.Run(async () => | ||
{ | ||
var module = await moduleTask.Value; | ||
if (module != null) | ||
{ | ||
await module.InvokeVoidAsync(functionName, Id, JsonConvert.SerializeObject(param)); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace SiemensIXBlazor.Objects | ||
{ | ||
public class Category | ||
{ | ||
[JsonProperty("label")] | ||
public string? Label { get; set; } | ||
[JsonProperty("options")] | ||
public string[]? Options { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace SiemensIXBlazor.Objects | ||
{ | ||
public class FilterState | ||
{ | ||
[JsonProperty("tokens")] | ||
public string[]? Tokens { get; set; } | ||
[JsonProperty("categories")] | ||
public FilterStateCategory[]? Categories { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace SiemensIXBlazor.Objects | ||
{ | ||
public class FilterStateCategory | ||
{ | ||
[JsonProperty("id")] | ||
public string? Id { get; set; } | ||
[JsonProperty("value")] | ||
public string? Value { get; set; } | ||
[JsonProperty("operator")] | ||
public string? Operator { get; set; } | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
SiemensIXBlazor/wwwroot/js/interops/categoryFilterInterop.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export function setCategories(id, categories) { | ||
try { | ||
const element = document.getElementById(id); | ||
element.categories = JSON.parse(categories); | ||
} | ||
catch { | ||
|
||
} | ||
|
||
} | ||
|
||
export function setFilterState(id, filterState) { | ||
try { | ||
const element = document.getElementById(id); | ||
element.filterState = JSON.parse(filterState); | ||
} | ||
catch { | ||
|
||
} | ||
|
||
} | ||
|
||
export function setNonSelectableCategories(id, nonSelectableCategories) { | ||
try { | ||
const element = document.getElementById(id); | ||
element.nonSelectableCategories = JSON.parse(nonSelectableCategories); | ||
} | ||
catch { | ||
|
||
} | ||
|
||
} |