-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
208 additions
and
134 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,14 +13,16 @@ | |
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
* | ||
* Copyright (c) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); | ||
* Copyright (c) 2014-2022 Open Assessment Technologies SA (under the project TAO-PRODUCT); | ||
* | ||
*/ | ||
|
||
/** | ||
* @author Sam Sipasseuth <[email protected]> | ||
* @author Bertrand Chevrier <[email protected]> | ||
* @author Andrey Shaveko <[email protected]> | ||
*/ | ||
|
||
import $ from 'jquery'; | ||
import _ from 'lodash'; | ||
import __ from 'i18n'; | ||
|
@@ -31,28 +33,35 @@ import pciResponse from 'taoQtiItem/qtiCommonRenderer/helpers/PciResponse'; | |
import patternMaskHelper from 'taoQtiItem/qtiCommonRenderer/helpers/patternMask'; | ||
import locale from 'util/locale'; | ||
import tooltip from 'ui/tooltip'; | ||
import loggerFactory from 'core/logger'; | ||
import converter from 'util/converter'; | ||
|
||
/** | ||
* Create a logger | ||
*/ | ||
const logger = loggerFactory('taoQtiItem/qtiCommonRenderer/renderers/interactions/TextEntryInteraction.js'); | ||
|
||
/** | ||
* Hide the tooltip for the text input | ||
* @param {jQuery} $input | ||
*/ | ||
var hideTooltip = function hideTooltip($input) { | ||
function hideTooltip($input) { | ||
if ($input.data('$tooltip')) { | ||
$input.data('$tooltip').hide(); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Create/Show tooltip for the text input | ||
* @param {jQuery} $input | ||
* @param {String} theme | ||
* @param {String} message | ||
*/ | ||
var showTooltip = function showTooltip($input, theme, message) { | ||
function showTooltip($input, theme, message) { | ||
if ($input.data('$tooltip')) { | ||
$input.data('$tooltip').updateTitleContent(message); | ||
} else { | ||
var textEntryTooltip = tooltip.create($input, message, { | ||
const textEntryTooltip = tooltip.create($input, message, { | ||
theme: theme, | ||
trigger: 'manual' | ||
}); | ||
|
@@ -61,7 +70,7 @@ var showTooltip = function showTooltip($input, theme, message) { | |
} | ||
|
||
$input.data('$tooltip').show(); | ||
}; | ||
} | ||
|
||
/** | ||
* Init rendering, called after template injected into the DOM | ||
|
@@ -70,15 +79,13 @@ var showTooltip = function showTooltip($input, theme, message) { | |
* | ||
* @param {object} interaction | ||
*/ | ||
var render = function render(interaction) { | ||
var attributes = interaction.getAttributes(), | ||
baseType = interaction.getResponseDeclaration().attr('baseType'), | ||
$input = interaction.getContainer(), | ||
expectedLength, | ||
updateMaxCharsTooltip, | ||
updatePatternMaskTooltip, | ||
patternMask = interaction.attr('patternMask'), | ||
maxChars = parseInt(patternMaskHelper.parsePattern(patternMask, 'chars'), 10); | ||
function render(interaction) { | ||
const attributes = interaction.getAttributes(); | ||
const baseType = interaction.getResponseDeclaration().attr('baseType'); | ||
const $input = interaction.getContainer(); | ||
const patternMask = interaction.attr('patternMask'); | ||
const maxChars = parseInt(patternMaskHelper.parsePattern(patternMask, 'chars'), 10); | ||
let expectedLength; | ||
|
||
// Setting up baseType | ||
switch (baseType) { | ||
|
@@ -106,9 +113,10 @@ var render = function render(interaction) { | |
} | ||
|
||
if (maxChars) { | ||
updateMaxCharsTooltip = function updateMaxCharsTooltip() { | ||
var count = $input.val().length; | ||
var message, messageType; | ||
const updateMaxCharsTooltip = () => { | ||
const count = $input.val().length; | ||
let message; | ||
let messageType; | ||
|
||
if (count) { | ||
message = __('%d/%d', count, maxChars); | ||
|
@@ -143,8 +151,8 @@ var render = function render(interaction) { | |
hideTooltip($input); | ||
}); | ||
} else if (attributes.patternMask) { | ||
updatePatternMaskTooltip = function updatePatternMaskTooltip() { | ||
var regex = new RegExp(attributes.patternMask); | ||
const updatePatternMaskTooltip = () => { | ||
const regex = new RegExp(attributes.patternMask); | ||
|
||
hideTooltip($input); | ||
|
||
|
@@ -174,11 +182,11 @@ var render = function render(interaction) { | |
containerHelper.triggerResponseChangeEvent(interaction); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
var resetResponse = function resetResponse(interaction) { | ||
function resetResponse(interaction) { | ||
interaction.getContainer().val(''); | ||
}; | ||
} | ||
|
||
/** | ||
* Set the response to the rendered interaction. | ||
|
@@ -194,17 +202,19 @@ var resetResponse = function resetResponse(interaction) { | |
* @param {object} interaction | ||
* @param {object} response | ||
*/ | ||
var setResponse = function setResponse(interaction, response) { | ||
var responseValue; | ||
function setResponse(interaction, response) { | ||
let responseValue; | ||
|
||
try { | ||
responseValue = pciResponse.unserialize(response, interaction); | ||
} catch (e) {} | ||
} catch (e) { | ||
logger.warn(`setResponse error ${e}`); | ||
} | ||
|
||
if (responseValue && responseValue.length) { | ||
interaction.getContainer().val(responseValue[0]); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Return the response of the rendered interaction | ||
|
@@ -218,35 +228,38 @@ var setResponse = function setResponse(interaction, response) { | |
* @param {object} interaction | ||
* @returns {object} | ||
*/ | ||
var getResponse = function getResponse(interaction) { | ||
var ret = { base: {} }, | ||
value, | ||
$input = interaction.getContainer(), | ||
attributes = interaction.getAttributes(), | ||
baseType = interaction.getResponseDeclaration().attr('baseType'), | ||
numericBase = attributes.base || 10; | ||
|
||
if ($input.hasClass('invalid') || (attributes.placeholderText && $input.val() === attributes.placeholderText)) { | ||
function getResponse(interaction) { | ||
const ret = { base: {} }; | ||
const $input = interaction.getContainer(); | ||
const attributes = interaction.getAttributes(); | ||
const baseType = interaction.getResponseDeclaration().attr('baseType'); | ||
const numericBase = attributes.base || 10; | ||
|
||
const inputValue = $input.val(); | ||
let value; | ||
|
||
if ($input.hasClass('invalid') || (attributes.placeholderText && inputValue === attributes.placeholderText)) { | ||
//invalid response or response equals to the placeholder text are considered empty | ||
value = ''; | ||
} else { | ||
const convertedValue = converter.convert(inputValue.trim()); | ||
if (baseType === 'integer') { | ||
value = locale.parseInt($input.val(), numericBase); | ||
value = locale.parseInt(convertedValue, numericBase); | ||
} else if (baseType === 'float') { | ||
value = locale.parseFloat($input.val()); | ||
value = locale.parseFloat(convertedValue); | ||
} else if (baseType === 'string') { | ||
value = $input.val(); | ||
value = convertedValue; | ||
} | ||
} | ||
|
||
ret.base[baseType] = isNaN(value) && typeof value === 'number' ? '' : value; | ||
|
||
return ret; | ||
}; | ||
} | ||
|
||
var destroy = function destroy(interaction) { | ||
function destroy(interaction) { | ||
$('input.qti-textEntryInteraction').each(function(index, el) { | ||
var $input = $(el); | ||
const $input = $(el); | ||
if ($input.data('$tooltip')) { | ||
$input.data('$tooltip').dispose(); | ||
$input.removeData('$tooltip'); | ||
|
@@ -262,38 +275,38 @@ var destroy = function destroy(interaction) { | |
|
||
//remove all references to a cache container | ||
containerHelper.reset(interaction); | ||
}; | ||
} | ||
|
||
/** | ||
* Set the interaction state. It could be done anytime with any state. | ||
* | ||
* @param {Object} interaction - the interaction instance | ||
* @param {Object} state - the interaction state | ||
*/ | ||
var setState = function setState(interaction, state) { | ||
function setState(interaction, state) { | ||
if (_.isObject(state)) { | ||
if (state.response) { | ||
interaction.resetResponse(); | ||
interaction.setResponse(state.response); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Get the interaction state. | ||
* | ||
* @param {Object} interaction - the interaction instance | ||
* @returns {Object} the interaction current state | ||
*/ | ||
var getState = function getState(interaction) { | ||
var state = {}; | ||
var response = interaction.getResponse(); | ||
function getState(interaction) { | ||
const state = {}; | ||
const response = interaction.getResponse(); | ||
|
||
if (response) { | ||
state.response = response; | ||
} | ||
return state; | ||
}; | ||
} | ||
|
||
export default { | ||
qtiClass: 'textEntryInteraction', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.