Skip to content

Commit

Permalink
feat(blazorui): add a new js functoin to BitChart to expose Chartjs i…
Browse files Browse the repository at this point in the history
…nstances #7154 (#7163)
  • Loading branch information
msynk authored Mar 16, 2024
1 parent 224ffd5 commit cb99137
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Bit.BlazorUI;
/// </summary>
internal static class BitChartJsInterop
{
private const string BitChartJsInteropName = "BitChartJsInterop";

internal static JsonSerializerSettings JsonSerializerSettings { get; } = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Expand All @@ -24,12 +22,12 @@ internal static class BitChartJsInterop

public static async ValueTask InitChartJs(this IJSRuntime jsRuntime, IEnumerable<string> scripts)
{
await jsRuntime.InvokeVoidAsync($"{BitChartJsInteropName}.initChartJs", scripts);
await jsRuntime.InvokeVoidAsync($"BitChart.initChartJs", scripts);
}

public static async ValueTask RemoveChart(this IJSRuntime jsRuntime, string canvasId)
{
await jsRuntime.InvokeVoidAsync($"{BitChartJsInteropName}.removeChart", canvasId);
await jsRuntime.InvokeVoidAsync($"BitChart.removeChart", canvasId);
}

/// <summary>
Expand All @@ -42,7 +40,7 @@ public static ValueTask<bool> SetupChart(this IJSRuntime jsRuntime, BitChartConf
{
dynamic dynParam = StripNulls(chartConfig);
Dictionary<string, object> param = ConvertExpandoObjectToDictionary(dynParam);
return jsRuntime.InvokeAsync<bool>($"{BitChartJsInteropName}.setupChart", param);
return jsRuntime.InvokeAsync<bool>($"BitChart.setupChart", param);
}

/// <summary>
Expand Down Expand Up @@ -97,7 +95,7 @@ public static ValueTask<bool> UpdateChart(this IJSRuntime jsRuntime, BitChartCon
{
dynamic dynParam = StripNulls(chartConfig);
Dictionary<string, object> param = ConvertExpandoObjectToDictionary(dynParam);
return jsRuntime.InvokeAsync<bool>($"{BitChartJsInteropName}.updateChart", param);
return jsRuntime.InvokeAsync<bool>($"BitChart.updateChart", param);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ interface DelegateHandler extends IMethodHandler {
ignoredIndices: number[];
}

class BitChart {
class BitChartJsInterop {
private static _initPromise?: Promise<unknown>;
private _bitCharts = new Map<string, Chart>();

public getChartJs(canvasId: string) {
if (!this._bitCharts.has(canvasId)) return null;

return this._bitCharts.get(canvasId)!;
}

public async initChartJs(scripts: string[]) {
if (BitChart._initPromise) {
await BitChart._initPromise;
if (BitChartJsInterop._initPromise) {
await BitChartJsInterop._initPromise;
}

const allScripts = Array.from(document.scripts).map(s => s.src);
Expand All @@ -42,7 +48,7 @@ class BitChart {
reject(e);
}
});
BitChart._initPromise = promise;
BitChartJsInterop._initPromise = promise;
return promise;

async function addScript(url: string) {
Expand Down Expand Up @@ -391,4 +397,5 @@ class BitChart {
return JSON.stringify(object, replacer);
}
}
(window as any)["BitChartJsInterop"] = new BitChart();

(window as any)["BitChart"] = new BitChartJsInterop();

0 comments on commit cb99137

Please sign in to comment.