@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
146 lines (145 loc) • 7.04 kB
JavaScript
;
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 _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _instructureValidations = require("instructure-validations");
var _quizScientificNotation = require("@instructure/quiz-scientific-notation");
var _uiText = require("@instructure/ui-text");
var _interaction_slugs = require("../../interaction_slugs");
var _InteractionType2 = _interopRequireDefault(require("../InteractionType"));
var _validationHelpers = require("../../util/validationHelpers");
var _formatMessage = _interopRequireDefault(require("@instructure/quiz-i18n/es/format-message"));
function _callSuper(_this, derived, args) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
return !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
} catch (e) {
return false;
}
}
derived = (0, _getPrototypeOf2["default"])(derived);
return (0, _possibleConstructorReturn2["default"])(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], (0, _getPrototypeOf2["default"])(_this).constructor) : derived.apply(_this, args));
}
var NumericInteractionType = exports["default"] = /*#__PURE__*/function (_InteractionType) {
function NumericInteractionType(obj) {
var _this2;
(0, _classCallCheck2["default"])(this, NumericInteractionType);
_this2 = _callSuper(this, NumericInteractionType);
(0, _defineProperty2["default"])(_this2, "slug", _interaction_slugs.NUMERIC_SLUG);
(0, _defineProperty2["default"])(_this2, "translatedName", (0, _formatMessage["default"])('Numeric'));
(0, _defineProperty2["default"])(_this2, "getDefaultScoringData", function (intData) {
return {
value: [{
type: 'exactResponse',
value: '',
id: '1'
}]
};
});
(0, _defineProperty2["default"])(_this2, "getDefaultInteractionData", function () {
return {};
});
(0, _defineProperty2["default"])(_this2, "getRenderedResponse", function (responseValue) {
return /*#__PURE__*/_react["default"].createElement(_uiText.Text, {
color: "primary"
}, responseValue);
});
(0, _get2["default"])((_this2, (0, _getPrototypeOf2["default"])(NumericInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj);
return _this2;
}
(0, _inherits2["default"])(NumericInteractionType, _InteractionType);
return (0, _createClass2["default"])(NumericInteractionType, [{
key: "getDefaultUserResponse",
value: function getDefaultUserResponse() {
return {
value: ''
};
}
}]);
}(_InteractionType2["default"]);
(0, _defineProperty2["default"])(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)]
}
};
});