59 lines
1.1 KiB
JavaScript
59 lines
1.1 KiB
JavaScript
import { ValueRequirement } from "./ValueRequirement.js";
|
|
import { Field } from "../Field.js";
|
|
|
|
export class Term extends Field {
|
|
separator = null;
|
|
isRequired = false;
|
|
position = null;
|
|
valueRequirement = ValueRequirement.REQUIRED;
|
|
|
|
constructor(key) {
|
|
super(key)
|
|
}
|
|
|
|
inputToString(fieldInputId) {
|
|
const input = this.getInputValue(fieldInputId);
|
|
const qualifier = this.getInputQualifier(fieldInputId);
|
|
|
|
return qualifier + this.key + (input ? this.separator + input : "");
|
|
}
|
|
|
|
isValidInput(fieldInputId) {
|
|
const input = this.getInputValue(fieldInputId);
|
|
const qualifier = this.getInputQualifier(fieldInputId);
|
|
|
|
|
|
if (this.valueRequirement !== ValueRequirement.REQUIRED && qualifier)
|
|
return true;
|
|
|
|
try {
|
|
return this.validate(input);
|
|
} catch(e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Virtual methods
|
|
|
|
getInputQualifier(fieldId) {
|
|
return "";
|
|
}
|
|
|
|
// Builder methods
|
|
|
|
required() {
|
|
this.isRequired = true;
|
|
return this;
|
|
}
|
|
|
|
pos(i) {
|
|
this.position = i;
|
|
return this;
|
|
}
|
|
|
|
value(requirement) {
|
|
this.valueRequirement = requirement;
|
|
return this;
|
|
}
|
|
}
|