UNPKG

@instructure/quiz-grading

Version:

The Quiz React SDK by Instructure Inc.

259 lines (258 loc) • 11.1 kB
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 _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 _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } 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; })(); } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ImmutablePropTypes from 'react-immutable-proptypes'; import extractTextFromHtml from '@instructure/quiz-interactions/util/extractTextFromHtml'; import { substituteVars } from '@instructure/quiz-interactions/components/formula/common/util'; import { generateDisplayPositions } from '@instructure/quiz-core/common/util/generateDisplayPositions'; import { Stimulus } from '@instructure/quiz-core/common/components/layout/sidebar/Stimulus/index'; import { Sidebar } from '@instructure/quiz-core/common/components/layout/sidebar/Sidebar/index'; import { onPhone } from '@instructure/quiz-core/common/util/windowChecks'; import SidebarItem from './SidebarItem'; import { SIDEBAR_ITEM_REF, FORMULA_SLUG } from '@instructure/quiz-common/constants'; import { withI18nSupport } from '@instructure/quiz-common/with-i18n-support'; var _withI18nSupport; export var GradingSidebar = /*#__PURE__*/ function(_superClass) { "use strict"; _inherits(GradingSidebar, _superClass); function GradingSidebar() { _class_call_check(this, GradingSidebar); var _this; _this = _call_super(this, GradingSidebar, arguments), _define_property(_this, "scrollToItem", function(position) { return function(itemId) { _this.props.scrollToItem(itemId); if (onPhone() && _this.props.sidebarOpen) { _this.props.toggleSidebar(); } }; }), _define_property(_this, "renderSidebarItem", function(sessionItem, displayPosition) { var item = sessionItem.getItem(); var key = _this.generateItemKey(item.id); var sidebarOpen = _this.props.sidebarOpen; // Use helper for textblock detection var isTextblock = _this.isTextblock(sessionItem); var position = isTextblock ? null : displayPosition !== null ? displayPosition : sessionItem.position; return /*#__PURE__*/ React.createElement(SidebarItem, { inStimulus: sessionItem.isStimulus, isGraded: sessionItem.sessionItemResult.get('score') !== null, itemBody: _this.getItemBody(item, _this.locale), itemId: item.id, itemName: item.getInteractionType().name, key: key, pointsPossible: sessionItem.pointsPossible, position: position, sessionItemId: sessionItem.id, ref: _this.generateItemKey(item.id), scrollToItem: _this.scrollToItem, sidebarOpen: sidebarOpen }); }); return _this; } _create_class(GradingSidebar, [ { key: "generateItemKey", value: function generateItemKey(id) { return "".concat(SIDEBAR_ITEM_REF, "_").concat(id); } }, { key: "setRef", value: function setRef(key) { var _this = this; return function(node) { _this[key] = node; }; } }, { key: "getItemBody", value: function getItemBody(item, locale) { var itemBody = item.getItemBody(); if (item.getInteractionType().slug === FORMULA_SLUG) { var variables = item.toJS().interactionData.variables; itemBody = substituteVars(itemBody, variables, locale); } return extractTextFromHtml(itemBody || ''); } }, { // Helper to determine if a sessionItem is a textblock key: "isTextblock", value: function isTextblock(sessionItem) { // Prefer interactionType if available var item = sessionItem.getItem ? sessionItem.getItem() : sessionItem.item; if (item && typeof item.getInteractionType === 'function') { var type = item.getInteractionType(); if (type && type.slug === 'textblock') return true; } // Fallback to questionNumber === 0 return sessionItem.questionNumber === 0; } }, { key: "renderSidebarItems", value: function renderSidebarItems() { var _this = this; var _this_props = this.props, sessionItems = _this_props.sessionItems, sidebarOpen = _this_props.sidebarOpen; var allItems = generateDisplayPositions(sessionItems); // Recalculate display positions starting from 1, skipping textblocks var displayPosition = 1; return allItems.map(function(si) { var sessionItem = si.record; // Use helper for textblock detection var isTextblock = _this.isTextblock(sessionItem); if (sessionItem.isStimulus) { var currentDisplayPosition = isTextblock ? null : displayPosition; var nestedCount = sessionItem.getNestedEntries().size; // Only increment position for non-textblock stimuli if (!isTextblock) { displayPosition += nestedCount; } var key = _this.generateItemKey(si.record.id); return /*#__PURE__*/ React.createElement(Stimulus, { displayPosition: currentDisplayPosition, itemId: sessionItem.id, key: key, ref: _this.setRef(key), renderSidebarItem: _this.renderSidebarItem, scrollToItem: _this.scrollToItem(sessionItem.position), sidebarOpen: sidebarOpen, stimulusParent: sessionItem }); } var currentDisplayPosition1 = isTextblock ? null : displayPosition; // Only increment position for non-textblock items if (!isTextblock) { displayPosition++; } return /*#__PURE__*/ React.createElement("div", { key: sessionItem.id }, _this.renderSidebarItem(sessionItem, currentDisplayPosition1)); }); } }, { key: "render", value: function render() { if (this.props.sessionNotLoaded) { return null; } return /*#__PURE__*/ React.createElement(Sidebar, { grading: true, role: this.props.role, scrollToItem: this.scrollToItem, sidebarOpen: this.props.sidebarOpen, toggleSidebar: this.props.toggleSidebar, screenreaderNotification: this.props.screenreaderNotification }, this.renderSidebarItems()); } } ]); return GradingSidebar; }(_withI18nSupport = withI18nSupport(Component)); _define_property(GradingSidebar, "propTypes", { quizTitle: PropTypes.string.isRequired, role: PropTypes.string, screenreaderNotification: PropTypes.func.isRequired, scrollToItem: PropTypes.func.isRequired, sessionItems: ImmutablePropTypes.list.isRequired, sessionNotLoaded: PropTypes.bool.isRequired, sidebarOpen: PropTypes.bool, toggleSidebar: PropTypes.func.isRequired, locale: PropTypes.string }); _define_property(GradingSidebar, "defaultProps", { role: 'navigation', sidebarOpen: false, locale: null }); _define_property(GradingSidebar, "contextTypes", { locale: PropTypes.string }); export default GradingSidebar;