SupplierPanel.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Threading;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. namespace PRSDesktop;
  13. public class SupplierPanelSettings : IUserConfigurationSettings
  14. {
  15. public SupplierPanelSettings()
  16. {
  17. AnchorWidth = 800F;
  18. View = DynamicSplitPanelView.Combined;
  19. Anchor = DynamicSplitPanelAnchor.Detail;
  20. }
  21. public DynamicSplitPanelView View { get; set; }
  22. public DynamicSplitPanelAnchor Anchor { get; set; }
  23. public double AnchorWidth { get; set; }
  24. }
  25. /// <summary>
  26. /// Interaction logic for SupplierPanel.xaml
  27. /// </summary>
  28. public partial class SupplierPanel : UserControl, IPanel<Supplier>
  29. {
  30. private SupplierPanelSettings settings;
  31. private Guid SupplierID = CoreUtils.FullGuid;
  32. public SupplierPanel()
  33. {
  34. InitializeComponent();
  35. Products.Visibility = Security.CanView<SupplierProduct>() ? Visibility.Visible : Visibility.Collapsed;
  36. if (Products.Visibility == Visibility.Visible)
  37. SupplierDetails.SelectedItem = Products;
  38. else
  39. SupplierDetails.SelectedItem = Contacts;
  40. }
  41. public event DataModelUpdateEvent OnUpdateDataModel;
  42. public bool IsReady { get; set; }
  43. public void CreateToolbarButtons(IPanelHost host)
  44. {
  45. }
  46. public string SectionName => "Suppliers";
  47. public DataModel DataModel(Selection selection)
  48. {
  49. var ids = Suppliers.ExtractValues(x => x.ID, selection).ToArray();
  50. return new BaseDataModel<Supplier>(new Filter<Supplier>(x => x.ID).InList(ids));
  51. }
  52. public void Heartbeat(TimeSpan time)
  53. {
  54. }
  55. public void Refresh()
  56. {
  57. Suppliers.Refresh(false, true);
  58. RefreshSubPage(SupplierDetails.SelectedContent as ISupplierGrid);
  59. }
  60. public Dictionary<string, object[]> Selected()
  61. {
  62. return new Dictionary<string, object[]> { { typeof(Supplier).EntityName(), Suppliers.SelectedRows } };
  63. }
  64. public void Setup()
  65. {
  66. settings = new UserConfiguration<SupplierPanelSettings>().Load();
  67. SplitPanel.View = settings.View;
  68. SplitPanel.Anchor = settings.Anchor;
  69. SplitPanel.AnchorWidth = settings.AnchorWidth;
  70. Suppliers.ColumnsTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  71. Suppliers.Refresh(true, false);
  72. SupplierContacts.Refresh(true,false);
  73. SupplierProducts.Refresh(true,false);
  74. SupplierSpreadsheets.Refresh(true,false);
  75. }
  76. public void Shutdown()
  77. {
  78. }
  79. private void RefreshSubPage(ISupplierGrid grid)
  80. {
  81. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("SupplierPanel: RefreshSubPage({0})", grid.GetType().EntityName()));
  82. if (grid == null)
  83. return;
  84. grid.ParentID = SupplierID;
  85. var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  86. timer.Tick += (o, e) =>
  87. {
  88. timer.Stop();
  89. grid.Refresh(!grid.IsReady, true);
  90. };
  91. timer.Start();
  92. }
  93. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  94. {
  95. settings.View = SplitPanel.View;
  96. settings.AnchorWidth = SplitPanel.AnchorWidth;
  97. new UserConfiguration<SupplierPanelSettings>().Save(settings);
  98. var newTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  99. if (Suppliers.ColumnsTag != newTag)
  100. {
  101. Suppliers.ColumnsTag = newTag;
  102. Suppliers.Refresh(true, true);
  103. }
  104. }
  105. private void SupplierDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
  106. {
  107. if (e.AddedItems.Count > 0)
  108. {
  109. var t = e.AddedItems[0] as TabItem;
  110. if (t != null && t.Visibility == Visibility.Visible)
  111. RefreshSubPage(t.Content as ISupplierGrid);
  112. }
  113. }
  114. private void Suppliers_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  115. {
  116. var row = e.Rows?.FirstOrDefault();
  117. SupplierID = row != null ? row.Get<Supplier, Guid>(c => c.ID) : CoreUtils.FullGuid;
  118. Dispatcher.Invoke(() => { RefreshSubPage(SupplierDetails.SelectedContent as ISupplierGrid); });
  119. }
  120. }