123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- using InABox.Wpf;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace PRSDesktop;
- /// <summary>
- /// Interaction logic for DigitalFormsLibrary.xaml
- /// </summary>
- public partial class DigitalFormsLibrary : UserControl, IPanel<DigitalForm>
- {
- private string? CurrentGroup = null;
- private bool AllItems = true;
- public List<Tuple<string, string?, BitmapImage>> GroupList { get; private set; }
- private bool _refreshing;
- public DigitalFormsLibrary()
- {
- InitializeComponent();
- }
- #region Panel Stuff
- public bool IsReady { get; set; }
- public string SectionName => "Digital Forms Library";
- public event DataModelUpdateEvent? OnUpdateDataModel;
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]> {
- { typeof(DigitalForm).EntityName(), Forms.SelectedRows }
- };
- }
- public DataModel DataModel(Selection selection)
- {
- var ids = Forms.ExtractValues(x => x.ID, selection).ToArray();
- return new AutoDataModel<DigitalForm>(new Filter<DigitalForm>(x => x.ID).InList(ids));
- }
- public void CreateToolbarButtons(IPanelHost host)
- {
- }
- public void Setup()
- {
- Forms.Refresh(true, false);
- RolesTab.Visibility = Security.CanView<DigitalForm>()
- && Security.CanView<Role>()
- && Security.CanView<RoleForm>() ? Visibility.Visible : Visibility.Collapsed;
- var visibleTabItems = Tab.Items.OfType<DynamicTabItem>().Where(x => x.Visibility == Visibility.Visible).ToList();
- if (visibleTabItems.Count <= 1)
- {
- foreach (var tab in visibleTabItems)
- {
- tab.Visibility = Visibility.Collapsed;
- Tab.SelectedItem = tab;
- }
- }
- }
- private void Tab_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (e.OriginalSource != Tab) return;
- RefreshCurrentTab();
- }
- private void RefreshCurrentTab()
- {
- if (Tab.SelectedTab == FormsTab)
- {
- RefreshFormsTab();
- }
- else if (Tab.SelectedTab == RolesTab)
- {
- RefreshRoleTab();
- }
- }
- private void RefreshFormsTab()
- {
- Forms.Refresh(false, true);
- }
- private void RefreshRoleTab()
- {
- DigitalFormRoles.Refresh(true, true);
- }
- public void Refresh()
- {
- RefreshCurrentTab();
- }
- public void Heartbeat(TimeSpan time)
- {
- }
- public void Shutdown(CancelEventArgs? cancel)
- {
- }
- #endregion
- private void Groups_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (_refreshing || e.OriginalSource != Groups)
- return;
- string? newCurrentGroup;
- if (e.AddedItems.Count == 0 || Groups.SelectedIndex == 0)
- {
- AllItems = true;
- newCurrentGroup = GroupList.Any() ? GroupList.First().Item2 : null;
- }
- else
- {
- AllItems = false;
- var selected = (Tuple<string, string?, BitmapImage>)e.AddedItems[0];
- newCurrentGroup = selected.Item2;
- }
- if (!Equals(newCurrentGroup, CurrentGroup))
- {
- CurrentGroup = newCurrentGroup;
- Forms.Refresh(false, false);
- }
- }
- private void Forms_AfterRefresh(object sender, InABox.DynamicGrid.AfterRefreshEventArgs args)
- {
- if (Forms.MasterData is null)
- return;
- _refreshing = true;
- GroupList = new List<Tuple<string, string?, BitmapImage>>
- {
- new Tuple<string, string?, BitmapImage>("All Items", null, PRSDesktop.Resources.doc_misc.AsBitmapImage())
- };
- foreach (var row in Forms.MasterData!.Rows)
- {
- var appliesTo = row.Get<DigitalForm, string>(x => x.AppliesTo);
- var display = appliesTo.NotWhiteSpaceOr("Unassigned");
- if (!GroupList.Any(x => x.Item1.Equals(display)))
- GroupList.Add(new Tuple<string, string?, BitmapImage>(display, appliesTo, PRSDesktop.Resources.doc_misc.AsBitmapImage()));
- }
- GroupList = GroupList.OrderBy(x => (x.Item1 == "All Items" ? "0" : x.Item1 == "Unassigned" ? "1" : "2") + x.Item1).ToList();
- Groups.ItemsSource = GroupList;
- Groups.Visibility = Visibility.Visible;
- var index = GroupList.FindIndex(x => Equals(x.Item2, CurrentGroup));
- if(index == -1)
- {
- index = 0;
- CurrentGroup = null;
- Groups.SelectedIndex = 0;
- AllItems = true;
- _refreshing = false;
- Forms.Refresh(false, false);
- }
- else
- {
- Groups.SelectedIndex = index;
- AllItems = index == 0;
- _refreshing = false;
- }
- }
- private void Forms_OnCreateItem(object sender, object item)
- {
- if (item is not DigitalForm form) return;
- form.AppliesTo = CurrentGroup ?? "";
- }
- private bool Forms_OnFilterRecord(CoreRow row)
- {
- return AllItems || Equals(row.Get<DigitalForm, string>(x => x.AppliesTo), CurrentGroup);
- }
- }
|