CustomerPanel.xaml.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. {
  14. public class CustomerPanelSettings : IUserConfigurationSettings
  15. {
  16. public CustomerPanelSettings()
  17. {
  18. AnchorWidth = 500F;
  19. View = DynamicSplitPanelView.Combined;
  20. }
  21. public DynamicSplitPanelView View { get; set; }
  22. public double AnchorWidth { get; set; }
  23. }
  24. public partial class CustomerPanel : UserControl, IPanel<Customer>
  25. {
  26. private CustomerPanelSettings settings;
  27. private Guid CustomerID = CoreUtils.FullGuid;
  28. private enum PageIndex
  29. {
  30. Contacts,
  31. Spreadsheets
  32. }
  33. public CustomerPanel()
  34. {
  35. InitializeComponent();
  36. }
  37. public bool IsReady { get; set; }
  38. public event DataModelUpdateEvent OnUpdateDataModel;
  39. public Dictionary<string, object[]> Selected()
  40. {
  41. return new Dictionary<string, object[]> { { typeof(Customer).EntityName(), Customers.SelectedRows } };
  42. }
  43. public void Setup()
  44. {
  45. settings = new UserConfiguration<CustomerPanelSettings>().Load();
  46. SplitPanel.View = settings.View;
  47. SplitPanel.AnchorWidth = settings.AnchorWidth;
  48. Customers.ColumnsTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  49. Customers.Refresh(true, false);
  50. CustomerContacts.Refresh(true, false);
  51. CustomerSpreadsheets.Refresh(true,false);
  52. CustomerActivities.Refresh(true,false);
  53. CustomerProducts.Refresh(true,false);
  54. }
  55. public void Shutdown()
  56. {
  57. }
  58. public void CreateToolbarButtons(IPanelHost host)
  59. {
  60. //host.CreateToolbarButton(new PanelAction() { Caption = "Select Columns", OnExecute = DoSelectColumns, Image = PRSDesktop.Resources.shared });
  61. }
  62. public void Refresh()
  63. {
  64. Customers.Refresh(true, true);
  65. }
  66. public string SectionName => "Customers";
  67. public DataModel DataModel(Selection selection)
  68. {
  69. var ids = Customers.ExtractValues(x => x.ID, selection).ToArray();
  70. return new BaseDataModel<Customer>(new Filter<Customer>(x => x.ID).InList(ids));
  71. }
  72. public void Heartbeat(TimeSpan time)
  73. {
  74. }
  75. public Type DataType()
  76. {
  77. return typeof(Customer);
  78. }
  79. private void RefreshSubPage(ICustomerGrid grid)
  80. {
  81. if (grid == null)
  82. return;
  83. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("CustomerPanel: RefreshSubPage({0})", grid.GetType().EntityName()));
  84. grid.ParentID = CustomerID;
  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<CustomerPanelSettings>().Save(settings);
  98. var newTag = settings.View == DynamicSplitPanelView.Master ? settings.View.ToString() : "";
  99. if (Customers.ColumnsTag != newTag)
  100. {
  101. Customers.ColumnsTag = newTag;
  102. Customers.Refresh(true, true);
  103. }
  104. }
  105. private void CustomerDetails_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 ICustomerGrid);
  112. }
  113. }
  114. private void Customers_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  115. {
  116. var row = e.Rows?.FirstOrDefault();
  117. CustomerID = row != null ? row.Get<Customer, Guid>(c => c.ID) : CoreUtils.FullGuid;
  118. Dispatcher.Invoke(() => { RefreshSubPage(CustomerDetails.SelectedContent as ICustomerGrid); });
  119. }
  120. }
  121. }