UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

277 lines (270 loc) • 10.2 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 _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = require("react"); var _propTypes = _interopRequireDefault(require("prop-types")); var _lodash = require("lodash"); var _uiHeading = require("@instructure/ui-heading"); var _emotion = require("@instructure/emotion"); var _Edit = _interopRequireDefault(require("../Edit")); var _Show = _interopRequireDefault(require("../Show")); var _Take = _interopRequireDefault(require("../Take")); var _Result = _interopRequireDefault(require("../Result")); var _default = _interopRequireDefault(require("../../common/example/default")); var _defaultTheme = _interopRequireDefault(require("../../common/example/defaultTheme")); var _quizCommon = require("@instructure/quiz-common"); var _dec, _class, _HotSpotExample; /** @jsx jsx */ /* eslint-disable react/jsx-no-literals */ 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)); } /** --- category: HotSpot --- HotSpot Example. ```jsx_example <SettingsSwitcher locales={LOCALES}> <HotSpotExample /> </SettingsSwitcher> ``` **/ var HotSpotExample = exports["default"] = (_dec = (0, _quizCommon.withStyleOverrides)(_default["default"], _defaultTheme["default"]), _dec(_class = (_HotSpotExample = /*#__PURE__*/function (_Component) { function HotSpotExample(props) { var _this2; (0, _classCallCheck2["default"])(this, HotSpotExample); _this2 = _callSuper(this, HotSpotExample, [props]); (0, _defineProperty2["default"])(_this2, "handleChangeItemState", function (itemState) { var newState = {}; if (itemState.interactionData) { newState.takeInteractionData = _this2.transformTakeInteractionData(itemState.interactionData); } var updatedState = Object.assign({}, itemState, newState); _this2.setState(updatedState); }); (0, _defineProperty2["default"])(_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; } (0, _inherits2["default"])(HotSpotExample, _Component); return (0, _createClass2["default"])(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 (0, _lodash.cloneDeep)(interactionData); } }, { key: "renderEdit", value: function renderEdit() { return (0, _emotion.jsx)("div", { title: "edit" }, (0, _emotion.jsx)(_uiHeading.Heading, { level: "h2" }, "Edit"), (0, _emotion.jsx)(_Edit["default"], { changeItemState: this.handleChangeItemState, interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData })); } }, { key: "renderShow", value: function renderShow() { return (0, _emotion.jsx)("div", { title: "show" }, (0, _emotion.jsx)(_uiHeading.Heading, { level: "h2" }, "Show"), (0, _emotion.jsx)(_Show["default"], { interactionData: this.state.interactionData, itemBody: this.state.itemBody, scoringData: this.state.scoringData })); } }, { key: "renderTake", value: function renderTake() { return (0, _emotion.jsx)("div", { title: "take" }, (0, _emotion.jsx)(_uiHeading.Heading, { level: "h2" }, "Take"), (0, _emotion.jsx)(_Take["default"], { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, userResponse: this.state.userResponse, handleResponseUpdate: this.handleResponseUpdate })); } }, { key: "renderResult", value: function renderResult() { return (0, _emotion.jsx)("div", { title: "result" }, (0, _emotion.jsx)(_uiHeading.Heading, { level: "h2" }, "Result"), (0, _emotion.jsx)(_Result["default"], { interactionData: this.state.takeInteractionData, itemBody: this.state.itemBody, scoredData: this.transformScoredData() })); } }, { key: "render", value: function render() { return (0, _emotion.jsx)("div", null, (0, _emotion.jsx)("div", { css: this.props.styles.row }, (0, _emotion.jsx)("div", { css: this.props.styles.panel }, this.renderEdit()), (0, _emotion.jsx)("div", { css: this.props.styles.panel }, this.renderShow())), (0, _emotion.jsx)("div", { css: this.props.styles.row }, (0, _emotion.jsx)("div", { css: this.props.styles.panel }, this.renderTake()), (0, _emotion.jsx)("div", { css: this.props.styles.panel }, this.renderResult()))); } }]); }(_react.Component), (0, _defineProperty2["default"])(_HotSpotExample, "displayName", 'HotSpotExample'), (0, _defineProperty2["default"])(_HotSpotExample, "componentId", "Quizzes".concat(_HotSpotExample.displayName)), (0, _defineProperty2["default"])(_HotSpotExample, "propTypes", { useDefaultData: _propTypes["default"].bool, styles: _propTypes["default"].object }), (0, _defineProperty2["default"])(_HotSpotExample, "defaultProps", { useDefaultData: true }), _HotSpotExample)) || _class);