Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptTextParameterNode.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Content;
12using Waher.Script;
14
16{
21 {
22 private string contentType;
23
28 : base()
29 {
30 }
31
35 [Page(2, "Script", 100)]
36 [Header(63, "Content-Type:")]
37 [ToolTip(64, "Content-Type of text.")]
38 [Required]
39 public string ContentType
40 {
41 get => this.contentType;
42 set
43 {
44 if (!string.IsNullOrEmpty(value) && (
47 {
48 throw new NotSupportedException("Content-Type not supported.");
49 }
50
51 this.contentType = value;
52 }
53 }
54
58 [Page(2, "Script", 100)]
59 [Header(29, "Default value:")]
60 [ToolTip(30, "Default value presented to user.")]
61 [DynamicContentType("GetContentType")]
62 public string[] DefaultValue { get; set; }
63
67 [Page(2, "Script", 100)]
68 [Header(68, "Minimum Count:")]
69 [ToolTip(69, "The smallest amount of items accepted.")]
70 public ushort? MinCount { get; set; }
71
75 [Page(2, "Script", 100)]
76 [Header(70, "Maximum Count:")]
77 [ToolTip(71, "The largest amount of items accepted.")]
78 public ushort? MaxCount { get; set; }
79
84 public string GetContentType()
85 {
86 return string.IsNullOrEmpty(this.contentType) ? PlainTextCodec.DefaultContentType : this.contentType;
87 }
88
94 public override Task<string> GetTypeNameAsync(Language Language)
95 {
96 return Language.GetStringAsync(typeof(ScriptNode), 65, "Text-valued parameter");
97 }
98
105 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
106 {
107 ValidationMethod Validation = new BasicValidation();
108 Field Field;
109
110 if (this.MinCount.HasValue || this.MaxCount.HasValue)
111 Validation = new ListRangeValidation(Validation, this.MinCount ?? 0, this.MaxCount ?? ushort.MaxValue);
112
113 if (this.RestrictToOptions)
114 {
115 Field = new ListMultiField(Parameters, this.ParameterName, this.Label, this.Required,
116 this.DefaultValue, await this.GetOptions(), this.Description, StringDataType.Instance, Validation, string.Empty,
117 false, false, false);
118 }
119 else
120 {
122 this.DefaultValue, await this.GetOptions(), this.Description, StringDataType.Instance, Validation, string.Empty,
123 false, false, false, this.GetContentType());
124 }
125
126 Parameters.Add(Field);
127
128 Page Page = Parameters.GetPage(this.Page);
129 Page.Add(Field);
130 }
131
141 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
143 {
145 if (Field is null)
146 {
147 if (this.Required)
148 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
149
150 Values[this.ParameterName] = null;
151 }
152 else
153 {
154 string[] s = Field.ValueStrings;
155
156 if (s is null || s.Length == 0)
157 {
158 if (this.Required)
159 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
160
161 Values[this.ParameterName] = null;
162 }
163 else
164 {
165 Values[this.ParameterName] = s;
166
167 if (this.MinCount.HasValue && s.Length < this.MinCount.Value)
168 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 72, "Too few rows."));
169 else if (this.MaxCount.HasValue && s.Length > this.MaxCount.Value)
170 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 73, "Too many rows."));
171 }
172 }
173 }
174
175 }
176}
Static class managing encoding and decoding of internet content.
static string[] CanDecodeContentTypes
Internet content types that can be decoded.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
static string[] CanEncodeContentTypes
Internet content types that can be encoded.
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 StringDataType Instance
Public instance of data type.
Base class for form fields
Definition: Field.cs:17
string[] ValueStrings
Values for the field (string representations).
Definition: Field.cs:97
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 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.
string ContentType
Internet Content-Type of text value.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
string GetContentType()
Gets the Content-Type of the text value of the parameter.
ScriptTextParameterNode()
Represents a text-valued script parameter.
Node defined by script.
Definition: ScriptNode.cs:19
bool Required
If parameter is required.
string Description
Parameter description.