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.Text.RegularExpressions;
3using System.Threading.Tasks;
4using Waher.Content;
13using Waher.Script;
15
17{
22 {
23 private string contentType;
24
29 : base()
30 {
31 }
32
36 [Page(2, "Script", 100)]
37 [Header(63, "Content-Type:")]
38 [ToolTip(64, "Content-Type of text.")]
39 [Required]
40 public string ContentType
41 {
42 get => this.contentType;
43 set
44 {
45 if (!string.IsNullOrEmpty(value) && (
48 {
49 throw new NotSupportedException("Content-Type not supported.");
50 }
51
52 this.contentType = value;
53 }
54 }
55
59 [Page(2, "Script", 100)]
60 [Header(29, "Default value:")]
61 [ToolTip(30, "Default value presented to user.")]
62 [DynamicContentType("GetContentType")]
63 public string[] DefaultValue { get; set; }
64
68 [Page(2, "Script", 100)]
69 [Header(68, "Minimum Count:")]
70 [ToolTip(69, "The smallest amount of items accepted.")]
71 public ushort? MinCount { get; set; }
72
76 [Page(2, "Script", 100)]
77 [Header(70, "Maximum Count:")]
78 [ToolTip(71, "The largest amount of items accepted.")]
79 public ushort? MaxCount { get; set; }
80
85 public string GetContentType()
86 {
87 return string.IsNullOrEmpty(this.contentType) ? PlainTextCodec.DefaultContentType : this.contentType;
88 }
89
95 public override Task<string> GetTypeNameAsync(Language Language)
96 {
97 return Language.GetStringAsync(typeof(ScriptNode), 65, "Text-valued parameter");
98 }
99
106 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
107 {
108 ValidationMethod Validation = new BasicValidation();
109 Field Field;
110
111 if (this.MinCount.HasValue || this.MaxCount.HasValue)
112 Validation = new ListRangeValidation(Validation, this.MinCount ?? 0, this.MaxCount ?? ushort.MaxValue);
113
114 if (this.RestrictToOptions)
115 {
116 Field = new ListMultiField(Parameters, this.ParameterName, this.Label, this.Required,
117 this.DefaultValue, await this.GetOptions(), this.Description, StringDataType.Instance, Validation, string.Empty,
118 false, false, false);
119 }
120 else
121 {
123 this.DefaultValue, await this.GetOptions(), this.Description, StringDataType.Instance, Validation, string.Empty,
124 false, false, false, this.GetContentType());
125 }
126
127 Parameters.Add(Field);
128
129 Page Page = Parameters.GetPage(this.Page);
130 Page.Add(Field);
131 }
132
142 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
144 {
146 if (Field is null)
147 {
148 if (this.Required)
149 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
150
151 Values[this.ParameterName] = null;
152 }
153 else
154 {
155 string[] s = Field.ValueStrings;
156
157 if (s is null || s.Length == 0)
158 {
159 if (this.Required)
160 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
161
162 Values[this.ParameterName] = null;
163 }
164 else
165 {
166 Values[this.ParameterName] = s;
167
168 if (this.MinCount.HasValue && s.Length < this.MinCount.Value)
169 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 72, "Too few rows."));
170 else if (this.MaxCount.HasValue && s.Length > this.MaxCount.Value)
171 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 73, "Too many rows."));
172 }
173 }
174 }
175
176 }
177}
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: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 StringDataType Instance
Public instance of data type.
Base class for form fields
Definition: Field.cs:16
string[] ValueStrings
Values for the field (string representations).
Definition: Field.cs:96
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.
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.