UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

323 lines (322 loc) • 13.2 kB
/** @jsx jsx */ 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 _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 _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; })(); } 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 { jsx } from '@instructure/emotion'; import { IconButton } from '@instructure/ui-buttons'; import { Text } from '@instructure/ui-text'; import { Spinner } from '@instructure/ui-spinner'; import { IconDocumentLine, IconTrashLine } from '@instructure/ui-icons'; import { SimpleModal, withStyleOverrides } from '@instructure/quiz-common'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import isImageString from '../isImage'; import t from '@instructure/quiz-i18n/format-message'; var SPACE_KEY = 0x20; var ENTER_KEY = 0x0d; var FilesList = /*#__PURE__*/ function(Component) { "use strict"; _inherits(FilesList, Component); function FilesList() { _class_call_check(this, FilesList); var _this; _this = _call_super(this, FilesList, arguments), _define_property(_this, "state", { isModalOpen: false, modalContent: { name: 'empty', url: '' } }), _define_property(_this, "handleCloseModal", function() { _this.setState({ isModalOpen: false }); }), _define_property(_this, "handleOpenModal", function(modalContent) { _this.setState({ isModalOpen: true, modalContent: modalContent }); }), _define_property(_this, "handleOpenUrl", function(url) { window.open(url, '_blank'); }), _define_property(_this, "renderImagePreview", function(url, name) { return /*#__PURE__*/ jsx("div", { css: _this.props.styles.previewContainer }, /*#__PURE__*/ jsx("img", { src: url, alt: name, css: _this.props.styles.previewImg })); }), _define_property(_this, "renderFilePreview", function() { return /*#__PURE__*/ jsx("div", { css: _this.props.styles.previewContainer }, /*#__PURE__*/ jsx(IconDocumentLine, { size: "medium" })); }), _define_property(_this, "renderPreview", function(file) { var url = file.url, size = file.size, name = file.name, isUploading = file.isUploading; if (!url && !isUploading) { return /*#__PURE__*/ jsx("div", { css: _this.props.styles.userResponse }, /*#__PURE__*/ jsx(Text, { color: "primary" }, t('(no answer)'))); } var isImage = isImageString(name); var onClick = isImage ? function() { return _this.handleOpenModal(file); } : function() { return _this.handleOpenUrl(url); }; var containerProps = _this.props.readOnly ? {} : { tabIndex: '0', role: 'button' }; return /*#__PURE__*/ jsx("div", { css: _this.props.styles.fileLinkWrapper }, /*#__PURE__*/ jsx("div", _object_spread_props(_object_spread({}, containerProps), { "data-testid": "button-container", onClick: onClick, css: _this.props.styles.fileLink, onKeyPress: _this.makeKeyPressHandler(onClick) }), /*#__PURE__*/ jsx("div", { css: _this.props.styles.userResponse }, isImage ? _this.renderImagePreview(url, name) : _this.renderFilePreview(), /*#__PURE__*/ jsx("div", { css: _this.props.styles.nameContainer }, /*#__PURE__*/ jsx("div", null, /*#__PURE__*/ jsx(Text, { color: "brand" }, name)), /*#__PURE__*/ jsx("div", null, /*#__PURE__*/ jsx(Text, { color: "primary" }, t('{size, number}Kb', { size: Math.round(size * 0.001), description: 'size of file in kilobytes' }))), !isUploading ? null : /*#__PURE__*/ jsx(Spinner, { themeOverride: _this.props.styles.spinner, renderTitle: t('Loading'), size: "small" })))), _this.props.makeRemoveHandler && /*#__PURE__*/ jsx("div", { css: _this.props.styles.removeButton, "data-role": "removeButton" }, /*#__PURE__*/ jsx(IconButton, { onClick: _this.props.makeRemoveHandler(file), name: "Close", size: "small", renderIcon: IconTrashLine, withBackground: false, withBorder: false, readOnly: _this.props.readOnly, screenReaderLabel: t('Remove File Named {name}', { name: name }) }))); }), _define_property(_this, "renderPreviews", function() { var _this_props = _this.props, files = _this_props.files, showIfEmpty = _this_props.showIfEmpty; if (typeof files === 'undefined') { return /*#__PURE__*/ jsx("div", { css: _this.props.styles.entryWrapper }, /*#__PURE__*/ jsx(Text, { color: "primary" }, t('(response not displayed)'))); } if (showIfEmpty && (!files || files.length === 0)) { return /*#__PURE__*/ jsx("div", { css: _this.props.styles.entryWrapper }, /*#__PURE__*/ jsx(Text, { color: "primary" }, t('(no answer)'))); } return files.map(function(file) { return /*#__PURE__*/ jsx("div", { css: _this.props.styles.entryWrapper, key: file.id }, _this.renderPreview(file)); }); }); return _this; } _create_class(FilesList, [ { key: "makeKeyPressHandler", value: function makeKeyPressHandler(callback) { return function(event) { if (event.charCode === SPACE_KEY || event.charCode === ENTER_KEY) { event.preventDefault(); callback(); } }; } }, { key: "render", value: function render() { var _this_state = this.state, modalContent = _this_state.modalContent, isModalOpen = _this_state.isModalOpen; return /*#__PURE__*/ jsx("div", null, /*#__PURE__*/ jsx("div", { className: "fs-mask" }, this.renderPreviews()), /*#__PURE__*/ jsx(SimpleModal, { isModalOpen: isModalOpen, title: modalContent.name, label: modalContent.name, onModalDismiss: this.handleCloseModal }, /*#__PURE__*/ jsx("img", { css: this.props.styles.modalImage, className: "fs-mask", src: modalContent.url, alt: modalContent.name }))); } } ]); return FilesList; }(Component); _define_property(FilesList, "displayName", 'FilesList'); _define_property(FilesList, "componentId", "Quizzes".concat(FilesList.displayName)); _define_property(FilesList, "propTypes", { files: PropTypes.arrayOf(PropTypes.shape({ id: PropTypes.number.isRequired, name: PropTypes.string.isRequired, url: PropTypes.string, size: PropTypes.number.isRequired, isUploading: PropTypes.bool })), makeRemoveHandler: PropTypes.func, showIfEmpty: PropTypes.bool, readOnly: PropTypes.bool, styles: PropTypes.object }); _define_property(FilesList, "defaultProps", { files: void 0, makeRemoveHandler: void 0, showIfEmpty: false, readOnly: false }); export { FilesList as default }; FilesList = _ts_decorate([ withStyleOverrides(generateStyle, generateComponentTheme) ], FilesList);