Skip to content

Commit

Permalink
JingetJsonVisualizer component added
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Aug 8, 2024
1 parent 9056309 commit f4e3a9e
Show file tree
Hide file tree
Showing 13 changed files with 436 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
@using System.Text.Json

@inject IJSRuntime JS;

<pre id="@Id"></pre>

@code {
[Parameter]
public string Id { get; set; } = "json-visualizer-" + Guid.NewGuid().ToString("N");

/// <summary>
/// All nodes are collapsed at html generation. default is false.
/// </summary>
[Parameter] public bool Collapsed { get; set; } = true;

/// <summary>
/// Allow root element to be collapsed. default is true.
/// </summary>
[Parameter] public bool RootCollapsable { get; set; } = false;

/// <summary>
/// All JSON keys are surrounded with double quotation marks ({"foobar": 1} instead of {foobar: 1}). default is false.
/// </summary>
[Parameter] public bool WithQuotes { get; set; } = false;

/// <summary>
/// All values that are valid links will be clickable, if false they will only be strings. default is true.
/// </summary>
[Parameter] public bool WithLinks { get; set; } = true;

/// <summary>
/// Support different libraries for big numbers,
/// if true, display the real number only, false shows object containing big number with all fields instead of number only.
/// </summary>
[Parameter] public bool BigNumbers { get; set; } = false;

DotNetObjectReference<JingetJsonVisualizer>? dotnet;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
dotnet = DotNetObjectReference.Create(this);
}
await base.OnAfterRenderAsync(firstRender);
}

/// <summary>
/// renders the given object in json visualizer
/// </summary>
public async Task Visualize(object data)
{
await Visualize(JsonSerializer.Serialize(data, new JsonSerializerOptions
{
ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve
}));
}

/// <summary>
/// renders the given string in json visualizer
/// </summary>
public async Task Visualize(string data)
{
await JS.InvokeVoidAsync("toJsonVisualizer",
new
{
dotnet,
Id,
data,
Collapsed,
RootCollapsable,
WithQuotes,
WithLinks,
BigNumbers
});
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@using Jinget.Blazor.Components.List
@using Jinget.Blazor.Components.Other
@using MudBlazor
@using Newtonsoft.Json.Linq
@using Jinget.Blazor.Components.Popup
@using Texnomic.Blazor.JsonViewer

@inherits JingetPopupBase

Expand All @@ -24,7 +24,7 @@
@Content
}

<JsonViewer @ref="@JsonViewer"></JsonViewer>
<JingetJsonVisualizer @ref=@JsonVisualizer></JingetJsonVisualizer>
<JingetList ShowTitle=false
Model=@verboseItems>
</JingetList>
Expand All @@ -33,7 +33,7 @@

@code {
JingetModal modal { get; set; } = new JingetModal();
JsonViewer? JsonViewer { get; set; }
JingetJsonVisualizer? JsonVisualizer { get; set; }

string? Title { get; set; }
string? Content { get; set; }
Expand All @@ -58,6 +58,7 @@
Severity theme = Severity.Warning,
bool isFancy = true)
{

this.Title = title;
this.Content = content;
if (verboseItems != null)
Expand All @@ -82,15 +83,15 @@
else
if (Jinget.Core.Utilities.Json.JsonUtility.IsValid(verboseContent))
{
if (JsonViewer != null)
await JsonViewer.Render(verboseContent);
if (JsonVisualizer != null)
await JsonVisualizer.Visualize(verboseContent);
}
else
{
try
{
if (JsonViewer != null)
await JsonViewer.Render(verboseContent);
if (JsonVisualizer != null)
await JsonVisualizer.Visualize(verboseContent);
}
catch { }
verboseItems.Add(new ListItem(verboseContent, "list-group-item list-group-item-danger"));
Expand Down
1 change: 0 additions & 1 deletion 04-Presentation/Jinget.Blazor/Jinget.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<ItemGroup>
<PackageReference Include="MudBlazor" Version="6.19.1" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="Texnomic.Blazor.JsonViewer" Version="0.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
53 changes: 40 additions & 13 deletions 04-Presentation/Jinget.Blazor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Blazor components optimised for RTL languaged such as Farsi, Arabic etc.
- [x] **JingetDropDownList**
- [x] **JingetDropDownListTree**
- [x] **Input**
- [x] **JsonVisualizer**

**Services list:**
- [x] **LocalStorage**
Expand All @@ -31,17 +32,14 @@ You can also use other methods supported by NuGet. Check [Here](https://www.nuge
Register the required services in your `Program.cs` file:

```
builder.Services.AddJingetBlazor();
```

If you need to use `TokenAuthenticationStateProvider` as a provider for handling authentication state(for example in your ASP.NET Core Blazor project),
then you need to pass `addAuthenticationStateProvider=true` to `AddJingetBlazor`.
If you need to use token storage services for storing/retrieving your tokens, then you need to pass `tokenConfigModel` and `addTokenRelatedServices=true` to `AddJingetBlazor`.
If you do not need to use token storage services, but you want to register `TokenConfigModel` then you need to pass `tokenConfigModel` and `addTokenRelatedServices=false` to `AddJingetBlazor`.
If you have no plan to use `Jinget.Blazor` components and you just want to use its services,
then you can pass `addComponents=false` to `AddJingetBlazor` method.
If you have no plan to use `Jinget.Blazor` components and you just want to use its services, then you can pass `addComponents=false` to `AddJingetBlazor` method.


## How to Use Methods:
Expand All @@ -52,7 +50,6 @@ If you want to authenticate the user and manage user's state via `TokenAuthentic
to your login form and authenticate the user:

```
@attribute [AllowAnonymous]
@layout LoginLayout
@page "/login"
Expand Down Expand Up @@ -109,12 +106,6 @@ Install `MudBlazor` and add reference to `MudBlazor.min.js` in your `_Host.razor
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
```

Install `Texnomic.Blazor.JsonViewer` and add reference to `jsonViewer.js` in your `_Host.razor` or `App.razor` files.

```
<script src="_content/Texnomic.Blazor.JsonViewer/scripts/jsonViewer.js"></script>
```

Add reference to `jinget.core.js` in your `_Host.razor` or `App.razor` files.

```
Expand Down Expand Up @@ -761,6 +752,8 @@ public async Task<List<JingetDropDownItemModel>> GetStatusAsync(string token, ob

`JingetTableElement`: Render a JingetTable on the page.(Used in JingetTable component, for more info go to JingetTable component section)

------------

**Jinget DropDownList components**

Add the `JingetDropDownList` to your page and start using it;-)
Expand Down Expand Up @@ -830,6 +823,8 @@ To avoid this problem, you may attach the dropdown to the modal itself by settin

`SetSelectedIndexAsync`: Select item in dropdownlist based on the index in `Items`. Index starts from zero(0).

------------

**Jinget DropDownList Tree components**

Add the `JingetDropDownListTree` to your page and start using it;-)
Expand All @@ -856,6 +851,8 @@ Other Parameters/properties/callbacks and methods for this components are exactl
Except that `JingetDropDownList` uses `JingetDropDownItemModel` as it's data model provider class which contains `Value` and `Text` properties.
But `JingetDropDownListTree` uses `JingetDropDownTreeItemModel` as data model provider which in addition to `JingetDropDownItemModel` properties, also contains `ParentValue` property to construct the tree structure

------------

**Jinget Input**

Add the `JingetInput` to your page and start using it;-)
Expand Down Expand Up @@ -895,11 +892,41 @@ Add the `JingetInput` to your page and start using it;-)

`OnChange`: Fires a callback whenever the selected item changed.

------------

**Jinget JsonVisualizer component**

Add the `JingetJsonVisualizer` to your page and start using it;-)

*Note: that this component is based on [Alexandre Bodelot](https://github.com/abodelot "Alexandre Bodelot") jquery.json-viewer library.* [View on GitHub](htthttps://github.com/abodelot/jquery.json-viewerp:// "View on GitHub")

```
<JingetJsonVisualizer @ref=@JsonVisualizer></JingetJsonVisualizer>
```

***Parameters:***

`Id`: Unique identifier for component in page. This parameter is required.

`Collapsed`: All nodes are collapsed at html generation. default is false.

`RootCollapsable`: Allow root element to be collapsed. default is true.

`WithQuotes`: All JSON keys are surrounded with double quotation marks ({"foobar": 1} instead of {foobar: 1}). default is false.

`WithLinks`: All values that are valid links will be clickable, if false they will only be strings. default is true.

`BigNumbers`: Support different libraries for big numbers, if true, display the real number only,
false shows object containing big number with all fields instead of number only.

***Methods:***

`Visualize`: Renders the given object in json visualizer

------------
**Services**
## How to Use Services:

***LocalStorage/SessionStorage:***
**LocalStorage/SessionStorage:**

If you need to work with `localStorage` then you need to inject `ILocalStorageService` and
if you want to work with `sessionStorage` then you need to inject `ISessionStorageService` to your page or classes.
Expand Down
1 change: 1 addition & 0 deletions 04-Presentation/Jinget.Blazor/wwwroot/css/jinget.core.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
@import url('jinget.select2.css');
@import url('jinget.select2.theme.material.css');
@import url('jinget.custom.css');
@import url('jinget.json.visualizer.css');
17 changes: 0 additions & 17 deletions 04-Presentation/Jinget.Blazor/wwwroot/css/jinget.custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@
margin-right: auto !important;
}


/*.select2-container .select2-selection--single .select2-selection__arrow {
position: absolute !important;
left: 0px !important;
}*/


/*.select2-container .select2-selection--single .select2-selection__rendered {
padding-left: 20px !important;
}*/
/*Select2.js END*/

/*MudBlazor START*/
Expand Down Expand Up @@ -65,10 +55,3 @@
direction: ltr !important;
}
/*MudBlazor END*/

/*JsonViewer START*/
json-viewer {
direction: ltr !important;
overflow-wrap: break-word !important;
}
/*JsonViewer END*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Root element */
.json-document {
padding: 1em 2em;
}

/* Syntax highlighting for JSON objects */
ul.json-dict, ol.json-array {
list-style-type: none;
margin: 0 0 0 1px;
border-left: 1px dotted #ccc;
padding-left: 2em;
}

.json-string {
color: #0B7500;
white-space: break-spaces;
}

.json-literal {
color: #1A01CC;
font-weight: bold;
}

/* Toggle button */
a.json-toggle {
position: relative;
color: inherit;
text-decoration: none;
}

a.json-toggle:focus {
outline: none;
}

a.json-toggle:before {
font-size: 1.1em;
color: #c0c0c0;
content: "\25BC"; /* down arrow */
position: absolute;
display: inline-block;
width: 1em;
text-align: center;
line-height: 1em;
left: -1.2em;
}

a.json-toggle:hover:before {
color: #aaa;
}

a.json-toggle.collapsed:before {
/* Use rotated down arrow, prevents right arrow appearing smaller than down arrow in some browsers */
transform: rotate(-90deg);
}

/* Collapsable placeholder links */
a.json-placeholder {
color: #aaa;
padding: 0 1em;
text-decoration: none;
}

a.json-placeholder:hover {
text-decoration: underline;
}
11 changes: 7 additions & 4 deletions 04-Presentation/Jinget.Blazor/wwwroot/js/jinget.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
loadScript({
url: '_content/Jinget.Blazor/js/jinget.select2.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.jalali.picker.date.js',
url: '_content/Jinget.Blazor/js/jinget.json.visualizer.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.custom.js',
url: '_content/Jinget.Blazor/js/jinget.jalali.picker.date.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.select2ext.js',
url: '_content/Jinget.Blazor/js/jinget.custom.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.select2tree.js'
url: '_content/Jinget.Blazor/js/jinget.select2ext.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.select2tree.js'
})
})
})
})
Expand Down
Loading

0 comments on commit f4e3a9e

Please sign in to comment.