JobITPGrid.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.Wpf;
  11. namespace PRSDesktop;
  12. public class JobITPGrid : DynamicDataGrid<JobITP>, IMasterDetailControl<Job,JobITP>, IDataModelSource
  13. {
  14. public Job? Master { get; set; }
  15. public Filter<JobITP> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
  16. ? new Filter<JobITP>(x => x.Job.ID).IsEqualTo(Master.ID)
  17. : new Filter<JobITP>().None();
  18. public JobITPGrid()
  19. {
  20. ActionColumns.Add(new DynamicMenuColumn(BuildMenu) { Position = DynamicActionColumnPosition.Start });
  21. }
  22. protected override void DoReconfigure(DynamicGridOptions options)
  23. {
  24. base.DoReconfigure(options);
  25. options.RecordCount = true;
  26. options.SelectColumns = true;
  27. options.AddRows = true;
  28. options.ImportData = true;
  29. options.FilterRows = true;
  30. options.MultiSelect = true;
  31. options.EditRows = true;
  32. options.DeleteRows = true;
  33. }
  34. public event DataModelUpdateEvent? OnUpdateDataModel;
  35. public string SectionName => "Job ITPs";
  36. public DataModel DataModel(Selection selection)
  37. {
  38. var ids = ExtractValues(x => x.ID, selection).ToArray();
  39. return new BaseDataModel<JobITP>(new Filter<JobITP>(x => x.ID).InList(ids));
  40. }
  41. private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
  42. {
  43. if (row == null) return;
  44. var item = column.AddItem("Digital Forms", PRSDesktop.Resources.kanban, null);
  45. DynamicGridUtils.PopulateFormMenu<JobITPForm, JobITP, JobITPLink>(item, row.Get<JobITP, Guid>(x => x.ID), () => row.ToObject<JobITP>());
  46. }
  47. protected override bool CanCreateItems()
  48. {
  49. return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
  50. }
  51. public override JobITP CreateItem()
  52. {
  53. var result = base.CreateItem();
  54. result.Job.ID = Master?.ID ?? Guid.Empty;
  55. result.Job.Synchronise(Master ?? new Job());
  56. return result;
  57. }
  58. protected override bool CustomiseImportItem(JobITP item)
  59. {
  60. item.Job.ID = Master?.ID ?? Guid.Empty;
  61. item.Job.Synchronise(Master ?? new Job());
  62. return base.CustomiseImportItem(item);
  63. }
  64. protected override void Reload(
  65. Filters<JobITP> criteria, Columns<JobITP> columns, ref SortOrder<JobITP>? sort,
  66. CancellationToken token, Action<CoreTable?, Exception?> action)
  67. {
  68. criteria.Add(MasterDetailFilter);
  69. base.Reload(criteria, columns, ref sort, token, action);
  70. }
  71. protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, JobITP[] items, string name, object value)
  72. {
  73. var result = base.EditorValueChanged(editor, items, name, value);
  74. if (name == "ITPTypeLink.ID")
  75. {
  76. var type = typeof(IDynamicEnclosedListGrid<,>).MakeGenericType(typeof(JobITP), typeof(QAQuestion));
  77. var page = editor.Pages?.FirstOrDefault(x => x.GetType().GetInterface(type.Name) != null);
  78. if (page != null)
  79. {
  80. var template = new Client<DigitalForm>().Load(new Filter<DigitalForm>(x => x.ID).IsEqualTo(value)).FirstOrDefault();
  81. var load = page.GetType().GetMethod("LoadItems");
  82. //load.Invoke(page, new object[] { template != null ? template.Questions : null });
  83. }
  84. }
  85. return result;
  86. }
  87. }