DigitalFormsLibrary.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. namespace PRSDesktop;
  21. /// <summary>
  22. /// Interaction logic for DigitalFormsLibrary.xaml
  23. /// </summary>
  24. public partial class DigitalFormsLibrary : UserControl, IPanel<DigitalForm>
  25. {
  26. private string? CurrentGroup = null;
  27. private bool AllItems = true;
  28. public List<Tuple<string, string?, BitmapImage>> GroupList { get; private set; }
  29. private bool _refreshing;
  30. public DigitalFormsLibrary()
  31. {
  32. InitializeComponent();
  33. }
  34. #region Panel Stuff
  35. public bool IsReady { get; set; }
  36. public string SectionName => "Digital Forms Library";
  37. public event DataModelUpdateEvent? OnUpdateDataModel;
  38. public Dictionary<string, object[]> Selected()
  39. {
  40. return new Dictionary<string, object[]> {
  41. { typeof(DigitalForm).EntityName(), Forms.SelectedRows }
  42. };
  43. }
  44. public DataModel DataModel(Selection selection)
  45. {
  46. var ids = Forms.ExtractValues(x => x.ID, selection).ToArray();
  47. return new AutoDataModel<DigitalForm>(new Filter<DigitalForm>(x => x.ID).InList(ids));
  48. }
  49. public void CreateToolbarButtons(IPanelHost host)
  50. {
  51. }
  52. public void Setup()
  53. {
  54. Forms.Refresh(true, false);
  55. RolesTab.Visibility = Security.CanView<DigitalForm>()
  56. && Security.CanView<Role>()
  57. && Security.CanView<RoleForm>() ? Visibility.Visible : Visibility.Collapsed;
  58. var visibleTabItems = Tab.Items.OfType<DynamicTabItem>().Where(x => x.Visibility == Visibility.Visible).ToList();
  59. if (visibleTabItems.Count <= 1)
  60. {
  61. foreach (var tab in visibleTabItems)
  62. {
  63. tab.Visibility = Visibility.Collapsed;
  64. Tab.SelectedItem = tab;
  65. }
  66. }
  67. }
  68. private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
  69. {
  70. if (e.OriginalSource != Tab) return;
  71. RefreshCurrentTab();
  72. }
  73. private void RefreshCurrentTab()
  74. {
  75. if (Tab.SelectedTab == FormsTab)
  76. {
  77. RefreshFormsTab();
  78. }
  79. else if (Tab.SelectedTab == RolesTab)
  80. {
  81. RefreshRoleTab();
  82. }
  83. }
  84. private void RefreshFormsTab()
  85. {
  86. Forms.Refresh(false, true);
  87. }
  88. private void RefreshRoleTab()
  89. {
  90. DigitalFormRoles.Refresh(true, true);
  91. }
  92. public void Refresh()
  93. {
  94. RefreshCurrentTab();
  95. }
  96. public void Heartbeat(TimeSpan time)
  97. {
  98. }
  99. public void Shutdown(CancelEventArgs? cancel)
  100. {
  101. }
  102. #endregion
  103. private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
  104. {
  105. if (_refreshing || e.OriginalSource != Groups)
  106. return;
  107. string? newCurrentGroup;
  108. if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
  109. {
  110. AllItems = true;
  111. newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null;
  112. }
  113. else
  114. {
  115. AllItems = false;
  116. var selected = (Tuple<string, string?, BitmapImage>)e.AddedItems[0];
  117. newCurrentGroup = selected.Item2;
  118. }
  119. if (!Equals(newCurrentGroup, CurrentGroup))
  120. {
  121. CurrentGroup = newCurrentGroup;
  122. Forms.Refresh(false, false);
  123. }
  124. }
  125. private void Forms_AfterRefresh(object sender, InABox.DynamicGrid.AfterRefreshEventArgs args)
  126. {
  127. if (Forms.MasterData is null)
  128. return;
  129. _refreshing = true;
  130. GroupList = new List<Tuple<string, string?, BitmapImage>>
  131. {
  132. new Tuple<string, string?, BitmapImage>("All Items", null, PRSDesktop.Resources.doc_misc.AsBitmapImage())
  133. };
  134. foreach (var row in Forms.MasterData!.Rows)
  135. {
  136. var appliesTo = row.Get<DigitalForm, string>(x => x.AppliesTo);
  137. var display = appliesTo.NotWhiteSpaceOr("Unassigned");
  138. if (!GroupList.Any(x => x.Item1.Equals(display)))
  139. GroupList.Add(new Tuple<string, string?, BitmapImage>(display, appliesTo, PRSDesktop.Resources.doc_misc.AsBitmapImage()));
  140. }
  141. GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList();
  142. Groups.ItemsSource = GroupList;
  143. Groups.Visibility = Visibility.Visible;
  144. var index = GroupList.FindIndex(x => Equals(x.Item2, CurrentGroup));
  145. if(index == -1)
  146. {
  147. index = 0;
  148. CurrentGroup = null;
  149. Groups.SelectedIndex = 0;
  150. AllItems = true;
  151. _refreshing = false;
  152. Forms.Refresh(false, false);
  153. }
  154. else
  155. {
  156. Groups.SelectedIndex = index;
  157. AllItems = index == 0;
  158. _refreshing = false;
  159. }
  160. }
  161. private void Forms_OnCreateItem(object sender, object item)
  162. {
  163. if (item is not DigitalForm form) return;
  164. form.AppliesTo = CurrentGroup ?? "";
  165. }
  166. private bool Forms_OnFilterRecord(CoreRow row)
  167. {
  168. return AllItems || Equals(row.Get<DigitalForm, string>(x => x.AppliesTo), CurrentGroup);
  169. }
  170. }