RenderContext.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Microsoft.AspNetCore.Components.Rendering;
  4. using Microsoft.AspNetCore.Components;
  5. using static FastReport.Web.Blazor.Export.BlazorConstants;
  6. using System.Collections.Generic;
  7. using System.Diagnostics.CodeAnalysis;
  8. namespace FastReport.Web.Blazor.Export
  9. {
  10. interface IRenderContext : IDisposable
  11. {
  12. public void OpenElement(string elementName);
  13. public void CloseElement();
  14. public void AddMarkupContent(string? markupContent);
  15. public void AddContent(string? textContent);
  16. public void AddContent(RenderFragment? fragment);
  17. public void AddContent<TValue>(RenderFragment<TValue>? fragment, TValue value);
  18. public void AddContent(MarkupString markupContent);
  19. //public void AddContent(object? textContent);
  20. //public void AddAttribute(string name);
  21. public void AddAttribute(string name, bool value);
  22. public void AddAttribute(string name, string? value);
  23. public void AddAttribute(string name, MulticastDelegate? value);
  24. //public void AddAttribute(string name, EventCallback value);
  25. //public void AddAttribute<TArgument>(string name, EventCallback<TArgument> value);
  26. public void AddAttribute(string name, object? value);
  27. //public void AddMultipleAttributes(IEnumerable<KeyValuePair<string, object>>? attributes);
  28. //public void SetUpdatesAttributeName(string updatesAttributeName);
  29. public void OpenComponent<[DynamicallyAccessedMembers(LinkerFlags.All)] TComponent>() where TComponent : notnull, IComponent;
  30. //public void OpenComponent(Type componentType);
  31. public void CloseComponent();
  32. public void AddOnClick(OnClickEventArgs click);
  33. }
  34. internal sealed class RenderContext : IRenderContext
  35. {
  36. private int _seq;
  37. private readonly RenderTreeBuilder _builder;
  38. private readonly EventHandler<OnClickEventArgs> _onClick;
  39. public RenderContext(int seq, RenderTreeBuilder builder) : this(builder)
  40. {
  41. _seq = seq;
  42. }
  43. public RenderContext(RenderTreeBuilder builder, EventHandler<OnClickEventArgs> eventHandler)
  44. : this(builder)
  45. {
  46. _onClick = eventHandler;
  47. }
  48. public RenderContext(RenderTreeBuilder builder)
  49. {
  50. _builder = builder;
  51. }
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. private int GetSequence()
  54. {
  55. return _seq++;
  56. }
  57. public void AddMarkupContent(string? markupContent)
  58. {
  59. _builder.AddMarkupContent(GetSequence(), markupContent);
  60. }
  61. public void AddContent(RenderFragment? fragment)
  62. {
  63. _builder.AddContent(GetSequence(), fragment);
  64. }
  65. public void AddContent(MarkupString markupContent)
  66. {
  67. _builder.AddContent(GetSequence(), markupContent);
  68. }
  69. public void AddContent(string? textContent)
  70. {
  71. _builder.AddContent(GetSequence(), textContent);
  72. }
  73. public void AddContent<TValue>(RenderFragment<TValue>? fragment, TValue value)
  74. {
  75. _builder.AddContent(GetSequence(), fragment, value);
  76. }
  77. public void AddAttribute(string name, bool value)
  78. {
  79. _builder.AddAttribute(GetSequence(), name, value);
  80. }
  81. public void AddAttribute(string name, string? value)
  82. {
  83. _builder.AddAttribute(GetSequence(), name, value);
  84. }
  85. public void AddAttribute(string name, object? value)
  86. {
  87. _builder.AddAttribute(GetSequence(), name, value);
  88. }
  89. public void AddAttribute(string name, MulticastDelegate? value)
  90. {
  91. _builder.AddAttribute(GetSequence(), name, value);
  92. }
  93. public void AddOnClick(OnClickEventArgs click)
  94. {
  95. _builder.AddAttribute(GetSequence(), ONCLICK, (Action)(() =>
  96. {
  97. _onClick?.Invoke(null, click);
  98. }));
  99. }
  100. public void AddAction(string actionName, Action<ReportComponentBase> action)
  101. {
  102. _builder.AddAttribute(GetSequence(), actionName, action);
  103. }
  104. public void AddOnChange(OnClickEventArgs click)
  105. {
  106. _builder.AddAttribute(GetSequence(), ONCHANGE, (Action)(() =>
  107. {
  108. _onClick?.Invoke(null, click);
  109. }));
  110. }
  111. public void AddAttribute(string name, EventCallback<OnClickEventArgs> value)
  112. {
  113. _builder.AddAttribute(GetSequence(), name, value);
  114. }
  115. public void OpenElement(string elementName)
  116. {
  117. _builder.OpenElement(GetSequence(), elementName);
  118. }
  119. public void OpenComponent<[DynamicallyAccessedMembers(LinkerFlags.All)] TComponent>() where TComponent : IComponent
  120. {
  121. _builder.OpenComponent<TComponent>(GetSequence());
  122. }
  123. public void CloseElement()
  124. {
  125. _builder.CloseElement();
  126. }
  127. public void CloseComponent()
  128. {
  129. _builder.CloseComponent();
  130. }
  131. public void Dispose()
  132. {
  133. // Does it dispose the global context??
  134. //throw new NotImplementedException();
  135. //_builder.Dispose();
  136. }
  137. }
  138. }