From fb8d67b4c46ce6ca65fc774108c0de361851fcbb Mon Sep 17 00:00:00 2001 From: Max Chopart Date: Tue, 9 Apr 2024 16:14:39 +0200 Subject: [PATCH] [Upd #279] Split constants and vocabulary (model) into two distinct files --- .storybook/preview.js | 2 +- src/components/Answer.jsx | 28 +-- src/components/MediaContent.tsx | 4 +- src/components/PrefixIcon.jsx | 9 +- src/components/Question.jsx | 68 +++---- src/components/answer/InputAnswer.jsx | 66 +++---- src/components/answer/MaskedInputAnswer.jsx | 4 +- src/components/answer/SelectAnswer.jsx | 12 +- src/components/answer/TypeaheadAnswer.jsx | 14 +- src/components/comment/CommentList.tsx | 10 +- src/components/comment/CommentView.jsx | 2 +- .../comment/QuestionCommentIcon.jsx | 18 +- src/components/wizard/Wizard.jsx | 4 +- src/constants/Constants.js | 156 ---------------- src/constants/Vocabulary.js | 154 ++++++++++++++++ src/model/FormGenerator.js | 8 +- src/model/QuestionAnswerProcessor.js | 48 ++--- src/model/ValidationProcessor.js | 10 +- src/model/ValidatorFactory.js | 54 +++--- src/s-forms.js | 4 +- src/util/FormUtils.js | 171 +++++++++--------- src/util/JsonLdFramingUtils.js | 16 +- src/util/Utils.js | 4 +- test/__tests__/Answer.test.js | 77 ++++---- test/__tests__/DefaultFormGenerator.test.js | 8 +- test/__tests__/FormUtils.test.js | 59 +++--- test/__tests__/InputAnswer.test.js | 46 ++--- test/__tests__/MaskedInputAnswer.test.js | 20 +- .../__tests__/QuestionAnswerProcessor.test.js | 72 ++++---- test/__tests__/TypeheadAnswer.test.js | 16 +- 30 files changed, 591 insertions(+), 573 deletions(-) create mode 100644 src/constants/Vocabulary.js diff --git a/.storybook/preview.js b/.storybook/preview.js index 90986529..c09b263a 100644 --- a/.storybook/preview.js +++ b/.storybook/preview.js @@ -1,4 +1,4 @@ -import Constants from "../src/constants/Constants"; +import Constants from "../src/constants/Constants.js"; import IntlContextProvider from "../src/contexts/IntlContextProvider"; import { FormGenContextProvider } from "../src/contexts/FormGenContext"; import { ConfigurationContextProvider } from "../src/contexts/ConfigurationContext"; diff --git a/src/components/Answer.jsx b/src/components/Answer.jsx index 60c8fcde..59ffff68 100644 --- a/src/components/Answer.jsx +++ b/src/components/Answer.jsx @@ -9,7 +9,7 @@ import SelectAnswer from "./answer/SelectAnswer"; import FormUtils from "../util/FormUtils"; import Utils from "../util/Utils"; import TypeaheadAnswer from "./answer/TypeaheadAnswer"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; import { FormGenContext } from "../contexts/FormGenContext"; import { ConfigurationContext } from "../contexts/ConfigurationContext"; import QuestionStatic from "./QuestionStatic.jsx"; @@ -36,17 +36,17 @@ const Answer = (props) => { const _setValue = (change, value) => { if (value === null) { - change[Constants.HAS_OBJECT_VALUE] = null; - change[Constants.HAS_DATA_VALUE] = null; + change[Vocabulary.HAS_OBJECT_VALUE] = null; + change[Vocabulary.HAS_DATA_VALUE] = null; } else if ( - props.answer[Constants.HAS_OBJECT_VALUE] || + props.answer[Vocabulary.HAS_OBJECT_VALUE] || FormUtils.isTypeahead(props.question) ) { - change[Constants.HAS_OBJECT_VALUE] = { + change[Vocabulary.HAS_OBJECT_VALUE] = { "@id": value, }; } else { - change[Constants.HAS_DATA_VALUE] = { + change[Vocabulary.HAS_DATA_VALUE] = { "@value": value, }; } @@ -54,7 +54,7 @@ const Answer = (props) => { const _hasOptions = (item) => { return ( - item[Constants.HAS_OPTION] && item[Constants.HAS_OPTION].length !== 0 + item[Vocabulary.HAS_OPTION] && item[Vocabulary.HAS_OPTION].length !== 0 ); }; @@ -177,7 +177,7 @@ const Answer = (props) => { const _getLabel = (question) => { const label = JsonldUtils.getLocalized( - question[Constants.RDFS_LABEL], + question[Vocabulary.RDFS_LABEL], options.intl ); @@ -200,17 +200,17 @@ const Answer = (props) => { classname: "", message: null, }); - if (question[Constants.HAS_VALID_ANSWER] === false) { + if (question[Vocabulary.HAS_VALID_ANSWER] === false) { if ( - question[Constants.HAS_VALIDATION_SEVERITY] === - Constants.VALIDATION_SEVERITY.WARNING + question[Vocabulary.HAS_VALIDATION_SEVERITY] === + Vocabulary.VALIDATION_SEVERITY.WARNING ) { setValidation({ severity: "warning", classname: "is-warning", message: ( - {question[Constants.HAS_VALIDATION_MESSAGE]} + {question[Vocabulary.HAS_VALIDATION_MESSAGE]} ), }); @@ -220,7 +220,7 @@ const Answer = (props) => { classname: "is-invalid", message: ( - {question[Constants.HAS_VALIDATION_MESSAGE]} + {question[Vocabulary.HAS_VALIDATION_MESSAGE]} ), }); @@ -233,7 +233,7 @@ const Answer = (props) => { const label = _getLabel(question); const title = JsonldUtils.getLocalized( - question[Constants.RDFS_COMMENT], + question[Vocabulary.RDFS_COMMENT], options.intl ); let component; diff --git a/src/components/MediaContent.tsx b/src/components/MediaContent.tsx index 18391823..ddde746c 100644 --- a/src/components/MediaContent.tsx +++ b/src/components/MediaContent.tsx @@ -1,5 +1,5 @@ import React from "react"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary"; interface Props { question: object; @@ -8,7 +8,7 @@ interface Props { const MediaContent = ({ question }: Props) => { const renderMedia = () => { // @ts-ignore - const mediaContent = question[Constants.HAS_MEDIA_CONTENT]; + const mediaContent = question[Vocabulary.HAS_MEDIA_CONTENT]; if (mediaContent) { if (Array.isArray(mediaContent)) { diff --git a/src/components/PrefixIcon.jsx b/src/components/PrefixIcon.jsx index 128b2176..c2a10919 100644 --- a/src/components/PrefixIcon.jsx +++ b/src/components/PrefixIcon.jsx @@ -6,7 +6,7 @@ import { OverlayTrigger, Popover, } from "react-bootstrap"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; class PrefixIcon extends React.Component { constructor(props) { @@ -22,13 +22,14 @@ class PrefixIcon extends React.Component { {this.state.prefixes .sort((l, r) => { - const res = l[Constants.PREFIX] < r[Constants.PREFIX]; + const res = l[Vocabulary.PREFIX] < r[Vocabulary.PREFIX]; if (res) return -1; return 1; }) .map((p) => ( - - {p[Constants.PREFIX]}: {p[Constants.NAMESPACE]} + + {p[Vocabulary.PREFIX]}:{" "} + {p[Vocabulary.NAMESPACE]} ))} diff --git a/src/components/Question.jsx b/src/components/Question.jsx index 8d5945e0..7556aab9 100644 --- a/src/components/Question.jsx +++ b/src/components/Question.jsx @@ -3,7 +3,7 @@ import { Card, Accordion } from "react-bootstrap"; import * as JsonLdUtils from "jsonld-utils"; import PropTypes from "prop-types"; import Answer from "./Answer"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; import FormUtils from "../util/FormUtils"; import JsonLdObjectMap from "../util/JsonLdObjectMap"; import QuestionAnswerProcessor from "../model/QuestionAnswerProcessor"; @@ -43,7 +43,7 @@ export default class Question extends React.Component { componentDidUpdate() { const question = this.props.question; const startingQuestionId = this.context.options.startingQuestionId; - const subQuestions = question[Constants.HAS_SUBQUESTION]; + const subQuestions = question[Vocabulary.HAS_SUBQUESTION]; const isSubQuestionStartingQuestionId = subQuestions.find( (o) => o["@id"] === startingQuestionId ); @@ -71,21 +71,21 @@ export default class Question extends React.Component { this.setState({ expanded: expanded }); } - this._handleChange(Constants.HAS_ANSWER, answerIndex, change); + this._handleChange(Vocabulary.HAS_ANSWER, answerIndex, change); }; handleSubQuestionChange = (subQuestionIndex, change) => { - this._handleChange(Constants.HAS_SUBQUESTION, subQuestionIndex, change); + this._handleChange(Vocabulary.HAS_SUBQUESTION, subQuestionIndex, change); }; handleCommentChange = (commentIndex, change) => { - this._handleChange(Constants.HAS_COMMENT, commentIndex, change); + this._handleChange(Vocabulary.HAS_COMMENT, commentIndex, change); }; _handleChange(att, valueIndex, newValue) { let newState = { ...this.props.question }; newState[att][valueIndex] = newValue; - if (att === Constants.HAS_ANSWER) { + if (att === Vocabulary.HAS_ANSWER) { const result = this.state.validator(newValue); newState = { ...newState, ...result }; } @@ -320,7 +320,7 @@ export default class Question extends React.Component { getShowIrrelevantClassname(question) { const debugMode = this.context.options.debugMode; const startingQuestionId = this.context.options.startingQuestionId; - const subQuestion = question[Constants.HAS_SUBQUESTION]; + const subQuestion = question[Vocabulary.HAS_SUBQUESTION]; if ( (debugMode || @@ -378,27 +378,27 @@ export default class Question extends React.Component { _getAnswers() { const question = this.props.question; - if (!question[Constants.HAS_ANSWER]) { - question[Constants.HAS_ANSWER] = []; + if (!question[Vocabulary.HAS_ANSWER]) { + question[Vocabulary.HAS_ANSWER] = []; } - if (!Array.isArray(question[Constants.HAS_ANSWER])) { - question[Constants.HAS_ANSWER] = [question[Constants.HAS_ANSWER]]; + if (!Array.isArray(question[Vocabulary.HAS_ANSWER])) { + question[Vocabulary.HAS_ANSWER] = [question[Vocabulary.HAS_ANSWER]]; } - if (question[Constants.HAS_ANSWER].length === 0) { + if (question[Vocabulary.HAS_ANSWER].length === 0) { if (FormUtils.isSection(question) && !FormUtils.isAnswerable(question)) { - question[Constants.HAS_ANSWER] = []; + question[Vocabulary.HAS_ANSWER] = []; } else { - question[Constants.HAS_ANSWER] = [ + question[Vocabulary.HAS_ANSWER] = [ QuestionAnswerProcessor.generateAnswer(question), ]; } } - return question[Constants.HAS_ANSWER]; + return question[Vocabulary.HAS_ANSWER]; } _getAnswerWidthStyle() { const length = Number( - this.props.question[Constants.HAS_INITIAL_INPUT_LENGTH] + this.props.question[Vocabulary.HAS_INITIAL_INPUT_LENGTH] ); if (!length) { return {}; @@ -414,9 +414,9 @@ export default class Question extends React.Component { static _getAnswerClass(question, isTextarea) { let columns = isTextarea ? "col-12" - : Constants.GENERATED_ROW_SIZE === 1 + : Vocabulary.GENERATED_ROW_SIZE === 1 ? "col-6" - : "col-" + Constants.COLUMN_COUNT / Constants.GENERATED_ROW_SIZE; + : "col-" + Vocabulary.COLUMN_COUNT / Vocabulary.GENERATED_ROW_SIZE; return columns; } @@ -434,8 +434,8 @@ export default class Question extends React.Component { if ( JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.EMPHASISE_ON_RELEVANT + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.EMPHASISE_ON_RELEVANT ) ) { return "emphasise-on-relevant"; @@ -464,10 +464,10 @@ export default class Question extends React.Component { _renderPrefixes() { const question = this.props.question; - return question[Constants.HAS_DECLARED_PREFIX] && - question[Constants.HAS_DECLARED_PREFIX].length ? ( + return question[Vocabulary.HAS_DECLARED_PREFIX] && + question[Vocabulary.HAS_DECLARED_PREFIX].length ? ( @@ -477,8 +477,8 @@ export default class Question extends React.Component { _renderUnits() { const question = this.props.question; - return question[Constants.HAS_UNIT] ? ( -
{question[Constants.HAS_UNIT]}
+ return question[Vocabulary.HAS_UNIT] ? ( +
{question[Vocabulary.HAS_UNIT]}
) : null; } @@ -512,28 +512,28 @@ export default class Question extends React.Component { _getSubQuestions() { const question = this.props.question; - if (!question[Constants.HAS_SUBQUESTION]) { - question[Constants.HAS_SUBQUESTION] = []; + if (!question[Vocabulary.HAS_SUBQUESTION]) { + question[Vocabulary.HAS_SUBQUESTION] = []; } - if (!Array.isArray(question[Constants.HAS_SUBQUESTION])) { - question[Constants.HAS_SUBQUESTION] = [ - question[Constants.HAS_SUBQUESTION], + if (!Array.isArray(question[Vocabulary.HAS_SUBQUESTION])) { + question[Vocabulary.HAS_SUBQUESTION] = [ + question[Vocabulary.HAS_SUBQUESTION], ]; } // sort by label JsonLdObjectUtils.orderByLocalizedLabels( - question[Constants.HAS_SUBQUESTION], + question[Vocabulary.HAS_SUBQUESTION], this.context.options.intl ); // sort by property JsonLdObjectUtils.orderPreservingToplogicalSort( - question[Constants.HAS_SUBQUESTION], - Constants.HAS_PRECEDING_QUESTION + question[Vocabulary.HAS_SUBQUESTION], + Vocabulary.HAS_PRECEDING_QUESTION ); - return question[Constants.HAS_SUBQUESTION]; + return question[Vocabulary.HAS_SUBQUESTION]; } _getFirstAnswerValue() { diff --git a/src/components/answer/InputAnswer.jsx b/src/components/answer/InputAnswer.jsx index 422545d4..bc0e0014 100644 --- a/src/components/answer/InputAnswer.jsx +++ b/src/components/answer/InputAnswer.jsx @@ -1,7 +1,7 @@ import React, { useContext } from "react"; import * as JsonLdUtils from "jsonld-utils"; import PropTypes from "prop-types"; -import Constants from "../../constants/Constants"; +import Vocabulary from "../../constants/Vocabulary.js"; import FormUtils from "../../util/FormUtils"; import YASQE from "yasgui-yasqe"; import YATE from "yasgui-yasqe"; @@ -9,22 +9,22 @@ import { ConfigurationContext } from "../../contexts/ConfigurationContext"; import "@triply/yasgui/build/yasgui.min.css"; const NUMERIC_DATATYPES = [ - Constants.XSD.INT, - Constants.XSD.INTEGER, - Constants.XSD.NON_NEGATIVE_INTEGER, - Constants.XSD.NON_POSITIVE_INTEGER, - Constants.XSD.NEGATIVE_INTEGER, - Constants.XSD.POSITIVE_INTEGER, + Vocabulary.XSD.INT, + Vocabulary.XSD.INTEGER, + Vocabulary.XSD.NON_NEGATIVE_INTEGER, + Vocabulary.XSD.NON_POSITIVE_INTEGER, + Vocabulary.XSD.NEGATIVE_INTEGER, + Vocabulary.XSD.POSITIVE_INTEGER, ]; const DECLARED_PREFIXES = "http://onto.fel.cvut.cz/ontologies/form-spin/has-declared-prefix"; const NUMBER_RULES = {}; -NUMBER_RULES[Constants.XSD.NON_NEGATIVE_INTEGER] = { min: 0 }; -NUMBER_RULES[Constants.XSD.NON_POSITIVE_INTEGER] = { max: 0 }; -NUMBER_RULES[Constants.XSD.NEGATIVE_INTEGER] = { max: -1 }; -NUMBER_RULES[Constants.XSD.POSITIVE_INTEGER] = { min: 1 }; +NUMBER_RULES[Vocabulary.XSD.NON_NEGATIVE_INTEGER] = { min: 0 }; +NUMBER_RULES[Vocabulary.XSD.NON_POSITIVE_INTEGER] = { max: 0 }; +NUMBER_RULES[Vocabulary.XSD.NEGATIVE_INTEGER] = { max: -1 }; +NUMBER_RULES[Vocabulary.XSD.POSITIVE_INTEGER] = { min: 1 }; const tokenTypes = { "string-2": "prefixed", @@ -50,7 +50,7 @@ class InputPropertiesResolver { if ( JsonLdUtils.hasValue( question, - Constants.HAS_DATATYPE, + Vocabulary.HAS_DATATYPE, NUMERIC_DATATYPES[i] ) ) { @@ -85,10 +85,10 @@ class InputPropertiesResolver { static _resolveStepRestriction(question) { let restriction = {}; - if (question[Constants.STEP] !== undefined) { + if (question[Vocabulary.STEP] !== undefined) { restriction["step"] = JsonLdUtils.getJsonAttValue( question, - Constants.STEP + Vocabulary.STEP ); } return restriction; @@ -97,21 +97,21 @@ class InputPropertiesResolver { static _resolveNumberRestrictions(question) { let restriction = {}; Object.getOwnPropertyNames(NUMBER_RULES).forEach((key) => { - if (JsonLdUtils.hasValue(question, Constants.HAS_DATATYPE, key)) { + if (JsonLdUtils.hasValue(question, Vocabulary.HAS_DATATYPE, key)) { restriction = { ...restriction, ...NUMBER_RULES[key] }; } }); - if (question[Constants.XSD.MIN_INCLUSIVE] !== undefined) { - restriction["min"] = question[Constants.XSD.MIN_INCLUSIVE]; + if (question[Vocabulary.XSD.MIN_INCLUSIVE] !== undefined) { + restriction["min"] = question[Vocabulary.XSD.MIN_INCLUSIVE]; } - if (question[Constants.XSD.MIN_EXCLUSIVE] !== undefined) { - restriction["min"] = question[Constants.XSD.MIN_EXCLUSIVE] + 1; + if (question[Vocabulary.XSD.MIN_EXCLUSIVE] !== undefined) { + restriction["min"] = question[Vocabulary.XSD.MIN_EXCLUSIVE] + 1; } - if (question[Constants.XSD.MAX_EXCLUSIVE] !== undefined) { - restriction["max"] = question[Constants.XSD.MAX_EXCLUSIVE] - 1; + if (question[Vocabulary.XSD.MAX_EXCLUSIVE] !== undefined) { + restriction["max"] = question[Vocabulary.XSD.MAX_EXCLUSIVE] - 1; } - if (question[Constants.XSD.MAX_INCLUSIVE] !== undefined) { - restriction["max"] = question[Constants.XSD.MAX_INCLUSIVE]; + if (question[Vocabulary.XSD.MAX_INCLUSIVE] !== undefined) { + restriction["max"] = question[Vocabulary.XSD.MAX_INCLUSIVE]; } return restriction; } @@ -145,11 +145,11 @@ const InputAnswer = (props) => { // When the value is an object_value, but the layout does not specify neither typeahead nor select, // show at least the value's label if ( - answer[Constants.HAS_OBJECT_VALUE] && - answer[Constants.HAS_OBJECT_VALUE][JsonLdUtils.RDFS_LABEL] + answer[Vocabulary.HAS_OBJECT_VALUE] && + answer[Vocabulary.HAS_OBJECT_VALUE][JsonLdUtils.RDFS_LABEL] ) { value = JsonLdUtils.getJsonAttValue( - answer[Constants.HAS_OBJECT_VALUE], + answer[Vocabulary.HAS_OBJECT_VALUE], JsonLdUtils.RDFS_LABEL ); } @@ -212,19 +212,19 @@ const InputAnswer = (props) => { if (queryPrefixes[currentPrefix.slice(0, -1)] == null) { // ok, so it isnt added yet! if ( - props.question[Constants.HAS_DECLARED_PREFIX] && - props.question[Constants.HAS_DECLARED_PREFIX].filter( - (p) => p[Constants.PREFIX] === currentPrefix.slice(0, -1) + props.question[Vocabulary.HAS_DECLARED_PREFIX] && + props.question[Vocabulary.HAS_DECLARED_PREFIX].filter( + (p) => p[Vocabulary.PREFIX] === currentPrefix.slice(0, -1) ).length ) { const prefix = props.question[ - Constants.HAS_DECLARED_PREFIX + Vocabulary.HAS_DECLARED_PREFIX ].filter( - (p) => p[Constants.PREFIX] === currentPrefix.slice(0, -1) + (p) => p[Vocabulary.PREFIX] === currentPrefix.slice(0, -1) ); const px = {}; - px[prefix[0][Constants.PREFIX]] = - prefix[0][Constants.NAMESPACE]; + px[prefix[0][Vocabulary.PREFIX]] = + prefix[0][Vocabulary.NAMESPACE]; yasqe.addPrefixes(px); } else { const completions = yasqe.autocompleters diff --git a/src/components/answer/MaskedInputAnswer.jsx b/src/components/answer/MaskedInputAnswer.jsx index 83754dc7..0d182e03 100644 --- a/src/components/answer/MaskedInputAnswer.jsx +++ b/src/components/answer/MaskedInputAnswer.jsx @@ -2,7 +2,7 @@ import React, { useContext } from "react"; import * as JsonLdUtils from "jsonld-utils"; import PropTypes from "prop-types"; -import Constants from "../../constants/Constants"; +import Vocabulary from "../../constants/Vocabulary.js"; import FormUtils from "../../util/FormUtils"; import InputAnswer from "./InputAnswer"; import Logger from "../../util/Logger"; @@ -14,7 +14,7 @@ const MaskedInputAnswer = (props) => { const question = props.question; const value = props.value; - const mask = JsonLdUtils.getJsonAttValue(question, Constants.INPUT_MASK); + const mask = JsonLdUtils.getJsonAttValue(question, Vocabulary.INPUT_MASK); if (!mask) { Logger.warn("Input mask not provided. Falling back to regular input."); diff --git a/src/components/answer/SelectAnswer.jsx b/src/components/answer/SelectAnswer.jsx index c4c94dee..49eace8c 100644 --- a/src/components/answer/SelectAnswer.jsx +++ b/src/components/answer/SelectAnswer.jsx @@ -2,7 +2,7 @@ import React from "react"; import * as JsonLdUtils from "jsonld-utils"; import PropTypes from "prop-types"; -import Constants from "../../constants/Constants"; +import Vocabulary from "../../constants/Vocabulary.js"; import FormUtils from "../../util/FormUtils"; import { ConfigurationContext } from "../../contexts/ConfigurationContext"; @@ -10,8 +10,8 @@ export default class SelectAnswer extends React.Component { _generateSelectOptions(options) { const rendered = []; options.sort(function (a, b) { - const aLabel = JsonLdUtils.getJsonAttValue(a, Constants.RDFS_LABEL), - bLabel = JsonLdUtils.getJsonAttValue(b, Constants.RDFS_LABEL); + const aLabel = JsonLdUtils.getJsonAttValue(a, Vocabulary.RDFS_LABEL), + bLabel = JsonLdUtils.getJsonAttValue(b, Vocabulary.RDFS_LABEL); if (aLabel < bLabel) { return -1; } @@ -24,10 +24,10 @@ export default class SelectAnswer extends React.Component { for (let i = 0; i < options.length; i++) { rendered.push( ); } @@ -52,7 +52,7 @@ export default class SelectAnswer extends React.Component { this.context.componentsOptions.readOnly || FormUtils.isDisabled(question), }, - this._generateSelectOptions(question[Constants.HAS_OPTION]) + this._generateSelectOptions(question[Vocabulary.HAS_OPTION]) ); } } diff --git a/src/components/answer/TypeaheadAnswer.jsx b/src/components/answer/TypeaheadAnswer.jsx index bc33f2e6..7b72b1b1 100644 --- a/src/components/answer/TypeaheadAnswer.jsx +++ b/src/components/answer/TypeaheadAnswer.jsx @@ -1,7 +1,7 @@ import React, { useState, useEffect, useContext } from "react"; import * as JsonLdUtils from "jsonld-utils"; import PropTypes from "prop-types"; -import Constants from "../../constants/Constants"; +import Vocabulary from "../../constants/Vocabulary.js"; import FormUtils from "../../util/FormUtils"; import Utils from "../../util/Utils"; import JsonLdObjectUtils from "../../util/JsonLdObjectUtils"; @@ -24,7 +24,7 @@ const processTypeaheadOptions = (options, intl) => { // sort by property JsonLdObjectUtils.orderPreservingToplogicalSort( options, - Constants.HAS_PRECEDING_VALUE + Vocabulary.HAS_PRECEDING_VALUE ); return options; @@ -67,13 +67,13 @@ const TypeaheadAnswer = (props) => { } if ( - !question[Constants.HAS_OPTION] && + !question[Vocabulary.HAS_OPTION] && FormUtils.getPossibleValuesQuery(question) ) { loadFormOptions(); } else { setLoading(false); - generateTreeOptions(question[Constants.HAS_OPTION]); + generateTreeOptions(question[Vocabulary.HAS_OPTION]); } return () => { @@ -92,7 +92,7 @@ const TypeaheadAnswer = (props) => { const relations = []; for (let pValue of possibleValues) { - let label = JsonLdUtils.getLocalized(pValue[Constants.RDFS_LABEL], intl); + let label = JsonLdUtils.getLocalized(pValue[Vocabulary.RDFS_LABEL], intl); options[pValue["@id"]] = { id: pValue["@id"], @@ -100,7 +100,7 @@ const TypeaheadAnswer = (props) => { label: label, children: [], }; - for (let parent of Utils.asArray(pValue[Constants.BROADER])) { + for (let parent of Utils.asArray(pValue[Vocabulary.BROADER])) { relations.push({ type: "parent-child", parent: parent["@id"], @@ -159,7 +159,7 @@ const TypeaheadAnswer = (props) => { valueKey={valueKey} labelKey={labelKey} valueRenderer={ - props.question[Constants.PROVIDES_DEREFERENCEABLE_ANSWER_VALUES] === + props.question[Vocabulary.PROVIDES_DEREFERENCEABLE_ANSWER_VALUES] === false ? noLinksValueRenderer : null diff --git a/src/components/comment/CommentList.tsx b/src/components/comment/CommentList.tsx index 6d55de2d..ec4ec5fa 100644 --- a/src/components/comment/CommentList.tsx +++ b/src/components/comment/CommentList.tsx @@ -1,6 +1,6 @@ import React, { useState, useRef, useEffect } from "react"; import CommentView from "./CommentView"; -import Constants from "../../constants/Constants"; +import Vocabulary from "../../constants/Vocabulary"; import { Rings } from "react-loader-spinner"; interface Props { @@ -38,13 +38,13 @@ const CommentList = ({ comments, onDeleteCommentClick }: Props) => { {comments.map((comment, index) => (
{ const _getComments = () => { const question = props.question; - if (!question[Constants.HAS_COMMENT]) { - question[Constants.HAS_COMMENT] = []; + if (!question[Vocabulary.HAS_COMMENT]) { + question[Vocabulary.HAS_COMMENT] = []; } - if (!Array.isArray(question[Constants.HAS_COMMENT])) { - question[Constants.HAS_COMMENT] = [question[Constants.HAS_COMMENT]]; + if (!Array.isArray(question[Vocabulary.HAS_COMMENT])) { + question[Vocabulary.HAS_COMMENT] = [question[Vocabulary.HAS_COMMENT]]; } - return question[Constants.HAS_COMMENT]; + return question[Vocabulary.HAS_COMMENT]; }; const handleCommentValueChange = (value) => { @@ -47,10 +47,10 @@ const QuestionCommentIcon = (props) => { const _setComment = (change, value) => { if (context.options.currentUser) { - change[Constants.HAS_AUTHOR] = { "@id": context.options.currentUser }; + change[Vocabulary.HAS_AUTHOR] = { "@id": context.options.currentUser }; } - change[Constants.HAS_COMMENT_VALUE] = value; - change[Constants.HAS_TIMESTAMP] = Date.now().toString(); + change[Vocabulary.HAS_COMMENT_VALUE] = value; + change[Vocabulary.HAS_TIMESTAMP] = Date.now().toString(); }; const handleDeleteQuestionCommentClick = (index) => { diff --git a/src/components/wizard/Wizard.jsx b/src/components/wizard/Wizard.jsx index ac6fa8c1..a40a1e1f 100644 --- a/src/components/wizard/Wizard.jsx +++ b/src/components/wizard/Wizard.jsx @@ -6,7 +6,7 @@ import VerticalWizardNav from "./VerticalWizardNav"; import { ConfigurationContext } from "../../contexts/ConfigurationContext"; import { FormQuestionsContext } from "../../contexts/FormQuestionsContext"; import Utils from "../../util/Utils"; -import Constants from "../../constants/Constants"; +import Vocabulary from "../../constants/Vocabulary.js"; const findStepByQuestionId = (stepData, id) => { const findQuestionTraversal = (question, index) => { @@ -18,7 +18,7 @@ const findStepByQuestionId = (stepData, id) => { return index; } - const subQuestions = Utils.asArray(question[Constants.HAS_SUBQUESTION]); + const subQuestions = Utils.asArray(question[Vocabulary.HAS_SUBQUESTION]); return subQuestions.findIndex( (q, index) => findQuestionTraversal(q, index) !== -1 diff --git a/src/constants/Constants.js b/src/constants/Constants.js index 93097b91..c28832e6 100644 --- a/src/constants/Constants.js +++ b/src/constants/Constants.js @@ -1,165 +1,9 @@ -import * as JsonLdUtils from "jsonld-utils"; - export default class Constants { // Default bootstrap column count static COLUMN_COUNT = 12; static INPUT_LENGTH_THRESHOLD = 50; static DATETIME_NUMBER_FORMAT = "x"; - /** - * Contains mainly definition of constants used to parse the form declaration. - */ - static FORM = "http://onto.fel.cvut.cz/ontologies/documentation/form"; - static HAS_SUBQUESTION = - "http://onto.fel.cvut.cz/ontologies/documentation/has_related_question"; - static HAS_ANSWER = - "http://onto.fel.cvut.cz/ontologies/documentation/has_answer"; - static HAS_OPTION = - "http://onto.fel.cvut.cz/ontologies/form/has-possible-value"; - static HAS_OPTIONS_QUERY = - "http://onto.fel.cvut.cz/ontologies/form/has-possible-values-query"; - static HAS_VALUE_TYPE = - "http://onto.fel.cvut.cz/ontologies/form/has-value-type"; - static IS_DISABLED = - "http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled"; - static INPUT_MASK = "http://onto.fel.cvut.cz/ontologies/form/has-input-mask"; - static LAYOUT_CLASS = - "http://onto.fel.cvut.cz/ontologies/form-layout/has-layout-class"; - static LAYOUT = { - FORM: "form", - QUESTION_SECTION: "section", - WIZARD_STEP: "wizard-step", - DATE: "date", - TIME: "time", - DATETIME: "datetime", - TEXT: "text", - TEXTAREA: "textarea", - CHECKBOX: "checkbox", - QUESTION_TYPEAHEAD: "type-ahead", - MASKED_INPUT: "masked-input", - ANSWERABLE: "answerable", - SPARQL: "sparql", - TURTLE: "ttl", - DISABLED: "disabled", - HIDDEN: "hidden", - COLLAPSED: "collapsed", - EMPHASISED: "emphasised", - EMPHASISE_ON_RELEVANT: "emphasise-on-relevant", - CATEGORY: [ - "category-1", - "category-2", - "category-3", - "category-4", - "category-5", - ], - }; - static HAS_INITIAL_INPUT_LENGTH = - "http://onto.fel.cvut.cz/ontologies/form-layout/has-initial-input-length"; - static VALUE_TYPE_CODE = "code"; - static VALUE_TYPE_TEXT = "text"; - static GENERATED_ROW_SIZE = 1; - static HAS_QUESTION_ORIGIN = - "http://onto.fel.cvut.cz/ontologies/form/has-question-origin"; - static HAS_ANSWER_ORIGIN = - "http://onto.fel.cvut.cz/ontologies/form/has-answer-origin"; - static HAS_DATA_VALUE = - "http://onto.fel.cvut.cz/ontologies/documentation/has_data_value"; - static HAS_OBJECT_VALUE = - "http://onto.fel.cvut.cz/ontologies/documentation/has_object_value"; - static HELP_DESCRIPTION = "http://purl.org/dc/elements/1.1/description"; - static SOURCE = "http://purl.org/dc/elements/1.1/source"; - static XSD = { - MAX_EXCLUSIVE: "http://www.w3.org/2001/XMLSchema#maxExclusive", - MAX_INCLUSIVE: "http://www.w3.org/2001/XMLSchema#maxInclusive", - MIN_EXCLUSIVE: "http://www.w3.org/2001/XMLSchema#minExclusive", - MIN_INCLUSIVE: "http://www.w3.org/2001/XMLSchema#minInclusive", - - INT: "http://www.w3.org/2001/XMLSchema#int", - INTEGER: "http://www.w3.org/2001/XMLSchema#integer", - NEGATIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#negativeInteger", - NON_NEGATIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#nonNegativeInteger", - NON_POSITIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#nonPositiveInteger", - POSITIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#positiveInteger", - - BOOLEAN: "http://www.w3.org/2001/XMLSchema#boolean", - }; - static STEP = "http://onto.fel.cvut.cz/ontologies/form/step"; - static PATTERN = "http://onto.fel.cvut.cz/ontologies/form/pattern"; - static ACCEPTS_ANSWER_VALUE = - "http://onto.fel.cvut.cz/ontologies/form/accepts-answer-value"; - static ACCEPTS = "http://onto.fel.cvut.cz/ontologies/form/accepts"; - static HAS_DATATYPE = "http://onto.fel.cvut.cz/ontologies/form/has-datatype"; - static HAS_LAYOUT_CLASS = - "http://onto.fel.cvut.cz/ontologies/form/has-layout-class"; - static HAS_POSSIBLE_VALUES_QUERY = - "http://onto.fel.cvut.cz/ontologies/form/has-possible-values-query"; - static HAS_REQUIRED_VALUE = - "http://onto.fel.cvut.cz/ontologies/form/has-required-value"; - static HAS_TESTED_QUESTION = - "http://onto.fel.cvut.cz/ontologies/form/has-tested-question"; - static HAS_TESTED_FORM = - "http://onto.fel.cvut.cz/ontologies/form/has-tested-form"; - static HAS_UNIT = "http://onto.fel.cvut.cz/ontologies/form/has-unit"; - static HAS_VALID_ANSWER = - "http://onto.fel.cvut.cz/ontologies/form/has-valid-answer"; - static IS_RELEVANT_IF = - "http://onto.fel.cvut.cz/ontologies/form/is-relevant-if"; - static ACCEPTS_VALIDATION_VALUE = - "http://onto.fel.cvut.cz/ontologies/form/accepts-validation-value"; - static HAS_VALIDATION_MESSAGE = - "http://onto.fel.cvut.cz/ontologies/form/has-validation-message"; - static NEGATIVE_CONDITION = - "http://onto.fel.cvut.cz/ontologies/form/negative-condition"; - static REQUIRES_ANSWER = - "http://onto.fel.cvut.cz/ontologies/form/requires-answer"; - static USED_ONLY_FOR_COMPLETENESS = - "http://onto.fel.cvut.cz/ontologies/form/used-only-for-completeness"; - static REQUIRES_ANSWER_DESCRIPTION_IF = - "http://onto.fel.cvut.cz/ontologies/form/requires-answer-description-if"; - static REQUIRES_ANSWER_IF = - "http://onto.fel.cvut.cz/ontologies/form/requires-answer-if"; - static HAS_PRECEDING_QUESTION = - "http://onto.fel.cvut.cz/ontologies/form/has-preceding-question"; - static HAS_PRECEDING_VALUE = - "http://onto.fel.cvut.cz/ontologies/form/has-preceding-value"; - static HAS_MEDIA_CONTENT = - "http://onto.fel.cvut.cz/ontologies/form/has-media-content"; - static CONDITION = "http://onto.fel.cvut.cz/ontologies/form/condition"; - static OR_CONDITION = "http://onto.fel.cvut.cz/ontologies/form/or-condition"; - static HAS_SUB_CONDITION = - "http://onto.fel.cvut.cz/ontologies/form/has-sub-condition"; - static HAS_PATTERN = "http://onto.fel.cvut.cz/ontologies/form-lt/has-pattern"; - static HAS_DECLARED_PREFIX = - "http://onto.fel.cvut.cz/ontologies/form-spin/has-declared-prefix"; - static PREFIX = "http://www.w3.org/ns/shacl#prefix"; - static NAMESPACE = "http://www.w3.org/ns/shacl#namespace"; - static HAS_COMMENT = "http://onto.fel.cvut.cz/ontologies/form/has-comment"; - static HAS_COMMENT_VALUE = - "http://onto.fel.cvut.cz/ontologies/form/has-comment-value"; - static HAS_AUTHOR = "http://onto.fel.cvut.cz/ontologies/form/has-author"; - static HAS_TIMESTAMP = - "http://onto.fel.cvut.cz/ontologies/form/has-timestamp"; - static TIME_FORMAT = "http://onto.fel.cvut.cz/ontologies/form/time-format"; - static DATE_FORMAT = "http://onto.fel.cvut.cz/ontologies/form/date-format"; - static DATETIME_FORMAT = - "http://onto.fel.cvut.cz/ontologies/form/datetime-format"; - - static NOT_ANSWERED_QUESTION = - "http://onto.fel.cvut.cz/ontologies/form/not-answered-question"; - static ANSWERED_QUESTION = - "http://onto.fel.cvut.cz/ontologies/form/answered-question"; - static HAS_VALIDATION_SEVERITY = - "http://onto.fel.cvut.cz/ontologies/form/has-validation-severity"; - - static RDFS_LABEL = JsonLdUtils.RDFS_LABEL; - static RDFS_COMMENT = JsonLdUtils.RDFS_COMMENT; - static DEFAULT_HAS_CHILD = JsonLdUtils.DEFAULT_HAS_CHILD; - static BROADER = "http://www.w3.org/2004/02/skos/core#broader"; - static HAS_NON_SELECTABLE_VALUE = - "http://onto.fel.cvut.cz/ontologies/form/has-non-selectable-value"; - static PROVIDES_DEREFERENCEABLE_ANSWER_VALUES = - "http://onto.fel.cvut.cz/ontologies/form/provides-dereferenceable-answer-values"; - static ICONS = { QUESTION_COMMENTS: "questionComments", QUESTION_HELP: "questionHelp", diff --git a/src/constants/Vocabulary.js b/src/constants/Vocabulary.js new file mode 100644 index 00000000..b4a678cd --- /dev/null +++ b/src/constants/Vocabulary.js @@ -0,0 +1,154 @@ +import * as JsonLdUtils from "jsonld-utils"; + +export default class Vocabulary { + static FORM = "http://onto.fel.cvut.cz/ontologies/documentation/form"; + static HAS_SUBQUESTION = + "http://onto.fel.cvut.cz/ontologies/documentation/has_related_question"; + static HAS_ANSWER = + "http://onto.fel.cvut.cz/ontologies/documentation/has_answer"; + static HAS_OPTION = + "http://onto.fel.cvut.cz/ontologies/form/has-possible-value"; + static HAS_OPTIONS_QUERY = + "http://onto.fel.cvut.cz/ontologies/form/has-possible-values-query"; + static HAS_VALUE_TYPE = + "http://onto.fel.cvut.cz/ontologies/form/has-value-type"; + static IS_DISABLED = + "http://onto.fel.cvut.cz/ontologies/aviation/form-376/is-disabled"; + static INPUT_MASK = "http://onto.fel.cvut.cz/ontologies/form/has-input-mask"; + static LAYOUT_CLASS = + "http://onto.fel.cvut.cz/ontologies/form-layout/has-layout-class"; + static LAYOUT = { + FORM: "form", + QUESTION_SECTION: "section", + WIZARD_STEP: "wizard-step", + DATE: "date", + TIME: "time", + DATETIME: "datetime", + TEXT: "text", + TEXTAREA: "textarea", + CHECKBOX: "checkbox", + QUESTION_TYPEAHEAD: "type-ahead", + MASKED_INPUT: "masked-input", + ANSWERABLE: "answerable", + SPARQL: "sparql", + TURTLE: "ttl", + DISABLED: "disabled", + HIDDEN: "hidden", + COLLAPSED: "collapsed", + EMPHASISED: "emphasised", + EMPHASISE_ON_RELEVANT: "emphasise-on-relevant", + CATEGORY: [ + "category-1", + "category-2", + "category-3", + "category-4", + "category-5", + ], + }; + static HAS_INITIAL_INPUT_LENGTH = + "http://onto.fel.cvut.cz/ontologies/form-layout/has-initial-input-length"; + static VALUE_TYPE_CODE = "code"; + static VALUE_TYPE_TEXT = "text"; + static GENERATED_ROW_SIZE = 1; + static HAS_QUESTION_ORIGIN = + "http://onto.fel.cvut.cz/ontologies/form/has-question-origin"; + static HAS_ANSWER_ORIGIN = + "http://onto.fel.cvut.cz/ontologies/form/has-answer-origin"; + static HAS_DATA_VALUE = + "http://onto.fel.cvut.cz/ontologies/documentation/has_data_value"; + static HAS_OBJECT_VALUE = + "http://onto.fel.cvut.cz/ontologies/documentation/has_object_value"; + static HELP_DESCRIPTION = "http://purl.org/dc/elements/1.1/description"; + static SOURCE = "http://purl.org/dc/elements/1.1/source"; + static XSD = { + MAX_EXCLUSIVE: "http://www.w3.org/2001/XMLSchema#maxExclusive", + MAX_INCLUSIVE: "http://www.w3.org/2001/XMLSchema#maxInclusive", + MIN_EXCLUSIVE: "http://www.w3.org/2001/XMLSchema#minExclusive", + MIN_INCLUSIVE: "http://www.w3.org/2001/XMLSchema#minInclusive", + + INT: "http://www.w3.org/2001/XMLSchema#int", + INTEGER: "http://www.w3.org/2001/XMLSchema#integer", + NEGATIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#negativeInteger", + NON_NEGATIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#nonNegativeInteger", + NON_POSITIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#nonPositiveInteger", + POSITIVE_INTEGER: "http://www.w3.org/2001/XMLSchema#positiveInteger", + + BOOLEAN: "http://www.w3.org/2001/XMLSchema#boolean", + }; + static STEP = "http://onto.fel.cvut.cz/ontologies/form/step"; + static PATTERN = "http://onto.fel.cvut.cz/ontologies/form/pattern"; + static ACCEPTS_ANSWER_VALUE = + "http://onto.fel.cvut.cz/ontologies/form/accepts-answer-value"; + static ACCEPTS = "http://onto.fel.cvut.cz/ontologies/form/accepts"; + static HAS_DATATYPE = "http://onto.fel.cvut.cz/ontologies/form/has-datatype"; + static HAS_LAYOUT_CLASS = + "http://onto.fel.cvut.cz/ontologies/form/has-layout-class"; + static HAS_POSSIBLE_VALUES_QUERY = + "http://onto.fel.cvut.cz/ontologies/form/has-possible-values-query"; + static HAS_REQUIRED_VALUE = + "http://onto.fel.cvut.cz/ontologies/form/has-required-value"; + static HAS_TESTED_QUESTION = + "http://onto.fel.cvut.cz/ontologies/form/has-tested-question"; + static HAS_TESTED_FORM = + "http://onto.fel.cvut.cz/ontologies/form/has-tested-form"; + static HAS_UNIT = "http://onto.fel.cvut.cz/ontologies/form/has-unit"; + static HAS_VALID_ANSWER = + "http://onto.fel.cvut.cz/ontologies/form/has-valid-answer"; + static IS_RELEVANT_IF = + "http://onto.fel.cvut.cz/ontologies/form/is-relevant-if"; + static ACCEPTS_VALIDATION_VALUE = + "http://onto.fel.cvut.cz/ontologies/form/accepts-validation-value"; + static HAS_VALIDATION_MESSAGE = + "http://onto.fel.cvut.cz/ontologies/form/has-validation-message"; + static NEGATIVE_CONDITION = + "http://onto.fel.cvut.cz/ontologies/form/negative-condition"; + static REQUIRES_ANSWER = + "http://onto.fel.cvut.cz/ontologies/form/requires-answer"; + static USED_ONLY_FOR_COMPLETENESS = + "http://onto.fel.cvut.cz/ontologies/form/used-only-for-completeness"; + static REQUIRES_ANSWER_DESCRIPTION_IF = + "http://onto.fel.cvut.cz/ontologies/form/requires-answer-description-if"; + static REQUIRES_ANSWER_IF = + "http://onto.fel.cvut.cz/ontologies/form/requires-answer-if"; + static HAS_PRECEDING_QUESTION = + "http://onto.fel.cvut.cz/ontologies/form/has-preceding-question"; + static HAS_PRECEDING_VALUE = + "http://onto.fel.cvut.cz/ontologies/form/has-preceding-value"; + static HAS_MEDIA_CONTENT = + "http://onto.fel.cvut.cz/ontologies/form/has-media-content"; + static CONDITION = "http://onto.fel.cvut.cz/ontologies/form/condition"; + static OR_CONDITION = "http://onto.fel.cvut.cz/ontologies/form/or-condition"; + static HAS_SUB_CONDITION = + "http://onto.fel.cvut.cz/ontologies/form/has-sub-condition"; + static HAS_PATTERN = "http://onto.fel.cvut.cz/ontologies/form-lt/has-pattern"; + static HAS_DECLARED_PREFIX = + "http://onto.fel.cvut.cz/ontologies/form-spin/has-declared-prefix"; + static PREFIX = "http://www.w3.org/ns/shacl#prefix"; + static NAMESPACE = "http://www.w3.org/ns/shacl#namespace"; + static HAS_COMMENT = "http://onto.fel.cvut.cz/ontologies/form/has-comment"; + static HAS_COMMENT_VALUE = + "http://onto.fel.cvut.cz/ontologies/form/has-comment-value"; + static HAS_AUTHOR = "http://onto.fel.cvut.cz/ontologies/form/has-author"; + static HAS_TIMESTAMP = + "http://onto.fel.cvut.cz/ontologies/form/has-timestamp"; + static TIME_FORMAT = "http://onto.fel.cvut.cz/ontologies/form/time-format"; + static DATE_FORMAT = "http://onto.fel.cvut.cz/ontologies/form/date-format"; + static DATETIME_FORMAT = + "http://onto.fel.cvut.cz/ontologies/form/datetime-format"; + + static NOT_ANSWERED_QUESTION = + "http://onto.fel.cvut.cz/ontologies/form/not-answered-question"; + static ANSWERED_QUESTION = + "http://onto.fel.cvut.cz/ontologies/form/answered-question"; + static HAS_VALIDATION_SEVERITY = + "http://onto.fel.cvut.cz/ontologies/form/has-validation-severity"; + + static RDFS_LABEL = JsonLdUtils.RDFS_LABEL; + static RDFS_COMMENT = JsonLdUtils.RDFS_COMMENT; + static DEFAULT_HAS_CHILD = JsonLdUtils.DEFAULT_HAS_CHILD; + static BROADER = "http://www.w3.org/2004/02/skos/core#broader"; + static HAS_NON_SELECTABLE_VALUE = + "http://onto.fel.cvut.cz/ontologies/form/has-non-selectable-value"; + static PROVIDES_DEREFERENCEABLE_ANSWER_VALUES = + "http://onto.fel.cvut.cz/ontologies/form/provides-dereferenceable-answer-values"; +} diff --git a/src/model/FormGenerator.js b/src/model/FormGenerator.js index 1db5e3c4..85df5688 100644 --- a/src/model/FormGenerator.js +++ b/src/model/FormGenerator.js @@ -1,5 +1,5 @@ import * as jsonld from "jsonld"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; import DefaultFormGenerator from "./DefaultFormGenerator"; import FormUtils from "../util/FormUtils"; import Logger from "../util/Logger"; @@ -85,7 +85,7 @@ export default class FormGenerator { } form = structure["@graph"].find((item) => FormUtils.isForm(item)); - formElements = form[Constants.HAS_SUBQUESTION]; + formElements = form[Vocabulary.HAS_SUBQUESTION]; if (!formElements) { Logger.error("Could not find any questions in the received data."); @@ -106,7 +106,7 @@ export default class FormGenerator { "Could not find any wizard steps in the received data. Building form without steps" ); - form[Constants.HAS_SUBQUESTION].forEach((question) => + form[Vocabulary.HAS_SUBQUESTION].forEach((question) => formQuestions.push(question) ); } @@ -119,7 +119,7 @@ export default class FormGenerator { // sort by property JsonLdObjectUtils.orderPreservingToplogicalSort( formQuestions, - Constants.HAS_PRECEDING_QUESTION + Vocabulary.HAS_PRECEDING_QUESTION ); return [formQuestions, { root: form }]; diff --git a/src/model/QuestionAnswerProcessor.js b/src/model/QuestionAnswerProcessor.js index cb4f7c7f..9ed84cce 100644 --- a/src/model/QuestionAnswerProcessor.js +++ b/src/model/QuestionAnswerProcessor.js @@ -1,5 +1,5 @@ import * as JsonLdUtils from "jsonld-utils"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; export default class QuestionAnswerProcessor { /** @@ -16,7 +16,7 @@ export default class QuestionAnswerProcessor { question.uri = wizardData.root["@id"]; question.origin = JsonLdUtils.getJsonAttValue( wizardData.root, - Constants.HAS_QUESTION_ORIGIN, + Vocabulary.HAS_QUESTION_ORIGIN, "@id" ); } @@ -50,42 +50,42 @@ export default class QuestionAnswerProcessor { result.uri = question["@id"]; result.origin = JsonLdUtils.getJsonAttValue( question, - Constants.HAS_QUESTION_ORIGIN, + Vocabulary.HAS_QUESTION_ORIGIN, "@id" ); - if (question[Constants.HAS_SUBQUESTION]) { + if (question[Vocabulary.HAS_SUBQUESTION]) { result.subQuestions = []; - for (let i = 0; i < question[Constants.HAS_SUBQUESTION].length; i++) { + for (let i = 0; i < question[Vocabulary.HAS_SUBQUESTION].length; i++) { result.subQuestions.push( QuestionAnswerProcessor._processQuestion( - question[Constants.HAS_SUBQUESTION][i] + question[Vocabulary.HAS_SUBQUESTION][i] ) ); } } - if (question[Constants.HAS_ANSWER]) { + if (question[Vocabulary.HAS_ANSWER]) { result.answers = []; - if (!Array.isArray(question[Constants.HAS_ANSWER])) { - question[Constants.HAS_ANSWER] = [question[Constants.HAS_ANSWER]]; + if (!Array.isArray(question[Vocabulary.HAS_ANSWER])) { + question[Vocabulary.HAS_ANSWER] = [question[Vocabulary.HAS_ANSWER]]; } - for (let i = 0; i < question[Constants.HAS_ANSWER].length; i++) { + for (let i = 0; i < question[Vocabulary.HAS_ANSWER].length; i++) { result.answers.push( QuestionAnswerProcessor.processAnswer( - question[Constants.HAS_ANSWER][i] + question[Vocabulary.HAS_ANSWER][i] ) ); } } - if (question[Constants.HAS_COMMENT]) { + if (question[Vocabulary.HAS_COMMENT]) { result.comments = []; - if (!Array.isArray(question[Constants.HAS_COMMENT])) { - question[Constants.HAS_COMMENT] = [question[Constants.HAS_COMMENT]]; + if (!Array.isArray(question[Vocabulary.HAS_COMMENT])) { + question[Vocabulary.HAS_COMMENT] = [question[Vocabulary.HAS_COMMENT]]; } - for (let i = 0; i < question[Constants.HAS_COMMENT].length; i++) { + for (let i = 0; i < question[Vocabulary.HAS_COMMENT].length; i++) { result.comments.push( QuestionAnswerProcessor.processComment( - question[Constants.HAS_COMMENT][i] + question[Vocabulary.HAS_COMMENT][i] ) ); } @@ -98,16 +98,16 @@ export default class QuestionAnswerProcessor { const result = {}; result.author = JsonLdUtils.getJsonAttValue( comment, - Constants.HAS_AUTHOR, + Vocabulary.HAS_AUTHOR, "@id" ); result.value = JsonLdUtils.getJsonAttValue( comment, - Constants.HAS_COMMENT_VALUE + Vocabulary.HAS_COMMENT_VALUE ); result.timestamp = JsonLdUtils.getJsonAttValue( comment, - Constants.HAS_TIMESTAMP, + Vocabulary.HAS_TIMESTAMP, "@id" ); return result; @@ -118,19 +118,19 @@ export default class QuestionAnswerProcessor { result.uri = answer["@id"]; result.origin = JsonLdUtils.getJsonAttValue( answer, - Constants.HAS_ANSWER_ORIGIN, + Vocabulary.HAS_ANSWER_ORIGIN, "@id" ); - if (answer[Constants.HAS_OBJECT_VALUE]) { + if (answer[Vocabulary.HAS_OBJECT_VALUE]) { result.codeValue = JsonLdUtils.getJsonAttValue( answer, - Constants.HAS_OBJECT_VALUE, + Vocabulary.HAS_OBJECT_VALUE, "@id" ); } else { result.textValue = JsonLdUtils.getJsonAttValue( answer, - Constants.HAS_DATA_VALUE + Vocabulary.HAS_DATA_VALUE ); } return result; @@ -142,7 +142,7 @@ export default class QuestionAnswerProcessor { */ static generateAnswer(question) { const answer = {}; - answer[Constants.HAS_DATA_VALUE] = ""; + answer[Vocabulary.HAS_DATA_VALUE] = ""; return answer; } } diff --git a/src/model/ValidationProcessor.js b/src/model/ValidationProcessor.js index b925cff8..9a89de57 100644 --- a/src/model/ValidationProcessor.js +++ b/src/model/ValidationProcessor.js @@ -1,4 +1,4 @@ -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; import ValidatorFactory from "./ValidatorFactory.js"; export default class ValidationProcessor { @@ -10,7 +10,7 @@ export default class ValidationProcessor { * @param {Object} intl - The object used for internationalization. */ static updateQuestionValidation = (questions, question, index, intl) => { - if (question[Constants.HAS_ANSWER]) { + if (question[Vocabulary.HAS_ANSWER]) { const validator = ValidatorFactory.createValidator(question, intl); const update = validator(); @@ -27,10 +27,10 @@ export default class ValidationProcessor { */ static updateSubQuestionsValidation = (question, intl) => { if ( - question[Constants.HAS_SUBQUESTION] && - question[Constants.HAS_SUBQUESTION].length > 0 + question[Vocabulary.HAS_SUBQUESTION] && + question[Vocabulary.HAS_SUBQUESTION].length > 0 ) { - const subQuestions = question[Constants.HAS_SUBQUESTION]; + const subQuestions = question[Vocabulary.HAS_SUBQUESTION]; for (let j = 0; j < subQuestions.length; j++) { const subQuestion = subQuestions[j]; diff --git a/src/model/ValidatorFactory.js b/src/model/ValidatorFactory.js index 2fc7c119..d728defa 100644 --- a/src/model/ValidatorFactory.js +++ b/src/model/ValidatorFactory.js @@ -4,7 +4,7 @@ import * as JsonLdUtils from "jsonld-utils"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; import FormUtils from "../util/FormUtils"; export default class ValidatorFactory { @@ -31,36 +31,36 @@ export default class ValidatorFactory { for (const validator of validators) { const validationResult = validator(question, intl, answerValue); if (!validationResult.isValid) { - result[Constants.HAS_VALID_ANSWER] = false; - result[Constants.HAS_VALIDATION_MESSAGE] = validationResult.message; - result[Constants.HAS_VALIDATION_SEVERITY] = + result[Vocabulary.HAS_VALID_ANSWER] = false; + result[Vocabulary.HAS_VALIDATION_MESSAGE] = validationResult.message; + result[Vocabulary.HAS_VALIDATION_SEVERITY] = validationResult.validationSeverity; return result; } - if (result[Constants.HAS_VALID_ANSWER] === false) { + if (result[Vocabulary.HAS_VALID_ANSWER] === false) { break; } } - result[Constants.HAS_VALID_ANSWER] = true; - result[Constants.HAS_VALIDATION_MESSAGE] = ""; - result[Constants.HAS_VALIDATION_SEVERITY] = ""; + result[Vocabulary.HAS_VALID_ANSWER] = true; + result[Vocabulary.HAS_VALIDATION_MESSAGE] = ""; + result[Vocabulary.HAS_VALIDATION_SEVERITY] = ""; return result; } static _patternValidator(question, intl) { const answerValue = FormUtils.getAnswerValue(question); if (answerValue && answerValue.length > 0) { - if (question[Constants.PATTERN]) { - let pattern = question[Constants.PATTERN]; + if (question[Vocabulary.PATTERN]) { + let pattern = question[Vocabulary.PATTERN]; const regExp = new RegExp(pattern); const isValid = regExp.test(answerValue) || !FormUtils.hasAnswer(question); if (!isValid) { return { isValid: false, - validationSeverity: Constants.VALIDATION_SEVERITY.ERROR, - message: question[Constants.HAS_VALIDATION_MESSAGE] - ? question[Constants.HAS_VALIDATION_MESSAGE] + validationSeverity: Vocabulary.VALIDATION_SEVERITY.ERROR, + message: question[Vocabulary.HAS_VALIDATION_MESSAGE] + ? question[Vocabulary.HAS_VALIDATION_MESSAGE] : "Please enter a valid answer to " + JsonLdUtils.getLocalized( question[JsonLdUtils.RDFS_LABEL], @@ -75,14 +75,14 @@ export default class ValidatorFactory { static _requiredValidator(question, intl) { if ( - question[Constants.REQUIRES_ANSWER] && - !question[Constants.USED_ONLY_FOR_COMPLETENESS] + question[Vocabulary.REQUIRES_ANSWER] && + !question[Vocabulary.USED_ONLY_FOR_COMPLETENESS] ) { const isValid = FormUtils.hasAnswer(question); if (!isValid) { return { isValid: false, - validationSeverity: Constants.VALIDATION_SEVERITY.ERROR, + validationSeverity: Vocabulary.VALIDATION_SEVERITY.ERROR, message: JsonLdUtils.getLocalized(question[JsonLdUtils.RDFS_LABEL], intl) + " is required", @@ -94,12 +94,12 @@ export default class ValidatorFactory { static _checkboxValidator(question, intl) { if (FormUtils.isCheckbox(question)) { - if (question[Constants.REQUIRES_ANSWER]) { + if (question[Vocabulary.REQUIRES_ANSWER]) { const isValid = FormUtils.hasAnswer(question); if (!isValid) { return { isValid: false, - validationSeverity: Constants.VALIDATION_SEVERITY.ERROR, + validationSeverity: Vocabulary.VALIDATION_SEVERITY.ERROR, message: JsonLdUtils.getLocalized(question[JsonLdUtils.RDFS_LABEL], intl) + " must be checked", @@ -112,14 +112,14 @@ export default class ValidatorFactory { static _completenessValidator(question, intl) { if ( - question[Constants.REQUIRES_ANSWER] && - question[Constants.USED_ONLY_FOR_COMPLETENESS] + question[Vocabulary.REQUIRES_ANSWER] && + question[Vocabulary.USED_ONLY_FOR_COMPLETENESS] ) { const isValid = FormUtils.hasAnswer(question); if (!isValid) { return { isValid: false, - validationSeverity: Constants.VALIDATION_SEVERITY.WARNING, + validationSeverity: Vocabulary.VALIDATION_SEVERITY.WARNING, message: JsonLdUtils.getLocalized(question[JsonLdUtils.RDFS_LABEL], intl) + " should be filled to complete the form.", @@ -130,10 +130,10 @@ export default class ValidatorFactory { } static _intMinInclusiveValidator(question, intl) { - if (question[Constants.XSD.MIN_INCLUSIVE]) { + if (question[Vocabulary.XSD.MIN_INCLUSIVE]) { const answerValue = FormUtils.getAnswerValue(question); if (answerValue && answerValue.length > 0) { - const minInclusive = question[Constants.XSD.MIN_INCLUSIVE]; + const minInclusive = question[Vocabulary.XSD.MIN_INCLUSIVE]; const isValid = Number.isInteger(parseInt(answerValue)) && @@ -142,7 +142,7 @@ export default class ValidatorFactory { if (!isValid) { return { isValid: false, - validationSeverity: Constants.VALIDATION_SEVERITY.ERROR, + validationSeverity: Vocabulary.VALIDATION_SEVERITY.ERROR, message: "The answer should be a number equal or greater than " + minInclusive + @@ -156,9 +156,9 @@ export default class ValidatorFactory { static _intMaxInclusiveValidator(question, intl) { const answerValue = FormUtils.getAnswerValue(question); - if (question[Constants.XSD.MAX_INCLUSIVE]) { + if (question[Vocabulary.XSD.MAX_INCLUSIVE]) { if (answerValue && answerValue.length > 0) { - const maxInclusive = question[Constants.XSD.MAX_INCLUSIVE]; + const maxInclusive = question[Vocabulary.XSD.MAX_INCLUSIVE]; const isValid = Number.isInteger(parseInt(answerValue)) && @@ -167,7 +167,7 @@ export default class ValidatorFactory { if (!isValid) { return { isValid: false, - validationSeverity: Constants.VALIDATION_SEVERITY.ERROR, + validationSeverity: Vocabulary.VALIDATION_SEVERITY.ERROR, message: "The answer should be a number equal or lower than " + maxInclusive + diff --git a/src/s-forms.js b/src/s-forms.js index 4fecbfda..3072d7b2 100644 --- a/src/s-forms.js +++ b/src/s-forms.js @@ -1,5 +1,6 @@ import SForms from "./components/SForms"; -import Constants from "./constants/Constants"; +import Vocabulary from "./constants/Vocabulary.js"; +import Constants from "./constants/Constants.js"; import JsonLdObjectUtils from "./util/JsonLdObjectUtils"; import JsonLdFramingUtils from "./util/JsonLdFramingUtils"; import JsonLdObjectMap from "./util/JsonLdObjectMap"; @@ -15,6 +16,7 @@ import IntlContextProvider from "./contexts/IntlContextProvider.js"; export default SForms; export { + Vocabulary, Constants, JsonLdObjectUtils, JsonLdFramingUtils, diff --git a/src/util/FormUtils.js b/src/util/FormUtils.js index 1e3ce2a2..09fc9f24 100644 --- a/src/util/FormUtils.js +++ b/src/util/FormUtils.js @@ -1,7 +1,8 @@ import * as JsonLdUtils from "jsonld-utils"; import * as jsonld from "jsonld"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; +import Constants from "../constants/Constants.js"; import Utils from "./Utils"; import JsonLdObjectMap from "./JsonLdObjectMap"; import JsonLdObjectUtils from "./JsonLdObjectUtils"; @@ -10,60 +11,60 @@ export default class FormUtils { static isForm(structure) { return JsonLdUtils.hasValue( structure, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.FORM + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.FORM ); } static isWizardStep(structure) { return JsonLdUtils.hasValue( structure, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.WIZARD_STEP + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.WIZARD_STEP ); } static isSection(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.QUESTION_SECTION + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.QUESTION_SECTION ); } static isAnswerable(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.ANSWERABLE + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.ANSWERABLE ); } static isTypeahead(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.QUESTION_TYPEAHEAD + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.QUESTION_TYPEAHEAD ); } static getPossibleValuesQuery(question) { - return JsonLdUtils.getJsonAttValue(question, Constants.HAS_OPTIONS_QUERY); + return JsonLdUtils.getJsonAttValue(question, Vocabulary.HAS_OPTIONS_QUERY); } static isDisabled(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.DISABLED + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.DISABLED ); } static isHidden(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.HIDDEN + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.HIDDEN ); } @@ -74,8 +75,8 @@ export default class FormUtils { !FormUtils.isTypeahead(question)) || JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.TEXTAREA + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.TEXTAREA ) ); } @@ -83,8 +84,8 @@ export default class FormUtils { static isText(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.TEXT + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.TEXT ); } @@ -99,78 +100,78 @@ export default class FormUtils { static isDate(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.DATE + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.DATE ); } static isTime(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.TIME + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.TIME ); } static isDateTime(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.DATETIME + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.DATETIME ); } static isCheckbox(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.CHECKBOX + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.CHECKBOX ); } static isMaskedInput(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.MASKED_INPUT + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.MASKED_INPUT ); } static isSparqlInput(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.SPARQL + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.SPARQL ); } static isTurtleInput(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.TURTLE + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.TURTLE ); } static isCollapsed(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.COLLAPSED + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.COLLAPSED ); } static isEmphasised(question) { return JsonLdUtils.hasValue( question, - Constants.LAYOUT_CLASS, - Constants.LAYOUT.EMPHASISED + Vocabulary.LAYOUT_CLASS, + Vocabulary.LAYOUT.EMPHASISED ); } static getCategory(question) { - return Constants.LAYOUT.CATEGORY.find((c) => - JsonLdUtils.hasValue(question, Constants.LAYOUT_CLASS, c) + return Vocabulary.LAYOUT.CATEGORY.find((c) => + JsonLdUtils.hasValue(question, Vocabulary.LAYOUT_CLASS, c) ); } @@ -178,10 +179,10 @@ export default class FormUtils { if (!answer) { return null; } - if (answer[Constants.HAS_OBJECT_VALUE]) { - return answer[Constants.HAS_OBJECT_VALUE]["@id"]; + if (answer[Vocabulary.HAS_OBJECT_VALUE]) { + return answer[Vocabulary.HAS_OBJECT_VALUE]["@id"]; } else { - return JsonLdUtils.getJsonAttValue(answer, Constants.HAS_DATA_VALUE); + return JsonLdUtils.getJsonAttValue(answer, Vocabulary.HAS_DATA_VALUE); } } @@ -189,21 +190,21 @@ export default class FormUtils { if (!answer) { return null; } - if (answer[Constants.HAS_OBJECT_VALUE]) { - return Utils.asArray(answer[Constants.HAS_OBJECT_VALUE])[0]; + if (answer[Vocabulary.HAS_OBJECT_VALUE]) { + return Utils.asArray(answer[Vocabulary.HAS_OBJECT_VALUE])[0]; } - if (answer[Constants.HAS_DATA_VALUE]) { - return Utils.asArray(answer[Constants.HAS_DATA_VALUE])[0]; + if (answer[Vocabulary.HAS_DATA_VALUE]) { + return Utils.asArray(answer[Vocabulary.HAS_DATA_VALUE])[0]; } return null; } static isRelevant(question) { - if (!question[Constants.IS_RELEVANT_IF]) { + if (!question[Vocabulary.IS_RELEVANT_IF]) { return true; } - for (let cond of Utils.asArray(question[Constants.IS_RELEVANT_IF])) { + for (let cond of Utils.asArray(question[Vocabulary.IS_RELEVANT_IF])) { if (!FormUtils.testCondition(cond)) { return false; } @@ -213,13 +214,13 @@ export default class FormUtils { } static hasValidationLogic(question) { - if (question[Constants.REQUIRES_ANSWER]) { + if (question[Vocabulary.REQUIRES_ANSWER]) { return true; } - if (question[Constants.REQUIRES_ANSWER_IF]) { + if (question[Vocabulary.REQUIRES_ANSWER_IF]) { return true; } - if (question[Constants.PATTERN]) { + if (question[Vocabulary.PATTERN]) { return true; } if (this.containsXSDProperty(question)) { @@ -229,10 +230,10 @@ export default class FormUtils { } static isValid(question) { - if (question[Constants.HAS_VALID_ANSWER] === false) { + if (question[Vocabulary.HAS_VALID_ANSWER] === false) { return false; } - for (const subQ of Utils.asArray(question[Constants.HAS_SUBQUESTION])) { + for (const subQ of Utils.asArray(question[Vocabulary.HAS_SUBQUESTION])) { if (this.isValid(subQ) === false) { return false; } @@ -242,7 +243,7 @@ export default class FormUtils { } static testOrCondition(condition) { - const hasSubCondition = condition[Constants.HAS_SUB_CONDITION]; + const hasSubCondition = condition[Vocabulary.HAS_SUB_CONDITION]; if (!hasSubCondition) { console.warn("Or condition does not have any sub-condition !"); } @@ -255,12 +256,12 @@ export default class FormUtils { } static testCondition(condition) { - const isOrCondition = condition[Constants.HAS_SUB_CONDITION]; + const isOrCondition = condition[Vocabulary.HAS_SUB_CONDITION]; const acceptedValidationsValues = - condition[Constants.ACCEPTS_VALIDATION_VALUE]; - const acceptedAnswerValues = condition[Constants.ACCEPTS_ANSWER_VALUE]; - const accepts = condition[Constants.ACCEPTS]; - const testedQuestions = condition[Constants.HAS_TESTED_QUESTION]; + condition[Vocabulary.ACCEPTS_VALIDATION_VALUE]; + const acceptedAnswerValues = condition[Vocabulary.ACCEPTS_ANSWER_VALUE]; + const accepts = condition[Vocabulary.ACCEPTS]; + const testedQuestions = condition[Vocabulary.HAS_TESTED_QUESTION]; let question; if (isOrCondition) { @@ -281,7 +282,7 @@ export default class FormUtils { "Support for multiple accepts values is not implemented !" ); } - if (arr[0]["@id"] === Constants.ANSWERED_QUESTION) { + if (arr[0]["@id"] === Vocabulary.ANSWERED_QUESTION) { if (acceptedAnswerValues || acceptedValidationsValues) { console.warn( "Support for accepted answer/validations values is not implemented !" @@ -328,11 +329,11 @@ export default class FormUtils { console.warn("Question is not defined."); return true; } - if (!question.hasOwnProperty(Constants.HAS_ANSWER)) { + if (!question.hasOwnProperty(Vocabulary.HAS_ANSWER)) { //console.warn('Question does not have answer value defined.'); return false; } - const answers = jsonld.getValues(question, Constants.HAS_ANSWER); + const answers = jsonld.getValues(question, Vocabulary.HAS_ANSWER); if (answers.length === 0) { return false; @@ -387,8 +388,8 @@ export default class FormUtils { return false; } - if (question.hasOwnProperty(Constants.HAS_ANSWER)) { - const answers = jsonld.getValues(question, Constants.HAS_ANSWER); + if (question.hasOwnProperty(Vocabulary.HAS_ANSWER)) { + const answers = jsonld.getValues(question, Vocabulary.HAS_ANSWER); if (answers.length) { const qValue = FormUtils.resolveValueObject(answers[0]); if (qValue) { @@ -399,7 +400,7 @@ export default class FormUtils { } } - for (const subQ of Utils.asArray(question[Constants.HAS_SUBQUESTION])) { + for (const subQ of Utils.asArray(question[Vocabulary.HAS_SUBQUESTION])) { if (FormUtils.hasAnswer(subQ)) { return true; } @@ -413,8 +414,8 @@ export default class FormUtils { return null; } - if (question.hasOwnProperty(Constants.HAS_ANSWER)) { - const answers = jsonld.getValues(question, Constants.HAS_ANSWER); + if (question.hasOwnProperty(Vocabulary.HAS_ANSWER)) { + const answers = jsonld.getValues(question, Vocabulary.HAS_ANSWER); if (answers.length) { const qValue = FormUtils.resolveValueObject(answers[0]); if (qValue) { @@ -425,7 +426,7 @@ export default class FormUtils { } } - for (const subQ of Utils.asArray(question[Constants.HAS_SUBQUESTION])) { + for (const subQ of Utils.asArray(question[Vocabulary.HAS_SUBQUESTION])) { const subQAnswer = FormUtils.getAnswerValue(subQ); if (subQAnswer) { return subQAnswer; @@ -452,13 +453,13 @@ export default class FormUtils { return Constants.DATETIME_NUMBER_FORMAT; } if (isDate) { - return question[Constants.DATE_FORMAT] || options.dateFormat; + return question[Vocabulary.DATE_FORMAT] || options.dateFormat; } if (isTime) { - return question[Constants.TIME_FORMAT] || options.timeFormat; + return question[Vocabulary.TIME_FORMAT] || options.timeFormat; } if (isDatetime) { - return question[Constants.DATETIME_FORMAT] || options.dateTimeFormat; + return question[Vocabulary.DATETIME_FORMAT] || options.dateTimeFormat; } return null; } @@ -477,7 +478,7 @@ export default class FormUtils { ) { function recursiveTraverse(question) { onEnterQuestion(question); - Utils.asArray(question[Constants.HAS_SUBQUESTION]).forEach((q) => { + Utils.asArray(question[Vocabulary.HAS_SUBQUESTION]).forEach((q) => { recursiveTraverse(q); }); onLeaveQuestion(question); @@ -499,41 +500,41 @@ export default class FormUtils { const ind = indentation.repeat(level); let requiredValue = ""; if ( - q[Constants.REQUIRES_ANSWER] && - !q[Constants.USED_ONLY_FOR_COMPLETENESS] + q[Vocabulary.REQUIRES_ANSWER] && + !q[Vocabulary.USED_ONLY_FOR_COMPLETENESS] ) { requiredValue = ind + propertyIndentation + "required: " + - q[Constants.REQUIRES_ANSWER] + + q[Vocabulary.REQUIRES_ANSWER] + "\n"; - } else if (q[Constants.USED_ONLY_FOR_COMPLETENESS]) { + } else if (q[Vocabulary.USED_ONLY_FOR_COMPLETENESS]) { requiredValue = ind + propertyIndentation + "required only for completeness: " + - q[Constants.USED_ONLY_FOR_COMPLETENESS] + + q[Vocabulary.USED_ONLY_FOR_COMPLETENESS] + "\n"; } - let description = q[Constants.HELP_DESCRIPTION] + let description = q[Vocabulary.HELP_DESCRIPTION] ? ind + propertyIndentation + "description: " + - q[Constants.HELP_DESCRIPTION] + + q[Vocabulary.HELP_DESCRIPTION] + "\n" : ""; - let pattern = q[Constants.PATTERN] - ? ind + propertyIndentation + "pattern: " + q[Constants.PATTERN] + "\n" + let pattern = q[Vocabulary.PATTERN] + ? ind + propertyIndentation + "pattern: " + q[Vocabulary.PATTERN] + "\n" : ""; - let validationMessage = q[Constants.HAS_VALIDATION_MESSAGE] + let validationMessage = q[Vocabulary.HAS_VALIDATION_MESSAGE] ? ind + propertyIndentation + "validation-message: " + - q[Constants.HAS_VALIDATION_MESSAGE] + + q[Vocabulary.HAS_VALIDATION_MESSAGE] + "\n" : ""; @@ -565,7 +566,7 @@ export default class FormUtils { */ static containsXSDProperty(question) { const objKeys = Object.keys(question); - const xsdValues = Object.values(Constants.XSD); + const xsdValues = Object.values(Vocabulary.XSD); for (const value of xsdValues) { if (objKeys.includes(value)) { diff --git a/src/util/JsonLdFramingUtils.js b/src/util/JsonLdFramingUtils.js index d38ac8ae..a628f8db 100644 --- a/src/util/JsonLdFramingUtils.js +++ b/src/util/JsonLdFramingUtils.js @@ -1,19 +1,19 @@ import * as jsonld from "jsonld"; -import Constants from "../constants/Constants"; +import Vocabulary from "../constants/Vocabulary.js"; import FormUtils from "../util/FormUtils"; import Utils from "./Utils"; const formShape = { root: { - [Constants.HAS_LAYOUT_CLASS]: Constants.FORM, + [Vocabulary.HAS_LAYOUT_CLASS]: Vocabulary.FORM, }, expandProperties: [ - Constants.HAS_SUBQUESTION, - Constants.IS_RELEVANT_IF, - Constants.HAS_ANSWER, - Constants.HAS_DECLARED_PREFIX, - Constants.HAS_OPTION, - Constants.HAS_COMMENT, + Vocabulary.HAS_SUBQUESTION, + Vocabulary.IS_RELEVANT_IF, + Vocabulary.HAS_ANSWER, + Vocabulary.HAS_DECLARED_PREFIX, + Vocabulary.HAS_OPTION, + Vocabulary.HAS_COMMENT, ], }; diff --git a/src/util/Utils.js b/src/util/Utils.js index 0b85c1d7..63af2998 100644 --- a/src/util/Utils.js +++ b/src/util/Utils.js @@ -1,4 +1,4 @@ -import Constants from "../constants/Constants.js"; +import Vocabulary from "../constants/Vocabulary.js"; export default class Utils { /** @@ -43,7 +43,7 @@ export default class Utils { } } - const subQuestions = Utils.asArray(question[Constants.HAS_SUBQUESTION]); // we have such method in some kind of json-ld utils + const subQuestions = Utils.asArray(question[Vocabulary.HAS_SUBQUESTION]); // we have such method in some kind of json-ld utils if (asserted) { for (let q of subQuestions) { let foundQ = Utils.findQuestionById(id, q, true, false, false); diff --git a/test/__tests__/Answer.test.js b/test/__tests__/Answer.test.js index 9d4c1b74..2f7eab22 100644 --- a/test/__tests__/Answer.test.js +++ b/test/__tests__/Answer.test.js @@ -3,7 +3,8 @@ import { format } from "date-fns"; import * as Generator from "../environment/Generator"; import Answer from "../../src/components/Answer"; -import Constants from "../../src/constants/Constants"; +import Vocabulary from "../../src/constants/Vocabulary.js"; +import Constants from "../../src/constants/Constants.js"; import { FormGenContext } from "../../src/contexts/FormGenContext"; import { ConfigurationContext } from "../../src/contexts/ConfigurationContext"; import DefaultInput from "../../src/components/DefaultInput"; @@ -24,12 +25,12 @@ describe("Answer component", () => { question = { "@id": Generator.getRandomUri(), }; - question[Constants.LAYOUT_CLASS] = []; - question[Constants.RDFS_LABEL] = { + question[Vocabulary.LAYOUT_CLASS] = []; + question[Vocabulary.RDFS_LABEL] = { "@language": "en", "@value": "1 - Aerodrome General", }; - question[Constants.RDFS_COMMENT] = { + question[Vocabulary.RDFS_COMMENT] = { "@language": "en", "@value": "The identification of the aerodrome/helicopter landing area by name, location and status.", @@ -52,13 +53,15 @@ describe("Answer component", () => { answer = { id: Generator.getRandomUri(), }; - question[Constants.HAS_ANSWER] = [answer]; + question[Vocabulary.HAS_ANSWER] = [answer]; getOptions = jest.fn(() => []); loadFormOptions = jest.fn(); }); test("renders a Typeahead when layout class is typeahead", async () => { - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.QUESTION_TYPEAHEAD); + question[Vocabulary.LAYOUT_CLASS].push( + Vocabulary.LAYOUT.QUESTION_TYPEAHEAD + ); render( { answer = answerWithCodeValue(value); getOptions = jest.fn(() => typeAheadOptions); - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.QUESTION_TYPEAHEAD); - question[Constants.HAS_OPTIONS_QUERY] = "SELECT * WHERE {?x ?y ?z. }"; + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.LAYOUT_CLASS].push( + Vocabulary.LAYOUT.QUESTION_TYPEAHEAD + ); + question[Vocabulary.HAS_OPTIONS_QUERY] = "SELECT * WHERE {?x ?y ?z. }"; render( { }); it("loads typeahead options when layout class is typeahead and no possible values are specified", async () => { - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.QUESTION_TYPEAHEAD); + question[Vocabulary.LAYOUT_CLASS].push( + Vocabulary.LAYOUT.QUESTION_TYPEAHEAD + ); const query = "SELECT * WHERE { ?x ?y ?z .}"; - question[Constants.HAS_OPTIONS_QUERY] = query; + question[Vocabulary.HAS_OPTIONS_QUERY] = query; render( { const res = { "@id": Generator.getRandomUri(), }; - res[Constants.HAS_OBJECT_VALUE] = { + res[Vocabulary.HAS_OBJECT_VALUE] = { "@id": value, }; return res; @@ -173,7 +180,7 @@ describe("Answer component", () => { it("shows input with text value of the answer when no layout class is specified", () => { const value = "masterchief"; answer = answerWithTextValue(value); - question[Constants.HAS_ANSWER] = [answer]; + question[Vocabulary.HAS_ANSWER] = [answer]; render( { const res = { "@id": Generator.getRandomUri(), }; - res[Constants.HAS_DATA_VALUE] = { + res[Vocabulary.HAS_DATA_VALUE] = { "@language": "en", "@value": value, }; @@ -215,8 +222,8 @@ describe("Answer component", () => { const value = format(date, "yyyy-MM-dd HH:mm:ss"); answer = answerWithTextValue(value); - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.DATE); + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.LAYOUT_CLASS].push(Vocabulary.LAYOUT.DATE); render( { const value = format(date, "HH:mm:ss"); answer = answerWithTextValue(date); - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.TIME); + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.LAYOUT_CLASS].push(Vocabulary.LAYOUT.TIME); render( { const testDate = format(date, "yyyy-MM"); const value = format(date, "yyyy-MM-dd HH:mm:ss"); answer = answerWithTextValue(value); - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.DATETIME); + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.LAYOUT_CLASS].push(Vocabulary.LAYOUT.DATETIME); render( { answer = { "@id": Generator.getRandomUri(), }; - answer[Constants.HAS_DATA_VALUE] = value; - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.DATETIME); + answer[Vocabulary.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.LAYOUT_CLASS].push(Vocabulary.LAYOUT.DATETIME); render( { const answer = { "@id": Generator.getRandomUri(), }; - answer[Constants.HAS_DATA_VALUE] = true; - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.CHECKBOX); + answer[Vocabulary.HAS_DATA_VALUE] = true; + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.LAYOUT_CLASS].push(Vocabulary.LAYOUT.CHECKBOX); render( { const answer = { "@id": Generator.getRandomUri(), }; - answer[Constants.HAS_DATA_VALUE] = value; - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.HAS_DATATYPE] = Constants.XSD.INT; + answer[Vocabulary.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.HAS_DATATYPE] = Vocabulary.XSD.INT; render( { value += "a"; } answer = answerWithTextValue(value); - answer[Constants.HAS_DATA_VALUE] = value; - question[Constants.HAS_ANSWER] = [answer]; + answer[Vocabulary.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_ANSWER] = [answer]; render( { const answer = { "@id": Generator.getRandomUri(), }; - answer[Constants.HAS_DATA_VALUE] = value; - question[Constants.HAS_ANSWER] = [answer]; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.MASKED_INPUT); - question[Constants.INPUT_MASK] = mask; + answer[Vocabulary.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_ANSWER] = [answer]; + question[Vocabulary.LAYOUT_CLASS].push(Vocabulary.LAYOUT.MASKED_INPUT); + question[Vocabulary.INPUT_MASK] = mask; const container = render( { expect(structure).toEqual({ root: form["@graph"][0] }); - expect(formQuestions[0][Constants.HAS_SUBQUESTION]).toEqual([ - form["@graph"][0][Constants.HAS_SUBQUESTION][0][ - Constants.HAS_SUBQUESTION + expect(formQuestions[0][Vocabulary.HAS_SUBQUESTION]).toEqual([ + form["@graph"][0][Vocabulary.HAS_SUBQUESTION][0][ + Vocabulary.HAS_SUBQUESTION ][0], ]); }); diff --git a/test/__tests__/FormUtils.test.js b/test/__tests__/FormUtils.test.js index df8ee8e6..bdaa0b1b 100644 --- a/test/__tests__/FormUtils.test.js +++ b/test/__tests__/FormUtils.test.js @@ -1,5 +1,6 @@ import FormUtils from "../../src/util/FormUtils"; -import Constants from "../../src/constants/Constants"; +import Vocabulary from "../../src/constants/Vocabulary.js"; +import Constants from "../../src/constants/Constants.js"; import JsonObjectMap from "../../src/util/JsonLdObjectMap"; describe("FormUtils", () => { @@ -12,38 +13,38 @@ describe("FormUtils", () => { describe("isForm", () => { it("returns true for a form element.", () => { const form = { - "@type": Constants.FORM, + "@type": Vocabulary.FORM, hasQuestion: [{}, {}], }; - form[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.FORM]; + form[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.FORM]; expect(FormUtils.isForm(form)).toBeTruthy(); }); it("returns false for non-form element.", () => { const question = {}; - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.QUESTION_SECTION]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.QUESTION_SECTION]; expect(FormUtils.isForm(question)).toBeFalsy(); }); }); describe("isWizardStep", () => { it("returns true for a wizard step question", () => { - question[Constants.LAYOUT_CLASS] = [ - Constants.LAYOUT.QUESTION_SECTION, - Constants.LAYOUT.WIZARD_STEP, + question[Vocabulary.LAYOUT_CLASS] = [ + Vocabulary.LAYOUT.QUESTION_SECTION, + Vocabulary.LAYOUT.WIZARD_STEP, ]; expect(FormUtils.isWizardStep(question)).toBeTruthy(); }); it("returns false for a section", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.QUESTION_SECTION]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.QUESTION_SECTION]; expect(FormUtils.isWizardStep(question)).toBeFalsy(); }); }); describe("isSection", () => { it("returns true for a section.", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.QUESTION_SECTION]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.QUESTION_SECTION]; expect(FormUtils.isSection(question)).toBeTruthy(); }); @@ -54,7 +55,9 @@ describe("FormUtils", () => { describe("isTypeahead", () => { it("returns true for a typeahead question.", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.QUESTION_TYPEAHEAD]; + question[Vocabulary.LAYOUT_CLASS] = [ + Vocabulary.LAYOUT.QUESTION_TYPEAHEAD, + ]; expect(FormUtils.isTypeahead(question)).toBeTruthy(); }); @@ -77,14 +80,16 @@ describe("FormUtils", () => { for (let i = 0; i < Constants.INPUT_LENGTH_THRESHOLD + 1; i++) { dataValue += i.toString(); } - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.QUESTION_TYPEAHEAD]; + question[Vocabulary.LAYOUT_CLASS] = [ + Vocabulary.LAYOUT.QUESTION_TYPEAHEAD, + ]; expect(FormUtils.isTextarea(question, dataValue)).toBeFalsy(); }); }); describe("isDisabled", () => { it("returns true for a disabled question.", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.DISABLED]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.DISABLED]; expect(FormUtils.isDisabled(question)).toBeTruthy(); }); @@ -95,7 +100,7 @@ describe("FormUtils", () => { describe("isHidden", () => { it("returns true for a hidden question.", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.HIDDEN]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.HIDDEN]; expect(FormUtils.isHidden(question)).toBeTruthy(); }); @@ -106,7 +111,7 @@ describe("FormUtils", () => { describe("isCalendar", () => { it("returns true for a date question", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.DATE]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.DATE]; expect(FormUtils.isCalendar(question)).toBeTruthy(); }); @@ -118,7 +123,7 @@ describe("FormUtils", () => { describe("isDate", () => { it("returns true for a date question", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.DATE]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.DATE]; expect(FormUtils.isCalendar(question)).toBeTruthy(); }); @@ -129,7 +134,7 @@ describe("FormUtils", () => { describe("isTime", () => { it("returns true for a time question", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.TIME]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.TIME]; expect(FormUtils.isCalendar(question)).toBeTruthy(); }); @@ -140,7 +145,7 @@ describe("FormUtils", () => { describe("isDateTime", () => { it("returns true for a datetime question", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.DATETIME]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.DATETIME]; expect(FormUtils.isCalendar(question)).toBeTruthy(); }); @@ -151,32 +156,32 @@ describe("FormUtils", () => { describe("isCheckbox", () => { it("returns true for a checkbox question", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.CHECKBOX]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.CHECKBOX]; expect(FormUtils.isCheckbox(question)).toBeTruthy(); }); it("returns false for a non-checkbox question", () => { - question[Constants.LAYOUT_CLASS] = []; + question[Vocabulary.LAYOUT_CLASS] = []; expect(FormUtils.isCheckbox(question)).toBeFalsy(); }); }); describe("isAnswerable", () => { it("returns true for an answerable section-question", () => { - question[Constants.LAYOUT_CLASS] = [ - Constants.LAYOUT.QUESTION_SECTION, - Constants.LAYOUT.ANSWERABLE, + question[Vocabulary.LAYOUT_CLASS] = [ + Vocabulary.LAYOUT.QUESTION_SECTION, + Vocabulary.LAYOUT.ANSWERABLE, ]; expect(FormUtils.isAnswerable(question)).toBeTruthy(); }); it("returns false for a non-answerable section-question", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.QUESTION_SECTION]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.QUESTION_SECTION]; expect(FormUtils.isAnswerable(question)).toBeFalsy(); }); }); describe("isMaskedInput", () => { it("returns true for a question with masked input layout class", () => { - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.MASKED_INPUT]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.MASKED_INPUT]; expect(FormUtils.isMaskedInput(question)).toBeTruthy(); }); @@ -283,9 +288,9 @@ describe("FormUtils", () => { describe("isCollapsed", () => { it("returns true when question layout class contains collapsed", () => { - question[Constants.LAYOUT_CLASS] = [ - Constants.LAYOUT.QUESTION_SECTION, - Constants.LAYOUT.COLLAPSED, + question[Vocabulary.LAYOUT_CLASS] = [ + Vocabulary.LAYOUT.QUESTION_SECTION, + Vocabulary.LAYOUT.COLLAPSED, ]; expect(FormUtils.isCollapsed(question)).toBeTruthy(); }); diff --git a/test/__tests__/InputAnswer.test.js b/test/__tests__/InputAnswer.test.js index 6328adc8..f3d9a79a 100644 --- a/test/__tests__/InputAnswer.test.js +++ b/test/__tests__/InputAnswer.test.js @@ -1,7 +1,7 @@ import React from "react"; import Answer from "../../src/components/Answer"; -import Constants from "../../src/constants/Constants"; +import Vocabulary from "../../src/constants/Vocabulary.js"; import * as Generator from "../environment/Generator"; import { ConfigurationContext } from "../../src/contexts/ConfigurationContext"; import DefaultInput from "../../src/components/DefaultInput"; @@ -15,12 +15,12 @@ describe("InputAnswer", () => { question = { "@id": Generator.getRandomUri(), }; - question[Constants.LAYOUT_CLASS] = []; - question[Constants.RDFS_LABEL] = { + question[Vocabulary.LAYOUT_CLASS] = []; + question[Vocabulary.RDFS_LABEL] = { "@language": "en", "@value": LABEL, }; - question[Constants.RDFS_COMMENT] = { + question[Vocabulary.RDFS_COMMENT] = { "@language": "en", "@value": "Javascript sucks!!!", }; @@ -42,15 +42,15 @@ describe("InputAnswer", () => { answer = { id: Generator.getRandomUri(), }; - question[Constants.HAS_ANSWER] = [answer]; + question[Vocabulary.HAS_ANSWER] = [answer]; }); it("sets min on numeric input when xsd:minInclusive is used in question", () => { const min = 100; const value = 117; - question[Constants.HAS_DATATYPE] = Constants.XSD.INT; - question[Constants.XSD.MIN_INCLUSIVE] = min; - answer[Constants.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_DATATYPE] = Vocabulary.XSD.INT; + question[Vocabulary.XSD.MIN_INCLUSIVE] = min; + answer[Vocabulary.HAS_DATA_VALUE] = value; const component = mount( { it("sets min on numeric input when xsd:minExclusive is used in question", () => { const min = 100; const value = 117; - question[Constants.HAS_DATATYPE] = Constants.XSD.INT; - question[Constants.XSD.MIN_EXCLUSIVE] = min; - answer[Constants.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_DATATYPE] = Vocabulary.XSD.INT; + question[Vocabulary.XSD.MIN_EXCLUSIVE] = min; + answer[Vocabulary.HAS_DATA_VALUE] = value; const component = mount( { it("sets max on numeric input when xsd:maxExclusive is used in question", () => { const max = 1000; const value = 117; - question[Constants.HAS_DATATYPE] = Constants.XSD.INT; - question[Constants.XSD.MAX_EXCLUSIVE] = max; - answer[Constants.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_DATATYPE] = Vocabulary.XSD.INT; + question[Vocabulary.XSD.MAX_EXCLUSIVE] = max; + answer[Vocabulary.HAS_DATA_VALUE] = value; const component = mount( { it("sets max on numeric input when xsd:maxInclusive is used in question", () => { const max = 1000; const value = 117; - question[Constants.HAS_DATATYPE] = Constants.XSD.INT; - question[Constants.XSD.MAX_INCLUSIVE] = max; - answer[Constants.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_DATATYPE] = Vocabulary.XSD.INT; + question[Vocabulary.XSD.MAX_INCLUSIVE] = max; + answer[Vocabulary.HAS_DATA_VALUE] = value; const component = mount( { const max = 1000; const min = 100; const value = 117; - question[Constants.HAS_DATATYPE] = Constants.XSD.INT; - question[Constants.XSD.MAX_INCLUSIVE] = max; - question[Constants.XSD.MIN_INCLUSIVE] = min; - answer[Constants.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_DATATYPE] = Vocabulary.XSD.INT; + question[Vocabulary.XSD.MAX_INCLUSIVE] = max; + question[Vocabulary.XSD.MIN_INCLUSIVE] = min; + answer[Vocabulary.HAS_DATA_VALUE] = value; const component = mount( { it("sets min when xsd:positiveInteger is used as question datatype", () => { const value = 117; - question[Constants.HAS_DATATYPE] = Constants.XSD.POSITIVE_INTEGER; - answer[Constants.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_DATATYPE] = Vocabulary.XSD.POSITIVE_INTEGER; + answer[Vocabulary.HAS_DATA_VALUE] = value; const component = mount( { "@id": Generator.getRandomUri(), }; - answer[Constants.HAS_DATA_VALUE] = value; - question[Constants.HAS_ANSWER] = [answer]; + answer[Vocabulary.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_ANSWER] = [answer]; question[JsonLdUtils.RDFS_LABEL] = "Test"; - question[Constants.LAYOUT_CLASS] = [Constants.LAYOUT.MASKED_INPUT]; + question[Vocabulary.LAYOUT_CLASS] = [Vocabulary.LAYOUT.MASKED_INPUT]; const component = mount( { const answer = { "@id": Generator.getRandomUri(), }; - answer[Constants.HAS_DATA_VALUE] = value; - question[Constants.HAS_ANSWER] = [answer]; + answer[Vocabulary.HAS_DATA_VALUE] = value; + question[Vocabulary.HAS_ANSWER] = [answer]; question[JsonLdUtils.RDFS_LABEL] = "Test"; - question[Constants.INPUT_MASK] = mask; - question[Constants.LAYOUT_CLASS] = [ - Constants.LAYOUT.MASKED_INPUT, - Constants.LAYOUT.DISABLED, + question[Vocabulary.INPUT_MASK] = mask; + question[Vocabulary.LAYOUT_CLASS] = [ + Vocabulary.LAYOUT.MASKED_INPUT, + Vocabulary.LAYOUT.DISABLED, ]; const component = mount( diff --git a/test/__tests__/QuestionAnswerProcessor.test.js b/test/__tests__/QuestionAnswerProcessor.test.js index 6367a3ea..3c3baa27 100644 --- a/test/__tests__/QuestionAnswerProcessor.test.js +++ b/test/__tests__/QuestionAnswerProcessor.test.js @@ -1,4 +1,4 @@ -import Constants from "../../src/constants/Constants"; +import Vocabulary from "../../src/constants/Vocabulary.js"; import * as Generator from "../environment/Generator"; import QuestionAnswerProcessor from "../../src/model/QuestionAnswerProcessor"; @@ -12,50 +12,52 @@ describe("Question answer processor", () => { }); function generateAnswers(question) { - question[Constants.HAS_ANSWER] = []; + question[Vocabulary.HAS_ANSWER] = []; for (let i = 0; i < Generator.getRandomPositiveInt(1, 5); i++) { const codeValue = Generator.getRandomBoolean(); const answer = {}; answer["@id"] = Generator.getRandomUri(); - answer[Constants.HAS_ANSWER_ORIGIN] = Generator.getRandomUri(); + answer[Vocabulary.HAS_ANSWER_ORIGIN] = Generator.getRandomUri(); if (codeValue) { - answer[Constants.HAS_OBJECT_VALUE] = { + answer[Vocabulary.HAS_OBJECT_VALUE] = { "@id": Generator.getRandomUri(), }; } else { - answer[Constants.HAS_DATA_VALUE] = { + answer[Vocabulary.HAS_DATA_VALUE] = { "@value": i, }; } - question[Constants.HAS_ANSWER].push(answer); + question[Vocabulary.HAS_ANSWER].push(answer); } } function verifyAnswers(expectedQuestion, actualQuestion) { - if (!expectedQuestion[Constants.HAS_ANSWER]) { + if (!expectedQuestion[Vocabulary.HAS_ANSWER]) { return; } expect(actualQuestion.answers).toBeDefined(); expect(actualQuestion.answers.length).toEqual( - expectedQuestion[Constants.HAS_ANSWER].length + expectedQuestion[Vocabulary.HAS_ANSWER].length ); for (let i = 0; i < actualQuestion.answers.length; i++) { expect(actualQuestion.answers[i].uri).toEqual( - expectedQuestion[Constants.HAS_ANSWER][i]["@id"] + expectedQuestion[Vocabulary.HAS_ANSWER][i]["@id"] ); - if (expectedQuestion[Constants.HAS_ANSWER][i][Constants.HAS_DATA_VALUE]) { + if ( + expectedQuestion[Vocabulary.HAS_ANSWER][i][Vocabulary.HAS_DATA_VALUE] + ) { expect(actualQuestion.answers[i].textValue).toEqual( - expectedQuestion[Constants.HAS_ANSWER][i][Constants.HAS_DATA_VALUE][ + expectedQuestion[Vocabulary.HAS_ANSWER][i][Vocabulary.HAS_DATA_VALUE][ "@value" ] ); } else { expect(actualQuestion.answers[i].codeValue).toEqual( - expectedQuestion[Constants.HAS_ANSWER][i][Constants.HAS_OBJECT_VALUE][ - "@id" - ] + expectedQuestion[Vocabulary.HAS_ANSWER][i][ + Vocabulary.HAS_OBJECT_VALUE + ]["@id"] ); } } @@ -72,12 +74,12 @@ describe("Question answer processor", () => { function generateQuestions() { const question = {}; question["@id"] = Generator.getRandomUri(); - question[Constants.RDFS_LABEL] = "Test0"; - question[Constants.RDFS_COMMENT] = "Test0 Comment"; - question[Constants.HAS_QUESTION_ORIGIN] = Generator.getRandomUri(); - question[Constants.HAS_SUBQUESTION] = []; + question[Vocabulary.RDFS_LABEL] = "Test0"; + question[Vocabulary.RDFS_COMMENT] = "Test0 Comment"; + question[Vocabulary.HAS_QUESTION_ORIGIN] = Generator.getRandomUri(); + question[Vocabulary.HAS_SUBQUESTION] = []; for (let i = 0; i < Generator.getRandomPositiveInt(1, 5); i++) { - question[Constants.HAS_SUBQUESTION].push(generateSubQuestions(0, 5)); + question[Vocabulary.HAS_SUBQUESTION].push(generateSubQuestions(0, 5)); } return question; } @@ -85,13 +87,13 @@ describe("Question answer processor", () => { function generateSubQuestions(depth, maxDepth) { const question = {}; question["@id"] = Generator.getRandomUri(); - question[Constants.HAS_QUESTION_ORIGIN] = Generator.getRandomUri(); - question[Constants.RDFS_LABEL] = "Test" + Generator.getRandomInt(); - question[Constants.RDFS_COMMENT] = "Test Comment"; + question[Vocabulary.HAS_QUESTION_ORIGIN] = Generator.getRandomUri(); + question[Vocabulary.RDFS_LABEL] = "Test" + Generator.getRandomInt(); + question[Vocabulary.RDFS_COMMENT] = "Test Comment"; if (depth < maxDepth) { - question[Constants.HAS_SUBQUESTION] = []; + question[Vocabulary.HAS_SUBQUESTION] = []; for (let i = 0; i < Generator.getRandomPositiveInt(1, 5); i++) { - question[Constants.HAS_SUBQUESTION].push( + question[Vocabulary.HAS_SUBQUESTION].push( generateSubQuestions(depth + 1, maxDepth) ); } @@ -103,14 +105,14 @@ describe("Question answer processor", () => { function verifyQuestions(expected, actual) { expect(actual.uri).toEqual(expected["@id"]); verifyAnswers(expected, actual); - if (expected[Constants.HAS_SUBQUESTION]) { + if (expected[Vocabulary.HAS_SUBQUESTION]) { expect(actual.subQuestions).toBeDefined(); expect(actual.subQuestions.length).toEqual( - expected[Constants.HAS_SUBQUESTION].length + expected[Vocabulary.HAS_SUBQUESTION].length ); for (let i = 0; i < actual.subQuestions.length; i++) { verifyQuestions( - expected[Constants.HAS_SUBQUESTION][i], + expected[Vocabulary.HAS_SUBQUESTION][i], actual.subQuestions[i] ); } @@ -127,11 +129,11 @@ describe("Question answer processor", () => { function verifyPresenceOfQuestionOrigin(expected, actual) { expect(actual.origin).toBeDefined(); - expect(actual.origin).toEqual(expected[Constants.HAS_QUESTION_ORIGIN]); - if (expected[Constants.HAS_SUBQUESTION]) { + expect(actual.origin).toEqual(expected[Vocabulary.HAS_QUESTION_ORIGIN]); + if (expected[Vocabulary.HAS_SUBQUESTION]) { for (let i = 0; i < actual.subQuestions.length; i++) { verifyQuestions( - expected[Constants.HAS_SUBQUESTION][i], + expected[Vocabulary.HAS_SUBQUESTION][i], actual.subQuestions[i] ); } @@ -147,16 +149,16 @@ describe("Question answer processor", () => { }); function verifyPresenceOfAnswerOrigin(actualQuestion, expectedQuestion) { - if (!expectedQuestion[Constants.HAS_ANSWER]) { + if (!expectedQuestion[Vocabulary.HAS_ANSWER]) { return; } expect(actualQuestion.answers).toBeDefined(); expect(actualQuestion.answers.length).toEqual( - expectedQuestion[Constants.HAS_ANSWER].length + expectedQuestion[Vocabulary.HAS_ANSWER].length ); for (let i = 0; i < actualQuestion.answers.length; i++) { expect(actualQuestion.answers[i].origin).toEqual( - expectedQuestion[Constants.HAS_ANSWER][i][Constants.HAS_ANSWER_ORIGIN] + expectedQuestion[Vocabulary.HAS_ANSWER][i][Vocabulary.HAS_ANSWER_ORIGIN] ); } } @@ -167,14 +169,14 @@ describe("Question answer processor", () => { }; const questions = [generateQuestions()]; data.root["@id"] = Generator.getRandomUri(); - data.root[Constants.HAS_QUESTION_ORIGIN] = Generator.getRandomUri(); + data.root[Vocabulary.HAS_QUESTION_ORIGIN] = Generator.getRandomUri(); const result = QuestionAnswerProcessor.buildQuestionAnswerModel( data, questions ); expect(result.uri).toEqual(data.root["@id"]); - expect(result.origin).toEqual(data.root[Constants.HAS_QUESTION_ORIGIN]); + expect(result.origin).toEqual(data.root[Vocabulary.HAS_QUESTION_ORIGIN]); expect(result.subQuestions.length).toEqual(1); verifyQuestions(questions[0], result.subQuestions[0]); }); diff --git a/test/__tests__/TypeheadAnswer.test.js b/test/__tests__/TypeheadAnswer.test.js index c29f9d02..d7e5db89 100644 --- a/test/__tests__/TypeheadAnswer.test.js +++ b/test/__tests__/TypeheadAnswer.test.js @@ -2,7 +2,7 @@ import React from "react"; import * as JsonLdUtils from "jsonld-utils"; import * as Generator from "../environment/Generator"; -import Constants from "../../src/constants/Constants"; +import Vocabulary from "../../src/constants/Vocabulary.js"; import TypeaheadAnswer from "../../src/components/answer/TypeaheadAnswer"; import { FormGenContext } from "../../src/contexts/FormGenContext"; import { ConfigurationContext } from "../../src/contexts/ConfigurationContext"; @@ -18,12 +18,12 @@ describe("TypeaheadAnswer", () => { question = { "@id": Generator.getRandomUri(), }; - question[Constants.LAYOUT_CLASS] = []; - question[Constants.RDFS_LABEL] = { + question[Vocabulary.LAYOUT_CLASS] = []; + question[Vocabulary.RDFS_LABEL] = { "@language": "en", "@value": "1 - Aerodrome General", }; - question[Constants.RDFS_COMMENT] = { + question[Vocabulary.RDFS_COMMENT] = { "@language": "en", "@value": "The identification of the aerodrome/helicopter landing area by name, location and status.", @@ -46,8 +46,10 @@ describe("TypeaheadAnswer", () => { ); const query = "SELECT * WHERE { ?x ?y ?z .}"; - question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.QUESTION_TYPEAHEAD); - question[Constants.HAS_OPTIONS_QUERY] = query; + question[Vocabulary.LAYOUT_CLASS].push( + Vocabulary.LAYOUT.QUESTION_TYPEAHEAD + ); + question[Vocabulary.HAS_OPTIONS_QUERY] = query; const component = mount( { const secondId = rule.substring(rule.indexOf("<") + 1); const secondIndex = ids.indexOf(secondId); - options[secondIndex][Constants.HAS_PRECEDING_VALUE] = firstId; + options[secondIndex][Vocabulary.HAS_PRECEDING_VALUE] = firstId; }); return options; }