NotificationPanel.xaml.cs 14 KB

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