UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

132 lines 5.13 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _get from "@babel/runtime/helpers/esm/get"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; 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 = _getPrototypeOf(derived); return _possibleConstructorReturn(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], _getPrototypeOf(_this).constructor) : derived.apply(_this, args)); } import React from 'react'; import { rule, each, addValidator } from 'instructure-validations'; import { HOT_SPOT_SLUG } from '../../interaction_slugs'; import InteractionType from '../InteractionType'; import { presenceMessage } from '../../util/validationHelpers'; import TargetContainer from '../../components/hotspot/common/TargetContainer'; import Target from '../../components/hotspot/Take/Target'; import t from '@instructure/quiz-i18n/es/format-message'; import isEqual from 'lodash/isEqual'; var defaultInteractionData = {}; var defaultSoringData = { value: [] }; function validateHotSpot() { var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var scoringData = data.scoringData || {}; if (scoringData.value && scoringData.value.length > 0) { return { interactionData: { imageUrl: [rule('presence', { message: presenceMessage(t('Image')) })] }, scoringData: { value: [rule('listSize', { minMessage: t('You must draw a hotspot'), min: 1 }), each({ type: [rule('presence', { message: t('You must select a hotspot type') })], coordinates: [rule('validateCoordinates', { minMessage: t('You must draw a hotspot'), data: scoringData })] })] } }; } else { var isPolygon = scoringData.value && scoringData.value.type === 'polygon'; var minCoordinates = isPolygon ? 3 : 2; return { interactionData: { imageUrl: [rule('presence', { message: presenceMessage(t('Image')) })] }, scoringData: { value: { type: [rule('presence', { message: t('You must select a hotspot type') })], coordinates: [rule('listSize', { minMessage: t('You must draw a hotspot'), min: minCoordinates })] } } }; } } addValidator('validateCoordinates', function (coordinates, options) { if (!coordinates) { return null; } var minMessage = options.minMessage, value = options.data.value; var hotspots = Array.isArray(value) ? value : [value]; var matchingCoordinates = hotspots.find(function (value) { return isEqual(value.coordinates, coordinates); }); var min = (matchingCoordinates === null || matchingCoordinates === void 0 ? void 0 : matchingCoordinates.type) === 'polygon' ? 3 : 2; return (coordinates === null || coordinates === void 0 ? void 0 : coordinates.length) >= min ? null : minMessage; }); var HotSpotInteractionType = /*#__PURE__*/function (_InteractionType) { function HotSpotInteractionType(obj) { var _this2; _classCallCheck(this, HotSpotInteractionType); _this2 = _callSuper(this, HotSpotInteractionType); _defineProperty(_this2, "slug", HOT_SPOT_SLUG); _defineProperty(_this2, "translatedName", t('Hot Spot')); _defineProperty(_this2, "getDefaultScoringData", function () { return defaultSoringData; }); _defineProperty(_this2, "getDefaultInteractionData", function () { return defaultInteractionData; }); _defineProperty(_this2, "getRenderedResponse", function (responseValue, interactionData) { return /*#__PURE__*/React.createElement(TargetContainer, { disabled: true, coordinates: [responseValue], drawingType: Target, justifyContent: "left", url: interactionData.imageUrl }); }); _get((_this2, _getPrototypeOf(HotSpotInteractionType.prototype)), "initializeProps", _this2).call(_this2, obj); return _this2; } _inherits(HotSpotInteractionType, _InteractionType); return _createClass(HotSpotInteractionType, [{ key: "getDefaultUserResponse", value: function getDefaultUserResponse() { return { value: {} }; } }]); }(InteractionType); _defineProperty(HotSpotInteractionType, "validations", validateHotSpot); export { HotSpotInteractionType as default };