Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptTimeParameterNode.cs
1using System;
2using System.Threading.Tasks;
10using Waher.Script;
12
14{
19 {
24 : base()
25 {
26 }
27
31 [Page(2, "Script", 100)]
32 [Header(29, "Default value:")]
33 [ToolTip(30, "Default value presented to user.")]
34 public TimeSpan? DefaultValue { get; set; }
35
39 [Page(2, "Script", 100)]
40 [Header(44, "Minimum Value:")]
41 [ToolTip(45, "The smallest value allowed.")]
42 public TimeSpan? Min { get; set; }
43
47 [Page(2, "Script", 100)]
48 [Header(46, "Maximum Value:")]
49 [ToolTip(47, "The largest value allowed.")]
50 public TimeSpan? Max { get; set; }
51
57 public override Task<string> GetTypeNameAsync(Language Language)
58 {
59 return Language.GetStringAsync(typeof(ScriptNode), 56, "Time-valued parameter");
60 }
61
68 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
69 {
70 ValidationMethod Validation;
72
73 if (this.Min.HasValue || this.Max.HasValue)
74 Validation = new RangeValidation(this.Min?.ToString(), this.Max?.ToString());
75 else
76 Validation = new BasicValidation();
77
78 if (this.RestrictToOptions)
79 {
80 Field = new ListSingleField(Parameters, this.ParameterName, this.Label, this.Required,
81 new string[] { this.DefaultValue?.ToString() ?? string.Empty }, await this.GetOptions(), this.Description,
82 TimeDataType.Instance, Validation, string.Empty, false, false, false);
83 }
84 else
85 {
87 new string[] { this.DefaultValue?.ToString() ?? string.Empty }, await this.GetOptions(), this.Description,
88 TimeDataType.Instance, Validation, string.Empty, false, false, false);
89 }
90
91 Parameters.Add(Field);
92
93 Page Page = Parameters.GetPage(this.Page);
94 Page.Add(Field);
95 }
96
106 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
108 {
110 if (Field is null)
111 {
112 if (this.Required)
113 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
114
115 Values[this.ParameterName] = null;
116 }
117 else
118 {
119 string s = Field.ValueString;
120
121 if (string.IsNullOrEmpty(s))
122 {
123 if (this.Required)
124 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
125
126 Values[this.ParameterName] = null;
127 }
128 else if (TimeSpan.TryParse(s, out TimeSpan Parsed))
129 Values[this.ParameterName] = Parsed;
130 else
131 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 49, "Invalid value."));
132 }
133 }
134
135 }
136}
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 TimeDataType Instance
Public instance of data type.
Definition: TimeDataType.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
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.
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 async Task PopulateForm(DataForm Parameters, Language Language, object Value)
Populates a data form with parameters for the object.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
ScriptTimeParameterNode()
Represents a Time-valued script parameter.
Node defined by script.
Definition: ScriptNode.cs:19
bool Required
If parameter is required.
string Description
Parameter description.