File size: 14,826 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | <template>
<span>
<span v-for="formElem of modelValue" v-bind:key="formElem.id">
<!-- a form group (accordion) -->
<FormWrapper v-if="formElem.type === FormElementType.Group && !disabled(formElem)"
:cls="'border-0 mt-' + spacing">
<Accordian :id="formElem.id">
<AccordianItem :id="itemId(formElem)" :title="formElem.label" :showInitially="asGroup(formElem).startOpened">
<!-- :hideIfDisabled="hideIfDisabled" -->
<FormFull v-model="asGroup(formElem).val" :id="id" @onChange="onDataUpdated"
@onMolCountsChanged="onMolCountsChanged"></FormFull>
</AccordianItem>
</Accordian>
</FormWrapper>
<FormWrapper v-else-if="!hideIfDisabled || makeGeneric(formElem).enabled" :label="labelToUse(formElem)"
:cls="'border-0 mt-' + spacing" :disabled="disabled(formElem)">
<!-- any of the other form elements (not group) -->
<FormInput v-if="formElem.type === FormElementType.Text" type="text" v-model="makeGeneric(formElem).val"
:placeHolder="getPlaceHolder(formElem)" :filterFunc="makeGeneric(formElem).filterFunc"
:warningFunc="makeGeneric(formElem).warningFunc" @onChange="onDataUpdated" :id="itemId(formElem)"
:disabled="disabled(formElem)" :description="makeGeneric(formElem).description"
:delayBetweenChangesDetected="makeGeneric(formElem).delayBetweenChangesDetected" />
<FormInput v-else-if="formElem.type === FormElementType.Number" type="number"
v-model.number="makeGeneric(formElem).val" :placeHolder="getPlaceHolder(formElem)"
:filterFunc="makeGeneric(formElem).filterFunc" :warningFunc="makeGeneric(formElem).warningFunc"
@onChange="onDataUpdated" :id="itemId(formElem)" :disabled="disabled(formElem)"
:description="makeGeneric(formElem).description"
:delayBetweenChangesDetected="makeGeneric(formElem).delayBetweenChangesDetected" />
<FormInput v-else-if="formElem.type === FormElementType.Color" type="color"
v-model.number="makeGeneric(formElem).val" @onChange="onDataUpdated" :id="itemId(formElem)"
:disabled="disabled(formElem)" :description="makeGeneric(formElem).description"
:delayBetweenChangesDetected="makeGeneric(formElem).delayBetweenChangesDetected"
:warningFunc="makeGeneric(formElem).warningFunc" />
<FormInput v-else-if="formElem.type === FormElementType.Range" v-model.number="makeGeneric(formElem).val"
type="range" :min="getRangeMinMaxStep(formElem).min" :max="getRangeMinMaxStep(formElem).max"
:step="getRangeMinMaxStep(formElem).step" @onChange="onDataUpdated" :id="itemId(formElem)"
:disabled="disabled(formElem)" :description="makeGeneric(formElem).description"
:delayBetweenChangesDetected="0" :warningFunc="makeGeneric(formElem).warningFunc" />
<FormTextArea v-else-if="formElem.type === FormElementType.TextArea" v-model="makeGeneric(formElem).val"
@onChange="onDataUpdated" :id="itemId(formElem)" :disabled="disabled(formElem)"
:description="makeGeneric(formElem).description" :delayBetweenChangesDetected="0"
:placeHolder="getPlaceHolder(formElem)"></FormTextArea>
<FormSelect v-else-if="formElem.type === FormElementType.Select" v-model="makeGeneric(formElem).val"
:options="getSelectOptions(formElem)" @onChange="onDataUpdated" :id="itemId(formElem)"
:disabled="disabled(formElem)" :description="makeGeneric(formElem).description" />
<FormCheckBox v-else-if="formElem.type === FormElementType.Checkbox" v-model="makeGeneric(formElem).val"
:text="validateLabel(makeGeneric(formElem).label)" @onChange="onDataUpdated" :id="itemId(formElem)"
:disabled="disabled(formElem)" :description="makeGeneric(formElem).description" />
<MoleculeInputParams v-else-if="
formElem.type === FormElementType.MoleculeInputParams &&
makeGeneric(formElem).val.molsToConsider !== undefined" v-model="makeGeneric(formElem).val"
@onChange="onDataUpdated" :id="itemId(formElem)" :disabled="disabled(formElem)" :tag="id"
@onMolCountsChanged="onMolCountsChanged" :text="validateLabel(makeGeneric(formElem).label)">
</MoleculeInputParams>
<FormVector3D v-else-if="formElem.type === FormElementType.Vector3D" v-model="makeGeneric(formElem).val"
:placeHolder="getPlaceHolder(formElem)" @onChange="onDataUpdated" :id="itemId(formElem)"
:filterFunc="makeGeneric(formElem).filterFunc" :warningFunc="makeGeneric(formElem).warningFunc"
:disabled="disabled(formElem)" :description="makeGeneric(formElem).description"
:delayBetweenChangesDetected="0" />
<Alert v-else-if="formElem.type === FormElementType.Alert" :id="itemId(formElem)"
:type="makeGeneric(formElem).alertType" extraClasses="mt-2 mb-0">
<span v-if="formElem.val">
{{ formElem.val }}
</span>
</Alert>
<!-- extraClasses="mt-2 mb-0" -->
<!-- :placeHolder="makeGeneric(formElem).placeHolder" -->
<!-- @onChange="onDataUpdated" -->
<!-- :filterFunc="makeGeneric(formElem).filterFunc" -->
<FormSelectRegion v-else-if="formElem.type === FormElementType.SelectRegion" v-model="makeGeneric(formElem).val"
:id="itemId(formElem)" :disabled="disabled(formElem)" :description="makeGeneric(formElem).description"
:regionName="makeGeneric(formElem).regionName" :warningFunc="makeGeneric(formElem).warningFunc">
</FormSelectRegion>
<FormListSelect v-else-if="formElem.type === FormElementType.ListSelect" v-model="makeGeneric(formElem).val"
:id="itemId(formElem)" :inputType="makeGeneric(formElem).inputType" :options="makeGeneric(formElem).options"
:description="makeGeneric(formElem).description" :disabled="disabled(formElem)"
:placeHolder="getPlaceHolder(formElem)" :warningFunc="makeGeneric(formElem).warningFunc"
:validateFunc="makeGeneric(formElem).validateFunc"
:delayBetweenChangesDetected="makeGeneric(formElem).delayBetweenChangesDetected" @onChange="onDataUpdated"
@onRawValChange="(val) => onRawValChange(formElem.id, val)" />
<FormSelectMolecule v-else-if="formElem.type === FormElementType.SelectMolecule"
v-model="makeGeneric(formElem).val" :id="itemId(formElem)" :disabled="disabled(formElem)"
:description="makeGeneric(formElem).description" :filterType="makeGeneric(formElem).filterType"
@onChange="onDataUpdated" />
</FormWrapper>
</span>
</span>
</template>
<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import FormInput from "../FormInput.vue";
import FormWrapper from "../FormWrapper.vue";
import FormSelect from "../FormSelect.vue";
import {
UserArg,
UserArgType,
IUserArgGroup,
IUserArgMoleculeInputParams,
IUserArgOption,
IUserArgRange,
IUserArgSelect,
} from "./FormFullInterfaces";
import Accordian from "@/UI/Layout/Accordian/Accordian.vue";
import AccordianItem from "@/UI/Layout/Accordian/AccordianItem.vue";
import MoleculeInputParams from "../MoleculeInputParams/MoleculeInputParams.vue";
import FormCheckBox from "../FormCheckBox.vue";
import FormVector3D from "../FormVector3D.vue";
import Alert from "@/UI/Layout/Alert.vue";
import FormSelectRegion from "../FormSelectRegion/FormSelectRegion.vue";
import FormTextArea from "../FormTextArea.vue";
import FormListSelect from "../FormListSelect.vue";
import { isSentence } from "@/Core/Utils/StringUtils";
import { IProtCmpdCounts } from "../MoleculeInputParams/MoleculeInput";
import FormSelectMolecule from "../FormSelectMolecule/FormSelectMolecule.vue";
/**
* FormFull
*/
@Options({
components: {
FormWrapper,
FormInput,
FormSelect,
FormTextArea,
Accordian,
AccordianItem,
MoleculeInputParams,
FormCheckBox,
FormVector3D,
Alert,
FormSelectRegion,
FormListSelect,
FormSelectMolecule,
},
emits: ["update:modelValue", "onRawValChange", "onMolCountsChanged"],
})
export default class FormFull extends Vue {
@Prop({ required: true }) modelValue!: UserArg[];
@Prop({ required: true }) id!: string;
@Prop({ default: false }) hideIfDisabled!: boolean;
@Prop({ default: "3" }) spacing!: string;
FormElementType = UserArgType; // So accessible in template
/**
* Get the options for a select element.
*
* @param {any} val The form element.
* @returns {(string | IUserArgOption)[]} The options.
*/
getSelectOptions(val: any): (string | IUserArgOption)[] {
return (val as IUserArgSelect).options;
}
/**
* Get the min, max, and step for a range element.
*
* @param {any} val Contains information about the range (like IUserArgRange).
* @returns {any} The min, max, and step.
*/
getRangeMinMaxStep(val: any): any {
let val2 = val as IUserArgRange;
return {
min: val2.min,
max: val2.max,
step: val2.step,
};
}
/**
* Get the DOM id for a form element.
*
* @param {UserArg} formElem The form element.
* @returns {string} The DOM id.
*/
itemId(formElem: UserArg): string {
return `${formElem.id}-${this.id}-item`;
}
/**
* Determine if a given form element is currently disabled.
*
* @param {UserArg} formElem The form element.
* @returns {boolean} True if disabled, false otherwise.
*/
disabled(formElem: UserArg): boolean {
return this.makeGeneric(formElem).enabled === false;
}
/**
* Cast the form element to an IFormTextOrColor. Just to make typing work in
* template...
*
* @param {any} frmElem The form element.
* @returns {IUserArgGroup} The IFormTextOrColor.
*/
makeGeneric(frmElem: UserArg): any {
return frmElem as any;
}
/**
* Cast the form element to an IUserArgMoleculeInputParams. Just to make typing work
* in template...
*
* @param {any} val The form element.
* @returns {IUserArgGroup} The IUserArgMoleculeInputParams.
*/
allowConsiderCompounds(val: UserArg): IUserArgMoleculeInputParams {
return val as IUserArgMoleculeInputParams;
}
/**
* Cast the form element to an IUserArgGroup. Just to make typing work in
* template...
*
* @param {UserArg} val The form element.
* @returns {IUserArgGroup} The IUserArgGroup.
*/
asGroup(val: UserArg): IUserArgGroup {
return val as IUserArgGroup;
}
/**
* Validate a label.
*
* @param {string | undefined} label The label to validate.
* @returns {string | undefined} The label to use.
*/
validateLabel(label: string | undefined): string | undefined {
if (label === undefined) {
return label;
}
// Do some validation on the label. Label should not end in
// punctuation
if (
(label.endsWith(".") || label.endsWith("!") || label.endsWith("?")) &&
!label.endsWith("etc.")
) {
throw new Error(
`FormFull: Label should not end in punctuation: ${label} Use description for extended explanations.`
);
}
// Must start with capital letter
if (label[0] !== label[0].toUpperCase()) {
throw new Error(
`FormFull: Label must start with capital letter: ${label}`
);
}
// Words with more than three letters
let words = label.split(" ").filter((x) => x.length > 3);
// Remove some punctuation from the start of words
words = words.map((x) => {
return x.replace("(", "").replace("[", "").replace("{", "").replace("'", "").replace('"', "");
});
if (words.length > 1) {
// Percentage of words that are capitalized
let pct =
words.filter((x) => x[0] === x[0].toUpperCase()).length / words.length;
if (pct > 0.6) {
throw new Error(
`FormFull: At least 60% of big words in label must be lower case: ${label}. Current percent: ${100 * pct
}%.`
);
}
}
return label;
}
/**
* Get the label to use for a form element.
*
* @param {UserArg} formElem The form element.
* @returns {string | undefined} The label to use.
*/
labelToUse(formElem: UserArg): string | undefined {
if (
formElem.type === UserArgType.Checkbox ||
formElem.type === UserArgType.MoleculeInputParams
) {
// For checkbox and molinputparams, label is the text
return "";
}
if (formElem.label) {
// if (formElem.type === UserArgType.Range) {
// return `${formElem.label} (${this.makeGeneric(formElem).val})`;
// }
return this.validateLabel(formElem.label);
}
return "";
}
/**
* When data is updated, emit the new data.
*/
onDataUpdated() {
// this.$emit("onChange", this.modelValue); // TODO: Not sure this is needed. Does it break anything anywhere?
this.$emit("update:modelValue", this.modelValue);
}
/**
* Get the placeholder to use for a form element.
*
* @param {UserArg} formElem The form element.
* @returns {string} The placeholder to use.
*/
getPlaceHolder(formElem: any): string {
// If placeholder given, use that.
if (formElem.placeHolder !== undefined) {
// Placeholder must end in ...
if (!formElem.placeHolder.endsWith("...")) {
throw new Error(
`FormFull: Placeholder must end in ...: ${formElem.placeHolder}`
);
}
// Placeholder must be sentence case
if (isSentence(formElem.placeHolder) === false) {
throw new Error(
`FormFull: Placeholder must be sentence case: ${formElem.placeHolder}`
);
}
// Place holder should be a noun, not a verb
if (formElem.placeHolder.toLowerCase().startsWith("enter")) {
throw new Error(
`FormFull: Placeholder should be a noun, not a verb: ${formElem.placeHolder}`
);
}
return formElem.placeHolder;
}
// Otherwise, use the label
return (this.labelToUse(formElem) as string) + "...";
}
/**
* When the molecule counts change, emit the new counts.
*
* @param {IProtCmpdCounts} val The new counts.
*/
onMolCountsChanged(val: IProtCmpdCounts) {
this.$emit("onMolCountsChanged", val);
}
/**
* Called when the raw value of a user argument changes.
*
* @param {string} id The ID of the user argument.
* @param {string} val The new value for the user argument.
*/
onRawValChange(id: string, val: string): void {
// Emit the raw string value for FormListSelect
this.$emit("onRawValChange", id, val);
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss"></style>
|