UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

273 lines (265 loc) • 9.14 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 _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; var _dec, _class, _HotSpotExample; 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)); } /** @jsx jsx */ /* eslint-disable react/jsx-no-literals */ import { Component } from 'react'; import PropTypes from 'prop-types'; import { cloneDeep } from 'lodash'; import { Heading } from '@instructure/ui-heading'; import { jsx } from '@instructure/emotion'; import HotSpotEdit from '../Edit'; import HotSpotShow from '../Show'; import HotSpotTake from '../Take'; import HotSpotResult from '../Result'; import generateStyle from '../../common/example/default'; import generateComponentTheme from '../../common/example/defaultTheme'; import { withStyleOverrides } from '@instructure/quiz-common'; /** --- category: HotSpot --- HotSpot Example. ```jsx_example <SettingsSwitcher locales={LOCALES}> <HotSpotExample /> </SettingsSwitcher> ``` **/ var HotSpotExample = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_HotSpotExample = /*#__PURE__*/function (_Component) { function HotSpotExample(props) { var _this2; _classCallCheck(this, HotSpotExample); _this2 = _callSuper(this, HotSpotExample, [props]); _defineProperty(_this2, "handleChangeItemState", function (itemState) { var newState = {}; if (itemState.interactionData) { newState.takeInteractionData = _this2.transformTakeInteractionData(itemState.interactionData); } var updatedState = Object.assign({}, itemState, newState); _this2.setState(updatedState); }); _defineProperty(_this2, "handleResponseUpdate", function (userResponse) { _this2.setState({ userResponse: { value: userResponse } }); }); _this2.state = {}; if (props.useDefaultData) { Object.assign(_this2.state, { itemBody: 'Which of these players is David Ferrer', interactionData: { imageUrl: 'https://i.ytimg.com/vi/LgkAebhJ7iE/maxresdefault.jpg' }, scoringData: { value: { type: 'square', coordinates: [{ x: 0.1, y: 0.13 }, { x: 0.07, y: 0.14 }] } }, userResponse: { value: { x: 0.3, y: 0.3 } } }); _this2.state.scoredData = _this2.transformScoredData(); _this2.state.takeInteractionData = _this2.transformTakeInteractionData(_this2.state.interactionData); } return _this2; } _inherits(HotSpotExample, _Component); return _createClass(HotSpotExample, [{ key: "transformScoredData", value: function transformScoredData() { var coordinates = this.state.scoringData.value.coordinates; // fix coordinates length to do not break result view if (coordinates.length === 1) { coordinates.push(coordinates[0]); } else if (coordinates.length === 0) { coordinates = [{ x: 0, y: 0 }, { x: 0, y: 0 }]; } var resultScore = 0; var userResp = this.state.userResponse.value; var type = this.state.scoringData.value.type; if (type === 'square') { resultScore = this.isInsideSquare(userResp, coordinates) ? 1 : 0; } else if (type === 'oval') { resultScore = this.isInsideEllipse(userResp, coordinates) ? 1 : 0; } else if (type === 'polygon') { resultScore = this.isInsidePolygon(userResp, coordinates) ? 1 : 0; } return { value: { type: this.state.scoringData.value.type, resultScore: resultScore, userResponse: this.state.userResponse.value, correctAnswer: coordinates } }; } }, { key: "isInsideSquare", value: function isInsideSquare(userResp, coordinates) { var coord1 = coordinates[0]; var coord2 = coordinates[1]; // (x1 < px < x2) && (y1 < py < y2) if (coord1.x <= userResp.x && userResp.x <= coord2.x + coord1.x && coord1.y <= userResp.y && userResp.y <= coord2.y + coord1.y) { return true; } } }, { key: "isInsideEllipse", value: function isInsideEllipse(userResp, coordinates) { var coord1 = coordinates[0]; var coord2 = coordinates[1]; var radiusX = Math.abs((coord2.x - coord1.x) * 0.5); var radiusY = Math.abs((coord2.y - coord1.y) * 0.5); var centerX = coord1.x + radiusX; var centerY = coord1.y + radiusY; // fix the center var x = (userResp.x - centerX) * (userResp.x - centerX); var y = (userResp.y - centerY) * (userResp.y - centerY); // ((x*x)/(a*a) + (y*y)/(b*b)) <= 1 ellipse equation var resp = x / (radiusX * radiusX) + y / (radiusY * radiusY); if (resp <= 1) { return true; } } }, { key: "isInsidePolygon", value: function isInsidePolygon(point, vs) { var x = point.x; var y = point.y; // ray-casting algorithm based on // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html var inside = false; for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) { var xi = vs[i].x; var yi = vs[i].y; var xj = vs[j].x; var yj = vs[j].y; var intersect = yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi; if (intersect) { inside = !inside; } } return inside; } }, { key: "transformTakeInteractionData", value: function transformTakeInteractionData(interactionData) { return cloneDeep(interactionData); } }, { key: "renderEdit", value: function renderEdit() { return jsx("div", { title: "edit" }, jsx(Heading, { level: "h2" }, "Edit"), jsx(HotSpotEdit, { changeItemState: this.handleChangeItemState, interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData })); } }, { key: "renderShow", value: function renderShow() { return jsx("div", { title: "show" }, jsx(Heading, { level: "h2" }, "Show"), jsx(HotSpotShow, { interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData })); } }, { key: "renderTake", value: function renderTake() { return jsx("div", { title: "take" }, jsx(Heading, { level: "h2" }, "Take"), jsx(HotSpotTake, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, handleResponseUpdate: this.handleResponseUpdate })); } }, { key: "renderResult", value: function renderResult() { return jsx("div", { title: "result" }, jsx(Heading, { level: "h2" }, "Result"), jsx(HotSpotResult, { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, scoredData: this.transformScoredData() })); } }, { key: "render", value: function render() { return jsx("div", null, jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderEdit()), jsx("div", { css: this.props.styles.panel }, this.renderShow())), jsx("div", { css: this.props.styles.row }, jsx("div", { css: this.props.styles.panel }, this.renderTake()), jsx("div", { css: this.props.styles.panel }, this.renderResult()))); } }]); }(Component), _defineProperty(_HotSpotExample, "displayName", 'HotSpotExample'), _defineProperty(_HotSpotExample, "componentId", "Quizzes".concat(_HotSpotExample.displayName)), _defineProperty(_HotSpotExample, "propTypes", { useDefaultData: PropTypes.bool, styles: PropTypes.object }), _defineProperty(_HotSpotExample, "defaultProps", { useDefaultData: true }), _HotSpotExample)) || _class); export { HotSpotExample as default };