123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using System;
- using System.Runtime.CompilerServices;
- using Microsoft.AspNetCore.Components.Rendering;
- using Microsoft.AspNetCore.Components;
- using static FastReport.Web.Blazor.Export.BlazorConstants;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- namespace FastReport.Web.Blazor.Export
- {
- interface IRenderContext : IDisposable
- {
- public void OpenElement(string elementName);
- public void CloseElement();
- public void AddMarkupContent(string? markupContent);
- public void AddContent(string? textContent);
- public void AddContent(RenderFragment? fragment);
- public void AddContent<TValue>(RenderFragment<TValue>? fragment, TValue value);
- public void AddContent(MarkupString markupContent);
- //public void AddContent(object? textContent);
- //public void AddAttribute(string name);
- public void AddAttribute(string name, bool value);
- public void AddAttribute(string name, string? value);
- public void AddAttribute(string name, MulticastDelegate? value);
- //public void AddAttribute(string name, EventCallback value);
- //public void AddAttribute<TArgument>(string name, EventCallback<TArgument> value);
- public void AddAttribute(string name, object? value);
- //public void AddMultipleAttributes(IEnumerable<KeyValuePair<string, object>>? attributes);
- //public void SetUpdatesAttributeName(string updatesAttributeName);
- public void OpenComponent<[DynamicallyAccessedMembers(LinkerFlags.All)] TComponent>() where TComponent : notnull, IComponent;
- //public void OpenComponent(Type componentType);
- public void CloseComponent();
- public void AddOnClick(OnClickEventArgs click);
- }
- internal sealed class RenderContext : IRenderContext
- {
- private int _seq;
- private readonly RenderTreeBuilder _builder;
- private readonly EventHandler<OnClickEventArgs> _onClick;
- public RenderContext(int seq, RenderTreeBuilder builder) : this(builder)
- {
- _seq = seq;
- }
- public RenderContext(RenderTreeBuilder builder, EventHandler<OnClickEventArgs> eventHandler)
- : this(builder)
- {
- _onClick = eventHandler;
- }
- public RenderContext(RenderTreeBuilder builder)
- {
- _builder = builder;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private int GetSequence()
- {
- return _seq++;
- }
- public void AddMarkupContent(string? markupContent)
- {
- _builder.AddMarkupContent(GetSequence(), markupContent);
- }
- public void AddContent(RenderFragment? fragment)
- {
- _builder.AddContent(GetSequence(), fragment);
- }
- public void AddContent(MarkupString markupContent)
- {
- _builder.AddContent(GetSequence(), markupContent);
- }
- public void AddContent(string? textContent)
- {
- _builder.AddContent(GetSequence(), textContent);
- }
- public void AddContent<TValue>(RenderFragment<TValue>? fragment, TValue value)
- {
- _builder.AddContent(GetSequence(), fragment, value);
- }
- public void AddAttribute(string name, bool value)
- {
- _builder.AddAttribute(GetSequence(), name, value);
- }
- public void AddAttribute(string name, string? value)
- {
- _builder.AddAttribute(GetSequence(), name, value);
- }
- public void AddAttribute(string name, object? value)
- {
- _builder.AddAttribute(GetSequence(), name, value);
- }
- public void AddAttribute(string name, MulticastDelegate? value)
- {
- _builder.AddAttribute(GetSequence(), name, value);
- }
- public void AddOnClick(OnClickEventArgs click)
- {
- _builder.AddAttribute(GetSequence(), ONCLICK, (Action)(() =>
- {
- _onClick?.Invoke(null, click);
- }));
- }
- public void AddAction(string actionName, Action<ReportComponentBase> action)
- {
- _builder.AddAttribute(GetSequence(), actionName, action);
- }
- public void AddOnChange(OnClickEventArgs click)
- {
- _builder.AddAttribute(GetSequence(), ONCHANGE, (Action)(() =>
- {
- _onClick?.Invoke(null, click);
- }));
- }
- public void AddAttribute(string name, EventCallback<OnClickEventArgs> value)
- {
- _builder.AddAttribute(GetSequence(), name, value);
- }
- public void OpenElement(string elementName)
- {
- _builder.OpenElement(GetSequence(), elementName);
- }
- public void OpenComponent<[DynamicallyAccessedMembers(LinkerFlags.All)] TComponent>() where TComponent : IComponent
- {
- _builder.OpenComponent<TComponent>(GetSequence());
- }
- public void CloseElement()
- {
- _builder.CloseElement();
- }
- public void CloseComponent()
- {
- _builder.CloseComponent();
- }
- public void Dispose()
- {
- // Does it dispose the global context??
- //throw new NotImplementedException();
- //_builder.Dispose();
- }
- }
- }
|