WebDateTimePicker.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Globalization;
  3. using FastReport.Dialog;
  4. using System.Windows.Forms;
  5. using static FastReport.Web.Constants;
  6. namespace FastReport.Web
  7. {
  8. public partial class Dialog
  9. {
  10. /// <summary>
  11. /// Gets or sets date format in html input type="date"
  12. /// </summary>
  13. public static string DatePickerFormat {
  14. get;
  15. set;
  16. } = DEFAULT_DATE_PICKER_FORMAT;
  17. /// <summary>
  18. /// Gets or sets time format in html input type="time"
  19. /// </summary>
  20. public static string TimePickerFormat
  21. {
  22. get;
  23. set;
  24. } = DEFAULT_TIME_PICKER_FORMAT;
  25. private void DateTimePickerChangeDate(DateTimePickerControl dp, string value)
  26. {
  27. DateTime oldValue = dp.Value;
  28. DateTime parsedValue = DateTime.Parse(value);
  29. DateTime newDateTime = new DateTime(
  30. parsedValue.Year,
  31. parsedValue.Month,
  32. parsedValue.Day,
  33. oldValue.Hour,
  34. oldValue.Minute,
  35. oldValue.Second,
  36. oldValue.Kind);
  37. dp.Value = newDateTime;
  38. dp.OnValueChanged(null);
  39. }
  40. private void DateTimePickerChangeTime(DateTimePickerControl dp, string value)
  41. {
  42. DateTime oldValue = dp.Value;
  43. DateTime parsedValue = DateTime.Parse(value);
  44. DateTime newDateTime = new DateTime(
  45. oldValue.Year,
  46. oldValue.Month,
  47. oldValue.Day,
  48. parsedValue.Hour,
  49. parsedValue.Minute,
  50. parsedValue.Second,
  51. oldValue.Kind);
  52. dp.Value = newDateTime;
  53. dp.OnValueChanged(null);
  54. }
  55. private string OnChangeDate(DateTimePickerControl control, bool needReload = true)
  56. {
  57. return OnChangeDateTimePicker(control, "-date", needReload);
  58. }
  59. private string OnChangeTime(DateTimePickerControl control, bool needReload = true)
  60. {
  61. return OnChangeDateTimePicker(control, "-time", needReload);
  62. }
  63. private string OnChangeDateTimePicker(DateTimePickerControl control, string suffix, bool needReload)
  64. {
  65. var eventType = needReload ? ONCHANGE : ONBLUR;
  66. return $"{eventType}=\"{GetEvent(ONCHANGE, control.Name + suffix, needReload ? SILENT_RELOAD : DIALOG, $"this.value")}\"";
  67. }
  68. private string GetDateTimePickerHtml(DateTimePickerControl control)
  69. {
  70. control.FillData();
  71. ControlFilterRefresh(control);
  72. string id = GetControlID(control);
  73. string date = string.Empty;
  74. string time = string.Empty;
  75. string disabled = control.Enabled ? "" : "disabled";
  76. bool needUpdate = NeedRefresh(control);
  77. if (control.Format == DateTimePickerFormat.Short)
  78. {
  79. // we draw only date
  80. date = $"<input style=\"{GetDateTimePickerStyle(control)}\"" +
  81. $" type=\"date\" value=\"{control.Value.ToString(DatePickerFormat)}\"" +
  82. $" {OnChangeDate(control, needUpdate)}" +
  83. $" id=\"{id}-date\" {disabled} required/>";
  84. }
  85. else if (control.Format == DateTimePickerFormat.Time)
  86. {
  87. // we draw only time
  88. var timeValue = control.Value.ToString(TimePickerFormat);
  89. time = $"<input style=\"{GetDateTimePickerStyle(control)} min-width:5rem;\"" +
  90. $" type=\"time\" value=\"{timeValue}\"" +
  91. $" {OnChangeTime(control, needUpdate)}" +
  92. $" id=\"{id}-time\" {disabled} required/>";
  93. }
  94. else
  95. {
  96. // we draw date and time in one container
  97. date = $"<input style=\"{GetControlFont(control.Font)} {GetBackColor(control.BackColor)} border-right-width:0px; padding-right:0px\"" +
  98. $" type=\"date\" value=\"{control.Value.ToString(DatePickerFormat)}\"" +
  99. $" {OnChangeDate(control, needUpdate)}" +
  100. $" id=\"{id}-date\" {disabled} required/>";
  101. var timeValue = control.Value.ToString(TimePickerFormat);
  102. time = $"<input style=\"{GetControlFont(control.Font)} {GetBackColor(control.BackColor)} border-left-width:0px; padding-left:0px; min-width:5rem;\"" +
  103. $" type=\"time\" value=\"{timeValue}\"" +
  104. $" {OnChangeTime(control, needUpdate)}" +
  105. $" id=\"{id}-time\" {disabled} required/>";
  106. }
  107. string datetimepicker = $"<div class=\"datetimepicker\" name=\"{control.Name}\" style=\"{GetDateTimePickerDivStyle(control)}\">" +
  108. date +
  109. time +
  110. "</div>";
  111. return datetimepicker;
  112. }
  113. private string GetDateTimePickerDivStyle(DateTimePickerControl control)
  114. {
  115. return $"{GetControlPosition(control)} display:inline-flex";
  116. }
  117. private string GetDateTimePickerStyle(DateTimePickerControl control)
  118. {
  119. return $"{GetControlFont(control.Font)} {GetBackColor(control.BackColor)}";
  120. }
  121. }
  122. }