VmInputDate.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using VberZero.Tools.StringModel;
  2. namespace VberAdmin.Web.Models.Input;
  3. public class VmInputDate : VmInputBase
  4. {
  5. public VmInputDate(string name, string displayName) : base(name, displayName, VmInputType.Date)
  6. {
  7. DefaultClass = "form-control form-control-sm form-control-solid ps-12 flatpickr-input";
  8. DateType = VmDateType.Date;
  9. //IsNoCalendar = false;
  10. Format = "Y-m-d";
  11. IsOnlyTime = false;
  12. }
  13. public VmInputDate(string name, string displayName, VmDateType dateType) : this(name, displayName)
  14. {
  15. DateType = dateType;
  16. }
  17. public VmDateType DateType { get; set; }
  18. public bool IsOnlyTime { get; set; }
  19. public bool EnableSeconds { get; set; }
  20. public int ShowMonths { get; set; } = 1;
  21. public int HourIncrement { get; set; } = 1;
  22. public int MinuteIncrement { get; set; } = 5;
  23. public string DefaultDate { get; protected set; }
  24. public string DefaultHour { get; protected set; }
  25. public string DefaultMinute { get; protected set; }
  26. public string MinDate { get; protected set; }
  27. public string MaxDate { get; protected set; }
  28. public string MinTime { get; protected set; }
  29. public string MaxTime { get; protected set; }
  30. public string Format { get; protected set; }
  31. /// <summary>
  32. /// 禁用的日期
  33. /// 数组 单个日期: ["2025-01-10", "22025-01-11"] ||
  34. /// 日期范围 [{ from: "2025-01-05", to: "2025-01-25" },{...}]
  35. /// 函数 [function(date) { return (date.getDay() === 0 || date.getDay() === 6);} ] 返回true禁用
  36. /// </summary>
  37. public string Disable { get; protected set; }
  38. /// <summary>
  39. /// 启用日期(其余禁用)
  40. /// </summary>
  41. public string Enable { get; protected set; }
  42. public string ControlDataStr
  43. => $"data-date-show-months=\"{ShowMonths}\"" +
  44. $"data-date-hour-increment=\"{HourIncrement}\"" +
  45. $"data-date-minute-increment=\"{MinuteIncrement}\"" +
  46. $"{(EnableSeconds ? "data-date-enable-seconds=\"true\"" : "")} " +
  47. $"{(IsOnlyTime ? "data-date-no-Calendar=\"true\"" : "")} " +
  48. $"{(DefaultDate.Empty() ? "" : $"data-date-default-date=\"{DefaultDate}\"")} " +
  49. $"{(DefaultHour.Empty() ? "" : $"data-date-default-hour=\"{DefaultHour}\"")} " +
  50. $"{(DefaultMinute.Empty() ? "" : $"data-date-default-minute=\"{DefaultMinute}\"")} " +
  51. $"{(MinDate.Empty() ? "" : $"data-date-min-date=\"{MinDate}\"")} " +
  52. $"{(MaxDate.Empty() ? "" : $"data-date-max-date=\"{MaxDate}\"")} " +
  53. $"{(MinTime.Empty() ? "" : $"data-date-min-time=\"{MinTime}\"")} " +
  54. $"{(MaxTime.Empty() ? "" : $"data-date-max-time=\"{MaxTime}\"")} " +
  55. $"{(Disable.Empty() ? "" : $"data-date-disable=\"{Disable}\"")} " +
  56. $"{(Enable.Empty() ? "" : $"data-date-enable=\"{Enable}\"")} " +
  57. $"{(Format.Empty() ? "" : $"data-date-format=\"{Format}\"")} ";
  58. public override string Other
  59. {
  60. get
  61. {
  62. string str;
  63. switch (DateType)
  64. {
  65. case VmDateType.DateTime:
  66. str = "date-time";
  67. break;
  68. case VmDateType.DateRange:
  69. str = "r_date";
  70. break;
  71. case VmDateType.DateTimeRange:
  72. str = "r_date-time";
  73. break;
  74. default:
  75. str = "date";
  76. break;
  77. }
  78. return $"{base.Other ?? ""} data-vb-date=\"{str}\" {ControlDataStr}";
  79. }
  80. set => _other = value;
  81. }
  82. public VmInputDate WithFormat(string format)
  83. {
  84. Format = format;
  85. return this;
  86. }
  87. public VmInputDate WithDefaultDate(string date)
  88. {
  89. DefaultDate = date;
  90. return this;
  91. }
  92. public VmInputDate WithMinDate(string date)
  93. {
  94. MinDate = date;
  95. return this;
  96. }
  97. public VmInputDate WithMaxDate(string date)
  98. {
  99. MaxDate = date;
  100. return this;
  101. }
  102. public VmInputDate WithShowMonths(int months)
  103. {
  104. ShowMonths = months < 1 ? 1 : months;
  105. return this;
  106. }
  107. /// <summary>
  108. /// 禁用的日期
  109. /// </summary>
  110. /// <param name="disable"> 数组 eg:
  111. /// 单个日期: ["2025-01-10", "22025-01-11"] ;
  112. /// 日期范围 [{ from: "2025-01-05", to: "2025-01-25" },{...}];
  113. /// 函数 [function(date) { return (date.getDay() === 0 || date.getDay() === 6);} ] 返回true禁用
  114. /// </param>
  115. /// <returns></returns>
  116. public VmInputDate WithDisable(string disable)
  117. {
  118. Disable = disable;
  119. return this;
  120. }
  121. /// <summary>
  122. /// 禁用的日期
  123. /// </summary>
  124. /// <param name="enable"> 数组 eg:
  125. /// 单个日期: ["2025-01-10", "22025-01-11"] ;
  126. /// 日期范围 [{ from: "2025-01-05", to: "2025-01-25" },{...}];
  127. /// 函数 [function(date) { return (date.getDay() === 0 || date.getDay() === 6);} ] 返回true启用
  128. /// </param>
  129. /// <returns></returns>
  130. public VmInputDate WithEnable(string enable)
  131. {
  132. Enable = enable;
  133. return this;
  134. }
  135. }
  136. public class VmInputDateTime : VmInputDate
  137. {
  138. public VmInputDateTime(string name, string displayName) : base(name, displayName, VmDateType.DateTime)
  139. {
  140. Format = "Y-m-d H:i";
  141. }
  142. public VmInputDateTime WithOnlyTime()
  143. {
  144. IsOnlyTime = true;
  145. Format = "H:i";
  146. return this;
  147. }
  148. public VmInputDateTime WithIncrement(int hour = 1, int minute = 5)
  149. {
  150. HourIncrement = hour;
  151. MinuteIncrement = minute;
  152. return this;
  153. }
  154. public VmInputDateTime WithSeconds()
  155. {
  156. EnableSeconds = true;
  157. return this;
  158. }
  159. public VmInputDateTime WithDefaultTime(string hour, string minute)
  160. {
  161. DefaultHour = hour;
  162. DefaultMinute = minute;
  163. return this;
  164. }
  165. public VmInputDateTime WithMinTime(string time)
  166. {
  167. MinTime = time;
  168. return this;
  169. }
  170. public VmInputDateTime WithMaxTime(string time)
  171. {
  172. MaxTime = time;
  173. return this;
  174. }
  175. }
  176. public class VmInputDateRange : VmInputDate
  177. {
  178. public VmInputDateRange(string name, string displayName) : base(name, displayName, VmDateType.DateRange)
  179. {
  180. DefaultClass += " vb-date-range";
  181. }
  182. /// <summary>
  183. /// 起始时间间隔
  184. /// </summary>
  185. /// <param name="interval">单位:天</param>
  186. /// <param name="value"></param>
  187. /// <returns></returns>
  188. public VmInputDateRange WithInterval(int interval, DateTime? value = null)
  189. {
  190. var date = value ?? DateTime.Today;
  191. DefaultDate = $"[{date.AddDays(0 - interval):yyyy-MM-dd},{date:yyyy-MM-dd}]";
  192. return this;
  193. }
  194. }
  195. public class VmInputDateTimeRange : VmInputDate
  196. {
  197. public VmInputDateTimeRange(string name, string displayName) : base(name, displayName, VmDateType.DateTimeRange)
  198. {
  199. DefaultClass += " vb-date-range";
  200. Format = "Y-m-d H:i";
  201. }
  202. public VmInputDateTimeRange WithIncrement(int hour = 1, int minute = 5)
  203. {
  204. HourIncrement = hour;
  205. MinuteIncrement = minute;
  206. return this;
  207. }
  208. public VmInputDateTimeRange WithSeconds()
  209. {
  210. EnableSeconds = true;
  211. return this;
  212. }
  213. public VmInputDateTimeRange WithDefaultTime(string hour, string minute)
  214. {
  215. DefaultHour = hour;
  216. DefaultMinute = minute;
  217. return this;
  218. }
  219. public VmInputDateTimeRange WithMinTime(string time)
  220. {
  221. MinTime = time;
  222. return this;
  223. }
  224. public VmInputDateTimeRange WithMaxTime(string time)
  225. {
  226. MaxTime = time;
  227. return this;
  228. }
  229. public VmInputDateTimeRange WithOnlyTime()
  230. {
  231. Format = "H:i";
  232. IsOnlyTime = true;
  233. return this;
  234. }
  235. /// <summary>
  236. /// 起始时间间隔
  237. /// </summary>
  238. /// <param name="interval">单位:天</param>
  239. /// <param name="value"></param>
  240. /// <returns></returns>
  241. public VmInputDateTimeRange WithInterval(int interval, DateTime? value = null)
  242. {
  243. var date = value ?? DateTime.Today;
  244. DefaultDate = $"[{date.AddDays(0 - interval):yyyy-MM-dd},{date:yyyy-MM-dd}]";
  245. return this;
  246. }
  247. }