ToolbarSelect.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace FastReport.Web.Toolbar
  6. {
  7. /// <summary>
  8. /// Element that opens a drop-down list when you hover over it
  9. /// </summary>
  10. public class ToolbarSelect : ToolbarElement
  11. {
  12. /// <summary>
  13. /// The image of the button that appears in the toolbar
  14. /// </summary>
  15. public ToolbarElementImage Image { get; set; } = new ToolbarElementImage();
  16. /// <summary>
  17. /// Contains items that will be displayed in the drop-down list
  18. ///
  19. /// List of ToolbarSelectItems
  20. /// </summary>
  21. public List<ToolbarSelectItem> Items { get; set; } = new List<ToolbarSelectItem>();
  22. internal override string Render(string template_FR)
  23. {
  24. if (!Enabled) return default;
  25. var sb = new StringBuilder();
  26. sb.Append(
  27. $@"<div class=""fr-toolbar-item {template_FR}-toolbar-item {ElementClasses}"" style = ""{ElementCustomStyle}"">
  28. <img src=""{Image.RenderedImage}"" title=""{Title}"" class=""{template_FR}-toolbar-image"">
  29. <div class=""fr-toolbar-dropdown-content {template_FR}-toolbar-dropdown-content"">");
  30. foreach (var item in Items.Where(item => item.Enabled))
  31. sb.Append(item.Render(template_FR));
  32. sb.Append("</div></div>");
  33. return sb.ToString();
  34. }
  35. }
  36. /// <summary>
  37. /// The element that appears in the drop-down list
  38. /// </summary>
  39. public class ToolbarSelectItem
  40. {
  41. public ToolbarSelectItem()
  42. {
  43. Name = ID.ToString();
  44. }
  45. internal Guid ID { get; } = Guid.NewGuid();
  46. /// <summary>
  47. /// Name of toolbar item required to interact with the items list
  48. /// </summary>
  49. public string Name { get; set; }
  50. /// <summary>
  51. /// The inscription displayed in the drop-down list
  52. /// </summary>
  53. public string Title { get; set; }
  54. /// <summary>
  55. /// Defines the visibility of the item in the drop-down list
  56. /// </summary>
  57. public bool Enabled { get; set; } = true;
  58. /// <summary>
  59. /// Action that is triggered when the item is clicked
  60. /// </summary>
  61. public IClickAction OnClickAction { get; set; }
  62. internal string Render(string template_FR)
  63. {
  64. var action = OnClickAction is ElementScript scriptButton
  65. ? scriptButton.Script
  66. : $"{template_FR}.customMethodInvoke('{ID}', this.value)";
  67. return $@"<a onclick=""{action}"">{Title}</a>";
  68. }
  69. }
  70. }