Ooui-tws-port/Ooui/Input.cs

155 lines
4.4 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
{
InputType typ = InputType.Text;
public InputType Type {
get => typ;
set => SetProperty (ref typ, value, "type");
}
string val = "";
public string Value {
get => val;
set => SetProperty (ref val, value ?? "", "value");
}
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);
}
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler Changed {
2017-06-16 06:15:15 +00:00
add => AddEventListener ("change", value);
remove => RemoveEventListener ("change", value);
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler Inputted {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("input", value);
remove => RemoveEventListener ("input", value);
}
2017-11-16 22:19:31 +00:00
string placeholder = "";
public string Placeholder {
get => placeholder;
set => SetProperty (ref placeholder, value, "placeholder");
}
2017-06-16 05:33:12 +00:00
bool isChecked = false;
public bool IsChecked {
get => isChecked;
2017-06-24 23:25:14 +00:00
set {
SetProperty (ref isChecked, value, "checked");
TriggerEventFromMessage (Message.Event (Id, "change", isChecked));
}
2017-06-16 05:33:12 +00:00
}
double minimum = 0;
public double Minimum {
get => minimum;
set => SetProperty (ref minimum, value, "min");
}
double maximum = 100;
public double Maximum {
get => maximum;
set => SetProperty (ref maximum, value, "max");
}
2017-06-16 06:47:14 +00:00
double step = 1;
2017-06-16 05:33:12 +00:00
public double Step {
get => step;
set => SetProperty (ref step, value, "step");
}
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
Changed += (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") {
2017-06-24 21:52:34 +00:00
// Don't need to notify here because the base implementation will fire the event
2017-06-24 22:21:48 +00:00
if (Type == InputType.Checkbox) {
isChecked = message.Value != null ? Convert.ToBoolean (message.Value) : false;
}
else {
val = message.Value != null ? Convert.ToString (message.Value) : "";
}
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,
}
}