DigitalFormsLibrary.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. Variables.LayoutGrid = Layouts;
  97. Layouts.VariableGrid = Variables;
  98. Reports.VariableGrid = Variables;
  99. Reports.LayoutGrid = Layouts;
  100. Forms.Refresh(true, false);
  101. Variables.Refresh(true, false);
  102. Layouts.Refresh(true, false);
  103. Reports.Refresh(true, false);
  104. Documents.Refresh(true, false);
  105. RolesTab.Visibility = Security.CanView<DigitalForm>()
  106. && Security.CanView<Role>()
  107. && Security.CanView<RoleForm>() ? Visibility.Visible : Visibility.Collapsed;
  108. var visibleTabItems = Tab.Items.OfType<DynamicTabItem>().Where(x => x.Visibility == Visibility.Visible).ToList();
  109. if (visibleTabItems.Count <= 1)
  110. {
  111. foreach (var tab in visibleTabItems)
  112. {
  113. tab.Visibility = Visibility.Collapsed;
  114. Tab.SelectedItem = tab;
  115. }
  116. }
  117. }
  118. private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
  119. {
  120. if (e.OriginalSource != Tab) return;
  121. RefreshCurrentTab();
  122. }
  123. private void RefreshCurrentTab()
  124. {
  125. if (Tab.SelectedTab == FormsTab)
  126. {
  127. RefreshFormsTab();
  128. }
  129. else if (Tab.SelectedTab == RolesTab)
  130. {
  131. RefreshRoleTab();
  132. }
  133. }
  134. private void RefreshFormsTab()
  135. {
  136. Forms.Refresh(false, true);
  137. }
  138. private void RefreshRoleTab()
  139. {
  140. DigitalFormRoles.Refresh(true, true);
  141. }
  142. public void Refresh()
  143. {
  144. RefreshCurrentTab();
  145. }
  146. public void Heartbeat(TimeSpan time)
  147. {
  148. }
  149. public void Shutdown(CancelEventArgs? cancel)
  150. {
  151. }
  152. #endregion
  153. private void Forms_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  154. {
  155. var form = e.Rows?.FirstOrDefault()?.ToObject<DigitalForm>();
  156. DetailsEnabled = form is not null;
  157. Layouts.Form = form;
  158. Layouts.Refresh(false, true);
  159. Variables.Form = form;
  160. Variables.Refresh(false, true);
  161. Reports.Form = form;
  162. Reports.Refresh(false, true);
  163. Documents.Item = form;
  164. Documents.Refresh(false, true);
  165. }
  166. private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
  167. {
  168. if (_refreshing || e.OriginalSource != Groups)
  169. return;
  170. string? newCurrentGroup;
  171. if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
  172. {
  173. AllItems = true;
  174. newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null;
  175. }
  176. else
  177. {
  178. AllItems = false;
  179. var selected = (Tuple<string, string?, BitmapImage>)e.AddedItems[0];
  180. newCurrentGroup = selected.Item2;
  181. }
  182. if (!Equals(newCurrentGroup, CurrentGroup))
  183. {
  184. CurrentGroup = newCurrentGroup;
  185. Forms.Refresh(false, false);
  186. }
  187. }
  188. private void Forms_AfterRefresh(object sender, InABox.DynamicGrid.AfterRefreshEventArgs args)
  189. {
  190. if (Forms.MasterData is null)
  191. return;
  192. _refreshing = true;
  193. GroupList = new List<Tuple<string, string?, BitmapImage>>
  194. {
  195. new Tuple<string, string?, BitmapImage>("All Items", null, PRSDesktop.Resources.doc_misc.AsBitmapImage())
  196. };
  197. foreach (var row in Forms.MasterData!.Rows)
  198. {
  199. var appliesTo = row.Get<DigitalForm, string>(x => x.AppliesTo);
  200. var display = appliesTo.NotWhiteSpaceOr("Unassigned");
  201. if (!GroupList.Any(x => x.Item1.Equals(display)))
  202. GroupList.Add(new Tuple<string, string?, BitmapImage>(display, appliesTo, PRSDesktop.Resources.doc_misc.AsBitmapImage()));
  203. }
  204. GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList();
  205. Groups.ItemsSource = GroupList;
  206. Groups.Visibility = Visibility.Visible;
  207. var index = GroupList.FindIndex(x => Equals(x.Item2, CurrentGroup));
  208. if(index == -1)
  209. {
  210. index = 0;
  211. CurrentGroup = null;
  212. Groups.SelectedIndex = 0;
  213. AllItems = true;
  214. _refreshing = false;
  215. Forms.Refresh(false, false);
  216. }
  217. else
  218. {
  219. Groups.SelectedIndex = index;
  220. AllItems = index == 0;
  221. _refreshing = false;
  222. }
  223. }
  224. private void Forms_OnCreateItem(object sender, object item)
  225. {
  226. if (item is not DigitalForm form) return;
  227. form.AppliesTo = CurrentGroup ?? "";
  228. }
  229. private bool Forms_OnFilterRecord(CoreRow row)
  230. {
  231. return AllItems || Equals(row.Get<DigitalForm, string>(x => x.AppliesTo), CurrentGroup);
  232. }
  233. private void DynamicSplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  234. {
  235. DoSaveSettings();
  236. if (FormsSplitPanel.IsDetailVisible())
  237. {
  238. Layouts.Refresh(false, true);
  239. Variables.Refresh(false, true);
  240. }
  241. }
  242. private void OnPropertyChanged([CallerMemberName] string? property = null)
  243. {
  244. PropertyChanged?.Invoke(this, new(property));
  245. }
  246. }