WebTemplateGrid.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using ScriptEditor = InABox.DynamicGrid.ScriptEditorWindow;
  11. namespace PRSServer.Forms.WebTemplates
  12. {
  13. public class WebTemplateGrid : DynamicDataGrid<WebTemplate>
  14. {
  15. private readonly string defaultTemplate = @"
  16. @{Model.LoadModel(new string[] { ""CompanyInformation"", ""CompanyLogo"", ""User"" });}
  17. <!DOCTYPE html>
  18. <html>
  19. <head>
  20. <title>Untitled page</title>
  21. <meta charset=""utf8"">
  22. <meta name=""viewport"" content=""width=device-width,initial-scale=1"">
  23. </head>
  24. <body>
  25. <h1>New Page</h1>
  26. <p>
  27. This is an empty page.
  28. </p>
  29. </body>
  30. </html>";
  31. public WebTemplateGrid()
  32. {
  33. ActionColumns.Add(new DynamicImageColumn(Properties.Resources.html.AsBitmapImage(), EditTemplateAction));
  34. OnCreateItem += (sender, item) => { (item as WebTemplate).Template = defaultTemplate; };
  35. OnAfterSave += (sender, items) =>
  36. {
  37. foreach (var item in items.Cast<WebTemplate>()) SaveToLocalFolder(item);
  38. };
  39. OnDoubleClick += (sender, args) =>
  40. {
  41. if (SelectedRows.Length == 1) EditWebTemplateScript(SelectedRows[0]);
  42. args.Handled = true;
  43. };
  44. }
  45. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  46. {
  47. base.DoReconfigure(options);
  48. options.AddRange(DynamicGridOption.AddRows, DynamicGridOption.EditRows, DynamicGridOption.DeleteRows);
  49. }
  50. private bool EditTemplateAction(CoreRow arg)
  51. {
  52. if (arg != null) EditWebTemplateScript(arg);
  53. return false;
  54. }
  55. private static void SaveToLocalFolder(WebTemplate template)
  56. {
  57. if (CoreUtils.GetVersion() == "???")
  58. File.WriteAllText(
  59. string.Format("{0}/WebBackup/Templates/{1}_{2}_{3}.html",
  60. Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
  61. template.DataModel,
  62. template.Slug,
  63. template.ID
  64. ),
  65. template.Template
  66. );
  67. }
  68. private void EditWebTemplateScript(CoreRow arg)
  69. {
  70. var template = LoadItem(arg);
  71. var editor = new ScriptEditor(template.Template, SyntaxLanguage.HTML, $"Template: {template}");
  72. editor.OnSave += (e, args) =>
  73. {
  74. template.Template = editor.Script;
  75. SaveToLocalFolder(template);
  76. SaveItem(template);
  77. };
  78. editor.OnCompile += (e, args) =>
  79. {
  80. editor.Save();
  81. editor.ClearErrors().AddError("Template Saved").AddError("Compiling Template...");
  82. Task.Run(() =>
  83. {
  84. var errors = new List<string>();
  85. try
  86. {
  87. WebHandler.CompileTemplate(template);
  88. errors.Add("Template Compiled Successfully!");
  89. }
  90. catch (Exception e0)
  91. {
  92. errors.Add("ERROR: ");
  93. errors.AddRange(e0.Message.Split(new[] { "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries));
  94. }
  95. Dispatcher.Invoke(() =>
  96. {
  97. editor.ClearErrors();
  98. foreach (var error in errors) editor.AddError(error);
  99. });
  100. });
  101. };
  102. editor.Show();
  103. }
  104. }
  105. }