Skip to content

Commit

Permalink
Added custom authoring validation for to only accept numbers.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsharman committed Feb 26, 2024
1 parent 4fdb2d7 commit e37591e
Show file tree
Hide file tree
Showing 33 changed files with 649 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Assessment_Activity.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Assessment_App.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Assessment_Diagnostics.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Assessment_Items.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Assessment_Player.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Assessment_Questions.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Assessment_Sections.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Authoring_App.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Authoring_Diagnostics.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Authoring_Navigation.html

Large diffs are not rendered by default.

268 changes: 268 additions & 0 deletions docs/module-Authoring_Widgets.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-Utils_Logger.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_ariaCountOnNav.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_blockGrammarChecks.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_columnResizer.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_hideAlternatives.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_keyboardShortcuts.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_magnifier.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_mcqLabelPrefix.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_pageOverlay.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_renderPDF.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Assessment_resetResponse.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Authoring_contentTabs.html

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/module-_Extensions_Authoring_renderPDF.html

Large diffs are not rendered by default.

266 changes: 266 additions & 0 deletions docs/module-_Extensions_Authoring_validation_essayMaxLength.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@caspingus/lt",
"version": "2.3.0",
"version": "2.4.0",
"description": "A utility library of helpers and tools for working with Learnosity APIs.",
"main": "src/index.js",
"author": "michael@learnosity.com",
Expand Down
3 changes: 2 additions & 1 deletion src/authoring/core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as app from './core/app';
import * as diagnostics from './core/diagnostics';
import * as navigation from './core/navigation';
import * as widgets from './core/widgets';
import logger from '../utils/logger';

const utils = {
Expand All @@ -9,4 +10,4 @@ const utils = {
},
};

export const LT = { ...app, ...diagnostics, ...navigation, ...utils };
export const LT = { ...app, ...diagnostics, ...navigation, ...widgets, ...utils };
82 changes: 82 additions & 0 deletions src/authoring/extensions/validation/essayMaxLength/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as app from '../../../core/app';
import * as widgets from '../../../core/widgets';

/**
* Extensions add specific functionality to Learnosity APIs.
* They rely on modules within LT being available.
*
* --
*
* Adds custom validation to ensure that only numbers are added
* to the `max_length` field for the listed types.
*
* The extension will strip any invalid characters. Eg:
* ```
* 10.5
* ```
*
* Will be changed to:
* ```
* 10
* ```
*
* @module _Extensions/Authoring/validation/essayMaxLength
*/

const state = {
validTypes: ['longtextV2', 'plaintext'],
};

/**
* Extension constructor.
* @example
* import { LT } from '@caspingus/lt/src/authoring/index';
*
* LT.init(authorApp); // Set up LT with the Author API application instance variable
* LT.extensions.essayMaxLength.run();
* @since 2.4.0
*/
export function run() {
setupListeners();
}

/**
* Sets up listeners to add input validation.
* @since 2.4.0
* @ignore
*/
function setupListeners() {
app.appInstance().on('widgetedit:editor:ready', () => {
// Race condition with the event firing and the instance
// actually being loaded
setTimeout(() => {
const widgetType = widgets.type();

if (state.validTypes.includes(widgetType)) {
const elMaxLength = document.querySelector('[data-lrn-qe-input-path="max_length"] input.lrn-qe-input');

if (elMaxLength) {
elMaxLength.addEventListener('input', () => {
validateInput(elMaxLength);
});
}
}
}, 500);
});
}

/**
* Inspects the value of the passed DOM element. If the
* value contains non-numeric values (including period)
* those invalid characters are removed.
* @since 2.4.0
* @param {*} el
* @ignore
*/
function validateInput(el) {
const regex = /^\d+$/;

if (!regex.test(el.value)) {
el.value = el.value.replace(/[^0-9]/g, '');
}
}
2 changes: 2 additions & 0 deletions src/authoring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { LT as core } from './core';
import * as contentTabs from './extensions/ui/contentTabs/index';
import * as languageTextDirection from './extensions/ui/languageTextDirection/index';
import * as renderPDF from './extensions/ui/renderPDF/index';
import * as essayMaxLength from './extensions/validation/essayMaxLength/index';

const extensions = {
extensions: {
contentTabs: { ...contentTabs },
essayMaxLength: { ...essayMaxLength },
languageTextDirection: { ...languageTextDirection },
renderPDF: { ...renderPDF },
},
Expand Down

0 comments on commit e37591e

Please sign in to comment.