ToolbarElement.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using FastReport.Utils;
  2. using System;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. using static System.String;
  9. namespace FastReport.Web.Toolbar
  10. {
  11. public abstract class ToolbarElement
  12. {
  13. public ToolbarElement()
  14. {
  15. Name = ID.ToString();
  16. }
  17. internal Guid ID { get; } = Guid.NewGuid();
  18. /// <summary>
  19. /// Name of button required to interact with the element list
  20. /// </summary>
  21. public string Name { get; set; }
  22. /// <summary>
  23. /// Defines the visibility of the button on the toolbar
  24. /// </summary>
  25. public bool Enabled { get; set; } = true;
  26. /// <summary>
  27. /// Element styles to be specified in the style tag
  28. /// </summary>
  29. public string ElementCustomStyle { get; set; } = Empty;
  30. /// <summary>
  31. /// Classes to be specified in the class tag for the element
  32. /// </summary>
  33. public string ElementClasses { get; set; } = Empty;
  34. /// <summary>
  35. /// Text to be displayed when the element is hovered over
  36. /// </summary>
  37. public string Title { get; set; } = Empty;
  38. /// <summary>
  39. /// Property by which the buttons in the toolbar will be sorted
  40. /// </summary>
  41. public int Position { get; set; } = -1;
  42. internal abstract string Render(string template_FR);
  43. }
  44. /// <summary>
  45. /// Action that is triggered when the element is clicked
  46. /// </summary>
  47. public interface IClickAction
  48. {
  49. }
  50. /// <summary>
  51. /// Action that is triggered when the element is changed
  52. /// </summary>
  53. public interface IChangeAction
  54. {
  55. }
  56. /// <summary>
  57. /// A script that is called when you click on element
  58. /// </summary>
  59. public class ElementScript : IClickAction, IChangeAction
  60. {
  61. /// <summary>
  62. /// A script that is called when you click on element
  63. /// </summary>
  64. public string Script { get; set; }
  65. }
  66. /// <summary>
  67. /// Action that is called when you click on an item
  68. /// </summary>
  69. public class ElementClickAction : IClickAction
  70. {
  71. /// <summary>
  72. /// Action that is called when you click on an item. WebReport - WebReport for which the button is made
  73. /// </summary>
  74. public Func<WebReport, Task> OnClickAction { get; set; }
  75. }
  76. /// <summary>
  77. /// Action that is called when item changed
  78. /// </summary>
  79. public class ElementChangeAction : IChangeAction
  80. {
  81. /// <summary>
  82. /// Action that is called when item changed. WebReport - WebReport for which the button is mad
  83. /// </summary>
  84. public Func<WebReport, string, Task> OnChangeAction { get; set; }
  85. }
  86. /// <summary>
  87. /// The image that will be displayed in the toolbar
  88. /// </summary>
  89. public class ToolbarElementImage
  90. {
  91. private string defaultButtonImage;
  92. private string base64;
  93. internal string RenderedImage => IsNullOrEmpty(Url) ? $"data:image/svg+xml;base64,{Base64}" : Url;
  94. /// <summary>
  95. /// Toolbar element image in Base64 format
  96. /// </summary>
  97. public string Base64
  98. {
  99. get
  100. {
  101. if (!IsNullOrWhiteSpace(base64))
  102. return base64;
  103. if (IsNullOrWhiteSpace(defaultButtonImage))
  104. LoadDefaultImage();
  105. return defaultButtonImage;
  106. }
  107. set => base64 = value;
  108. }
  109. /// <summary>
  110. /// Link to the image of the toolbar element
  111. /// </summary>
  112. public string Url { get; set; }
  113. private void LoadDefaultImage()
  114. {
  115. var assembly = Assembly.GetExecutingAssembly();
  116. var resourceStream =
  117. assembly.GetManifestResourceStream($"{assembly.GetName().Name}.Resources.default-custom-button.svg");
  118. string resource;
  119. using (var reader = new StreamReader(resourceStream, Encoding.UTF8))
  120. {
  121. resource = reader.ReadToEnd();
  122. }
  123. var svgBytes = System.Text.Encoding.UTF8.GetBytes(resource);
  124. var base64String = Convert.ToBase64String(svgBytes);
  125. defaultButtonImage = base64String;
  126. }
  127. }
  128. }