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.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 [DateOnly]
37 public DateTime? DefaultValue { get; set; }
38
42 [Page(2, "Script", 100)]
43 [Header(44, "Minimum Value:")]
44 [ToolTip(45, "The smallest value allowed.")]
45 [DateOnly]
46 public DateTime? Min { get; set; }
47
51 [Page(2, "Script", 100)]
52 [Header(46, "Maximum Value:")]
53 [ToolTip(47, "The largest value allowed.")]
54 [DateOnly]
55 public DateTime? Max { get; set; }
56
62 public override Task<string> GetTypeNameAsync(Language Language)
63 {
64 return Language.GetStringAsync(typeof(ScriptNode), 54, "Date and Time-valued parameter");
65 }
66
73 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
74 {
75 ValidationMethod Validation;
77
78 if (this.Min.HasValue || this.Max.HasValue)
79 {
80 Validation = new RangeValidation(
81 this.Min.HasValue ? XML.Encode(this.Min.Value, true) : null,
82 this.Max.HasValue ? XML.Encode(this.Max.Value, true) : null);
83 }
84 else
85 Validation = new BasicValidation();
86
87 if (this.RestrictToOptions)
88 {
89 Field = new ListSingleField(Parameters, this.ParameterName, this.Label, this.Required,
90 new string[] { this.DefaultValue.HasValue ? XML.Encode(this.DefaultValue.Value, true) : string.Empty },
91 await this.GetOptions(), this.Description, DateDataType.Instance, Validation, string.Empty, false, false, false);
92 }
93 else
94 {
96 new string[] { this.DefaultValue.HasValue ? XML.Encode(this.DefaultValue.Value, true) : string.Empty },
97 await this.GetOptions(), this.Description, DateDataType.Instance, Validation, string.Empty, false, false, false);
98 }
99
100 Parameters.Add(Field);
101
102 Page Page = Parameters.GetPage(this.Page);
103 Page.Add(Field);
104 }
105
115 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
117 {
119 if (Field is null)
120 {
121 if (this.Required)
122 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
123
124 Values[this.ParameterName] = null;
125 }
126 else
127 {
128 string s = Field.ValueString;
129
130 if (string.IsNullOrEmpty(s))
131 {
132 if (this.Required)
133 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
134
135 Values[this.ParameterName] = null;
136 }
137 else if (XML.TryParse(s, out DateTime Parsed))
138 {
139 Values[this.ParameterName] = Parsed.Date;
140
141 if (Parsed.TimeOfDay != TimeSpan.Zero)
142 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 55, "Only date acceptable."));
143 }
144 else
145 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 49, "Invalid value."));
146 }
147 }
148
149 }
150}
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 DateDataType Instance
Public instance of data type.
Definition: DateDataType.cs:13
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
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.
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.