1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using InABox.Clients;
- using InABox.Core;
- namespace InABox.DynamicGrid
- {
- public interface IDynamicEditorHost
- {
- IEnumerable<DynamicGridColumn> Columns { get; }
- /// <summary>
- /// Loads into <paramref name="columns"/> all columns that start with the same prefix as <paramref name="column"/>; e.g, when taking a
- /// lookup defined for column EntityLink.ID, <paramref name="columns"/> will contain all EntityLink.* except EntityLink.ID.
- ///
- /// This essentially gives us the other columns we need to load from the database for lookups.
- /// </summary>
- /// <remarks>
- /// This is dumb; we don't want it, because the presence of <see cref="Columns"/> kinda makes it redundant.
- /// </remarks>
- /// <param name="column">The column to use the prefix of.</param>
- /// <param name="columns">The dictionary into which the columns will be loaded, in the form "FieldName": "EntityLink.FieldName"</param>
- void LoadColumns(string column, Dictionary<string, string> columns);
- /// <summary>
- /// In most cases, calls <see cref="LookupFactory.DefineFilter(Type)"/>.
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- IFilter? DefineFilter(Type type);
- void LoadLookups(ILookupEditorControl editor);
- Document? FindDocument(string filename);
- Document? GetDocument(Guid id);
- void SaveDocument(Document document);
- object?[] GetItems();
- BaseEditor? GetEditor(DynamicGridColumn column);
- }
- public class DefaultDynamicEditorHost<T> : IDynamicEditorHost
- where T : BaseObject
- {
- public virtual T[]? Items { get; set; }
- public IEnumerable<DynamicGridColumn> Columns => new DynamicGridColumns().ExtractColumns(typeof(T));
- public virtual IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(Items ?? Enumerable.Empty<object?>(), type);
- public Document? FindDocument(string filename)
- {
- return new Client<Document>().Load(new Filter<Document>(x => x.FileName).IsEqualTo(filename)).FirstOrDefault();
- }
- public Document? GetDocument(Guid id)
- {
- Document? doc = null;
- if (id != Guid.Empty)
- doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
- return doc;
- }
- public void SaveDocument(Document document)
- {
- new Client<Document>().Save(document, "");
- }
- public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
- public object?[] GetItems() => Items ?? Array.Empty<object?>();
- public void LoadColumns(string column, Dictionary<string, string> columns)
- {
- columns.Clear();
- var comps = column.Split('.').ToList();
- comps.RemoveAt(comps.Count - 1);
- var prefix = string.Format("{0}.", string.Join(".", comps));
- var cols = Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix));
- foreach (var col in cols)
- columns[col.ColumnName.Replace(prefix, "")] = col.ColumnName;
- }
- public void LoadLookups(ILookupEditorControl sender)
- {
- var editor = sender.EditorDefinition as ILookupEditor;
- var colname = sender.ColumnName;
- var values = editor.Values(colname, Items);
- sender.LoadLookups(values);
- }
- }
- }
|