Ooui-tws-port/Ooui/Input.cs

148 lines
4.3 KiB
C#
Raw Normal View History

2017-06-16 05:33:12 +00:00
using System;
2017-06-24 21:52:34 +00:00
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2017-06-16 05:33:12 +00:00
namespace Ooui
{
public class Input : FormControl
{
public InputType Type {
2018-02-02 02:43:23 +00:00
get => GetAttribute ("type", InputType.Text);
set => SetAttributeProperty ("type", value);
2017-06-16 05:33:12 +00:00
}
string val = "";
2017-06-16 05:33:12 +00:00
public string Value {
get => val;
set => SetProperty (ref val, value ?? "", "value");
2017-06-16 05:33:12 +00:00
}
public double NumberValue {
get {
double v;
double.TryParse (Value, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentUICulture, out v);
return v;
}
set {
Value = value.ToString (System.Globalization.CultureInfo.CurrentUICulture);
}
}
public event TargetEventHandler Change {
2017-06-16 06:15:15 +00:00
add => AddEventListener ("change", value);
remove => RemoveEventListener ("change", value);
}
2017-11-16 22:19:31 +00:00
public string Placeholder {
2018-02-02 02:43:23 +00:00
get => GetStringAttribute ("placeholder", "");
set => SetAttributeProperty ("placeholder", value ?? "");
2017-11-16 22:19:31 +00:00
}
2017-06-16 05:33:12 +00:00
public bool IsChecked {
2018-02-02 02:43:23 +00:00
get => GetBooleanAttribute ("checked");
set {
if (SetBooleanAttributeProperty ("checked", value)) {
TriggerEvent ("change");
}
}
2017-06-16 05:33:12 +00:00
}
public double Minimum {
2018-02-02 02:43:23 +00:00
get => GetAttribute ("min", 0.0);
set => SetAttributeProperty ("min", value);
2017-06-16 05:33:12 +00:00
}
public double Maximum {
2018-02-02 02:43:23 +00:00
get => GetAttribute ("max", 100.0);
set => SetAttributeProperty ("max", value);
2017-06-16 05:33:12 +00:00
}
public double Step {
2018-02-02 02:43:23 +00:00
get => GetAttribute ("step", 1.0);
set => SetAttributeProperty ("step", value);
2017-06-16 05:33:12 +00:00
}
public Input ()
: base ("input")
{
2017-06-24 20:34:47 +00:00
// Subscribe to the change event so we always get up-to-date values
Change += (s, e) => {};
2017-06-16 05:33:12 +00:00
}
public Input (InputType type)
: this ()
{
Type = type;
}
2017-06-24 20:34:47 +00:00
protected override bool TriggerEventFromMessage (Message message)
{
if (message.TargetId == Id && message.MessageType == MessageType.Event && (message.Key == "change" || message.Key == "input" || message.Key == "keyup")) {
2017-06-24 22:21:48 +00:00
if (Type == InputType.Checkbox) {
2018-02-02 02:43:23 +00:00
UpdateBooleanAttributeProperty ("checked", message.Value != null ? Convert.ToBoolean (message.Value) : false, "IsChecked");
2017-06-24 22:21:48 +00:00
}
else {
var v = message.Value != null ? Convert.ToString (message.Value) : "";
if (val != v) {
val = v;
OnPropertyChanged ("Value");
}
2017-06-24 22:21:48 +00:00
}
2017-06-24 20:34:47 +00:00
}
return base.TriggerEventFromMessage (message);
}
2017-06-16 05:33:12 +00:00
}
2017-06-24 21:52:34 +00:00
[JsonConverter (typeof (StringEnumConverter))]
2017-06-16 05:33:12 +00:00
public enum InputType
{
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "text")]
2017-06-16 05:33:12 +00:00
Text,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "date")]
2017-06-16 05:33:12 +00:00
Date,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "week")]
2017-06-16 05:33:12 +00:00
Week,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "datetime")]
2017-06-16 05:33:12 +00:00
Datetime,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "datetimelocal")]
2017-06-16 05:33:12 +00:00
DatetimeLocal,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "time")]
2017-06-16 05:33:12 +00:00
Time,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "month")]
2017-06-16 05:33:12 +00:00
Month,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "range")]
2017-06-16 05:33:12 +00:00
Range,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "number")]
2017-06-16 05:33:12 +00:00
Number,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "hidden")]
2017-06-16 05:33:12 +00:00
Hidden,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "search")]
2017-06-16 05:33:12 +00:00
Search,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "email")]
2017-06-16 05:33:12 +00:00
Email,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "tel")]
2017-06-16 05:33:12 +00:00
Tel,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "url")]
2017-06-16 05:33:12 +00:00
Url,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "password")]
2017-06-16 05:33:12 +00:00
Password,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "color")]
2017-06-16 05:33:12 +00:00
Color,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "checkbox")]
2017-06-16 05:33:12 +00:00
Checkbox,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "radio")]
2017-06-16 05:33:12 +00:00
Radio,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "file")]
2017-06-16 05:33:12 +00:00
File,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "submit")]
2017-06-16 05:33:12 +00:00
Submit,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "reset")]
2017-06-16 05:33:12 +00:00
Reset,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "image")]
2017-06-16 05:33:12 +00:00
Image,
2017-06-24 21:52:34 +00:00
[EnumMember (Value = "button")]
2017-06-16 05:33:12 +00:00
Button,
}
}