Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptDateTimeParameterNode.cs
1using System;
2using System.Text.RegularExpressions;
3using System.Threading.Tasks;
12using Waher.Script;
14
16{
21 {
26 : base()
27 {
28 }
29
33 [Page(2, "Script", 100)]
34 [Header(29, "Default value:")]
35 [ToolTip(30, "Default value presented to user.")]
36 public DateTime? DefaultValue { get; set; }
37
41 [Page(2, "Script", 100)]
42 [Header(44, "Minimum Value:")]
43 [ToolTip(45, "The smallest value allowed.")]
44 public DateTime? Min { get; set; }
45
49 [Page(2, "Script", 100)]
50 [Header(46, "Maximum Value:")]
51 [ToolTip(47, "The largest value allowed.")]
52 public DateTime? Max { get; set; }
53
59 public override Task<string> GetTypeNameAsync(Language Language)
60 {
61 return Language.GetStringAsync(typeof(ScriptNode), 54, "Date and Time-valued parameter");
62 }
63
70 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
71 {
72 ValidationMethod Validation;
74
75 if (this.Min.HasValue || this.Max.HasValue)
76 {
77 Validation = new RangeValidation(
78 this.Min.HasValue ? XML.Encode(this.Min.Value) : null,
79 this.Max.HasValue ? XML.Encode(this.Max.Value) : null);
80 }
81 else
82 Validation = new BasicValidation();
83
84 if (this.RestrictToOptions)
85 {
86 Field = new ListSingleField(Parameters, this.ParameterName, this.Label, this.Required,
87 new string[] { this.DefaultValue.HasValue ? XML.Encode(this.DefaultValue.Value) : string.Empty },
88 await this.GetOptions(), this.Description, DateTimeDataType.Instance, Validation, string.Empty, false, false, false);
89 }
90 else
91 {
93 new string[] { this.DefaultValue.HasValue ? XML.Encode(this.DefaultValue.Value) : string.Empty },
94 await this.GetOptions(), this.Description, DateTimeDataType.Instance, Validation, string.Empty, false, false, false);
95 }
96
97 Parameters.Add(Field);
98
99 Page Page = Parameters.GetPage(this.Page);
100 Page.Add(Field);
101 }
102
112 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
114 {
116 if (Field is null)
117 {
118 if (this.Required)
119 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
120
121 Values[this.ParameterName] = null;
122 }
123 else
124 {
125 string s = Field.ValueString;
126
127 if (string.IsNullOrEmpty(s))
128 {
129 if (this.Required)
130 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
131
132 Values[this.ParameterName] = null;
133 }
134 else if (XML.TryParse(s, out DateTime Parsed))
135 Values[this.ParameterName] = Parsed;
136 else
137 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 49, "Invalid value."));
138 }
139 }
140
141 }
142}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:25
void AddError(string Key, string Value)
Adds an error to the list of errors.
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
static readonly DateTimeDataType Instance
Public instance of data type.
Base class for form fields
Definition: Field.cs:16
string ValueString
Value as a single string. If field contains multiple values, they will be concatenated into a single ...
Definition: Field.cs:101
Class managing a page in a data form layout.
Definition: Page.cs:11
void Add(LayoutElement Element)
Adds a layout element.
Definition: Section.cs:135
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Collection of variables.
Definition: Variables.cs:25
Represents a Date and Time-valued script parameter.
ScriptDateTimeParameterNode()
Represents a Date and Time-valued script parameter.
override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
Populates a data form with parameters for the object.
override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values, SetEditableFormResult Result)
Sets the parameters of the object, based on contents in the data form.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Represents a parameter with possible options on a command.
async Task< KeyValuePair< string, string >[]> GetOptions()
Gets available options, if any are defined.
Node defined by script.
Definition: ScriptNode.cs:19
bool Required
If parameter is required.
string Description
Parameter description.