@pie-element/drag-in-the-blank 5.10.2-next.1 → 5.10.2-next.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.tileSource = exports["default"] = exports.BlankContent = void 0;
9
+
10
+ var _react = _interopRequireDefault(require("react"));
11
+
12
+ var _MoreVert = _interopRequireDefault(require("@material-ui/icons/MoreVert"));
13
+
14
+ var _Delete = _interopRequireDefault(require("@material-ui/icons/Delete"));
15
+
16
+ var _reactDnd = require("react-dnd");
17
+
18
+ var _styles = require("@material-ui/core/styles");
19
+
20
+ var _markupUtils = require("./markupUtils");
21
+
22
+ var _propTypes = _interopRequireDefault(require("prop-types"));
23
+
24
+ var GripIcon = function GripIcon(_ref) {
25
+ var style = _ref.style;
26
+ return /*#__PURE__*/_react["default"].createElement("span", {
27
+ style: style
28
+ }, /*#__PURE__*/_react["default"].createElement(_MoreVert["default"], {
29
+ style: {
30
+ margin: '0 -16px'
31
+ }
32
+ }), /*#__PURE__*/_react["default"].createElement(_MoreVert["default"], null));
33
+ };
34
+
35
+ GripIcon.propTypes = {
36
+ style: _propTypes["default"].object
37
+ };
38
+ var BlankContent = (0, _styles.withStyles)(function (theme) {
39
+ return {
40
+ choice: {
41
+ display: 'inline-flex',
42
+ minWidth: '178px',
43
+ minHeight: '36px',
44
+ background: theme.palette.common.white,
45
+ boxSizing: 'border-box',
46
+ borderRadius: '3px',
47
+ overflow: 'hidden',
48
+ position: 'relative',
49
+ padding: '8px 35px 8px 35px',
50
+ cursor: 'grab'
51
+ },
52
+ deleteIcon: {
53
+ position: 'absolute',
54
+ top: '6px',
55
+ right: '0',
56
+ color: theme.palette.grey[500],
57
+ zIndex: 2,
58
+ '& :hover': {
59
+ cursor: 'pointer',
60
+ color: theme.palette.common.black
61
+ }
62
+ }
63
+ };
64
+ })(function (props) {
65
+ var classes = props.classes,
66
+ connectDragSource = props.connectDragSource,
67
+ choice = props.choice,
68
+ onClick = props.onClick,
69
+ onRemoveChoice = props.onRemoveChoice,
70
+ error = props.error;
71
+ return connectDragSource( /*#__PURE__*/_react["default"].createElement("div", {
72
+ className: classes.choice,
73
+ style: {
74
+ border: "1px solid ".concat(error ? '#f44336' : '#C0C3CF')
75
+ },
76
+ onClick: onClick
77
+ }, /*#__PURE__*/_react["default"].createElement(GripIcon, {
78
+ style: {
79
+ position: 'absolute',
80
+ top: '6px',
81
+ left: '15px',
82
+ color: '#9e9e9e',
83
+ zIndex: 2
84
+ }
85
+ }), /*#__PURE__*/_react["default"].createElement("span", {
86
+ dangerouslySetInnerHTML: {
87
+ __html: choice.value
88
+ }
89
+ }), /*#__PURE__*/_react["default"].createElement(_Delete["default"], {
90
+ className: classes.deleteIcon,
91
+ onClick: function onClick(e) {
92
+ e.preventDefault();
93
+ e.stopPropagation();
94
+ onRemoveChoice(e);
95
+ }
96
+ })));
97
+ });
98
+ exports.BlankContent = BlankContent;
99
+ var tileSource = {
100
+ canDrag: function canDrag(props) {
101
+ if ((0, _markupUtils.choiceIsEmpty)(props.choice)) {
102
+ alert('You need to define a value for an answer choice before it can be associated with a response area.');
103
+ return false;
104
+ }
105
+
106
+ return !props.disabled;
107
+ },
108
+ beginDrag: function beginDrag(props) {
109
+ return {
110
+ id: props.targetId,
111
+ value: props.choice,
112
+ instanceId: props.instanceId
113
+ };
114
+ }
115
+ };
116
+ exports.tileSource = tileSource;
117
+
118
+ var _default = (0, _reactDnd.DragSource)('drag-in-the-blank-choice', tileSource, function (connect, monitor) {
119
+ return {
120
+ connectDragSource: connect.dragSource(),
121
+ isDragging: monitor.isDragging()
122
+ };
123
+ })(BlankContent);
124
+
125
+ exports["default"] = _default;
126
+ //# sourceMappingURL=choice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/choice.jsx"],"names":["GripIcon","style","margin","propTypes","PropTypes","object","BlankContent","theme","choice","display","minWidth","minHeight","background","palette","common","white","boxSizing","borderRadius","overflow","position","padding","cursor","deleteIcon","top","right","color","grey","zIndex","black","props","classes","connectDragSource","onClick","onRemoveChoice","error","border","left","__html","value","e","preventDefault","stopPropagation","tileSource","canDrag","alert","disabled","beginDrag","id","targetId","instanceId","connect","monitor","dragSource","isDragging"],"mappings":";;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AAEA,IAAMA,QAAQ,GAAG,SAAXA,QAAW,OAAe;AAAA,MAAZC,KAAY,QAAZA,KAAY;AAC9B,sBACE;AAAM,IAAA,KAAK,EAAEA;AAAb,kBACE,gCAAC,oBAAD;AAAU,IAAA,KAAK,EAAE;AAAEC,MAAAA,MAAM,EAAE;AAAV;AAAjB,IADF,eAEE,gCAAC,oBAAD,OAFF,CADF;AAMD,CAPD;;AASAF,QAAQ,CAACG,SAAT,GAAqB;AACnBF,EAAAA,KAAK,EAAEG,sBAAUC;AADE,CAArB;AAIO,IAAMC,YAAY,GAAG,wBAAW,UAACC,KAAD;AAAA,SAAY;AACjDC,IAAAA,MAAM,EAAE;AACNC,MAAAA,OAAO,EAAE,aADH;AAENC,MAAAA,QAAQ,EAAE,OAFJ;AAGNC,MAAAA,SAAS,EAAE,MAHL;AAINC,MAAAA,UAAU,EAAEL,KAAK,CAACM,OAAN,CAAcC,MAAd,CAAqBC,KAJ3B;AAKNC,MAAAA,SAAS,EAAE,YALL;AAMNC,MAAAA,YAAY,EAAE,KANR;AAONC,MAAAA,QAAQ,EAAE,QAPJ;AAQNC,MAAAA,QAAQ,EAAE,UARJ;AASNC,MAAAA,OAAO,EAAE,mBATH;AAUNC,MAAAA,MAAM,EAAE;AAVF,KADyC;AAajDC,IAAAA,UAAU,EAAE;AACVH,MAAAA,QAAQ,EAAE,UADA;AAEVI,MAAAA,GAAG,EAAE,KAFK;AAGVC,MAAAA,KAAK,EAAE,GA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,CAAC,CAACC,cAAF;AACAD,MAAAA,CAAC,CAACE,eAAF;AAEAR,MAAAA,cAAc,CAACM,CAAD,CAAd;AACD;AAPH,IAbF,CADsB,CAAxB;AAyBD,CArD2B,CAArB;;AAuDA,IAAMG,UAAU,GAAG;AACxBC,EAAAA,OADwB,mBAChBd,KADgB,EACT;AACb,QAAI,gCAAcA,KAAK,CAACrB,MAApB,CAAJ,EAAiC;AAC/BoC,MAAAA,KAAK,CAAC,mGAAD,CAAL;AACA,aAAO,KAAP;AACD;;AAED,WAAO,CAACf,KAAK,CAACgB,QAAd;AACD,GARuB;AASxBC,EAAAA,SATwB,qBASdjB,KATc,EASP;AACf,WAAO;AACLkB,MAAAA,EAAE,EAAElB,KAAK,CAACmB,QADL;AAELV,MAAAA,KAAK,EAAET,KAAK,CAACrB,MAFR;AAGLyC,MAAAA,UAAU,EAAEpB,KAAK,CAACoB;AAHb,KAAP;AAKD;AAfuB,CAAnB;;;eAkBQ,0BAAW,0BAAX,EAAuCP,UAAvC,EAAmD,UAACQ,OAAD,EAAUC,OAAV;AAAA,SAAuB;AACvFpB,IAAAA,iBAAiB,EAAEmB,OAAO,CAACE,UAAR,EADoE;AAEvFC,IAAAA,UAAU,EAAEF,OAAO,CAACE,UAAR;AAF2E,GAAvB;AAAA,CAAnD,EAGX/C,YAHW,C","sourcesContent":["import React from 'react';\nimport MoreVert from '@material-ui/icons/MoreVert';\nimport Delete from '@material-ui/icons/Delete';\nimport { DragSource } from 'react-dnd';\nimport { withStyles } from '@material-ui/core/styles';\nimport { choiceIsEmpty } from './markupUtils';\nimport PropTypes from 'prop-types';\n\nconst GripIcon = ({ style }) => {\n return (\n <span style={style}>\n <MoreVert style={{ margin: '0 -16px' }} />\n <MoreVert />\n </span>\n );\n};\n\nGripIcon.propTypes = {\n style: PropTypes.object,\n};\n\nexport const BlankContent = withStyles((theme) => ({\n choice: {\n display: 'inline-flex',\n minWidth: '178px',\n minHeight: '36px',\n background: theme.palette.common.white,\n boxSizing: 'border-box',\n borderRadius: '3px',\n overflow: 'hidden',\n position: 'relative',\n padding: '8px 35px 8px 35px',\n cursor: 'grab',\n },\n deleteIcon: {\n position: 'absolute',\n top: '6px',\n right: '0',\n color: theme.palette.grey[500],\n zIndex: 2,\n\n '& :hover': {\n cursor: 'pointer',\n color: theme.palette.common.black,\n },\n },\n}))((props) => {\n const { classes, connectDragSource, choice, onClick, onRemoveChoice, error } = props;\n\n return connectDragSource(\n <div className={classes.choice} style={{ border: `1px solid ${error ? '#f44336' : '#C0C3CF'}` }} onClick={onClick}>\n <GripIcon\n style={{\n position: 'absolute',\n top: '6px',\n left: '15px',\n color: '#9e9e9e',\n zIndex: 2,\n }}\n />\n\n <span dangerouslySetInnerHTML={{ __html: choice.value }} />\n\n <Delete\n className={classes.deleteIcon}\n onClick={(e) => {\n e.preventDefault();\n e.stopPropagation();\n\n onRemoveChoice(e);\n }}\n />\n </div>,\n );\n});\n\nexport const tileSource = {\n canDrag(props) {\n if (choiceIsEmpty(props.choice)) {\n alert('You need to define a value for an answer choice before it can be associated with a response area.');\n return false;\n }\n\n return !props.disabled;\n },\n beginDrag(props) {\n return {\n id: props.targetId,\n value: props.choice,\n instanceId: props.instanceId,\n };\n },\n};\n\nexport default DragSource('drag-in-the-blank-choice', tileSource, (connect, monitor) => ({\n connectDragSource: connect.dragSource(),\n isDragging: monitor.isDragging(),\n}))(BlankContent);\n"],"file":"choice.js"}
@@ -0,0 +1,388 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = exports.Choices = void 0;
9
+
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
+
12
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
+
14
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
+
16
+ var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
17
+
18
+ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
19
+
20
+ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
21
+
22
+ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
23
+
24
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
25
+
26
+ var _react = _interopRequireDefault(require("react"));
27
+
28
+ var _reactDom = _interopRequireDefault(require("react-dom"));
29
+
30
+ var _propTypes = _interopRequireDefault(require("prop-types"));
31
+
32
+ var _editableHtml = require("@pie-lib/pie-toolbox/editable-html");
33
+
34
+ var _mathRenderingAccessible = require("@pie-lib/pie-toolbox/math-rendering-accessible");
35
+
36
+ var _find = _interopRequireDefault(require("lodash/find"));
37
+
38
+ var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
39
+
40
+ var _choice = _interopRequireDefault(require("./choice"));
41
+
42
+ var _markupUtils = require("./markupUtils");
43
+
44
+ var _styles = require("@material-ui/core/styles");
45
+
46
+ var _configUi = require("@pie-lib/pie-toolbox/config-ui");
47
+
48
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
49
+
50
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
51
+
52
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
53
+
54
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
55
+
56
+ var styles = function styles(theme) {
57
+ return {
58
+ design: {
59
+ display: 'flex',
60
+ flexDirection: 'column',
61
+ marginBottom: theme.spacing.unit * 1.5
62
+ },
63
+ addButton: {
64
+ marginLeft: 'auto'
65
+ },
66
+ altChoices: {
67
+ alignItems: 'flex-start',
68
+ display: 'flex',
69
+ flexWrap: 'wrap',
70
+ justifyContent: 'space-evenly',
71
+ marginTop: theme.spacing.unit,
72
+ '& > *': {
73
+ margin: theme.spacing.unit
74
+ }
75
+ },
76
+ errorText: {
77
+ fontSize: theme.typography.fontSize - 2,
78
+ color: theme.palette.error.main,
79
+ paddingBottom: theme.spacing.unit * 2
80
+ }
81
+ };
82
+ };
83
+
84
+ var Choices = /*#__PURE__*/function (_React$Component) {
85
+ (0, _inherits2["default"])(Choices, _React$Component);
86
+
87
+ var _super = _createSuper(Choices);
88
+
89
+ function Choices() {
90
+ var _this;
91
+
92
+ (0, _classCallCheck2["default"])(this, Choices);
93
+
94
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
95
+ args[_key] = arguments[_key];
96
+ }
97
+
98
+ _this = _super.call.apply(_super, [this].concat(args));
99
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "state", {
100
+ warning: {
101
+ open: false
102
+ }
103
+ });
104
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "preventDone", false);
105
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "rerenderMath", function () {
106
+ //eslint-disable-next-line
107
+ var domNode = _reactDom["default"].findDOMNode((0, _assertThisInitialized2["default"])(_this));
108
+
109
+ (0, _mathRenderingAccessible.renderMath)(domNode);
110
+ });
111
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChoiceChanged", function (prevValue, val, key) {
112
+ var _this$props = _this.props,
113
+ onChange = _this$props.onChange,
114
+ model = _this$props.model;
115
+ var choices = model.choices,
116
+ correctResponse = model.correctResponse,
117
+ alternateResponses = model.alternateResponses;
118
+ var duplicatedValue = (choices || []).find(function (c) {
119
+ return c.value === val && c.id !== key;
120
+ }); // discard the new added choice or the changes if the choice would be a duplicate to one that already exists
121
+
122
+ if (duplicatedValue) {
123
+ if (prevValue === '') {
124
+ // remove the new added choice from choices
125
+ var _newChoices = (choices || []).filter(function (c) {
126
+ return c.id !== key;
127
+ });
128
+
129
+ onChange(_newChoices);
130
+ }
131
+
132
+ _this.setState({
133
+ warning: {
134
+ open: true,
135
+ text: 'Identical answer choices are not allowed and the changes will be discarded.'
136
+ }
137
+ });
138
+
139
+ return;
140
+ }
141
+
142
+ var newChoices = (choices === null || choices === void 0 ? void 0 : choices.map(function (choice) {
143
+ return choice.id === key ? _objectSpread(_objectSpread({}, choice), {}, {
144
+ value: val
145
+ }) : choice;
146
+ })) || [];
147
+
148
+ if (!(0, _markupUtils.choiceIsEmpty)({
149
+ value: val
150
+ })) {
151
+ onChange(newChoices);
152
+ return;
153
+ } // if the edited content is empty, its usage has to be searched in the correct response definitions
154
+
155
+
156
+ var usedForResponse = false;
157
+
158
+ if (correctResponse) {
159
+ Object.keys(correctResponse).forEach(function (responseKey) {
160
+ if (correctResponse[responseKey] === key) {
161
+ usedForResponse = true;
162
+ }
163
+ });
164
+ }
165
+
166
+ if (alternateResponses && !usedForResponse) {
167
+ Object.values(alternateResponses).forEach(function (alternate) {
168
+ if (alternate.indexOf(key) >= 0) {
169
+ usedForResponse = true;
170
+ }
171
+ });
172
+ }
173
+
174
+ if (usedForResponse) {
175
+ _this.setState({
176
+ warning: {
177
+ open: true,
178
+ text: 'Answer choices cannot be blank and the changes will be discarded.'
179
+ }
180
+ });
181
+
182
+ return;
183
+ }
184
+
185
+ var newChoicesWithoutTheEmptyOne = newChoices.filter(function (choice) {
186
+ return choice.id !== key;
187
+ });
188
+ onChange(newChoicesWithoutTheEmptyOne);
189
+
190
+ _this.setState({
191
+ warning: {
192
+ open: true,
193
+ text: 'Answer choices cannot be blank.'
194
+ }
195
+ });
196
+ });
197
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChoiceFocus", function (id) {
198
+ return _this.setState({
199
+ focusedEl: id
200
+ });
201
+ });
202
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onAddChoice", function () {
203
+ var _this$props2 = _this.props,
204
+ oldChoices = _this$props2.model.choices,
205
+ onChange = _this$props2.onChange;
206
+
207
+ _this.setState({
208
+ focusedEl: "".concat(oldChoices.length)
209
+ }, function () {
210
+ onChange([].concat((0, _toConsumableArray2["default"])(oldChoices), [{
211
+ id: "".concat(oldChoices.length),
212
+ value: ''
213
+ }]));
214
+ });
215
+ });
216
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChoiceRemove", function (id) {
217
+ var _this$props3 = _this.props,
218
+ onChange = _this$props3.onChange,
219
+ choices = _this$props3.model.choices;
220
+ var newChoices = (choices || []).filter(function (choice) {
221
+ return choice.id !== id;
222
+ });
223
+ onChange(newChoices);
224
+ });
225
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getVisibleChoices", function () {
226
+ var _this$props4 = _this.props,
227
+ duplicates = _this$props4.duplicates,
228
+ _this$props4$model = _this$props4.model,
229
+ choices = _this$props4$model.choices,
230
+ correctResponse = _this$props4$model.correctResponse;
231
+
232
+ if (!choices) {
233
+ return [];
234
+ }
235
+
236
+ if (duplicates) {
237
+ return choices;
238
+ } // if duplicates not allowed, remove the choices that are used to define the correct response
239
+
240
+
241
+ return choices.filter(function (choice) {
242
+ return !(0, _find["default"])(correctResponse, function (v) {
243
+ return v === choice.id;
244
+ });
245
+ });
246
+ });
247
+ return _this;
248
+ }
249
+
250
+ (0, _createClass2["default"])(Choices, [{
251
+ key: "componentDidMount",
252
+ value: function componentDidMount() {
253
+ this.rerenderMath();
254
+ }
255
+ }, {
256
+ key: "componentDidUpdate",
257
+ value: function componentDidUpdate() {
258
+ this.rerenderMath();
259
+
260
+ if (this.focusedNodeRef) {
261
+ this.focusedNodeRef.focus('end');
262
+ }
263
+ }
264
+ }, {
265
+ key: "render",
266
+ value: function render() {
267
+ var _this2 = this;
268
+
269
+ var _this$state = this.state,
270
+ focusedEl = _this$state.focusedEl,
271
+ warning = _this$state.warning;
272
+ var _this$props5 = this.props,
273
+ classes = _this$props5.classes,
274
+ duplicates = _this$props5.duplicates,
275
+ error = _this$props5.error,
276
+ _this$props5$mathMlOp = _this$props5.mathMlOptions,
277
+ mathMlOptions = _this$props5$mathMlOp === void 0 ? {} : _this$props5$mathMlOp,
278
+ maxChoices = _this$props5.maxChoices,
279
+ choices = _this$props5.model.choices,
280
+ toolbarOpts = _this$props5.toolbarOpts,
281
+ uploadSoundSupport = _this$props5.uploadSoundSupport,
282
+ _this$props5$imageSup = _this$props5.imageSupport,
283
+ imageSupport = _this$props5$imageSup === void 0 ? {} : _this$props5$imageSup,
284
+ _this$props5$pluginPr = _this$props5.pluginProps,
285
+ pluginProps = _this$props5$pluginPr === void 0 ? {} : _this$props5$pluginPr;
286
+ var visibleChoices = this.getVisibleChoices() || [];
287
+ return /*#__PURE__*/_react["default"].createElement("div", {
288
+ className: classes.design
289
+ }, /*#__PURE__*/_react["default"].createElement(_Button["default"], {
290
+ className: classes.addButton,
291
+ variant: "contained",
292
+ color: "primary",
293
+ onClick: this.onAddChoice,
294
+ disabled: maxChoices && choices && maxChoices === choices.length
295
+ }, "Add Choice"), /*#__PURE__*/_react["default"].createElement("div", {
296
+ className: classes.altChoices
297
+ }, visibleChoices.map(function (choice, index) {
298
+ return focusedEl === choice.id ? /*#__PURE__*/_react["default"].createElement("div", {
299
+ key: index,
300
+ style: {
301
+ minWidth: '100%',
302
+ zIndex: '100'
303
+ }
304
+ }, /*#__PURE__*/_react["default"].createElement(_editableHtml.EditableHtml, {
305
+ ref: function ref(_ref) {
306
+ return _this2.focusedNodeRef = _ref;
307
+ },
308
+ className: classes.prompt,
309
+ imageSupport: imageSupport,
310
+ markup: choice.value,
311
+ pluginProps: pluginProps,
312
+ languageCharactersProps: [{
313
+ language: 'spanish'
314
+ }, {
315
+ language: 'special'
316
+ }],
317
+ onChange: function onChange(val) {
318
+ if (_this2.preventDone) {
319
+ return;
320
+ }
321
+
322
+ _this2.onChoiceChanged(choice.value, val, choice.id);
323
+ },
324
+ onDone: function onDone() {
325
+ if (_this2.preventDone) {
326
+ return;
327
+ }
328
+
329
+ _this2.setState({
330
+ focusedEl: undefined
331
+ });
332
+ },
333
+ onBlur: function onBlur(e) {
334
+ var inInInsertCharacter = e.relatedTarget && e.relatedTarget.closest('.insert-character-dialog');
335
+ _this2.preventDone = inInInsertCharacter;
336
+ },
337
+ disableUnderline: true,
338
+ toolbarOpts: toolbarOpts,
339
+ uploadSoundSupport: uploadSoundSupport,
340
+ mathMlOptions: mathMlOptions
341
+ })) : /*#__PURE__*/_react["default"].createElement(_choice["default"], {
342
+ key: index,
343
+ duplicates: duplicates,
344
+ targetId: "0",
345
+ choice: choice,
346
+ error: error,
347
+ onClick: function onClick() {
348
+ return _this2.onChoiceFocus(choice.id);
349
+ },
350
+ onRemoveChoice: function onRemoveChoice() {
351
+ return _this2.onChoiceRemove(choice.id);
352
+ }
353
+ });
354
+ })), error && /*#__PURE__*/_react["default"].createElement("div", {
355
+ className: classes.errorText
356
+ }, error), /*#__PURE__*/_react["default"].createElement(_configUi.AlertDialog, {
357
+ open: warning.open,
358
+ title: "Warning",
359
+ text: warning.text,
360
+ onConfirm: function onConfirm() {
361
+ return _this2.setState({
362
+ warning: {
363
+ open: false
364
+ }
365
+ });
366
+ }
367
+ }));
368
+ }
369
+ }]);
370
+ return Choices;
371
+ }(_react["default"].Component);
372
+
373
+ exports.Choices = Choices;
374
+ (0, _defineProperty2["default"])(Choices, "propTypes", {
375
+ duplicates: _propTypes["default"].bool,
376
+ error: _propTypes["default"].string,
377
+ model: _propTypes["default"].object.isRequired,
378
+ onChange: _propTypes["default"].func.isRequired,
379
+ classes: _propTypes["default"].object.isRequired,
380
+ toolbarOpts: _propTypes["default"].object,
381
+ pluginProps: _propTypes["default"].object,
382
+ maxChoices: _propTypes["default"].number,
383
+ uploadSoundSupport: _propTypes["default"].object
384
+ });
385
+ var Styled = (0, _styles.withStyles)(styles)(Choices);
386
+ var _default = Styled;
387
+ exports["default"] = _default;
388
+ //# sourceMappingURL=choices.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/choices.jsx"],"names":["styles","theme","design","display","flexDirection","marginBottom","spacing","unit","addButton","marginLeft","altChoices","alignItems","flexWrap","justifyContent","marginTop","margin","errorText","fontSize","typography","color","palette","error","main","paddingBottom","Choices","warning","open","domNode","ReactDOM","findDOMNode","prevValue","val","key","props","onChange","model","choices","correctResponse","alternateResponses","duplicatedValue","find","c","value","id","newChoices","filter","setState","text","map","choice","usedForResponse","Object","keys","forEach","responseKey","values","alternate","indexOf","newChoicesWithoutTheEmptyOne","focusedEl","oldChoices","length","duplicates","v","rerenderMath","focusedNodeRef","focus","state","classes","mathMlOptions","maxChoices","toolbarOpts","uploadSoundSupport","imageSupport","pluginProps","visibleChoices","getVisibleChoices","onAddChoice","index","minWidth","zIndex","ref","prompt","language","preventDone","onChoiceChanged","undefined","e","inInInsertCharacter","relatedTarget","closest","onChoiceFocus","onChoiceRemove","React","Component","PropTypes","bool","string","object","isRequired","func","number","Styled"],"mappings":";;;;;;;;;;;;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G;AATb,C;AAqQrB,IAAMI,MAAM,GAAG,wBAAWtG,MAAX,EAAmBwB,OAAnB,CAAf;eAEe8E,M","sourcesContent":["import React from 'react';\nimport ReactDOM from 'react-dom';\nimport PropTypes from 'prop-types';\nimport { EditableHtml } from '@pie-lib/pie-toolbox/editable-html';\nimport { renderMath } from '@pie-lib/pie-toolbox/math-rendering-accessible';\nimport find from 'lodash/find';\nimport Button from '@material-ui/core/Button';\nimport Choice from './choice';\nimport { choiceIsEmpty } from './markupUtils';\nimport { withStyles } from '@material-ui/core/styles';\nimport { AlertDialog } from '@pie-lib/pie-toolbox/config-ui';\n\nconst styles = (theme) => ({\n design: {\n display: 'flex',\n flexDirection: 'column',\n marginBottom: theme.spacing.unit * 1.5,\n },\n addButton: {\n marginLeft: 'auto',\n },\n altChoices: {\n alignItems: 'flex-start',\n display: 'flex',\n flexWrap: 'wrap',\n justifyContent: 'space-evenly',\n marginTop: theme.spacing.unit,\n\n '& > *': {\n margin: theme.spacing.unit,\n },\n },\n errorText: {\n fontSize: theme.typography.fontSize - 2,\n color: theme.palette.error.main,\n paddingBottom: theme.spacing.unit * 2,\n },\n});\n\nexport class Choices extends React.Component {\n static propTypes = {\n duplicates: PropTypes.bool,\n error: PropTypes.string,\n model: PropTypes.object.isRequired,\n onChange: PropTypes.func.isRequired,\n classes: PropTypes.object.isRequired,\n toolbarOpts: PropTypes.object,\n pluginProps: PropTypes.object,\n maxChoices: PropTypes.number,\n uploadSoundSupport: PropTypes.object,\n };\n\n state = { warning: { open: false } };\n preventDone = false;\n\n componentDidMount() {\n this.rerenderMath();\n }\n\n componentDidUpdate() {\n this.rerenderMath();\n\n if (this.focusedNodeRef) {\n this.focusedNodeRef.focus('end');\n }\n }\n\n rerenderMath = () => {\n //eslint-disable-next-line\n const domNode = ReactDOM.findDOMNode(this);\n\n renderMath(domNode);\n };\n\n onChoiceChanged = (prevValue, val, key) => {\n const { onChange, model } = this.props;\n const { choices, correctResponse, alternateResponses } = model;\n const duplicatedValue = (choices || []).find((c) => c.value === val && c.id !== key);\n\n // discard the new added choice or the changes if the choice would be a duplicate to one that already exists\n if (duplicatedValue) {\n if (prevValue === '') {\n // remove the new added choice from choices\n const newChoices = (choices || []).filter((c) => c.id !== key);\n\n onChange(newChoices);\n }\n\n this.setState({\n warning: {\n open: true,\n text: 'Identical answer choices are not allowed and the changes will be discarded.',\n },\n });\n\n return;\n }\n\n const newChoices = choices?.map((choice) => (choice.id === key ? { ...choice, value: val } : choice)) || [];\n\n if (!choiceIsEmpty({ value: val })) {\n onChange(newChoices);\n\n return;\n }\n\n // if the edited content is empty, its usage has to be searched in the correct response definitions\n let usedForResponse = false;\n\n if (correctResponse) {\n Object.keys(correctResponse).forEach((responseKey) => {\n if (correctResponse[responseKey] === key) {\n usedForResponse = true;\n }\n });\n }\n\n if (alternateResponses && !usedForResponse) {\n Object.values(alternateResponses).forEach((alternate) => {\n if (alternate.indexOf(key) >= 0) {\n usedForResponse = true;\n }\n });\n }\n\n if (usedForResponse) {\n this.setState({\n warning: {\n open: true,\n text: 'Answer choices cannot be blank and the changes will be discarded.',\n },\n });\n\n return;\n }\n\n const newChoicesWithoutTheEmptyOne = newChoices.filter((choice) => choice.id !== key);\n\n onChange(newChoicesWithoutTheEmptyOne);\n\n this.setState({\n warning: {\n open: true,\n text: 'Answer choices cannot be blank.',\n },\n });\n };\n\n onChoiceFocus = (id) =>\n this.setState({\n focusedEl: id,\n });\n\n onAddChoice = () => {\n const {\n model: { choices: oldChoices },\n onChange,\n } = this.props;\n\n this.setState(\n {\n focusedEl: `${oldChoices.length}`,\n },\n () => {\n onChange([\n ...oldChoices,\n {\n id: `${oldChoices.length}`,\n value: '',\n },\n ]);\n },\n );\n };\n\n onChoiceRemove = (id) => {\n const {\n onChange,\n model: { choices },\n } = this.props;\n const newChoices = (choices || []).filter((choice) => choice.id !== id);\n\n onChange(newChoices);\n };\n\n getVisibleChoices = () => {\n const {\n duplicates,\n model: { choices, correctResponse },\n } = this.props;\n\n if (!choices) {\n return [];\n }\n\n if (duplicates) {\n return choices;\n }\n\n // if duplicates not allowed, remove the choices that are used to define the correct response\n return choices.filter((choice) => !find(correctResponse, (v) => v === choice.id));\n };\n\n render() {\n const { focusedEl, warning } = this.state;\n const {\n classes,\n duplicates,\n error,\n mathMlOptions = {},\n maxChoices,\n model: { choices },\n toolbarOpts,\n uploadSoundSupport,\n imageSupport = {},\n pluginProps = {},\n } = this.props;\n const visibleChoices = this.getVisibleChoices() || [];\n\n return (\n <div className={classes.design}>\n <Button\n className={classes.addButton}\n variant=\"contained\"\n color=\"primary\"\n onClick={this.onAddChoice}\n disabled={maxChoices && choices && maxChoices === choices.length}\n >\n Add Choice\n </Button>\n\n <div className={classes.altChoices}>\n {visibleChoices.map((choice, index) =>\n focusedEl === choice.id ? (\n <div\n key={index}\n style={{\n minWidth: '100%',\n zIndex: '100',\n }}\n >\n <EditableHtml\n ref={(ref) => (this.focusedNodeRef = ref)}\n className={classes.prompt}\n imageSupport={imageSupport}\n markup={choice.value}\n pluginProps={pluginProps}\n languageCharactersProps={[{ language: 'spanish' }, { language: 'special' }]}\n onChange={(val) => {\n if (this.preventDone) {\n return;\n }\n\n this.onChoiceChanged(choice.value, val, choice.id);\n }}\n onDone={() => {\n if (this.preventDone) {\n return;\n }\n\n this.setState({\n focusedEl: undefined,\n });\n }}\n onBlur={(e) => {\n const inInInsertCharacter = e.relatedTarget && e.relatedTarget.closest('.insert-character-dialog');\n\n this.preventDone = inInInsertCharacter;\n }}\n disableUnderline\n toolbarOpts={toolbarOpts}\n uploadSoundSupport={uploadSoundSupport}\n mathMlOptions={mathMlOptions}\n />\n </div>\n ) : (\n <Choice\n key={index}\n duplicates={duplicates}\n targetId=\"0\"\n choice={choice}\n error={error}\n onClick={() => this.onChoiceFocus(choice.id)}\n onRemoveChoice={() => this.onChoiceRemove(choice.id)}\n />\n ),\n )}\n </div>\n {error && <div className={classes.errorText}>{error}</div>}\n\n <AlertDialog\n open={warning.open}\n title=\"Warning\"\n text={warning.text}\n onConfirm={() => this.setState({ warning: { open: false } })}\n />\n </div>\n );\n }\n}\n\nconst Styled = withStyles(styles)(Choices);\n\nexport default Styled;\n"],"file":"choices.js"}