DatabaseScriptGrid.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Windows;
  4. using InABox.Clients;
  5. using InABox.Core;
  6. using InABox.DynamicGrid;
  7. using InABox.WPF;
  8. using ScriptEditor = InABox.DynamicGrid.ScriptEditorWindow;
  9. namespace PRSDesktop
  10. {
  11. public class DatabaseScriptGrid : DynamicDataGrid<Script>
  12. {
  13. public DatabaseScriptGrid()
  14. {
  15. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns);
  16. HiddenColumns.Add(x => x.Section);
  17. HiddenColumns.Add(x => x.Code);
  18. ActionColumns.Add(new DynamicImageColumn(PRSDesktop.Resources.script.AsBitmapImage(), EditScript));
  19. OnCustomiseEditor += CustomiseEditor;
  20. }
  21. protected override void DoValidate(Script[] items, List<string> errors)
  22. {
  23. base.DoValidate(items, errors);
  24. if (items.Any(x => string.IsNullOrWhiteSpace(x.Section)))
  25. errors.Add("[Section] must not be blank");
  26. if (items.Any(x => x.ScriptType == ScriptType.None))
  27. errors.Add("[Script Type] must not be None or empty");
  28. }
  29. private bool EditScript(CoreRow arg)
  30. {
  31. if (arg == null)
  32. {
  33. MessageBox.Show("Please select a module first");
  34. return false;
  35. }
  36. var script = arg.ToObject<Script>();
  37. var code = script.Code;
  38. var editor = new ScriptEditor(code);
  39. if (editor.ShowDialog() == true)
  40. {
  41. script.Code = editor.Script;
  42. new Client<Script>().Save(script, "Updated by User");
  43. return true;
  44. }
  45. return false;
  46. }
  47. private void CustomiseEditor(IDynamicEditorForm sender, Script[]? items, DynamicGridColumn column, BaseEditor editor)
  48. {
  49. if (string.Equals(column.ColumnName, "Section"))
  50. {
  51. var script = items?.FirstOrDefault();
  52. editor.Editable = !string.IsNullOrWhiteSpace(script?.Section) ? Editable.Disabled : Editable.Enabled;
  53. }
  54. }
  55. }
  56. }