DynamicDashboardAdditionalTableGrid.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.Wpf.Dashboard.Editor;
  9. internal class CoreColumnEditItem : BaseObject
  10. {
  11. [EditorSequence(1)]
  12. public string ColumnName { get; set; } = "";
  13. [EditorSequence(2)]
  14. [ComboLookupEditor(typeof(PropertyTypeLookups), Visible = Core.Visible.Default)]
  15. public Type DataType { get; set; } = typeof(string);
  16. internal class PropertyTypeLookups : LookupGenerator<object>
  17. {
  18. private static IEnumerable<Type> Types => [
  19. typeof(string),
  20. typeof(int),
  21. typeof(bool),
  22. typeof(DateTime),
  23. typeof(TimeSpan),
  24. typeof(double),
  25. ];
  26. public PropertyTypeLookups(object[] items) : base(items)
  27. {
  28. foreach(var type in Types)
  29. {
  30. AddValue(type, type.Name);
  31. }
  32. }
  33. }
  34. }
  35. internal class DynamicDashboardAdditionalTableEditItem : BaseObject
  36. {
  37. [EditorSequence(1)]
  38. public string Name { get; set; } = "";
  39. [EditorSequence(2)]
  40. [ListEditor]
  41. public List<CoreColumnEditItem> Columns { get; set; } = new();
  42. [EditorSequence(3)]
  43. [ScriptEditor]
  44. public string Script { get; set; } = "";
  45. public DynamicDashboardAdditionalTableEditItem()
  46. {
  47. }
  48. public DynamicDashboardAdditionalTableEditItem(DynamicDashboardAdditionalTable table)
  49. {
  50. Name = table.Key;
  51. Columns = table.Columns.ToList(x => new CoreColumnEditItem
  52. {
  53. DataType = x.DataType,
  54. ColumnName = x.ColumnName
  55. });
  56. Script = table.Script ?? "";
  57. }
  58. public DynamicDashboardAdditionalTable ToTable()
  59. {
  60. return new DynamicDashboardAdditionalTable
  61. {
  62. Key = Name,
  63. Columns = Columns.ToList(x => new CoreColumn
  64. {
  65. ColumnName = x.ColumnName,
  66. DataType = x.DataType
  67. }),
  68. Script = Script.IsNullOrWhiteSpace() ? null : Script
  69. };
  70. }
  71. }
  72. internal class DynamicDashboardAdditionalTableGrid : DynamicItemsListGrid<DynamicDashboardAdditionalTableEditItem>
  73. {
  74. protected override void DoReconfigure(DynamicGridOptions options)
  75. {
  76. base.DoReconfigure(options);
  77. options.AddRows = true;
  78. options.EditRows = true;
  79. options.DeleteRows = true;
  80. }
  81. protected override void CustomiseEditor(IDynamicEditorForm form, DynamicDashboardAdditionalTableEditItem[] items, DynamicGridColumn column, BaseEditor editor)
  82. {
  83. base.CustomiseEditor(form, items, column, editor);
  84. var item = items[0];
  85. if(column.ColumnName == nameof(DynamicDashboardAdditionalTableEditItem.Script) && editor is ScriptEditor scriptEditor)
  86. {
  87. scriptEditor.Type = ScriptEditorType.TemplateEditor;
  88. scriptEditor.OnEditorClicked += () =>
  89. {
  90. var script = item.Script.NotWhiteSpaceOr()
  91. ?? DynamicDashboardAdditionalTable.DefaultScript();
  92. var editor = new ScriptEditorWindow(script, SyntaxLanguage.CSharp);
  93. if (editor.ShowDialog() == true)
  94. {
  95. form.SetEditorValue(column.ColumnName, editor.Script);
  96. item.Script = editor.Script;
  97. }
  98. };
  99. }
  100. }
  101. }