IDynamicEditorHost.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. namespace InABox.DynamicGrid;
  7. public interface IDynamicEditorHost
  8. {
  9. /// <summary>
  10. /// Trigger the loading of the lookup values; the canonical implementation calls <see cref="ILookupEditor.Values(string, object[]?)"/>,
  11. /// and calls (either sync/async) <see cref="ILookupEditorControl.LoadLookups(CoreTable)"/> when the values are loaded.
  12. /// </summary>
  13. /// <param name="editor">The editor to load the lookups for.</param>
  14. void LoadLookups(ILookupEditorControl editor);
  15. /// <summary>
  16. /// Get a document for a given filename.
  17. /// </summary>
  18. /// <remarks>
  19. /// The usual implementation will go through the <see cref="Client{TEntity}"/> interface.
  20. /// </remarks>
  21. /// <param name="filename">The filename of the document.</param>
  22. /// <returns>The document with the right filename, or <see langword="null"/> if not found.</returns>
  23. Document? FindDocument(string filename)
  24. {
  25. return new Client<Document>().Load(new Filter<Document>(x => x.FileName).IsEqualTo(filename)).FirstOrDefault();
  26. }
  27. /// <summary>
  28. /// Get a document for a given ID.
  29. /// </summary>
  30. /// <remarks>
  31. /// The usual implementation will go through the <see cref="Client{TEntity}"/> interface.
  32. /// </remarks>
  33. /// <param name="id">The ID of the document.</param>
  34. /// <returns>The document, or <see langword="null"/> if not found.</returns>
  35. Document? GetDocument(Guid id)
  36. {
  37. return new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  38. }
  39. /// <summary>
  40. /// Saves a document.
  41. /// </summary>
  42. /// <remarks>
  43. /// The usual implementation will go through the <see cref="Client{TEntity}"/> interface.
  44. /// </remarks>
  45. /// <param name="document">The document to save.</param>
  46. void SaveDocument(Document document)
  47. {
  48. new Client<Document>().Save(document, "Updated by Editor");
  49. }
  50. /// <summary>
  51. /// Returns a list of the currently edited items; may be an empty array.
  52. ///
  53. /// This should probably always be a <see cref="BaseObject"/>[].
  54. /// </summary>
  55. /// <returns>The items being edited.</returns>
  56. BaseObject[] GetItems();
  57. /// <summary>
  58. /// Return the type of the edited object.
  59. /// </summary>
  60. /// <returns></returns>
  61. Type GetEditorType();
  62. /// <summary>
  63. /// Um... I'm really not sure; achieves the same function as <see cref="DynamicEditorGrid.OnGetEditor"/> - if you know what that does, good job.
  64. /// </summary>
  65. /// <remarks>
  66. /// I think you should be fine to just return <paramref name="column"/>.Editor, as defined by <see cref="DynamicGridColumn"/>.
  67. /// </remarks>
  68. /// <param name="column">The column to get the editor of.</param>
  69. /// <returns>The editor, or <see langword="null"/> if it doesn't exist.</returns>
  70. BaseEditor? GetEditor(DynamicGridColumn column);
  71. }
  72. public class DefaultDynamicEditorHost<T> : IDynamicEditorHost
  73. where T : BaseObject
  74. {
  75. public virtual T[]? Items { get; set; }
  76. public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
  77. public BaseObject[] GetItems() => Items ?? Array.Empty<BaseObject>();
  78. public Type GetEditorType() => typeof(T);
  79. public void LoadLookups(ILookupEditorControl sender)
  80. {
  81. var editor = sender.EditorDefinition as ILookupEditor;
  82. var colname = sender.ColumnName;
  83. var values = editor.Values(typeof(T), colname, Items);
  84. sender.LoadLookups(values);
  85. }
  86. }