Skip to content

Commit

Permalink
fix: remove helper and extended logic for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill-Hatalski committed Jan 19, 2024
1 parent 6589991 commit 72568fa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 54 deletions.
44 changes: 0 additions & 44 deletions views/js/qtiCreator/helper/textEntryConverterHelper.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ define([
'taoQtiItem/qtiCreator/widgets/states/Correct',
'taoQtiItem/qtiCommonRenderer/helpers/instructions/instructionManager',
'util/locale',
'taoQtiItem/qtiCreator/helper/textEntryConverterHelper'
], function ($, __, stringResponseHelper, stateFactory, Correct, instructionMgr, locale, textEntryConverterHelper) {
'util/converter'
], function ($, __, stringResponseHelper, stateFactory, Correct, instructionMgr, locale, converter) {
'use strict';

function setErrorNotification(submitButton, element, baseType) {
submitButton.attr('disabled', true);
instructionMgr
.appendInstruction(
element,
__('This is not a valid value. Expected %s', baseType)
);
}

function start() {
const element = this.widget.element;
const $container = this.widget.$container;
Expand All @@ -42,15 +51,38 @@ define([
instructionMgr.removeInstructions(element);
instructionMgr.appendInstruction(element, __('Please type the correct response in the box below.'));
const $input = $(this);
const value = textEntryConverterHelper($input.val(), response.attributes);
if (locale.getDecimalSeparator() !== '.') {
$input.val(value.replace('.', locale.getDecimalSeparator()));
}
if (value === '') {
$submitButton.attr('disabled', true);
return instructionMgr.appendInstruction(element, __('This is not a valid value'));
let value = $input.val();
let responseValue = '';
const attributes = response.attributes;
const numericBase = attributes.base || 10;
const convertedValue = converter.convert(value.trim());
switch (attributes.baseType) {
case 'integer':
value = locale.parseInt(convertedValue, numericBase);
responseValue = isNaN(value) ? '' : value;
// check for parsing and integer
if (responseValue === '' || !/^[+-]?[0-9]+(e-?\d*)?$/.test(convertedValue)) {
return setErrorNotification($submitButton, element, attributes.baseType)
}
break;
case 'float':
value = locale.parseFloat(convertedValue);
responseValue = isNaN(value) ? '' : value;
const regex = new RegExp(`^[+-]?[0-9]+\\${locale.getDecimalSeparator()}[0-9]+(e-?\\d*)?$`)
if (responseValue === '' || !regex.test(convertedValue)) { // check for parsing and float
return setErrorNotification($submitButton, element, attributes.baseType)
}
break;
case 'string':
responseValue = convertedValue;
if (responseValue === '') {
return setErrorNotification($submitButton, element, attributes.baseType)
}
break;
default:
return false;
}
stringResponseHelper.setCorrectResponse(response, `${value}`, { trim: true });
stringResponseHelper.setCorrectResponse(response, `${responseValue}`, { trim: true });
});
}
function exit() {
Expand Down

0 comments on commit 72568fa

Please sign in to comment.