UNPKG

quiz-interactions

Version:

A React UI component Library for quiz interaction types.

170 lines (135 loc) 6.31 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get")); var _react = _interopRequireDefault(require("react")); var _instructureValidations = require("instructure-validations"); var _quizScientificNotation = require("@instructure/quiz-scientific-notation"); var _uiElements = require("@instructure/ui-elements"); var _interaction_slugs = require("../../interaction_slugs"); var _InteractionType2 = _interopRequireDefault(require("../InteractionType")); var _validationHelpers = require("../../util/validationHelpers"); var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/lib/format-message")); var NumericInteractionType = /*#__PURE__*/ function (_InteractionType) { (0, _inherits2.default)(NumericInteractionType, _InteractionType); function NumericInteractionType(obj) { var _this; (0, _classCallCheck2.default)(this, NumericInteractionType); _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(NumericInteractionType).call(this)); _this.slug = _interaction_slugs.NUMERIC_SLUG; _this.translatedName = (0, _formatMessage.default)('Numeric'); _this.getDefaultScoringData = function (intData) { return { value: [{ type: 'exactResponse', value: '', id: '1' }] }; }; _this.getDefaultInteractionData = function () { return {}; }; _this.getRenderedResponse = function (responseValue) { return _react.default.createElement(_uiElements.Text, { color: "primary" }, responseValue); }; (0, _get2.default)((0, _getPrototypeOf2.default)(NumericInteractionType.prototype), "initializeProps", (0, _assertThisInitialized2.default)(_this)).call((0, _assertThisInitialized2.default)(_this), obj); return _this; } (0, _createClass2.default)(NumericInteractionType, [{ key: "getDefaultUserResponse", value: function getDefaultUserResponse() { return { value: '' }; } }]); return NumericInteractionType; }(_InteractionType2.default); exports.default = NumericInteractionType; NumericInteractionType.validations = function (data) { var exactResponseValidations = { value: [(0, _instructureValidations.rule)('numeric', { message: (0, _validationHelpers.numericMessage)((0, _formatMessage.default)('Answer')) })] }; var scientificNotationRuleOptions = { message: (0, _formatMessage.default)('Scientific notation not supported with margin of error') }; var marginOfErrorValidations = { value: [(0, _instructureValidations.rule)('noScientificNotation', scientificNotationRuleOptions), (0, _instructureValidations.rule)('numeric', { message: (0, _validationHelpers.numericMessage)((0, _formatMessage.default)('Answer')) })], margin: [(0, _instructureValidations.rule)('noScientificNotation', scientificNotationRuleOptions), (0, _instructureValidations.rule)('numeric', { message: (0, _validationHelpers.numericMessage)((0, _formatMessage.default)('Margin')) })] }; var withinARangeValidations = function withinARangeValidations(obj) { var startRuleOptions = { message: (0, _validationHelpers.numericMessage)((0, _formatMessage.default)('Range start')) }; var endRuleOptions = { message: (0, _validationHelpers.numericMessage)((0, _formatMessage.default)('Range end')), min: obj.start, minMessage: (0, _formatMessage.default)('Range end must be greater than range start') }; var scientificNotationRuleOptions = { message: (0, _formatMessage.default)('Must be in scientific notation') }; var start = []; var end = []; if ((0, _quizScientificNotation.isScientificNotation)(obj.end)) { start.push((0, _instructureValidations.rule)('scientificNotation', scientificNotationRuleOptions)); } start.push((0, _instructureValidations.rule)('numeric', startRuleOptions)); if ((0, _quizScientificNotation.isScientificNotation)(obj.start)) { end.push((0, _instructureValidations.rule)('scientificNotation', scientificNotationRuleOptions)); } end.push((0, _instructureValidations.rule)('numeric', endRuleOptions)); return { start: start, end: end }; }; var preciseResponseValidations = function preciseResponseValidations(obj) { var scientificNotationRuleOptions = { message: (0, _formatMessage.default)('Scientific notation not supported with precise response') }; return { value: [(0, _instructureValidations.rule)('noScientificNotation', scientificNotationRuleOptions), (0, _instructureValidations.rule)('numeric', { message: (0, _validationHelpers.numericMessage)((0, _formatMessage.default)('Answer')) })], precision: [(0, _instructureValidations.rule)('noScientificNotation', scientificNotationRuleOptions), (0, _instructureValidations.rule)('numeric', { message: (0, _validationHelpers.numericMessage)((0, _formatMessage.default)('Precision')), type: obj.precisionType, value: obj.value })] }; }; var scoredDatumValidator = function scoredDatumValidator(obj) { return { exactResponse: exactResponseValidations, marginOfError: marginOfErrorValidations, withinARange: withinARangeValidations, preciseResponse: preciseResponseValidations }[obj.type]; }; return { scoringData: { value: [(0, _instructureValidations.each)(scoredDatumValidator)] } }; };