DigitalFormsLibrary.xaml.cs 5.5 KB

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