Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some issues with serialization. #604

Merged
merged 1 commit into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions src/UniversalDashboard/Models/Dashboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,47 @@ public class Dashboard
{
[JsonProperty("id")]
public Guid Id { get; set;}
public string Title { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonIgnore]
public Theme[] Themes { get; set; }
public string NavBarColor { get; set; }
public string NavBarFontColor { get; set; }
public string BackgroundColor { get; set; }
public string FontColor { get; set; }
public string FontIconStyle { get; set; }
public IEnumerable<Link> NavbarLinks { get; set; }
public string[] Scripts { get; set; }
public string[] Stylesheets { get; set; }
public List<Page> Pages { get; set; } = new List<Page>();
public bool CyclePages { get; set; }
public int CyclePagesInterval { get; set; }
public string Error { get; set; }
public bool Design { get; set; }
public Footer Footer { get; set; }
public Element NavBarLogo { get; set; }
[JsonProperty("navBarColor")]
public string NavBarColor { get; set; }
[JsonProperty("navBarFontColor")]
public string NavBarFontColor { get; set; }
[JsonProperty("backgroundColor")]
public string BackgroundColor { get; set; }
[JsonProperty("fontColor")]
public string FontColor { get; set; }
[JsonProperty("fontIconStyle")]
public string FontIconStyle { get; set; }
[JsonProperty("navbarLinks")]
public IEnumerable<Link> NavbarLinks { get; set; }
[JsonProperty("scripts")]
public string[] Scripts { get; set; }
[JsonProperty("stylesheets")]
public string[] Stylesheets { get; set; }
[JsonProperty("pages")]
public List<Page> Pages { get; set; } = new List<Page>();
[JsonProperty("cyclePages")]
public bool CyclePages { get; set; }
[JsonProperty("cyclePagesInterval")]
public int CyclePagesInterval { get; set; }
[JsonProperty("error")]
public string Error { get; set; }
[JsonProperty("design")]
public bool Design { get; set; }
[JsonProperty("footer")]
public Footer Footer { get; set; }
[JsonProperty("navBarLogo")]
public Element NavBarLogo { get; set; }
[JsonIgnore]
public InitialSessionState EndpointInitialSessionState { get; set; }
public bool Demo { get; set; }
[JsonProperty("geolocation")]
public bool GeoLocation { get; set; }
public string FilterText { get; set; }
[JsonProperty("filterText")]
public string FilterText { get; set; }
[JsonIgnore]
public TimeSpan IdleTimeout { get; set; }
[JsonProperty("navigation")]
Expand Down
8 changes: 7 additions & 1 deletion src/UniversalDashboard/Server/ServerStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.AspNetCore.Mvc.Filters;
using UniversalDashboard.Controllers;
using System.IO;
using UniversalDashboard.Utilities;

namespace UniversalDashboard
{
Expand Down Expand Up @@ -46,14 +47,19 @@ public void ConfigureServices(IServiceCollection services)
services.AddSignalR(hubOptions =>
{
hubOptions.EnableDetailedErrors = true;
}).AddJsonProtocol(x =>
{
x.PayloadSerializerSettings.ContractResolver = new CustomContractResolver();
});
services.AddTransient<StateRequestService>();
services.AddSingleton<IHostedService, ScheduledEndpointManager>();
services.AddTransient<IExecutionService, ExecutionService>();
services.AddCors();
services.AddDirectoryBrowser();
services.AddSingleton(ExecutionService.MemoryCache);
services.AddMvc();
services.AddMvc().AddJsonOptions(x => {
x.SerializerSettings.ContractResolver = new CustomContractResolver();
});

services.AddScoped<IFilterProvider, EncFilterProvider>();

Expand Down
49 changes: 49 additions & 0 deletions src/UniversalDashboard/Utilities/CustomContractResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections;
using System.Management.Automation;
using UniversalDashboard.Models;

namespace UniversalDashboard.Utilities
{
public class CustomContractResolver : DefaultContractResolver
{
protected override JsonConverter ResolveContractConverter(Type objectType)
{
if (objectType.IsAssignableFrom(typeof(GenericComponent)))
{
return new GenericComponentJsonConvert();
}

if (typeof(PSObject).IsAssignableFrom(objectType) || typeof(Hashtable).IsAssignableFrom(objectType))
{
return new PSObjectJsonConvert();
}

return base.ResolveContractConverter(objectType);
}

//protected override JsonObjectContract CreateObjectContract(Type objectType)
//{
// JsonObjectContract contract = base.CreateObjectContract(objectType);
// if (objectType.IsAssignableFrom(typeof(GenericComponent)))
// {
// contract.Converter = new GenericComponentJsonConvert();
// }

// if (typeof(PSObject).IsAssignableFrom(objectType) || typeof(Hashtable).IsAssignableFrom(objectType))
// {
// contract.Converter = new PSObjectJsonConvert();
// }

// return contract;
//}

protected override string ResolvePropertyName(string propertyName)
{
propertyName = base.ResolvePropertyName(propertyName);
return char.ToLowerInvariant(propertyName[0]) + propertyName.Substring(1);
}
}
}
3 changes: 0 additions & 3 deletions src/UniversalDashboard/Utilities/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Security.Cryptography;
using System.Text;

namespace UniversalDashboard.Utilities
{
Expand Down
36 changes: 36 additions & 0 deletions src/UniversalDashboard/Utilities/PSObjectJsonConvert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;

namespace UniversalDashboard.Utilities
{
public class PSObjectJsonConvert : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(PSObject).IsAssignableFrom(objectType) || typeof(Hashtable).IsAssignableFrom(objectType);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return null;
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var dictionary = value.ToDictionary() as Dictionary<string, object>;

writer.WriteStartObject();

foreach (var property in dictionary)
{
writer.WritePropertyName(property.Key);
serializer.Serialize(writer, property.Value);
}

writer.WriteEndObject();
}
}
}
4 changes: 2 additions & 2 deletions src/client/src/app/services/render-service.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ export function internalRenderComponent(component, history) {
}
}

export default function renderComponent(component, history) {
return window.UniversalDashboard.renderComponent(component, history);
export default function renderComponent(component, history, dynamicallyLoaded) {
return window.UniversalDashboard.renderComponent(component, history, dynamicallyLoaded);
}
2 changes: 2 additions & 0 deletions src/client/src/app/services/universal-dashboard-service.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const UniversalDashboardService = {
publish: PubSub.publishSync,
renderComponent: function(component, history, dynamicallyLoaded) {

if (component == null) return <React.Fragment/>;

if (Array.isArray(component)) {
return component.map(x => this.renderComponent(x, history));
}
Expand Down
3 changes: 3 additions & 0 deletions src/client/src/app/ud-dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export default class UdDashboard extends React.Component {
});

connection.on('addElement', (componentId, elements) => {

if (componentId == null) return;

PubSub.publish(componentId, {
type: "addElement",
componentId: componentId,
Expand Down
25 changes: 20 additions & 5 deletions src/client/src/app/ud-input-field.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,26 @@ export default class UdInputField extends React.Component {
}
}

componentDidUpdate() {
if (Materialize && Materialize.updateTextFields)
{
Materialize.updateTextFields();
updateTextField() {
if (this.textField != null) {
let $this = this.textField;
if (
element.value.length > 0 ||
$(element).is(':focus') ||
element.autofocus ||
$this.attr('placeholder') !== null
) {
$this.siblings('label').addClass('active');
} else if (element.validity) {
$this.siblings('label').toggleClass('active', element.validity.badInput === true);
} else {
$this.siblings('label').removeClass('active');
}
}
}

componentDidUpdate() {
this.updateTextField();

if (this.props.type === 'date') {
this.setupDatePicker();
Expand Down Expand Up @@ -172,7 +187,7 @@ export default class UdInputField extends React.Component {
var textfield = null;
if (this.props.debounceTimeout == null) {

textfield = <input id={field.name} name={field.name} type={type} onChange={e => this.onTextFieldChange(field, e) } value={field.value} onBlur={e => this.onValidateField(field, e)} onKeyDown={this.onKeyDown.bind(this)}/>
textfield = <input id={field.name} name={field.name} type={type} onChange={e => this.onTextFieldChange(field, e) } value={field.value} onBlur={e => this.onValidateField(field, e)} onKeyDown={this.onKeyDown.bind(this)} ref={ref => this.textField}/>
} else {
textfield = <DebounceInput id={field.name} name={field.name} onChange={e => this.onTextFieldChange(field, e) } value={field.value} debounceTimeout={this.props.debounceTimeout} onKeyDown={this.onKeyDown.bind(this)}/>
}
Expand Down
1 change: 1 addition & 0 deletions src/client/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = (env) => {
})
],
optimization: {
minimize: false,
splitChunks: {
cacheGroups: {
vendor:{
Expand Down
88 changes: 88 additions & 0 deletions tools/control-laboratory.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Import-Module F:\universal-dashboard\src\output\UniversalDashboard.Community.psd1

$Dashboard = New-UDDashboard -Title "Control Labratory" -Content {
New-UDRow -Columns {
New-UDColumn -Size 3 -Content {
New-UDButton -Text "Load Control Library" -OnClick {
Show-UDMOdal -Content {
New-UDInput -Title "Load Control Library" -SubmitText "Import" -Endpoint {
param(
$controlLibrary
)

$ControlLibrary = Import-Module $controlLibrary -Force -PassThru
$Commands = $controlLibrary.ExportedCommands

Clear-UDElement -Id "controls"

$Collapsible = New-UDCollapsible -Items {
foreach($Command in $Commands.GetEnumerator()) {
New-UDCollapsibleItem -Title $Command.Key -Content {

$CommandInfo = Get-Command $Command.Key

New-UDTabContainer -Tabs {
foreach($parameterSet in $CommandInfo.ParameterSets.GetEnumerator()) {
New-UDTab -Text $parameterSet.Name -Content {
New-UDInput -Title "Create Control" -Content {
$CommonParameters = @("Verbose", "Debug", "ErrorAction", "WarningAction", "InformationAction", "ErrorVariable", "WarningVariable", "InformationVariable", "OutVariable", "OutBuffer", "PipelineVariable")
foreach($parameter in $parameterSet.Parameters) {
if ($CommonParameters -contains $parameter.Name) {
continue
}

New-UDInputField -Name $parameter.Name -Type textbox
}
} -Endpoint {


$arguments = @{}

foreach($parameter in $PSBoundParameters.GetEnumerator()) {
if ($parameter.Value -ne $null) {
$value = $parameter.Value

if ($Value.Contains(",")) {
$value = $Value.Split(",")
}

$arguments[$parameter.Key] = $Value
}
}

Show-UDToast -Message $PSBoundParameters.Count

Clear-UDElement -Id "Container"
Add-UDElement -ParentId "Container" -Content {
. $ArgumentList[0] @arguments
}

} -ArgumentList @($Command.Key, $parameterSet.Name)
}
}
}


}
}
}

Add-UDElement -ParentId "controls" -Content {
$Collapsible
}

} -Content {
New-UDInputField -Name "controlLibrary" -Type textbox
}
}
}
New-UDElement -Tag "div" -Id "controls" -Content {}
}

New-UDColumn -Size 9 -Content {
New-UDElement -Tag "div" -Id "Container" -Content {}
}
}
}

Start-UDDashboard -Dashboard $Dashboard -Port 1234