WebReportDesigner.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #if DESIGNER
  2. using Microsoft.AspNetCore.Html;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.ComponentModel;
  10. using FastReport.Web.Services;
  11. namespace FastReport.Web
  12. {
  13. partial class WebReport
  14. {
  15. #region Designer Properties
  16. /// <summary>
  17. /// Designer settings
  18. /// </summary>
  19. public DesignerSettings Designer { get; set; } = DesignerSettings.Default;
  20. /// <summary>
  21. /// Enable code editor in the Report Designer
  22. /// </summary>
  23. [Obsolete("DesignerScriptCode is obsolete, please use Designer.ScriptCode")]
  24. [EditorBrowsable(EditorBrowsableState.Never)]
  25. public bool DesignScriptCode { get => Designer.ScriptCode; set => Designer.ScriptCode = value; }
  26. /// <summary>
  27. /// Gets or sets path to the Report Designer
  28. /// </summary>
  29. [Obsolete("DesignerPath is obsolete, please use Designer.Path")]
  30. [EditorBrowsable(EditorBrowsableState.Never)]
  31. public string DesignerPath { get => Designer.Path; set => Designer.Path = value; }
  32. /// <summary>
  33. /// Gets or sets path to a folder for save designed reports
  34. /// If value is empty then designer posts saved report in variable ReportFile on call the DesignerSaveCallBack // TODO
  35. /// </summary>
  36. [Obsolete("DesignerPath is obsolete, please use Designer.SavePath")]
  37. [EditorBrowsable(EditorBrowsableState.Never)]
  38. public string DesignerSavePath { get => Designer.SavePath; set => Designer.SavePath = value; }
  39. /// <summary>
  40. /// Gets or sets path to callback page after Save from Designer
  41. /// </summary>
  42. [Obsolete("DesignerSaveCallBack is obsolete, please use Designer.SaveCallBack instead.")]
  43. [EditorBrowsable(EditorBrowsableState.Never)]
  44. public string DesignerSaveCallBack { get => Designer.SaveCallBack; set => Designer.SaveCallBack = value; }
  45. /// <summary>
  46. /// Callback method for saving an edited report by Online Designer
  47. /// Params: reportID, report file name, report, out - message
  48. /// </summary>
  49. /// <example>
  50. /// webReport.DesignerSaveMethod = (string reportID, string filename, string report) =>
  51. /// {
  52. /// string webRootPath = _hostingEnvironment.WebRootPath;
  53. /// string pathToSave = Path.Combine(webRootPath, filename);
  54. /// System.IO.File.WriteAllTextAsync(pathToSave, report);
  55. ///
  56. /// return "OK";
  57. /// };
  58. /// </example>
  59. [Obsolete("DesignerSaveMethod is obsolete, please use Designer.SaveMethod instead.")]
  60. [EditorBrowsable(EditorBrowsableState.Never)]
  61. public Func<string, string, string, string> DesignerSaveMethod { get => Designer.SaveMethod; set => Designer.SaveMethod = value; }
  62. /// <summary>
  63. /// Report name without extension
  64. /// </summary>
  65. public string ReportName
  66. {
  67. get
  68. {
  69. return (!string.IsNullOrEmpty(Report.ReportInfo.Name) ?
  70. Report.ReportInfo.Name : Path.GetFileNameWithoutExtension(Report.FileName));
  71. }
  72. }
  73. /// <summary>
  74. /// Report file name with extension (*.frx)
  75. /// </summary>
  76. public string ReportFileName => $"{ReportName}.frx";
  77. /// <summary>
  78. /// Gets or sets the locale of Designer
  79. /// </summary>
  80. [Obsolete("DesignerLocale is obsolete, please use Designer.Locale")]
  81. [EditorBrowsable(EditorBrowsableState.Never)]
  82. public string DesignerLocale { get => Designer.Locale; set => Designer.Locale = value; }
  83. /// <summary>
  84. /// Gets or sets the text of configuration of Online Designer
  85. /// </summary>
  86. [Obsolete("DesignerConfig is obsolete, please use Designer.Config")]
  87. [EditorBrowsable(EditorBrowsableState.Never)]
  88. public string DesignerConfig { get => Designer.Config; set => Designer.Config = value; }
  89. /// <summary>
  90. /// Gets or sets the request headers
  91. /// </summary>
  92. public WebHeaderCollection RequestHeaders { get; set; }
  93. /// <summary>
  94. /// Occurs when designed report save is started.
  95. /// </summary>
  96. public event EventHandler<SaveDesignedReportEventArgs> SaveDesignedReport;
  97. #endregion
  98. #region Public Methods
  99. /// <summary>
  100. /// Runs on designed report save
  101. /// </summary>
  102. internal void OnSaveDesignedReport()
  103. {
  104. if (SaveDesignedReport != null)
  105. {
  106. SaveDesignedReportEventArgs e = new SaveDesignedReportEventArgs();
  107. e.Stream = new MemoryStream();
  108. Report.Save(e.Stream);
  109. e.Stream.Position = 0;
  110. SaveDesignedReport.Invoke(this, e);
  111. }
  112. }
  113. #endregion
  114. #region Private Methods
  115. HtmlString RenderDesigner()
  116. {
  117. //string designerPath = WebUtils.GetAppRoot(DesignerPath);
  118. string designerLocale = Designer.Locale.IsNullOrWhiteSpace() ? "" : $"&lang={Designer.Locale}";
  119. return new HtmlString($@"
  120. <iframe src=""{Designer.Path}?uuid={ID}{ReportDesignerService.GetARRAffinity()}{designerLocale}"" style=""border:none;"" width=""{Width}"" height=""{Height}"">
  121. <p style=""color:red"">ERROR: Browser does not support IFRAME!</p>
  122. </iframe >
  123. ");
  124. // TODO: add fit script
  125. }
  126. //void SendPreviewObjectResponse(HttpContext context)
  127. //{
  128. // string uuid = context.Request.Params["previewobject"];
  129. // SetUpWebReport(uuid, context);
  130. // WebUtils.SetupResponse(webReport, context);
  131. // if (!NeedExport(context) && !NeedPrint(context))
  132. // SendReport(context);
  133. // cache.PutObject(uuid, webReport);
  134. // Finalize(context);
  135. //}
  136. // On-line Designer
  137. //void SendDesigner(HttpContext context, string uuid)
  138. //{
  139. // WebUtils.SetupResponse(webReport, context);
  140. // StringBuilder sb = new StringBuilder();
  141. // context.Response.AddHeader("Content-Type", "html/text");
  142. // try
  143. // {
  144. // string designerPath = WebUtils.GetAppRoot(context, webReport.DesignerPath);
  145. // string designerLocale = String.IsNullOrEmpty(webReport.Designer.Locale) ? "" : "&lang=" + webReport.Designer.Locale;
  146. // sb.AppendFormat("<iframe src=\"{0}?uuid={1}{2}{3}\" style=\"border:none;\" width=\"{4}\" height=\"{5}\" >",
  147. // designerPath, //0
  148. // uuid, //1
  149. // WebUtils.GetARRAffinity(), //2
  150. // designerLocale, //3
  151. // webReport.Width.ToString(), //4
  152. // webReport.Height.ToString() //5
  153. // );
  154. // sb.Append("<p style=\"color:red\">ERROR: Browser does not support IFRAME!</p>");
  155. // sb.AppendLine("</iframe>");
  156. // // add resize here
  157. // if (webReport.Height == System.Web.UI.WebControls.Unit.Percentage(100))
  158. // sb.Append(GetFitScript(uuid));
  159. // }
  160. // catch (Exception e)
  161. // {
  162. // log.AddError(e);
  163. // }
  164. // if (log.Text.Length > 0)
  165. // {
  166. // context.Response.Write(log.Text);
  167. // log.Clear();
  168. // }
  169. // SetContainer(context, Properties.ControlID);
  170. // context.Response.Write(sb.ToString());
  171. //}
  172. string GetFitScript(string ID)
  173. {
  174. StringBuilder sb = new StringBuilder();
  175. sb.AppendLine("<script>");
  176. sb.AppendLine("(function() {");
  177. sb.AppendLine($"var div = document.querySelector('#{ID}'),");
  178. sb.AppendLine("iframe,");
  179. sb.AppendLine("rect,");
  180. sb.AppendLine("e = document.documentElement,");
  181. sb.AppendLine("g = document.getElementsByTagName('body')[0],");
  182. //sb.AppendLine("x = window.innerWidth || e.clientWidth || g.clientWidth,");
  183. sb.AppendLine("y = window.innerHeight|| e.clientHeight|| g.clientHeight;");
  184. sb.AppendLine("if (div) {");
  185. sb.AppendLine("iframe = div.querySelector('iframe');");
  186. sb.AppendLine("if (iframe) {");
  187. sb.AppendLine("rect = iframe.getBoundingClientRect();");
  188. //sb.AppendLine("iframe.setAttribute('width', x - rect.left);");
  189. sb.AppendLine("iframe.setAttribute('height', y - rect.top - 11);");
  190. sb.AppendLine("}}}());");
  191. sb.AppendLine("</script>");
  192. return sb.ToString();
  193. }
  194. #endregion
  195. }
  196. }
  197. #endif