DynamicFormLayoutGrid.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls;
  5. using System.Windows.Media.Imaging;
  6. using InABox.Core;
  7. using InABox.DynamicGrid;
  8. using InABox.WPF;
  9. namespace InABox.DynamicGrid
  10. {
  11. public abstract class DynamicFormLayoutGrid<T> : DynamicOneToManyGrid<DigitalForm, DigitalFormLayout> where T : Entity, IRemotable, IPersistent, new()
  12. {
  13. private readonly BitmapImage design = Properties.Resources.design.AsBitmapImage();
  14. public DynamicFormLayoutGrid()
  15. {
  16. Options.AddRange(DynamicGridOption.RecordCount);
  17. ActionColumns.Add(new DynamicImageColumn(DesignImage, DesignClick));
  18. //AddButton("Design", PRSDesktop.Resources.design.AsBitmapImage(), DesignClick);
  19. HiddenColumns.Add(x => x.Layout);
  20. AddButton("Auto Generate", null, AutoGenerate_Click);
  21. AddButton("Duplicate", null, Duplicate_Click);
  22. }
  23. private bool Duplicate_Click(Button btn, CoreRow[] rows)
  24. {
  25. if (!rows.Any()) return false;
  26. SaveItems(rows.Select(x =>
  27. {
  28. var layout = x.ToObject<DigitalFormLayout>();
  29. layout.ID = Guid.Empty;
  30. return layout;
  31. }).ToArray());
  32. return true;
  33. }
  34. private bool AutoGenerate_Click(Button btn, CoreRow[] rows)
  35. {
  36. var menu = new ContextMenu();
  37. menu.AddItem("Desktop Layout", null, AddDesktop_Click);
  38. menu.AddItem("Mobile Layout", null, AddMobile_Click);
  39. menu.IsOpen = true;
  40. return false;
  41. }
  42. private BitmapImage? DesignImage(CoreRow? row)
  43. {
  44. return row != null ? design : null;
  45. }
  46. private void AddMobile_Click()
  47. {
  48. var item = CreateItem();
  49. item.Layout = DFLayout.GenerateAutoMobileLayout(GetVariables()).SaveLayout();
  50. item.Type = DFLayoutType.Mobile;
  51. if (EditItems(new[] { item }))
  52. {
  53. SaveItem(item);
  54. Refresh(false, true);
  55. OnChanged?.Invoke(this);
  56. }
  57. }
  58. private void AddDesktop_Click()
  59. {
  60. var item = CreateItem();
  61. item.Layout = DFLayout.GenerateAutoDesktopLayout(GetVariables()).SaveLayout();
  62. item.Type = DFLayoutType.Desktop;
  63. if (EditItems(new[] { item }))
  64. {
  65. SaveItem(item);
  66. Refresh(false, true);
  67. OnChanged?.Invoke(this);
  68. }
  69. }
  70. private DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>? GetVariableGrid()
  71. => EditorGrid.Pages?.FirstOrDefault(x => x is DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>)
  72. as DynamicOneToManyGrid<DigitalForm, DigitalFormVariable>;
  73. private List<DigitalFormVariable> GetVariables()
  74. => GetVariableGrid()?.Items.ToList() ?? new List<DigitalFormVariable>();
  75. private void Design(DigitalFormLayout layout)
  76. {
  77. var variables = GetVariables();
  78. var newVariables = new List<DigitalFormVariable>();
  79. var form = new DynamicFormDesignWindow
  80. {
  81. Type = layout.Type
  82. };
  83. form.OnCreateVariable += (fieldType) =>
  84. {
  85. if (DynamicVariableUtils.CreateAndEdit(Item, GetVariables(), fieldType, out var variable))
  86. {
  87. newVariables.Add(variable);
  88. return variable;
  89. }
  90. return null;
  91. };
  92. form.LoadLayout(layout, variables);
  93. form.Initialize();
  94. if (form.ShowDialog() == true)
  95. {
  96. layout.Layout = form.SaveLayout();
  97. SaveItem(layout);
  98. var grid = GetVariableGrid();
  99. if (grid is not null)
  100. {
  101. grid.SaveItems(newVariables.ToArray());
  102. grid.Refresh(false, true);
  103. }
  104. }
  105. }
  106. private bool DesignClick(CoreRow? row)
  107. {
  108. if (row == null)
  109. return false;
  110. Design(LoadItem(row));
  111. return false;
  112. }
  113. //public override void SaveItem(DigitalFormLayout item)
  114. //{
  115. // bool bActive = item.Active;
  116. // foreach (var other in Items.Where(x=>(x != item) && (x.Type == item.Type)))
  117. // {
  118. // if (item.Active)
  119. // {
  120. // if (other.Active)
  121. // other.Active = false;
  122. // }
  123. // else
  124. // bActive = bActive || other.Active;
  125. // }
  126. // if (!bActive)
  127. // item.Active = true;
  128. // base.SaveItem(item);
  129. //}
  130. protected override void DoDoubleClick(object sender)
  131. {
  132. DesignClick(SelectedRows.FirstOrDefault());
  133. }
  134. }
  135. }