IDynamicEditorHost.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. {
  8. public interface IDynamicEditorHost
  9. {
  10. IEnumerable<DynamicGridColumn> Columns { get; }
  11. /// <summary>
  12. /// Loads into <paramref name="columns"/> all columns that start with the same prefix as <paramref name="column"/>; e.g, when taking a
  13. /// lookup defined for column EntityLink.ID, <paramref name="columns"/> will contain all EntityLink.* except EntityLink.ID.
  14. ///
  15. /// This essentially gives us the other columns we need to load from the database for lookups.
  16. /// </summary>
  17. /// <remarks>
  18. /// This is dumb; we don't want it, because the presence of <see cref="Columns"/> kinda makes it redundant.
  19. /// </remarks>
  20. /// <param name="column">The column to use the prefix of.</param>
  21. /// <param name="columns">The dictionary into which the columns will be loaded, in the form "FieldName": "EntityLink.FieldName"</param>
  22. void LoadColumns(string column, Dictionary<string, string> columns);
  23. /// <summary>
  24. /// In most cases, calls <see cref="LookupFactory.DefineFilter(Type)"/>.
  25. /// </summary>
  26. /// <param name="type"></param>
  27. /// <returns></returns>
  28. IFilter? DefineFilter(Type type);
  29. void LoadLookups(ILookupEditorControl editor);
  30. Document? FindDocument(string filename);
  31. Document? GetDocument(Guid id);
  32. void SaveDocument(Document document);
  33. object?[] GetItems();
  34. BaseEditor? GetEditor(DynamicGridColumn column);
  35. }
  36. public class DefaultDynamicEditorHost<T> : IDynamicEditorHost
  37. where T : BaseObject
  38. {
  39. public virtual T[]? Items { get; set; }
  40. public IEnumerable<DynamicGridColumn> Columns => new DynamicGridColumns().ExtractColumns(typeof(T));
  41. public virtual IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter(Items ?? Enumerable.Empty<object?>(), type);
  42. public Document? FindDocument(string filename)
  43. {
  44. return new Client<Document>().Load(new Filter<Document>(x => x.FileName).IsEqualTo(filename)).FirstOrDefault();
  45. }
  46. public Document? GetDocument(Guid id)
  47. {
  48. Document? doc = null;
  49. if (id != Guid.Empty)
  50. doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  51. return doc;
  52. }
  53. public void SaveDocument(Document document)
  54. {
  55. new Client<Document>().Save(document, "");
  56. }
  57. public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
  58. public object?[] GetItems() => Items ?? Array.Empty<object?>();
  59. public void LoadColumns(string column, Dictionary<string, string> columns)
  60. {
  61. columns.Clear();
  62. var comps = column.Split('.').ToList();
  63. comps.RemoveAt(comps.Count - 1);
  64. var prefix = string.Format("{0}.", string.Join(".", comps));
  65. var cols = Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix));
  66. foreach (var col in cols)
  67. columns[col.ColumnName.Replace(prefix, "")] = col.ColumnName;
  68. }
  69. public void LoadLookups(ILookupEditorControl sender)
  70. {
  71. var editor = sender.EditorDefinition as ILookupEditor;
  72. var colname = sender.ColumnName;
  73. var values = editor.Values(colname, Items);
  74. sender.LoadLookups(values);
  75. }
  76. }
  77. }