DigitalFormsLibrary.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. using InABox.Configuration;
  22. using System.Runtime.CompilerServices;
  23. namespace PRSDesktop;
  24. public class DigitalFormsLibrarySettings : IUserConfigurationSettings
  25. {
  26. public DynamicSplitPanelView View { get; set; }
  27. public double AnchorWidth { get; set; }
  28. public DigitalFormsLibrarySettings()
  29. {
  30. View = DynamicSplitPanelView.Combined;
  31. AnchorWidth = 700;
  32. }
  33. }
  34. /// <summary>
  35. /// Interaction logic for DigitalFormsLibrary.xaml
  36. /// </summary>
  37. public partial class DigitalFormsLibrary : UserControl, IPanel<DigitalForm>, INotifyPropertyChanged
  38. {
  39. private string? CurrentGroup = null;
  40. private bool AllItems = true;
  41. public List<Tuple<string, string?, BitmapImage>> GroupList { get; private set; }
  42. private bool _refreshing;
  43. private DigitalFormsLibrarySettings _settings;
  44. public event PropertyChangedEventHandler? PropertyChanged;
  45. private bool _detailsEnabled;
  46. public bool DetailsEnabled
  47. {
  48. get => _detailsEnabled;
  49. set
  50. {
  51. _detailsEnabled = value;
  52. OnPropertyChanged();
  53. }
  54. }
  55. public DigitalFormsLibrary()
  56. {
  57. InitializeComponent();
  58. Variables.Bind(IsEnabledProperty, this, x => x.DetailsEnabled, mode: BindingMode.OneWay);
  59. Layouts.Bind(IsEnabledProperty, this, x => x.DetailsEnabled);
  60. Reports.Bind(IsEnabledProperty, this, x => x.DetailsEnabled);
  61. Documents.Bind(IsEnabledProperty, this, x => x.DetailsEnabled);
  62. }
  63. private void DoLoadSettings()
  64. {
  65. _settings = new UserConfiguration<DigitalFormsLibrarySettings>().Load();
  66. FormsSplitPanel.View = _settings.View;
  67. FormsSplitPanel.AnchorWidth = _settings.AnchorWidth;
  68. }
  69. private void DoSaveSettings()
  70. {
  71. _settings.View = FormsSplitPanel.View;
  72. _settings.AnchorWidth = FormsSplitPanel.AnchorWidth;
  73. new UserConfiguration<DigitalFormsLibrarySettings>().Save(_settings);
  74. }
  75. #region Panel Stuff
  76. public bool IsReady { get; set; }
  77. public string SectionName => "Digital Forms Library";
  78. public event DataModelUpdateEvent? OnUpdateDataModel;
  79. public Dictionary<string, object[]> Selected()
  80. {
  81. return new Dictionary<string, object[]> {
  82. { typeof(DigitalForm).EntityName(), Forms.SelectedRows }
  83. };
  84. }
  85. public DataModel DataModel(Selection selection)
  86. {
  87. var ids = Forms.ExtractValues(x => x.ID, selection).ToArray();
  88. return new AutoDataModel<DigitalForm>(Filter<DigitalForm>.Where(x => x.ID).InList(ids));
  89. }
  90. public void CreateToolbarButtons(IPanelHost host)
  91. {
  92. }
  93. public void Setup()
  94. {
  95. DoLoadSettings();
  96. Layouts.VariableGrid = Variables;
  97. Reports.VariableGrid = Variables;
  98. Reports.LayoutGrid = Layouts;
  99. Forms.Refresh(true, false);
  100. Variables.Refresh(true, false);
  101. Layouts.Refresh(true, false);
  102. Reports.Refresh(true, false);
  103. Documents.Refresh(true, false);
  104. RolesTab.Visibility = Security.CanView<DigitalForm>()
  105. && Security.CanView<Role>()
  106. && Security.CanView<RoleForm>() ? Visibility.Visible : Visibility.Collapsed;
  107. var visibleTabItems = Tab.Items.OfType<DynamicTabItem>().Where(x => x.Visibility == Visibility.Visible).ToList();
  108. if (visibleTabItems.Count <= 1)
  109. {
  110. foreach (var tab in visibleTabItems)
  111. {
  112. tab.Visibility = Visibility.Collapsed;
  113. Tab.SelectedItem = tab;
  114. }
  115. }
  116. }
  117. private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
  118. {
  119. if (e.OriginalSource != Tab) return;
  120. RefreshCurrentTab();
  121. }
  122. private void RefreshCurrentTab()
  123. {
  124. if (Tab.SelectedTab == FormsTab)
  125. {
  126. RefreshFormsTab();
  127. }
  128. else if (Tab.SelectedTab == RolesTab)
  129. {
  130. RefreshRoleTab();
  131. }
  132. }
  133. private void RefreshFormsTab()
  134. {
  135. Forms.Refresh(false, true);
  136. }
  137. private void RefreshRoleTab()
  138. {
  139. DigitalFormRoles.Refresh(true, true);
  140. }
  141. public void Refresh()
  142. {
  143. RefreshCurrentTab();
  144. }
  145. public void Heartbeat(TimeSpan time)
  146. {
  147. }
  148. public void Shutdown(CancelEventArgs? cancel)
  149. {
  150. }
  151. #endregion
  152. private void Forms_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  153. {
  154. var form = e.Rows?.FirstOrDefault()?.ToObject<DigitalForm>();
  155. DetailsEnabled = form is not null;
  156. Layouts.Form = form;
  157. Layouts.Refresh(false, true);
  158. Variables.Form = form;
  159. Variables.Refresh(false, true);
  160. Reports.Form = form;
  161. Reports.Refresh(false, true);
  162. Documents.Item = form;
  163. Documents.Refresh(false, true);
  164. }
  165. private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
  166. {
  167. if (_refreshing || e.OriginalSource != Groups)
  168. return;
  169. string? newCurrentGroup;
  170. if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
  171. {
  172. AllItems = true;
  173. newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null;
  174. }
  175. else
  176. {
  177. AllItems = false;
  178. var selected = (Tuple<string, string?, BitmapImage>)e.AddedItems[0];
  179. newCurrentGroup = selected.Item2;
  180. }
  181. if (!Equals(newCurrentGroup, CurrentGroup))
  182. {
  183. CurrentGroup = newCurrentGroup;
  184. Forms.Refresh(false, false);
  185. }
  186. }
  187. private void Forms_AfterRefresh(object sender, InABox.DynamicGrid.AfterRefreshEventArgs args)
  188. {
  189. if (Forms.MasterData is null)
  190. return;
  191. _refreshing = true;
  192. GroupList = new List<Tuple<string, string?, BitmapImage>>
  193. {
  194. new Tuple<string, string?, BitmapImage>("All Items", null, PRSDesktop.Resources.doc_misc.AsBitmapImage())
  195. };
  196. foreach (var row in Forms.MasterData!.Rows)
  197. {
  198. var appliesTo = row.Get<DigitalForm, string>(x => x.AppliesTo);
  199. var display = appliesTo.NotWhiteSpaceOr("Unassigned");
  200. if (!GroupList.Any(x => x.Item1.Equals(display)))
  201. GroupList.Add(new Tuple<string, string?, BitmapImage>(display, appliesTo, PRSDesktop.Resources.doc_misc.AsBitmapImage()));
  202. }
  203. GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList();
  204. Groups.ItemsSource = GroupList;
  205. Groups.Visibility = Visibility.Visible;
  206. var index = GroupList.FindIndex(x => Equals(x.Item2, CurrentGroup));
  207. if(index == -1)
  208. {
  209. index = 0;
  210. CurrentGroup = null;
  211. Groups.SelectedIndex = 0;
  212. AllItems = true;
  213. _refreshing = false;
  214. Forms.Refresh(false, false);
  215. }
  216. else
  217. {
  218. Groups.SelectedIndex = index;
  219. AllItems = index == 0;
  220. _refreshing = false;
  221. }
  222. }
  223. private void Forms_OnCreateItem(object sender, object item)
  224. {
  225. if (item is not DigitalForm form) return;
  226. form.AppliesTo = CurrentGroup ?? "";
  227. }
  228. private bool Forms_OnFilterRecord(CoreRow row)
  229. {
  230. return AllItems || Equals(row.Get<DigitalForm, string>(x => x.AppliesTo), CurrentGroup);
  231. }
  232. private void DynamicSplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  233. {
  234. DoSaveSettings();
  235. if (FormsSplitPanel.IsDetailVisible())
  236. {
  237. Layouts.Refresh(false, true);
  238. Variables.Refresh(false, true);
  239. }
  240. }
  241. private void OnPropertyChanged([CallerMemberName] string? property = null)
  242. {
  243. PropertyChanged?.Invoke(this, new(property));
  244. }
  245. }