-
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
May 17, 2023
1 parent
52275ca
commit 4e56b34
Showing
8 changed files
with
252 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
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,10 @@ | ||
@using Microsoft.JSInterop; | ||
@inject IJSRuntime JSRuntime | ||
@inherits IXBaseComponent | ||
|
||
<ix-tree | ||
@attributes="UserAttributes" | ||
class="@Class" | ||
style="@Style" | ||
root="@Root" | ||
id="@Id"></ix-tree> |
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,110 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.JSInterop; | ||
using Newtonsoft.Json; | ||
using SiemensIXBlazor.Interops; | ||
using SiemensIXBlazor.Objects; | ||
using System.Dynamic; | ||
using System.Text.Json; | ||
|
||
namespace SiemensIXBlazor.Components.Tree | ||
{ | ||
public partial class Tree | ||
{ | ||
private Dictionary<string, TreeNode>? _treeModel; | ||
private Dictionary<string, TreeContextNode>? _treeContext; | ||
private Lazy<Task<IJSObjectReference>>? moduleTask; | ||
private BaseInterop? _interop; | ||
|
||
[Parameter, EditorRequired] | ||
public string Id { get; set; } = string.Empty; | ||
public Dictionary<string, TreeNode>? TreeModel | ||
{ | ||
get => _treeModel; | ||
set | ||
{ | ||
_treeModel = value; | ||
InitialParameter("setTreeModel", _treeModel); | ||
} | ||
} | ||
public Dictionary<string, TreeContextNode>? TreeContext | ||
{ | ||
get => _treeContext; | ||
set | ||
{ | ||
_treeContext = value; | ||
InitialParameter("setTreeContext", _treeContext); | ||
} | ||
} | ||
[Parameter] | ||
public string? Root { get; set; } | ||
[Parameter] | ||
public EventCallback<Dictionary<string, TreeContextNode>> ContextChangedEvent { get; set; } | ||
[Parameter] | ||
public EventCallback NodeRemovedEvent { get; set; } | ||
[Parameter] | ||
public EventCallback<string> NodeClickedEvent { get; set; } | ||
[Parameter] | ||
public EventCallback<TreeNodeToggledEventResult> NodeToggledEvent { get; set; } | ||
|
||
protected override void OnAfterRender(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
_interop = new(JSRuntime); | ||
|
||
Task.Run(async () => | ||
{ | ||
await _interop.AddEventListener(this, Id, "contextChange", "ContextChanged"); | ||
await _interop.AddEventListener(this, Id, "nodeRemoved", "NodeRemoved"); | ||
await _interop.AddEventListener(this, Id, "nodeClicked", "NodeClicked"); | ||
await _interop.AddEventListener(this, Id, "nodeToggled", "NodeToggled"); | ||
}); | ||
} | ||
} | ||
|
||
private void InitialParameter(string functionName, object param) | ||
{ | ||
|
||
moduleTask = new(() => JSRuntime.InvokeAsync<IJSObjectReference>( | ||
"import", $"./_content/SiemensIXBlazor/js/interops/treeInterop.js").AsTask()); | ||
|
||
Task.Run(async () => | ||
{ | ||
var module = await moduleTask.Value; | ||
if (module != null) | ||
{ | ||
var serObj = JsonConvert.SerializeObject(param); | ||
await module.InvokeVoidAsync(functionName, Id, JsonConvert.SerializeObject(param)); | ||
} | ||
}); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task ContextChanged(JsonElement context) | ||
{ | ||
string jsonDataText = context.GetRawText(); | ||
Dictionary<string, TreeContextNode>? changedContext = JsonConvert.DeserializeObject<Dictionary<string, TreeContextNode>>(jsonDataText); | ||
await ContextChangedEvent.InvokeAsync(changedContext); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task NodeRemoved() | ||
{ | ||
await NodeRemovedEvent.InvokeAsync(); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task NodeClicked(string label) | ||
{ | ||
await NodeClickedEvent.InvokeAsync(label); | ||
} | ||
|
||
[JSInvokable] | ||
public async Task NodeToggled(JsonElement toggledNode) | ||
{ | ||
string jsonDataText = toggledNode.GetRawText(); | ||
TreeNodeToggledEventResult result = JsonConvert.DeserializeObject<TreeNodeToggledEventResult>(jsonDataText); | ||
await NodeToggledEvent.InvokeAsync(result); | ||
} | ||
} | ||
} |
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 TreeContextNode | ||
{ | ||
[JsonProperty("isExpanded")] | ||
public bool IsExpanded { get; set; } | ||
[JsonProperty("isSelected")] | ||
public bool IsSelected { 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,10 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace SiemensIXBlazor.Objects | ||
{ | ||
public class TreeData | ||
{ | ||
[JsonProperty("name")] | ||
public string? Name { 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,16 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace SiemensIXBlazor.Objects | ||
{ | ||
public class TreeNode | ||
{ | ||
[JsonProperty("id")] | ||
public string? Id { get; set; } | ||
[JsonProperty("data")] | ||
public TreeData? Data { get; set; } | ||
[JsonProperty("hasChildren")] | ||
public bool HasChildren { get; set; } | ||
[JsonProperty("children")] | ||
public List<string>? Children { 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 TreeNodeToggledEventResult | ||
{ | ||
[JsonProperty("id")] | ||
public string Id { get; set; } | ||
[JsonProperty("isExpaned")] | ||
public bool IsExpaned { 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,20 @@ | ||
export function setTreeModel(id, treeModel) { | ||
try { | ||
const element = document.getElementById(id); | ||
element.model = JSON.parse(treeModel); | ||
console.log(element, JSON.parse(treeModel)); | ||
} | ||
catch { | ||
|
||
} | ||
} | ||
|
||
export function setTreeContext(id, treeContext) { | ||
try { | ||
const element = document.getElementById(id); | ||
element.context = JSON.parse(treeContext); | ||
} | ||
catch { | ||
|
||
} | ||
} |