DigitalFormOptionComboBox.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Linq;
  3. using InABox.Core;
  4. using InABox.Mobile;
  5. using Xamarin.Forms;
  6. namespace PRS.Mobile
  7. {
  8. public class DigitalFormOptionComboBox : Grid, IDigitalFormField<DFLayoutOptionField, DFLayoutOptionFieldProperties, string>
  9. {
  10. private readonly MobileCard _labelframe;
  11. private readonly Label _label;
  12. private readonly MobileCard _menuframe;
  13. private readonly MobileMenuButton _menu;
  14. private DFLayoutOptionField _definition;
  15. public DFLayoutOptionField Definition
  16. {
  17. get => _definition;
  18. set
  19. {
  20. _definition = value;
  21. Initialize(value ?? new DFLayoutOptionField());
  22. }
  23. }
  24. private String _value;
  25. public String Value
  26. {
  27. get => _value;
  28. set
  29. {
  30. _value = value;
  31. _label.Text = String.IsNullOrWhiteSpace(value) ? "Select Value" : value;
  32. }
  33. }
  34. public bool IsEmpty => String.IsNullOrWhiteSpace(Value);
  35. private bool _readOnly;
  36. public bool ReadOnly
  37. {
  38. get => _readOnly;
  39. set
  40. {
  41. _readOnly = value;
  42. UpdateStatus();
  43. }
  44. }
  45. public void Deserialize(string serialized)
  46. {
  47. Value = serialized;
  48. }
  49. public string Serialize() => Value;
  50. public event DigitalFormViewChangedHandler ValueChanged;
  51. public DigitalFormOptionComboBox()
  52. {
  53. HeightRequest = 40;
  54. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
  55. ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
  56. RowDefinitions.Add(new RowDefinition() { Height = GridLength.Star });
  57. _labelframe = new MobileCard()
  58. {
  59. Padding = 5
  60. };
  61. Children.Add(_labelframe);
  62. _label = new Label
  63. {
  64. Text = "Select Value",
  65. VerticalTextAlignment = TextAlignment.Center,
  66. FontSize = Device.GetNamedSize(NamedSize.Small,new Label()),
  67. Margin = 5
  68. };
  69. _labelframe.Content = _label;
  70. _menuframe = new MobileCard() { Padding = 0 };
  71. _menuframe.SetValue(Grid.ColumnProperty,1);
  72. Children.Add(_menuframe);
  73. _menu = new MobileMenuButton
  74. {
  75. WidthRequest = 35,
  76. Image = ImageSource.FromFile("lines")
  77. };
  78. _menuframe.Content = _menu;
  79. }
  80. private void Initialize(DFLayoutOptionField definition)
  81. {
  82. _menu.Items.Clear();
  83. foreach (var option in definition.Properties.Options.Replace(",","\n").Split('\n'))
  84. {
  85. var item = new MobileMenuItem() { Text = option.Trim() };
  86. item.Clicked += (sender, args) =>
  87. {
  88. Value = item.Text;
  89. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  90. };
  91. _menu.Items.Add(item);
  92. }
  93. if (_menu.Items.Any())
  94. _menu.Items.Add(new MobileMenuSeparator());
  95. var clear = new MobileMenuItem() { Text = "Clear Value" };
  96. clear.Clicked += (sender, args) =>
  97. {
  98. Value = "";
  99. ValueChanged?.Invoke(this, new DigitalFormViewChangedArgs(Definition,Value));
  100. };
  101. _menu.Items.Add(clear);
  102. UpdateStatus();
  103. }
  104. private void UpdateStatus()
  105. {
  106. IsEnabled = !_readOnly || Definition.Properties.Secure;
  107. var labelcolors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, false);
  108. _labelframe.BackgroundColor = labelcolors.Background;
  109. _labelframe.BorderColor = labelcolors.Border;
  110. _label.TextColor = labelcolors.Foreground;
  111. var menucolors = DigitalFormUtils.GetColors(!IsEnabled, Definition.Properties.Required, true);
  112. _menuframe.BackgroundColor = menucolors.Background;
  113. _menuframe.BorderColor = menucolors.Border;
  114. }
  115. }
  116. }