Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptDateParameterNode.cs
1using System;
2using System.Threading.Tasks;
11using Waher.Script;
13
15{
20 {
25 : base()
26 {
27 }
28
32 [Page(2, "Script", 100)]
33 [Header(29, "Default value:")]
34 [ToolTip(30, "Default value presented to user.")]
35 [DateOnly]
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 [DateOnly]
45 public DateTime? Min { get; set; }
46
50 [Page(2, "Script", 100)]
51 [Header(46, "Maximum Value:")]
52 [ToolTip(47, "The largest value allowed.")]
53 [DateOnly]
54 public DateTime? Max { get; set; }
55
61 public override Task<string> GetTypeNameAsync(Language Language)
62 {
63 return Language.GetStringAsync(typeof(ScriptNode), 54, "Date and Time-valued parameter");
64 }
65
72 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
73 {
74 ValidationMethod Validation;
76
77 if (this.Min.HasValue || this.Max.HasValue)
78 {
79 Validation = new RangeValidation(
80 this.Min.HasValue ? XML.Encode(this.Min.Value, true) : null,
81 this.Max.HasValue ? XML.Encode(this.Max.Value, true) : null);
82 }
83 else
84 Validation = new BasicValidation();
85
86 if (this.RestrictToOptions)
87 {
88 Field = new ListSingleField(Parameters, this.ParameterName, this.Label, this.Required,
89 new string[] { this.DefaultValue.HasValue ? XML.Encode(this.DefaultValue.Value, true) : string.Empty },
90 await this.GetOptions(), this.Description, DateDataType.Instance, Validation, string.Empty, false, false, false);
91 }
92 else
93 {
95 new string[] { this.DefaultValue.HasValue ? XML.Encode(this.DefaultValue.Value, true) : string.Empty },
96 await this.GetOptions(), this.Description, DateDataType.Instance, Validation, string.Empty, false, false, false);
97 }
98
99 Parameters.Add(Field);
100
101 Page Page = Parameters.GetPage(this.Page);
102 Page.Add(Field);
103 }
104
114 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
116 {
118 if (Field is null)
119 {
120 if (this.Required)
121 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
122
123 Values[this.ParameterName] = null;
124 }
125 else
126 {
127 string s = Field.ValueString;
128
129 if (string.IsNullOrEmpty(s))
130 {
131 if (this.Required)
132 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
133
134 Values[this.ParameterName] = null;
135 }
136 else if (XML.TryParse(s, out DateTime Parsed))
137 {
138 Values[this.ParameterName] = Parsed.Date;
139
140 if (Parsed.TimeOfDay != TimeSpan.Zero)
141 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 55, "Only date acceptable."));
142 }
143 else
144 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 49, "Invalid value."));
145 }
146 }
147
148 }
149}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:892
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:26
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 DateDataType Instance
Public instance of data type.
Definition: DateDataType.cs:13
Base class for form fields
Definition: Field.cs:17
string ValueString
Value as a single string. If field contains multiple values, they will be concatenated into a single ...
Definition: Field.cs:102
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
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
ScriptDateParameterNode()
Represents a Date-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.
Represents a parameter with possible options on a command.
bool RestrictToOptions
If only values defined in options are valid values.
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.