DFLayoutDateFieldProperties.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Globalization;
  4. namespace InABox.Core
  5. {
  6. public class DFLayoutDateFieldProperties : DFLayoutFieldProperties<DateTime, DateTime?>
  7. {
  8. [EditorSequence(-995)]
  9. [DateEditor]
  10. public override DateTime Default { get; set; }
  11. public DFLayoutDateFieldProperties()
  12. {
  13. Format = "dd MMM yy";
  14. }
  15. public string Format { get; set; }
  16. public override string FormatValue(DateTime? value)
  17. {
  18. return string.Format("{0" + (string.IsNullOrWhiteSpace(Format) ? "" : ":" + Format.Replace(":", "\\:")) + "}", value);
  19. }
  20. public override DateTime GetValue(DateTime? value)
  21. {
  22. return value ?? Default;
  23. }
  24. public override void SerializeValue(DFSaveStorageEntry entry, DateTime? value)
  25. {
  26. if(value != null)
  27. {
  28. entry.SetValue(value.Value.ToString("dd-MM-yyyy"));
  29. }
  30. }
  31. public override DateTime? DeserializeValue(DFLoadStorageEntry entry)
  32. {
  33. var value = entry.GetValue();
  34. if (value is DateTime date)
  35. return date;
  36. if (DateTime.TryParseExact(value as string, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out var result))
  37. return result;
  38. return null;
  39. }
  40. protected override void LoadProperties()
  41. {
  42. base.LoadProperties();
  43. Format = GetProperty("Format", Format);
  44. }
  45. protected override void SaveProperties()
  46. {
  47. base.SaveProperties();
  48. SetProperty("Format", Format);
  49. }
  50. }
  51. }