WebStyleGrid.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. using InABox.DynamicGrid;
  7. using InABox.WPF;
  8. using ScriptEditor = InABox.DynamicGrid.ScriptEditorWindow;
  9. namespace PRSServer.Forms.WebStyles
  10. {
  11. public class WebStyleGrid : DynamicDataGrid<WebStyle>
  12. {
  13. public WebStyleGrid()
  14. {
  15. ActionColumns.Add(new DynamicImageColumn(Properties.Resources.css.AsBitmapImage(), EditStyleAction));
  16. OnAfterSave += (sender, items) =>
  17. {
  18. foreach (var style in items.Cast<WebStyle>()) SaveToLocalFolder(style);
  19. };
  20. OnDoubleClick += (sender, args) =>
  21. {
  22. if (SelectedRows.Length == 1) EditWebStyle(SelectedRows[0]);
  23. args.Handled = true;
  24. };
  25. }
  26. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  27. {
  28. base.DoReconfigure(options);
  29. options.AddRange(DynamicGridOption.AddRows, DynamicGridOption.EditRows, DynamicGridOption.DeleteRows);
  30. }
  31. private bool EditStyleAction(CoreRow arg)
  32. {
  33. if (arg != null) EditWebStyle(arg);
  34. return false;
  35. }
  36. private static void SaveToLocalFolder(WebStyle style)
  37. {
  38. if (CoreUtils.GetVersion() == "???")
  39. File.WriteAllText(
  40. string.Format("{0}/WebBackup/Styles/{1}_{2}.css",
  41. Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
  42. style.Code,
  43. style.ID
  44. ),
  45. style.Style
  46. );
  47. }
  48. private void EditWebStyle(CoreRow arg)
  49. {
  50. var style = LoadItem(arg);
  51. var editor = new ScriptEditor(style.Style, SyntaxLanguage.CSS, $"Style: {style}");
  52. editor.OnSave += (e, args) =>
  53. {
  54. style.Style = editor.Script;
  55. SaveToLocalFolder(style);
  56. SaveItem(style);
  57. };
  58. editor.Show();
  59. }
  60. }
  61. }