using System; using System.Collections.Generic; using System.Linq; using InABox.Clients; using InABox.Core; namespace InABox.DynamicGrid; public interface IDynamicEditorHost { /// /// Trigger the loading of the lookup values; the canonical implementation calls , /// and calls (either sync/async) when the values are loaded. /// /// The editor to load the lookups for. void LoadLookups(ILookupEditorControl editor); /// /// Get a document for a given filename. /// /// /// The usual implementation will go through the interface. /// /// The filename of the document. /// The document with the right filename, or if not found. Document? FindDocument(string filename) { return new Client().Load(new Filter(x => x.FileName).IsEqualTo(filename)).FirstOrDefault(); } /// /// Get a document for a given ID. /// /// /// The usual implementation will go through the interface. /// /// The ID of the document. /// The document, or if not found. Document? GetDocument(Guid id) { return new Client().Load(new Filter(x => x.ID).IsEqualTo(id)).FirstOrDefault(); } /// /// Saves a document. /// /// /// The usual implementation will go through the interface. /// /// The document to save. void SaveDocument(Document document) { new Client().Save(document, "Updated by Editor"); } /// /// Returns a list of the currently edited items; may be an empty array. /// /// This should probably always be a []. /// /// The items being edited. BaseObject[] GetItems(); /// /// Return the type of the edited object. /// /// Type GetEditorType(); /// /// Um... I'm really not sure; achieves the same function as - if you know what that does, good job. /// /// /// I think you should be fine to just return .Editor, as defined by . /// /// The column to get the editor of. /// The editor, or if it doesn't exist. BaseEditor? GetEditor(DynamicGridColumn column); } public class DefaultDynamicEditorHost : IDynamicEditorHost where T : BaseObject { public virtual T[]? Items { get; set; } public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor(); public BaseObject[] GetItems() => Items ?? Array.Empty(); public Type GetEditorType() => typeof(T); public void LoadLookups(ILookupEditorControl sender) { var editor = sender.EditorDefinition as ILookupEditor; var colname = sender.ColumnName; var values = editor.Values(typeof(T), colname, Items); sender.LoadLookups(values); } }