NotificationPanel.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media.Imaging;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.WPF;
  14. using InABox.Wpf;
  15. using System.ComponentModel;
  16. using sun.net.www;
  17. using Syncfusion.Windows.Controls.RichTextBoxAdv;
  18. using static ICSharpCode.AvalonEdit.Document.TextDocumentWeakEventManager;
  19. using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
  20. namespace PRSDesktop;
  21. /// <summary>
  22. /// Interaction logic for NotificationPanel.xaml
  23. /// </summary>
  24. public partial class NotificationPanel : UserControl, IPanel<Notification>
  25. {
  26. private readonly Button Archive;
  27. private readonly Button AttachToJob;
  28. private readonly Button CreateDelivery;
  29. private readonly Button CreateRequi;
  30. private readonly Button CreateSetout;
  31. private readonly Button CreateTask;
  32. private readonly List<Tuple<string, BitmapImage>> folders = new()
  33. {
  34. new("Inbox", PRSDesktop.Resources.download.AsBitmapImage()),
  35. new("Sent", PRSDesktop.Resources.upload.AsBitmapImage()),
  36. new("Archive", PRSDesktop.Resources.box.AsBitmapImage())
  37. };
  38. private readonly Button Forward;
  39. private readonly Employee me = new Client<Employee>().Load(new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid))
  40. .FirstOrDefault();
  41. private readonly DynamicDataGrid<Notification> Notifications;
  42. private readonly Button Reopen;
  43. //Button WriteNew = null;
  44. private readonly Button Reply;
  45. private readonly Button ViewEntity;
  46. private readonly Button ViewForm;
  47. private readonly Button ViewJob;
  48. public NotificationPanel()
  49. {
  50. InitializeComponent();
  51. Notifications = new DynamicDataGrid<Notification>();
  52. Notifications.HiddenColumns.Add(x => x.Sender.ID);
  53. Notifications.HiddenColumns.Add(x => x.Employee.ID);
  54. Notifications.HiddenColumns.Add(x => x.Job.ID);
  55. Notifications.HiddenColumns.Add(x => x.EntityType);
  56. Notifications.HiddenColumns.Add(x => x.EntityID);
  57. //Notifications.HiddenColumns.Add(x => x.Kanban.ID);
  58. //Notifications.HiddenColumns.Add(x => x.Setout.ID);
  59. //Notifications.HiddenColumns.Add(x => x.Requisition.ID);
  60. //Notifications.HiddenColumns.Add(x => x.Delivery.ID);
  61. //Notifications.HiddenColumns.Add(x => x.LeaveRequestLink.ID);
  62. Notifications.HiddenColumns.Add(x => x.Closed);
  63. Notifications.Reconfigure(options => options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.FilterRows, DynamicGridOption.SelectColumns, DynamicGridOption.MultiSelect));
  64. Notifications.SetValue(Grid.RowProperty, 0);
  65. Notifications.SetValue(Grid.ColumnProperty, 1);
  66. Notifications.Margin = new Thickness(0);
  67. Notifications.OnReload += Notifications_OnReload;
  68. Notifications.OnSelectItem += Notifications_OnSelectItem;
  69. Notifications.OnValidate += Notifications_OnValidate;
  70. Notifications.OnCreateItem += Notifications_OnCreateItem;
  71. //WriteNew = CreateButton("Write New", null, WriteNewClick);
  72. //WriteNew.Visibility = Visibility.Visible;
  73. //WriteNew.Margin = new Thickness(WriteNew.Margin.Left, WriteNew.Margin.Top, 10.0F, WriteNew.Margin.Bottom);
  74. Reply = CreateButton("Reply", null, ReplyClick);
  75. Forward = CreateButton("Forward", null, ForwardClick);
  76. ViewForm = CreateButton("View Form", null, ViewFormClick);
  77. ViewEntity = CreateButton("View Item", null, ViewEntityClick);
  78. AttachToJob = CreateButton("Attach To Job", null, AttachToJobClick);
  79. ViewJob = CreateButton("View Job", null, ViewJobClick);
  80. CreateTask = CreateButton("Create Task", null, CreateTaskClick);
  81. CreateSetout = CreateButton("Create Setout", null, CreateSetoutClick);
  82. CreateRequi = CreateButton("Create Requi", null, CreateRequiClick);
  83. CreateDelivery = CreateButton("Create Delivery", null, CreateDeliveryClick);
  84. Archive = CreateButton("Archive", null, ArchiveClick);
  85. Archive.Margin = new Thickness(10.0F, Archive.Margin.Top, Archive.Margin.Right, Archive.Margin.Bottom);
  86. Reopen = CreateButton("Reopen", null, ReopenClick);
  87. Reopen.Margin = new Thickness(10.0F, Reopen.Margin.Top, Reopen.Margin.Right, Reopen.Margin.Bottom);
  88. Layout.Children.Add(Notifications);
  89. Folders.ItemsSource = folders;
  90. }
  91. private void Notifications_OnCreateItem(object sender, object item)
  92. {
  93. if (item is not Notification notification)
  94. return;
  95. notification.Sender.ID = me.ID;
  96. }
  97. private void Notifications_OnValidate(object sender, Notification[] items, List<string> errors)
  98. {
  99. foreach(var item in items)
  100. {
  101. if(!item.Sender.IsValid())
  102. {
  103. errors.Add("[Sender] may not be blank!");
  104. }
  105. if(!item.Employee.IsValid())
  106. {
  107. errors.Add("[Employee] may not be blank!");
  108. }
  109. }
  110. }
  111. public bool IsReady { get; set; }
  112. public event DataModelUpdateEvent? OnUpdateDataModel;
  113. private Button CreateButton(string caption, BitmapImage? umage, DynamicGridButtonClickEvent action)
  114. {
  115. var result = Notifications.AddButton(caption, umage, action);
  116. result.Width = 100.0F;
  117. result.Visibility = Visibility.Collapsed;
  118. return result;
  119. }
  120. private void Notifications_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  121. {
  122. var singlerow = e.Rows?.Length == 1;
  123. //WriteNew.Visibility = (Folders.SelectedIndex == 0) ? Visibility.Visible : Visibility.Collapsed;
  124. var row = e.Rows?.FirstOrDefault();
  125. if (row != null)
  126. {
  127. var hasjob = row.IsEntityLinkValid<Notification, JobLink>(x => x.Job);
  128. var entitytype = row.Get<Notification, string>(x => x.EntityType);
  129. NotificationUtils.MakeVisible(Reply, 0, 5, singlerow, Folders.SelectedIndex == 0 || Folders.SelectedIndex == 2);
  130. NotificationUtils.MakeVisible(Forward, 5, 10, singlerow);
  131. NotificationUtils.MakeVisible(ViewForm, 0, 5, singlerow, NotificationUtils.IsDigitalForm(entitytype));
  132. NotificationUtils.MakeVisible(ViewEntity, 0, 10, singlerow, !string.IsNullOrWhiteSpace(entitytype));
  133. ViewEntity.Content = string.Format("View {0}", NotificationUtils.GetEntityType(entitytype)?.GetCaption());
  134. NotificationUtils.MakeVisible(AttachToJob, 0, 10, singlerow, !hasjob);
  135. NotificationUtils.MakeVisible(ViewJob, 0, 10, singlerow, hasjob, !string.Equals(entitytype, typeof(Job).EntityName()));
  136. var create = NotificationUtils.MakeVisible(CreateTask, 0, 5, singlerow, !string.Equals(entitytype, typeof(Kanban).EntityName()));
  137. create = NotificationUtils.MakeVisible(CreateSetout, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Setout).EntityName())) ||
  138. create;
  139. create =
  140. NotificationUtils.MakeVisible(CreateRequi, 0, 5, singlerow, hasjob, !string.Equals(entitytype, typeof(Requisition).EntityName())) ||
  141. create;
  142. create = NotificationUtils.MakeVisible(CreateDelivery, 0, 5, singlerow, !string.Equals(entitytype, typeof(Delivery).EntityName())) ||
  143. create;
  144. NotificationUtils.MakeVisible(Archive, create ? 10 : 0, 5, Folders.SelectedIndex == 0);
  145. NotificationUtils.MakeVisible(Reopen, create ? 10 : 0, 5, Folders.SelectedIndex == 2);
  146. new Client<Notification>().Query(
  147. new Filter<Notification>(x => x.ID).IsEqualTo(row.Get<Notification, Guid>(x => x.ID)),
  148. new Columns<Notification>(x => x.Description),
  149. null,
  150. (table, error) =>
  151. {
  152. var drow = table?.Rows.FirstOrDefault();
  153. var desc = drow?.Get<Notification, string>(x => x.Description)?.Replace("background:NoColor;", "") ?? "";
  154. var ms = new MemoryStream(Encoding.ASCII.GetBytes(desc));
  155. Dispatcher.Invoke(() =>
  156. {
  157. Editor.Load(ms, FormatType.Html);
  158. //cursor.Dispose();
  159. });
  160. }
  161. );
  162. }
  163. else
  164. {
  165. NotificationUtils.MakeVisible(Reply, 0, 0, false);
  166. NotificationUtils.MakeVisible(Forward, 0, 0, false);
  167. NotificationUtils.MakeVisible(ViewForm, 0, 0, false);
  168. NotificationUtils.MakeVisible(ViewEntity, 0, 0, false);
  169. NotificationUtils.MakeVisible(AttachToJob, 0, 0, false);
  170. NotificationUtils.MakeVisible(ViewJob, 0, 0, false);
  171. NotificationUtils.MakeVisible(CreateTask, 0, 0, false);
  172. NotificationUtils.MakeVisible(CreateSetout, 0, 0, false);
  173. NotificationUtils.MakeVisible(CreateRequi, 0, 0, false);
  174. NotificationUtils.MakeVisible(CreateDelivery, 0, 0, false);
  175. NotificationUtils.MakeVisible(Archive, 0, 0, false);
  176. NotificationUtils.MakeVisible(Reopen, 0, 0, false);
  177. var ms = new MemoryStream(Encoding.ASCII.GetBytes(""));
  178. Editor.Load(ms, FormatType.Html);
  179. }
  180. }
  181. public void AddNotification(Notification notification)
  182. {
  183. Dispatcher.Invoke(() =>
  184. {
  185. if (IsInbox)
  186. {
  187. //Notifications.AddRow(notification);
  188. Notifications.Refresh(false, true);
  189. }
  190. });
  191. }
  192. private bool IsInbox => Folders.SelectedIndex == 0;
  193. private bool IsOutbox => Folders.SelectedIndex == 1;
  194. private bool IsArchive => Folders.SelectedIndex == 2;
  195. private void Notifications_OnReload(object sender, Filters<Notification> criteria, Columns<Notification> columns,
  196. ref SortOrder<Notification>? sortby)
  197. {
  198. var filter = new Filter<Notification>(x => x.ID).IsEqualTo(Guid.Empty);
  199. if (me != null)
  200. {
  201. if (IsInbox)
  202. filter = new Filter<Notification>(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsEqualTo(DateTime.MinValue);
  203. else if (IsOutbox)
  204. filter = new Filter<Notification>(x => x.Sender.ID).IsEqualTo(me.ID);
  205. else if (IsArchive)
  206. filter = new Filter<Notification>(x => x.Employee.ID).IsEqualTo(me.ID).And(x => x.Closed).IsNotEqualTo(DateTime.MinValue);
  207. }
  208. criteria.Add(filter);
  209. sortby = new SortOrder<Notification>(x => x.Created, SortDirection.Descending);
  210. }
  211. private void Folders_SelectionChanged(object sender, SelectionChangedEventArgs e)
  212. {
  213. if (IsReady)
  214. using (new WaitCursor())
  215. {
  216. Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;
  217. Notifications.Refresh(true, true);
  218. }
  219. }
  220. #region IPanel<T> stuff
  221. public void CreateToolbarButtons(IPanelHost host)
  222. {
  223. // host.CreatePanelAction(new PanelAction() { Caption = "Send Notification", Image = PRSDesktop.Resources.email, OnExecute = SendNotificationClick });
  224. }
  225. public void Setup()
  226. {
  227. Folders.SelectedIndex = 0;
  228. Notifications.ColumnsTag = folders[Folders.SelectedIndex].Item1;
  229. Notifications.Refresh(true, false);
  230. }
  231. public void Shutdown(CancelEventArgs? cancel)
  232. {
  233. }
  234. public void Refresh()
  235. {
  236. Notifications.Refresh(false, true);
  237. }
  238. public string SectionName => "Notifications";
  239. public DataModel DataModel(Selection selection)
  240. {
  241. var ids = Notifications.ExtractValues(c => c.ID, selection).ToArray();
  242. return new BaseDataModel<Notification>(new Filter<Notification>(x => x.ID).InList(ids));
  243. }
  244. public Dictionary<Type, CoreTable> DataEnvironment()
  245. {
  246. return new Dictionary<Type, CoreTable>
  247. {
  248. { typeof(Notification), Notifications.Data }
  249. };
  250. }
  251. public Dictionary<string, object[]> Selected()
  252. {
  253. return new Dictionary<string, object[]>();
  254. }
  255. #endregion
  256. #region Actions
  257. private Guid[] SelectedIDs => Notifications.ExtractValues(x => x.ID, Selection.Selected).ToArray();
  258. private bool ViewFormClick(Button sender, CoreRow[] rows)
  259. {
  260. return NotificationUtils.ViewForm(SelectedIDs);
  261. }
  262. private bool ViewEntityClick(Button sender, CoreRow[] rows)
  263. {
  264. return NotificationUtils.ViewEntity(SelectedIDs);
  265. }
  266. private bool WriteNewClick(Button sender, CoreRow[] rows)
  267. {
  268. var form = new NotificationForm();
  269. return form.ShowDialog() == true;
  270. }
  271. private bool ReplyClick(Button sender, CoreRow[] rows)
  272. {
  273. return NotificationUtils.ReplyOrForward(SelectedIDs, "RE:", true, Folders.SelectedIndex == 1);
  274. }
  275. private bool ForwardClick(Button sender, CoreRow[] rows)
  276. {
  277. return NotificationUtils.ReplyOrForward(SelectedIDs, "FW:", false, Folders.SelectedIndex == 1);
  278. }
  279. private bool CreateTaskClick(Button sender, CoreRow[] rows)
  280. {
  281. return NotificationUtils.CreateTask(SelectedIDs, me.ID);
  282. }
  283. private bool AttachToJobClick(Button sender, CoreRow[] rows)
  284. {
  285. return NotificationUtils.AttachToJob(SelectedIDs);
  286. }
  287. private bool ViewJobClick(Button sender, CoreRow[] rows)
  288. {
  289. return NotificationUtils.ViewJob(SelectedIDs);
  290. }
  291. private bool CreateSetoutClick(Button sender, CoreRow[] rows)
  292. {
  293. return NotificationUtils.CreateSetout(SelectedIDs);
  294. }
  295. private bool CreateRequiClick(Button sender, CoreRow[] rows)
  296. {
  297. return NotificationUtils.CreateRequi(SelectedIDs, me.ID);
  298. }
  299. private bool CreateDeliveryClick(Button sender, CoreRow[] rows)
  300. {
  301. return NotificationUtils.CreateDelivery(SelectedIDs);
  302. }
  303. private bool ArchiveClick(Button sender, CoreRow[] rows)
  304. {
  305. return NotificationUtils.Archive(SelectedIDs);
  306. }
  307. private bool ReopenClick(Button sender, CoreRow[] rows)
  308. {
  309. return NotificationUtils.Reopen(SelectedIDs);
  310. }
  311. public void Heartbeat(TimeSpan time)
  312. {
  313. }
  314. #endregion
  315. }