UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

432 lines (431 loc) • 17.8 kB
/** @jsx jsx */ function _array_like_to_array(arr, len) { if (len == null || len > arr.length) len = arr.length; for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } function _array_with_holes(arr) { if (Array.isArray(arr)) return arr; } function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _set_prototype_of(subClass, superClass); } function _iterable_to_array_limit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){ _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally{ try { if (!_n && _i["return"] != null) _i["return"](); } finally{ if (_d) throw _e; } } return _arr; } function _non_iterable_rest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function(sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _object_spread_props(target, source) { source = source != null ? source : {}; if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function(key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } function _sliced_to_array(arr, i) { return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest(); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _unsupported_iterable_to_array(o, minLen) { if (!o) return; if (typeof o === "string") return _array_like_to_array(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen); } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } import { Component } from 'react'; import PropTypes from 'prop-types'; import minBy from 'lodash/fp/minBy'; import maxBy from 'lodash/fp/maxBy'; import { jsx } from '@instructure/emotion'; import t from '@instructure/quiz-i18n/format-message'; import { ItemBodyWrapper } from '@instructure/quiz-rce'; import { NoResponse } from '@instructure/quiz-results-feedback'; import Oval from '../common/Oval'; import Square from '../common/Square'; import Polygon from '../common/Polygon'; import TargetContainer from '../common/TargetContainer'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import FeedbackPopover from './FeedbackPopover/index'; import { withStyleOverrides } from '@instructure/quiz-common'; var CORRECT = 'correct'; var INCORRECT = 'incorrect'; var UNKNOWN = 'unknown'; var coordinates = PropTypes.shape({ x: PropTypes.number, y: PropTypes.number }); function getDrawingType(type) { switch(type){ case 'oval': return Oval; case 'polygon': return Polygon; default: return Square; } } var HotSpotResult = /*#__PURE__*/ function(Component) { "use strict"; _inherits(HotSpotResult, Component); function HotSpotResult() { _class_call_check(this, HotSpotResult); var _this; _this = _call_super(this, HotSpotResult, arguments), _define_property(_this, "renderScoringDataFeedbackPolygon", function(currentCoordinates, flipVertically) { return /*#__PURE__*/ jsx(FeedbackPopover, { coordinates: (flipVertically ? maxBy : minBy)('y', currentCoordinates), flipVertically: flipVertically }); }), _define_property(_this, "renderScoringDataFeedbackOval", function(param, flipVertically) { var _param = _sliced_to_array(param, 2), _param_ = _param[0], x1 = _param_.x, y1 = _param_.y, _param_1 = _param[1], x2 = _param_1.x, y2 = _param_1.y; return /*#__PURE__*/ jsx(FeedbackPopover, { coordinates: { x: (x1 + x2) * 0.5, y: (flipVertically ? Math.max : Math.min)(y1, y2) }, flipVertically: flipVertically }); }), _define_property(_this, "renderScoringDataFeedbackSquare", function(param, flipVertically) { var _param = _sliced_to_array(param, 2), _param_ = _param[0], x1 = _param_.x, y1 = _param_.y, _param_1 = _param[1], x2 = _param_1.x, y2 = _param_1.y; return /*#__PURE__*/ jsx(FeedbackPopover, { coordinates: { x: (x1 + x2) * 0.5, y: (flipVertically ? Math.max : Math.min)(y1, y2) }, flipVertically: flipVertically }); }), _define_property(_this, "renderUserResponseFeedback", function(coordinates, flipVertically, status) { var text; if (!coordinates) { var userResponse = (_this.props.scoredData.value || {}).userResponse; var responseHidden = typeof userResponse === 'undefined'; return /*#__PURE__*/ jsx(NoResponse, { responseHidden: responseHidden, status: _this.status }); } switch(status){ case CORRECT: text = t('Selected Answer - Correct'); break; case INCORRECT: text = t('Selected Answer - Incorrect'); break; case UNKNOWN: text = t('Selected Answer'); } return /*#__PURE__*/ jsx(FeedbackPopover, { coordinates: coordinates, flipVertically: flipVertically, text: text, status: status, targetContent: /*#__PURE__*/ jsx("div", { css: _this.props.styles.userResponseWrapper }, /*#__PURE__*/ jsx("div", { css: _this.props.styles.userResponseTarget })) }); }), _define_property(_this, "prepareHotspotsWithFeedback", function() { var _this_correctAnswers, _this_correctAnswers1; if (!((_this_correctAnswers = _this.correctAnswers) === null || _this_correctAnswers === void 0 ? void 0 : _this_correctAnswers.length)) { var _this_userResponse; return (_this_userResponse = _this.userResponse) === null || _this_userResponse === void 0 ? void 0 : _this_userResponse.map(function() { return { coordinates: [], drawingType: getDrawingType(), renderScoringDataFeedback: Function.prototype }; }); } return (_this_correctAnswers1 = _this.correctAnswers) === null || _this_correctAnswers1 === void 0 ? void 0 : _this_correctAnswers1.map(function(hotspot) { return { coordinates: hotspot.coordinates, drawingType: getDrawingType(hotspot.type), renderScoringDataFeedback: _this.getFeedbackRendering(hotspot) }; }); }); return _this; } _create_class(HotSpotResult, [ { key: "componentDidMount", value: function componentDidMount() { this.props.makeStyles(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.props.makeStyles(); } }, { key: "correctAnswers", get: function get() { var correctAnswer = (this.props.scoredData.value || {}).correctAnswer; if (!correctAnswer) { return []; } if (Array.isArray(correctAnswer) && !(correctAnswer === null || correctAnswer === void 0 ? void 0 : correctAnswer.value)) { return [ { type: this.props.scoredData.value.type, coordinates: correctAnswer } ]; } return (correctAnswer === null || correctAnswer === void 0 ? void 0 : correctAnswer.value) && !Array.isArray(correctAnswer.value) ? [ correctAnswer === null || correctAnswer === void 0 ? void 0 : correctAnswer.value ] : correctAnswer === null || correctAnswer === void 0 ? void 0 : correctAnswer.value; } }, { key: "status", get: function get() { var resultScore = (this.props.scoredData.value || {}).resultScore; if (resultScore === 1) { return CORRECT; } if (resultScore <= 0) { return INCORRECT; } return UNKNOWN; } }, { key: "userResponse", get: function get() { var _this = this; var _ref = this.props.scoredData.value || {}, userResponse = _ref.userResponse, resultScore = _ref.resultScore; var userResponseArray = Array.isArray(userResponse) ? userResponse : [ userResponse ]; return userResponseArray === null || userResponseArray === void 0 ? void 0 : userResponseArray.map(function(response) { if ((response === null || response === void 0 ? void 0 : response.correct) == null) { return _object_spread_props(_object_spread({}, response), { correct: _this.status }); } return _object_spread_props(_object_spread({}, response), { correct: resultScore != null ? response.correct ? CORRECT : INCORRECT : UNKNOWN }); }); } }, { key: "getFeedbackRendering", value: function getFeedbackRendering(hotspot) { var type = hotspot.type; if (!this.correctAnswers.length) { return Function.prototype; } switch(type){ case 'oval': return this.renderScoringDataFeedbackOval; case 'polygon': return this.renderScoringDataFeedbackPolygon; default: return this.renderScoringDataFeedbackSquare; } } }, { key: "render", value: function render() { var hotspots = this.prepareHotspotsWithFeedback() || []; return /*#__PURE__*/ jsx(ItemBodyWrapper, { itemBody: this.props.itemBody }, /*#__PURE__*/ jsx(TargetContainer, { readOnly: true, results: true, hotspots: hotspots, url: this.props.interactionData.imageUrl, userResponseCoordinate: this.userResponse, renderUserResponseFeedback: this.renderUserResponseFeedback })); } } ]); return HotSpotResult; }(Component); _define_property(HotSpotResult, "displayName", 'HotSpotResult'); _define_property(HotSpotResult, "componentId", "Quizzes".concat(HotSpotResult.displayName)); _define_property(HotSpotResult, "propTypes", { interactionData: PropTypes.shape({ imageUrl: PropTypes.string.isRequired }).isRequired, itemBody: PropTypes.string.isRequired, scoredData: PropTypes.shape({ value: PropTypes.shape({ resultScore: PropTypes.number, type: PropTypes.string, correctAnswer: PropTypes.oneOfType([ PropTypes.shape({ value: PropTypes.arrayOf(PropTypes.shape({ type: PropTypes.string, coordinates: PropTypes.arrayOf(coordinates) })) }), PropTypes.arrayOf(coordinates) ]), userResponse: PropTypes.oneOfType([ PropTypes.arrayOf(PropTypes.shape({ x: PropTypes.number, y: PropTypes.number, correct: PropTypes.bool })), coordinates ]) }) }).isRequired, makeStyles: PropTypes.func, styles: PropTypes.object }); export { HotSpotResult as default }; HotSpotResult = _ts_decorate([ withStyleOverrides(generateStyle, generateComponentTheme) ], HotSpotResult);