2017-06-16 06:41:38 +00:00
|
|
|
using System;
|
2018-03-23 22:06:17 +00:00
|
|
|
using System.Linq;
|
2017-06-16 06:41:38 +00:00
|
|
|
|
|
|
|
namespace Ooui
|
|
|
|
{
|
|
|
|
public class Select : FormControl
|
|
|
|
{
|
2018-03-23 22:06:17 +00:00
|
|
|
bool gotValue = false;
|
|
|
|
|
2017-06-16 06:41:38 +00:00
|
|
|
public string Value {
|
2018-03-23 22:06:17 +00:00
|
|
|
get {
|
|
|
|
if (gotValue) return GetStringAttribute ("value", "");
|
|
|
|
return GetDefaultValue ();
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
gotValue = true;
|
|
|
|
SetAttributeProperty ("value", value ?? "");
|
|
|
|
}
|
2017-06-16 06:41:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-10 23:07:33 +00:00
|
|
|
public event TargetEventHandler Change {
|
2017-06-16 06:41:38 +00:00
|
|
|
add => AddEventListener ("change", value);
|
|
|
|
remove => RemoveEventListener ("change", value);
|
|
|
|
}
|
|
|
|
|
2017-12-10 23:07:33 +00:00
|
|
|
public event TargetEventHandler Input {
|
2017-12-09 21:19:32 +00:00
|
|
|
add => AddEventListener ("input", value);
|
|
|
|
remove => RemoveEventListener ("input", value);
|
|
|
|
}
|
|
|
|
|
2017-06-16 06:41:38 +00:00
|
|
|
public Select ()
|
|
|
|
: base ("select")
|
|
|
|
{
|
2017-12-09 21:19:32 +00:00
|
|
|
// Subscribe to the change event so we always get up-to-date values
|
2018-03-23 22:06:17 +00:00
|
|
|
Change += (s, e) => { gotValue = true; };
|
2017-12-09 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddOption (string label, string value)
|
|
|
|
{
|
2018-03-23 22:06:17 +00:00
|
|
|
AppendChild (new Option { Text = label, Value = value });
|
2017-12-09 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:06:17 +00:00
|
|
|
string GetDefaultValue ()
|
2017-12-09 21:19:32 +00:00
|
|
|
{
|
2018-03-23 22:06:17 +00:00
|
|
|
var options = Children.OfType<Option> ();
|
|
|
|
var r = options.FirstOrDefault (x => x.DefaultSelected);
|
|
|
|
if (r != null)
|
|
|
|
return r.Value;
|
|
|
|
r = options.FirstOrDefault ();
|
|
|
|
return r?.Value ?? "";
|
2017-12-09 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool TriggerEventFromMessage (Message message)
|
|
|
|
{
|
2018-03-23 22:06:17 +00:00
|
|
|
if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input" || message.Key == "keyup")) {
|
|
|
|
gotValue = true;
|
|
|
|
UpdateAttributeProperty ("value", message.Value != null ? Convert.ToString (message.Value) : "", "Value");
|
2017-12-09 21:19:32 +00:00
|
|
|
}
|
|
|
|
return base.TriggerEventFromMessage (message);
|
2017-06-16 06:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|